@@ -3,6 +3,8 @@ package store
3
3
import (
4
4
"reflect"
5
5
"testing"
6
+
7
+ "github.com/open-feature/flagd/core/pkg/model"
6
8
)
7
9
8
10
func TestSelector_IsEmpty (t * testing.T ) {
@@ -100,25 +102,92 @@ func TestSelector_ToQuery(t *testing.T) {
100
102
}
101
103
102
104
func TestSelector_ToMetadata (t * testing.T ) {
103
- s := Selector {indexMap : map [string ]string {"flagSetId" : "fsid" , "source" : "src" , "key" : "myKey" }}
104
- meta := s .ToMetadata ()
105
- if meta ["flagSetId" ] != "fsid" {
106
- t .Errorf ("ToMetadata missing flagSetId" )
107
- }
108
- if meta ["source" ] != "src" {
109
- t .Errorf ("ToMetadata missing source" )
105
+ tests := []struct {
106
+ name string
107
+ selector * Selector
108
+ want model.Metadata
109
+ }{
110
+ {
111
+ name : "nil selector" ,
112
+ selector : nil ,
113
+ want : model.Metadata {},
114
+ },
115
+ {
116
+ name : "nil indexMap" ,
117
+ selector : & Selector {indexMap : nil },
118
+ want : model.Metadata {},
119
+ },
120
+ {
121
+ name : "empty indexMap" ,
122
+ selector : & Selector {indexMap : map [string ]string {}},
123
+ want : model.Metadata {},
124
+ },
125
+ {
126
+ name : "flagSetId only" ,
127
+ selector : & Selector {indexMap : map [string ]string {"flagSetId" : "fsid" }},
128
+ want : model.Metadata {"flagSetId" : "fsid" },
129
+ },
130
+ {
131
+ name : "source only" ,
132
+ selector : & Selector {indexMap : map [string ]string {"source" : "src" }},
133
+ want : model.Metadata {"source" : "src" },
134
+ },
135
+ {
136
+ name : "flagSetId and source" ,
137
+ selector : & Selector {indexMap : map [string ]string {"flagSetId" : "fsid" , "source" : "src" }},
138
+ want : model.Metadata {"flagSetId" : "fsid" , "source" : "src" },
139
+ },
140
+ {
141
+ name : "flagSetId, source, and key (key should be ignored)" ,
142
+ selector : & Selector {indexMap : map [string ]string {"flagSetId" : "fsid" , "source" : "src" , "key" : "myKey" }},
143
+ want : model.Metadata {"flagSetId" : "fsid" , "source" : "src" },
144
+ },
110
145
}
111
- if _ , ok := meta ["key" ]; ok {
112
- t .Errorf ("ToMetadata should not include key" )
146
+
147
+ for _ , tt := range tests {
148
+ t .Run (tt .name , func (t * testing.T ) {
149
+ got := tt .selector .ToMetadata ()
150
+ if ! reflect .DeepEqual (got , tt .want ) {
151
+ t .Errorf ("ToMetadata() = %v, want %v" , got , tt .want )
152
+ }
153
+ })
113
154
}
114
155
}
115
156
116
157
func TestNewSelector (t * testing.T ) {
117
- s := NewSelector ("source=abc,flagSetId=1234" )
118
- if s .indexMap ["source" ] != "abc" {
119
- t .Errorf ("NewSelector did not parse source" )
158
+ tests := []struct {
159
+ name string
160
+ input string
161
+ wantMap map [string ]string
162
+ }{
163
+ {
164
+ name : "source and flagSetId" ,
165
+ input : "source=abc,flagSetId=1234" ,
166
+ wantMap : map [string ]string {"source" : "abc" , "flagSetId" : "1234" },
167
+ },
168
+ {
169
+ name : "source" ,
170
+ input : "source=abc" ,
171
+ wantMap : map [string ]string {"source" : "abc" },
172
+ },
173
+ {
174
+ name : "no equals, treat as source" ,
175
+ input : "mysource" ,
176
+ wantMap : map [string ]string {"source" : "mysource" },
177
+ },
178
+ {
179
+ name : "empty string" ,
180
+ input : "" ,
181
+ wantMap : map [string ]string {},
182
+ },
120
183
}
121
- if s .indexMap ["flagSetId" ] != "1234" {
122
- t .Errorf ("NewSelector did not parse flagSetId" )
184
+
185
+ for _ , tt := range tests {
186
+ t .Run (tt .name , func (t * testing.T ) {
187
+ s := NewSelector (tt .input )
188
+ if ! reflect .DeepEqual (s .indexMap , tt .wantMap ) {
189
+ t .Errorf ("NewSelector(%q) indexMap = %v, want %v" , tt .input , s .indexMap , tt .wantMap )
190
+ }
191
+ })
123
192
}
124
193
}
0 commit comments