@@ -217,52 +217,51 @@ func TestAPIListPredicate(t *testing.T) {
217
217
218
218
test := []struct {
219
219
name string
220
- predicate interface {}
220
+ m model.Model
221
+ predicate func (model.Model ) bool
221
222
content []model.Model
222
223
err bool
223
224
}{
224
225
{
225
226
name : "none" ,
226
- predicate : func (t * testLogicalSwitch ) bool {
227
+ m : & testLogicalSwitch {},
228
+ predicate : func (t model.Model ) bool {
227
229
return false
228
230
},
229
231
content : []model.Model {},
230
232
err : false ,
231
233
},
232
234
{
233
235
name : "all" ,
234
- predicate : func (t * testLogicalSwitch ) bool {
236
+ m : & testLogicalSwitch {},
237
+ predicate : func (t model.Model ) bool {
235
238
return true
236
239
},
237
240
content : lscacheList ,
238
241
err : false ,
239
242
},
240
243
{
241
244
name : "nil function must fail" ,
245
+ m : & testLogicalSwitch {},
242
246
err : true ,
243
247
},
244
248
{
245
249
name : "arbitrary condition" ,
246
- predicate : func (t * testLogicalSwitch ) bool {
250
+ m : & testLogicalSwitch {},
251
+ predicate : func (m model.Model ) bool {
252
+ t := m .(* testLogicalSwitch )
247
253
return strings .HasPrefix (t .Name , "magic" )
248
254
},
249
255
content : []model.Model {lscacheList [1 ], lscacheList [3 ]},
250
256
err : false ,
251
257
},
252
- {
253
- name : "error wrong type" ,
254
- predicate : func (t testLogicalSwitch ) string {
255
- return "foo"
256
- },
257
- err : true ,
258
- },
259
258
}
260
259
261
260
for _ , tt := range test {
262
261
t .Run (fmt .Sprintf ("ApiListPredicate: %s" , tt .name ), func (t * testing.T ) {
263
262
var result []* testLogicalSwitch
264
263
api := newAPI (tcache , & discardLogger )
265
- cond := api .WhereCache (tt .predicate )
264
+ cond := api .WhereCache (tt .m , tt . predicate )
266
265
err := cond .List (context .Background (), & result )
267
266
if tt .err {
268
267
assert .NotNil (t , err )
@@ -536,26 +535,14 @@ func TestAPIListMulti(t *testing.T) {
536
535
func TestConditionFromFunc (t * testing.T ) {
537
536
test := []struct {
538
537
name string
539
- arg interface {}
538
+ m model.Model
539
+ arg func (model.Model ) bool
540
540
err bool
541
541
}{
542
- {
543
- name : "wrong function must fail" ,
544
- arg : func (s string ) bool {
545
- return false
546
- },
547
- err : true ,
548
- },
549
- {
550
- name : "wrong function must fail2 " ,
551
- arg : func (t * testLogicalSwitch ) string {
552
- return "foo"
553
- },
554
- err : true ,
555
- },
556
542
{
557
543
name : "correct func should succeed" ,
558
- arg : func (t * testLogicalSwitch ) bool {
544
+ m : & testLogicalSwitch {},
545
+ arg : func (m model.Model ) bool {
559
546
return true
560
547
},
561
548
err : false ,
@@ -566,7 +553,7 @@ func TestConditionFromFunc(t *testing.T) {
566
553
t .Run (fmt .Sprintf ("conditionFromFunc: %s" , tt .name ), func (t * testing.T ) {
567
554
cache := apiTestCache (t , nil )
568
555
apiIface := newAPI (cache , & discardLogger )
569
- condition := apiIface .(api ).conditionFromFunc (tt .arg )
556
+ condition := apiIface .(api ).conditionFromFunc (tt .m , tt . arg )
570
557
if tt .err {
571
558
assert .IsType (t , & errorConditional {}, condition )
572
559
} else {
@@ -584,23 +571,6 @@ func TestConditionFromModel(t *testing.T) {
584
571
conds []model.Condition
585
572
err bool
586
573
}{
587
- {
588
- name : "wrong model must fail" ,
589
- models : []model.Model {
590
- & struct { a string }{},
591
- },
592
- err : true ,
593
- },
594
- {
595
- name : "wrong condition must fail" ,
596
- models : []model.Model {
597
- & struct {
598
- a string `ovsdb:"_uuid"`
599
- }{},
600
- },
601
- conds : []model.Condition {{Field : "foo" }},
602
- err : true ,
603
- },
604
574
{
605
575
name : "correct model must succeed" ,
606
576
models : []model.Model {
@@ -1009,7 +979,8 @@ func TestAPIMutate(t *testing.T) {
1009
979
{
1010
980
name : "select single by predicate name insert element in map" ,
1011
981
condition : func (a API ) ConditionalAPI {
1012
- return a .WhereCache (func (lsp * testLogicalSwitchPort ) bool {
982
+ return a .WhereCache (& testLogicalSwitchPort {}, func (m model.Model ) bool {
983
+ lsp := m .(* testLogicalSwitchPort )
1013
984
return lsp .Name == "lsp2"
1014
985
})
1015
986
},
@@ -1033,7 +1004,8 @@ func TestAPIMutate(t *testing.T) {
1033
1004
{
1034
1005
name : "select many by predicate name insert element in map" ,
1035
1006
condition : func (a API ) ConditionalAPI {
1036
- return a .WhereCache (func (lsp * testLogicalSwitchPort ) bool {
1007
+ return a .WhereCache (& testLogicalSwitchPort {}, func (m model.Model ) bool {
1008
+ lsp := m .(* testLogicalSwitchPort )
1037
1009
return lsp .Type == "someType"
1038
1010
})
1039
1011
},
@@ -1063,7 +1035,8 @@ func TestAPIMutate(t *testing.T) {
1063
1035
{
1064
1036
name : "No mutations should error" ,
1065
1037
condition : func (a API ) ConditionalAPI {
1066
- return a .WhereCache (func (lsp * testLogicalSwitchPort ) bool {
1038
+ return a .WhereCache (& testLogicalSwitchPort {}, func (m model.Model ) bool {
1039
+ lsp := m .(* testLogicalSwitchPort )
1067
1040
return lsp .Type == "someType"
1068
1041
})
1069
1042
},
@@ -1411,7 +1384,8 @@ func TestAPIUpdate(t *testing.T) {
1411
1384
{
1412
1385
name : "select multiple by predicate change multiple field" ,
1413
1386
condition : func (a API ) ConditionalAPI {
1414
- return a .WhereCache (func (t * testLogicalSwitchPort ) bool {
1387
+ return a .WhereCache (& testLogicalSwitchPort {}, func (m model.Model ) bool {
1388
+ t := m .(* testLogicalSwitchPort )
1415
1389
return t .Enabled != nil && * t .Enabled == true
1416
1390
})
1417
1391
},
@@ -1645,7 +1619,8 @@ func TestAPIDelete(t *testing.T) {
1645
1619
{
1646
1620
name : "select multiple by predicate" ,
1647
1621
condition : func (a API ) ConditionalAPI {
1648
- return a .WhereCache (func (t * testLogicalSwitchPort ) bool {
1622
+ return a .WhereCache (& testLogicalSwitchPort {}, func (m model.Model ) bool {
1623
+ t := m .(* testLogicalSwitchPort )
1649
1624
return t .Enabled != nil && * t .Enabled == true
1650
1625
})
1651
1626
},
@@ -1724,29 +1699,36 @@ func BenchmarkAPIList(b *testing.B) {
1724
1699
1725
1700
test := []struct {
1726
1701
name string
1727
- predicate interface {}
1702
+ m model.Model
1703
+ predicate func (model.Model ) bool
1728
1704
}{
1729
1705
{
1730
1706
name : "predicate returns none" ,
1731
- predicate : func (t * testLogicalSwitchPort ) bool {
1707
+ m : & testLogicalSwitchPort {},
1708
+ predicate : func (t model.Model ) bool {
1732
1709
return false
1733
1710
},
1734
1711
},
1735
1712
{
1736
1713
name : "predicate returns all" ,
1737
- predicate : func (t * testLogicalSwitchPort ) bool {
1714
+ m : & testLogicalSwitchPort {},
1715
+ predicate : func (t model.Model ) bool {
1738
1716
return true
1739
1717
},
1740
1718
},
1741
1719
{
1742
1720
name : "predicate on an arbitrary condition" ,
1743
- predicate : func (t * testLogicalSwitchPort ) bool {
1721
+ m : & testLogicalSwitchPort {},
1722
+ predicate : func (m model.Model ) bool {
1723
+ t := m .(* testLogicalSwitchPort )
1744
1724
return strings .HasPrefix (t .Name , "ls1" )
1745
1725
},
1746
1726
},
1747
1727
{
1748
1728
name : "predicate matches name" ,
1749
- predicate : func (t * testLogicalSwitchPort ) bool {
1729
+ m : & testLogicalSwitchPort {},
1730
+ predicate : func (m model.Model ) bool {
1731
+ t := m .(* testLogicalSwitchPort )
1750
1732
return t .Name == lscacheList [index ].Name
1751
1733
},
1752
1734
},
@@ -1763,7 +1745,7 @@ func BenchmarkAPIList(b *testing.B) {
1763
1745
api := newAPI (tcache , & discardLogger )
1764
1746
var cond ConditionalAPI
1765
1747
if tt .predicate != nil {
1766
- cond = api .WhereCache (tt .predicate )
1748
+ cond = api .WhereCache (tt .m , tt . predicate )
1767
1749
} else {
1768
1750
cond = api .Where (lscacheList [index ])
1769
1751
}
@@ -1979,7 +1961,7 @@ func TestAPIWait(t *testing.T) {
1979
1961
{
1980
1962
name : "no operation" ,
1981
1963
condition : func (a API ) ConditionalAPI {
1982
- return a .WhereCache (func (t * testLogicalSwitchPort ) bool { return false })
1964
+ return a .WhereCache (& testLogicalSwitchPort {}, func (t model. Model ) bool { return false })
1983
1965
},
1984
1966
until : "==" ,
1985
1967
prepare : func () (model.Model , []interface {}) {
0 commit comments