@@ -70,10 +70,11 @@ func TestBuildVersionRange(t *testing.T) {
7070
7171func TestMergeTwoRanges (t * testing.T ) {
7272 tests := []struct {
73- name string
74- range1 * osvschema.Range
75- range2 * osvschema.Range
76- want * osvschema.Range
73+ name string
74+ range1 * osvschema.Range
75+ range2 * osvschema.Range
76+ want * osvschema.Range
77+ wantErr bool
7778 }{
7879 {
7980 name : "Merge identical ranges" ,
@@ -101,7 +102,7 @@ func TestMergeTwoRanges(t *testing.T) {
101102 },
102103 },
103104 {
104- name : "Different repos should return nil" ,
105+ name : "Different repos should return nil and error " ,
105106 range1 : & osvschema.Range {
106107 Type : osvschema .Range_GIT ,
107108 Repo : "https://github.com/example/repo1" ,
@@ -110,10 +111,11 @@ func TestMergeTwoRanges(t *testing.T) {
110111 Type : osvschema .Range_GIT ,
111112 Repo : "https://github.com/example/repo2" ,
112113 },
113- want : nil ,
114+ want : nil ,
115+ wantErr : true ,
114116 },
115117 {
116- name : "Different types should return nil" ,
118+ name : "Different types should return nil and error " ,
117119 range1 : & osvschema.Range {
118120 Type : osvschema .Range_GIT ,
119121 Repo : "https://github.com/example/repo" ,
@@ -122,7 +124,8 @@ func TestMergeTwoRanges(t *testing.T) {
122124 Type : osvschema .Range_ECOSYSTEM ,
123125 Repo : "https://github.com/example/repo" ,
124126 },
125- want : nil ,
127+ want : nil ,
128+ wantErr : true ,
126129 },
127130 {
128131 name : "Merge with DatabaseSpecific" ,
@@ -198,10 +201,117 @@ func TestMergeTwoRanges(t *testing.T) {
198201
199202 for _ , tt := range tests {
200203 t .Run (tt .name , func (t * testing.T ) {
201- got := MergeTwoRanges (tt .range1 , tt .range2 )
204+ got , err := MergeTwoRanges (tt .range1 , tt .range2 )
205+ if (err != nil ) != tt .wantErr {
206+ t .Errorf ("MergeTwoRanges() error = %v, wantErr %v" , err , tt .wantErr )
207+ return
208+ }
202209 if diff := cmp .Diff (tt .want , got , protocmp .Transform ()); diff != "" {
203210 t .Errorf ("mergeTwoRanges() mismatch (-want +got):\n %s" , diff )
204211 }
205212 })
206213 }
207214}
215+
216+ func TestMergeDatabaseSpecificValues (t * testing.T ) {
217+ tests := []struct {
218+ name string
219+ val1 any
220+ val2 any
221+ want any
222+ wantErr bool
223+ }{
224+ {
225+ name : "Merge lists" ,
226+ val1 : []any {"a" , "b" },
227+ val2 : []any {"c" , "d" },
228+ want : []any {"a" , "b" , "c" , "d" },
229+ },
230+ {
231+ name : "List and string mismatch" ,
232+ val1 : []any {"a" , "b" },
233+ val2 : "c" ,
234+ wantErr : true ,
235+ },
236+ {
237+ name : "Merge maps" ,
238+ val1 : map [string ]any {"key1" : "value1" },
239+ val2 : map [string ]any {"key2" : "value2" },
240+ want : map [string ]any {"key1" : "value1" , "key2" : "value2" },
241+ },
242+ {
243+ name : "Merge nested maps" ,
244+ val1 : map [string ]any {
245+ "nested" : map [string ]any {
246+ "key1" : "value1" ,
247+ },
248+ },
249+ val2 : map [string ]any {
250+ "nested" : map [string ]any {
251+ "key2" : "value2" ,
252+ },
253+ },
254+ want : map [string ]any {
255+ "nested" : map [string ]any {
256+ "key1" : "value1" ,
257+ "key2" : "value2" ,
258+ },
259+ },
260+ },
261+ {
262+ name : "Map and string mismatch" ,
263+ val1 : map [string ]any {"key1" : "value1" },
264+ val2 : "string" ,
265+ wantErr : true ,
266+ },
267+ {
268+ name : "Merge same strings" ,
269+ val1 : "value1" ,
270+ val2 : "value1" ,
271+ want : "value1" ,
272+ },
273+ {
274+ name : "Merge different strings" ,
275+ val1 : "value1" ,
276+ val2 : "value2" ,
277+ want : []any {"value1" , "value2" },
278+ },
279+ {
280+ name : "String and int mismatch" ,
281+ val1 : "value1" ,
282+ val2 : 123 ,
283+ wantErr : true ,
284+ },
285+ {
286+ name : "Merge same ints" ,
287+ val1 : 123 ,
288+ val2 : 123 ,
289+ want : 123 ,
290+ },
291+ {
292+ name : "Merge different ints" ,
293+ val1 : 123 ,
294+ val2 : 456 ,
295+ want : []any {123 , 456 },
296+ },
297+ {
298+ name : "Int and float64 mismatch" ,
299+ val1 : 123 ,
300+ val2 : 456.0 ,
301+ wantErr : true ,
302+ },
303+ }
304+
305+ for _ , tt := range tests {
306+ t .Run (tt .name , func (t * testing.T ) {
307+ got , err := mergeDatabaseSpecificValues (tt .val1 , tt .val2 )
308+ if (err != nil ) != tt .wantErr {
309+ t .Errorf ("mergeDatabaseSpecificValues() error = %v, wantErr %v" , err , tt .wantErr )
310+ return
311+ }
312+ if ! tt .wantErr && ! cmp .Equal (got , tt .want ) {
313+ t .Errorf ("mergeDatabaseSpecificValues() mismatch (-want +got):\n %s" , cmp .Diff (tt .want , got ))
314+ }
315+ })
316+ }
317+ }
0 commit comments