1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
|
package mapper
import (
"encoding/json"
"fmt"
"testing"
"github.com/ovn-org/libovsdb/ovsdb"
"github.com/stretchr/testify/assert"
)
var (
aString = "foo"
aEnum = "enum1"
aSet = []string{"a", "set", "of", "strings"}
aUUID0 = "2f77b348-9768-4866-b761-89d5177ecda0"
aUUID1 = "2f77b348-9768-4866-b761-89d5177ecda1"
aUUID2 = "2f77b348-9768-4866-b761-89d5177ecda2"
aUUID3 = "2f77b348-9768-4866-b761-89d5177ecda3"
aUUIDSet = []string{
aUUID0,
aUUID1,
aUUID2,
aUUID3,
}
aIntSet = []int{
3,
2,
42,
}
aFloat = 42.00
aFloatSet = []float64{
3.0,
2.0,
42.0,
}
aMap = map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
)
var testSchema = []byte(`{
"cksum": "223619766 22548",
"name": "TestSchema",
"tables": {
"TestTable": {
"columns": {
"aString": {
"type": "string"
},
"aSet": {
"type": {
"key": "string",
"max": "unlimited",
"min": 0
}
},
"aSingleSet": {
"type": {
"key": "string",
"max": "unlimited",
"min": 0,
"max": 1
}
},
"aUUIDSet": {
"type": {
"key": {
"refTable": "SomeOtherTAble",
"refType": "weak",
"type": "uuid"
},
"min": 0,
"max": "unlimited"
}
},
"aUUID": {
"type": {
"key": {
"refTable": "SomeOtherTAble",
"refType": "weak",
"type": "uuid"
},
"min": 1,
"max": 1
}
},
"aIntSet": {
"type": {
"key": {
"type": "integer"
},
"min": 0,
"max": "unlimited"
}
},
"aFloat": {
"type": {
"key": {
"type": "real"
}
}
},
"aFloatSet": {
"type": {
"key": {
"type": "real"
},
"min": 0,
"max": 10
}
},
"aEmptySet": {
"type": {
"key": {
"type": "string"
},
"min": 0,
"max": "unlimited"
}
},
"aEnum": {
"type": {
"key": {
"enum": [
"set",
[
"enum1",
"enum2",
"enum3"
]
],
"type": "string"
}
}
},
"aMap": {
"type": {
"key": "string",
"max": "unlimited",
"min": 0,
"value": "string"
}
}
}
}
}
}`)
func getOvsTestRow(t *testing.T) ovsdb.Row {
ovsRow := ovsdb.NewRow()
ovsRow["aString"] = aString
ovsRow["aSet"] = testOvsSet(t, aSet)
// Set's can hold the value if they have len == 1
ovsRow["aSingleSet"] = aString
us := make([]ovsdb.UUID, 0)
for _, u := range aUUIDSet {
us = append(us, ovsdb.UUID{GoUUID: u})
}
ovsRow["aUUIDSet"] = testOvsSet(t, us)
ovsRow["aUUID"] = ovsdb.UUID{GoUUID: aUUID0}
ovsRow["aIntSet"] = testOvsSet(t, aIntSet)
ovsRow["aFloat"] = aFloat
ovsRow["aFloatSet"] = testOvsSet(t, aFloatSet)
ovsRow["aEmptySet"] = testOvsSet(t, []string{})
ovsRow["aEnum"] = aEnum
ovsRow["aMap"] = testOvsMap(t, aMap)
return ovsRow
}
func TestMapperGetData(t *testing.T) {
type ormTestType struct {
AString string `ovsdb:"aString"`
ASet []string `ovsdb:"aSet"`
ASingleSet *string `ovsdb:"aSingleSet"`
AUUIDSet []string `ovsdb:"aUUIDSet"`
AUUID string `ovsdb:"aUUID"`
AIntSet []int `ovsdb:"aIntSet"`
AFloat float64 `ovsdb:"aFloat"`
AFloatSet []float64 `ovsdb:"aFloatSet"`
YetAnotherStringSet []string `ovsdb:"aEmptySet"`
AEnum string `ovsdb:"aEnum"`
AMap map[string]string `ovsdb:"aMap"`
NonTagged string
}
var expected = ormTestType{
AString: aString,
ASet: aSet,
ASingleSet: &aString,
AUUIDSet: aUUIDSet,
AUUID: aUUID0,
AIntSet: aIntSet,
AFloat: aFloat,
AFloatSet: aFloatSet,
YetAnotherStringSet: []string{},
AEnum: aEnum,
AMap: aMap,
NonTagged: "something",
}
ovsRow := getOvsTestRow(t)
/* Code under test */
var schema ovsdb.DatabaseSchema
if err := json.Unmarshal(testSchema, &schema); err != nil {
t.Error(err)
}
mapper := NewMapper(schema)
test := ormTestType{
NonTagged: "something",
}
testInfo, err := NewInfo("TestTable", schema.Table("TestTable"), &test)
assert.NoError(t, err)
err = mapper.GetRowData(&ovsRow, testInfo)
assert.NoError(t, err)
/*End code under test*/
if err != nil {
t.Error(err)
}
assert.Equal(t, expected, test)
}
func TestMapperNewRow(t *testing.T) {
var schema ovsdb.DatabaseSchema
if err := json.Unmarshal(testSchema, &schema); err != nil {
t.Error(err)
}
tests := []struct {
name string
objInput interface{}
expectedRow ovsdb.Row
shoulderr bool
}{{
name: "string",
objInput: &struct {
AString string `ovsdb:"aString"`
}{
AString: aString,
},
expectedRow: ovsdb.Row(map[string]interface{}{"aString": aString}),
}, {
name: "set",
objInput: &struct {
SomeSet []string `ovsdb:"aSet"`
}{
SomeSet: aSet,
},
expectedRow: ovsdb.Row(map[string]interface{}{"aSet": testOvsSet(t, aSet)}),
}, {
name: "emptySet with no column specification",
objInput: &struct {
EmptySet []string `ovsdb:"aSet"`
}{
EmptySet: []string{},
},
expectedRow: ovsdb.Row(map[string]interface{}{}),
}, {
name: "UUID",
objInput: &struct {
MyUUID string `ovsdb:"aUUID"`
}{
MyUUID: aUUID0,
},
expectedRow: ovsdb.Row(map[string]interface{}{"aUUID": ovsdb.UUID{GoUUID: aUUID0}}),
}, {
name: "aUUIDSet",
objInput: &struct {
MyUUIDSet []string `ovsdb:"aUUIDSet"`
}{
MyUUIDSet: []string{aUUID0, aUUID1},
},
expectedRow: ovsdb.Row(map[string]interface{}{"aUUIDSet": testOvsSet(t, []ovsdb.UUID{{GoUUID: aUUID0}, {GoUUID: aUUID1}})}),
}, {
name: "aIntSet",
objInput: &struct {
MyIntSet []int `ovsdb:"aIntSet"`
}{
MyIntSet: []int{0, 42},
},
expectedRow: ovsdb.Row(map[string]interface{}{"aIntSet": testOvsSet(t, []int{0, 42})}),
}, {
name: "aFloat",
objInput: &struct {
MyFloat float64 `ovsdb:"aFloat"`
}{
MyFloat: 42.42,
},
expectedRow: ovsdb.Row(map[string]interface{}{"aFloat": 42.42}),
}, {
name: "aFloatSet",
objInput: &struct {
MyFloatSet []float64 `ovsdb:"aFloatSet"`
}{
MyFloatSet: aFloatSet,
},
expectedRow: ovsdb.Row(map[string]interface{}{"aFloatSet": testOvsSet(t, aFloatSet)}),
}, {
name: "Enum",
objInput: &struct {
MyEnum string `ovsdb:"aEnum"`
}{
MyEnum: aEnum,
},
expectedRow: ovsdb.Row(map[string]interface{}{"aEnum": aEnum}),
}, {
name: "untagged fields should not affect row",
objInput: &struct {
AString string `ovsdb:"aString"`
MyStuff map[string]string
}{
AString: aString,
MyStuff: map[string]string{"this is": "private"},
},
expectedRow: ovsdb.Row(map[string]interface{}{"aString": aString}),
}, {
name: "Maps",
objInput: &struct {
MyMap map[string]string `ovsdb:"aMap"`
}{
MyMap: aMap,
},
expectedRow: ovsdb.Row(map[string]interface{}{"aMap": testOvsMap(t, aMap)}),
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("NewRow: %s", test.name), func(t *testing.T) {
mapper := NewMapper(schema)
info, err := NewInfo("TestTable", schema.Table("TestTable"), test.objInput)
assert.NoError(t, err)
row, err := mapper.NewRow(info)
if test.shoulderr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equalf(t, test.expectedRow, row, "NewRow should match expected")
}
})
}
}
func TestMapperNewRowFields(t *testing.T) {
var schema ovsdb.DatabaseSchema
if err := json.Unmarshal(testSchema, &schema); err != nil {
t.Error(err)
}
type obj struct {
MyMap map[string]string `ovsdb:"aMap"`
MySet []string `ovsdb:"aSet"`
MyString string `ovsdb:"aString"`
MyFloat float64 `ovsdb:"aFloat"`
}
testObj := obj{}
tests := []struct {
name string
prepare func(*obj)
expectedRow ovsdb.Row
fields []interface{}
err bool
}{{
name: "string",
prepare: func(o *obj) {
o.MyString = aString
},
expectedRow: ovsdb.Row(map[string]interface{}{"aString": aString}),
}, {
name: "empty string with field specification",
prepare: func(o *obj) {
o.MyString = ""
},
fields: []interface{}{&testObj.MyString},
expectedRow: ovsdb.Row(map[string]interface{}{"aString": ""}),
}, {
name: "empty set without field specification",
prepare: func(o *obj) {
},
expectedRow: ovsdb.Row(map[string]interface{}{}),
}, {
name: "empty set without field specification",
prepare: func(o *obj) {
},
fields: []interface{}{&testObj.MySet},
expectedRow: ovsdb.Row(map[string]interface{}{"aSet": testOvsSet(t, []string{})}),
}, {
name: "empty maps",
prepare: func(o *obj) {
o.MyString = "foo"
},
expectedRow: ovsdb.Row(map[string]interface{}{"aString": aString}),
}, {
name: "empty maps with field specification",
prepare: func(o *obj) {
o.MyString = "foo"
},
fields: []interface{}{&testObj.MyMap},
expectedRow: ovsdb.Row(map[string]interface{}{"aMap": testOvsMap(t, map[string]string{})}),
}, {
name: "Complex object with field selection",
prepare: func(o *obj) {
o.MyString = aString
o.MyMap = aMap
o.MySet = aSet
o.MyFloat = aFloat
},
fields: []interface{}{&testObj.MyMap, &testObj.MySet},
expectedRow: ovsdb.Row(map[string]interface{}{"aMap": testOvsMap(t, aMap), "aSet": testOvsSet(t, aSet)}),
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("NewRow: %s", test.name), func(t *testing.T) {
mapper := NewMapper(schema)
// Clean the test object
testObj.MyString = ""
testObj.MyMap = nil
testObj.MySet = nil
testObj.MyFloat = 0
test.prepare(&testObj)
info, err := NewInfo("TestTable", schema.Table("TestTable"), &testObj)
assert.NoError(t, err)
row, err := mapper.NewRow(info, test.fields...)
if test.err {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equalf(t, test.expectedRow, row, "NewRow should match expected")
}
})
}
}
func TestMapperCondition(t *testing.T) {
var testSchema = []byte(`{
"cksum": "223619766 22548",
"name": "TestSchema",
"tables": {
"TestTable": {
"indexes": [["name"],["composed_1","composed_2"]],
"columns": {
"name": {
"type": "string"
},
"composed_1": {
"type": {
"key": "string"
}
},
"composed_2": {
"type": {
"key": "string"
}
},
"config": {
"type": {
"key": "string",
"max": "unlimited",
"min": 0,
"value": "string"
}
}
}
}
}
}`)
type testType struct {
ID string `ovsdb:"_uuid"`
MyName string `ovsdb:"name"`
Config map[string]string `ovsdb:"config"`
Comp1 string `ovsdb:"composed_1"`
Comp2 string `ovsdb:"composed_2"`
}
var schema ovsdb.DatabaseSchema
if err := json.Unmarshal(testSchema, &schema); err != nil {
t.Fatal(err)
}
mapper := NewMapper(schema)
type Test struct {
name string
prepare func(*testType)
expected []ovsdb.Condition
index []interface{}
err bool
}
testObj := testType{}
tests := []Test{
{
name: "simple index",
prepare: func(t *testType) {
t.ID = ""
t.MyName = "foo"
t.Config = nil
t.Comp1 = ""
t.Comp2 = ""
},
index: []interface{}{},
expected: []ovsdb.Condition{{Column: "name", Function: ovsdb.ConditionEqual, Value: "foo"}},
err: false,
},
{
name: "UUID",
prepare: func(t *testType) {
t.ID = aUUID0
t.MyName = "foo"
t.Config = nil
t.Comp1 = ""
t.Comp2 = ""
},
index: []interface{}{},
expected: []ovsdb.Condition{{Column: "_uuid", Function: ovsdb.ConditionEqual, Value: ovsdb.UUID{GoUUID: aUUID0}}},
err: false,
},
{
name: "specify index",
prepare: func(t *testType) {
t.ID = aUUID0
t.MyName = "foo"
t.Config = nil
t.Comp1 = ""
t.Comp2 = ""
},
index: []interface{}{&testObj.MyName},
expected: []ovsdb.Condition{{Column: "name", Function: ovsdb.ConditionEqual, Value: "foo"}},
err: false,
},
{
name: "complex index",
prepare: func(t *testType) {
t.ID = ""
t.MyName = ""
t.Config = nil
t.Comp1 = "foo"
t.Comp2 = "bar"
},
expected: []ovsdb.Condition{
{Column: "composed_1", Function: ovsdb.ConditionEqual, Value: "foo"},
{Column: "composed_2", Function: ovsdb.ConditionEqual, Value: "bar"}},
index: []interface{}{},
err: false,
},
{
name: "first index",
prepare: func(t *testType) {
t.ID = ""
t.MyName = "something"
t.Config = nil
t.Comp1 = "foo"
t.Comp2 = "bar"
},
expected: []ovsdb.Condition{{Column: "name", Function: ovsdb.ConditionEqual, Value: "something"}},
index: []interface{}{},
err: false,
},
{
name: "Error: None",
prepare: func(t *testType) {
t.ID = ""
t.MyName = ""
t.Config = map[string]string{"foo": "bar"}
t.Comp1 = ""
t.Comp2 = ""
},
index: []interface{}{},
err: true,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("newEqualityCondition_%s", tt.name), func(t *testing.T) {
tt.prepare(&testObj)
info, err := NewInfo("TestTable", schema.Table("TestTable"), &testObj)
assert.NoError(t, err)
conds, err := mapper.NewEqualityCondition(info, tt.index...)
if tt.err {
if err == nil {
t.Errorf("expected an error but got none")
}
} else {
if err != nil {
t.Error(err)
}
if !assert.ElementsMatch(t, tt.expected, conds, "Condition must match expected") {
t.Logf("%v \n", conds)
}
}
})
}
}
func TestMapperEqualIndexes(t *testing.T) {
var testSchema = []byte(`{
"cksum": "223619766 22548",
"name": "TestSchema",
"tables": {
"TestTable": {
"indexes": [["name"],["composed_1","composed_2"]],
"columns": {
"name": {
"type": "string"
},
"composed_1": {
"type": {
"key": "string"
}
},
"composed_2": {
"type": {
"key": "string"
}
},
"int1": {
"type": {
"key": "integer"
}
},
"int2": {
"type": {
"key": "integer"
}
},
"config": {
"type": {
"key": "string",
"max": "unlimited",
"min": 0,
"value": "string"
}
}
}
}
}
}`)
type testType struct {
ID string `ovsdb:"_uuid"`
MyName string `ovsdb:"name"`
Config map[string]string `ovsdb:"config"`
Comp1 string `ovsdb:"composed_1"`
Comp2 string `ovsdb:"composed_2"`
Int1 int `ovsdb:"int1"`
Int2 int `ovsdb:"int2"`
}
var schema ovsdb.DatabaseSchema
if err := json.Unmarshal(testSchema, &schema); err != nil {
t.Fatal(err)
}
mapper := NewMapper(schema)
type Test struct {
name string
obj1 testType
obj2 testType
expected bool
indexes []string
}
tests := []Test{
{
name: "same simple index",
obj1: testType{
MyName: "foo",
},
obj2: testType{
MyName: "foo",
},
expected: true,
indexes: []string{},
},
{
name: "diff simple index",
obj1: testType{
MyName: "foo",
},
obj2: testType{
MyName: "bar",
},
expected: false,
indexes: []string{},
},
{
name: "same uuid",
obj1: testType{
ID: aUUID0,
MyName: "foo",
},
obj2: testType{
ID: aUUID0,
MyName: "bar",
},
expected: true,
indexes: []string{},
},
{
name: "diff uuid",
obj1: testType{
ID: aUUID0,
MyName: "foo",
},
obj2: testType{
ID: aUUID1,
MyName: "bar",
},
expected: false,
indexes: []string{},
},
{
name: "same complex_index",
obj1: testType{
ID: aUUID0,
MyName: "foo",
Comp1: "foo",
Comp2: "bar",
},
obj2: testType{
ID: aUUID1,
MyName: "bar",
Comp1: "foo",
Comp2: "bar",
},
expected: true,
indexes: []string{},
},
{
name: "different",
obj1: testType{
ID: aUUID0,
MyName: "name1",
Comp1: "foo",
Comp2: "bar",
},
obj2: testType{
ID: aUUID1,
MyName: "name2",
Comp1: "foo",
Comp2: "bar2",
},
expected: false,
indexes: []string{},
},
{
name: "same additional index",
obj1: testType{
ID: aUUID0,
MyName: "name1",
Comp1: "foo",
Comp2: "bar1",
Int1: 42,
},
obj2: testType{
ID: aUUID1,
MyName: "name2",
Comp1: "foo",
Comp2: "bar2",
Int1: 42,
},
expected: true,
indexes: []string{"int1"},
},
{
name: "diff additional index",
obj1: testType{
ID: aUUID0,
MyName: "name1",
Comp1: "foo",
Comp2: "bar1",
Int1: 42,
},
obj2: testType{
ID: aUUID1,
MyName: "name2",
Comp1: "foo",
Comp2: "bar2",
Int1: 420,
},
expected: false,
indexes: []string{"int1"},
},
{
name: "same additional indexes ",
obj1: testType{
ID: aUUID0,
MyName: "name1",
Comp1: "foo",
Comp2: "bar1",
Int1: 42,
Int2: 25,
},
obj2: testType{
ID: aUUID1,
MyName: "name2",
Comp1: "foo",
Comp2: "bar2",
Int1: 42,
Int2: 25,
},
expected: true,
indexes: []string{"int1", "int2"},
},
{
name: "diff additional indexes ",
obj1: testType{
ID: aUUID0,
MyName: "name1",
Comp1: "foo",
Comp2: "bar1",
Int1: 42,
Int2: 50,
},
obj2: testType{
ID: aUUID1,
MyName: "name2",
Comp1: "foo",
Comp2: "bar2",
Int1: 42,
Int2: 25,
},
expected: false,
indexes: []string{"int1", "int2"},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Equal %s", test.name), func(t *testing.T) {
info1, err := NewInfo("TestTable", schema.Table("TestTable"), &test.obj1)
assert.NoError(t, err)
info2, err := NewInfo("TestTable", schema.Table("TestTable"), &test.obj2)
assert.NoError(t, err)
eq, err := mapper.equalIndexes(info1, info2, test.indexes...)
assert.Nil(t, err)
assert.Equalf(t, test.expected, eq, "equal value should match expected")
})
}
// Test we can also use field pointers
obj1 := testType{
ID: aUUID0,
MyName: "name1",
Comp1: "foo",
Comp2: "bar1",
Int1: 42,
Int2: 25,
}
obj2 := testType{
ID: aUUID1,
MyName: "name2",
Comp1: "foo",
Comp2: "bar2",
Int1: 42,
Int2: 25,
}
info1, err := NewInfo("TestTable", schema.Table("TestTable"), &obj1)
assert.NoError(t, err)
info2, err := NewInfo("TestTable", schema.Table("TestTable"), &obj2)
assert.NoError(t, err)
eq, err := mapper.EqualFields(info1, info2, &obj1.Int1, &obj1.Int2)
assert.Nil(t, err)
assert.True(t, eq)
// Using pointers to second value is not supported
_, err = mapper.EqualFields(info1, info2, &obj2.Int1, &obj2.Int2)
assert.NotNil(t, err)
}
func TestMapperMutation(t *testing.T) {
var testSchema = []byte(`{
"cksum": "223619766 22548",
"name": "TestSchema",
"tables": {
"TestTable": {
"columns": {
"string": {
"type": "string"
},
"set": {
"type": {
"key": "string",
"min": 0,
"max": "unlimited"
}
},
"map": {
"type": {
"key": "string",
"value": "string"
}
},
"unmutable": {
"mutable": false,
"type": {
"key": "integer"
}
},
"int": {
"type": {
"key": "integer"
}
}
}
}
}
}`)
type testType struct {
UUID string `ovsdb:"_uuid"`
String string `ovsdb:"string"`
Set []string `ovsdb:"set"`
Map map[string]string `ovsdb:"map"`
Int int `ovsdb:"int"`
UnMutable int `ovsdb:"unmutable"`
}
var schema ovsdb.DatabaseSchema
if err := json.Unmarshal(testSchema, &schema); err != nil {
t.Fatal(err)
}
mapper := NewMapper(schema)
type Test struct {
name string
column string
obj testType
expected *ovsdb.Mutation
mutator ovsdb.Mutator
value interface{}
err bool
}
tests := []Test{
{
name: "string",
column: "string",
obj: testType{},
mutator: ovsdb.MutateOperationAdd,
err: true,
},
{
name: "Increment integer",
column: "int",
obj: testType{},
mutator: ovsdb.MutateOperationAdd,
value: 1,
expected: ovsdb.NewMutation("int", ovsdb.MutateOperationAdd, 1),
err: false,
},
{
name: "Increment integer",
column: "int",
obj: testType{},
mutator: ovsdb.MutateOperationModulo,
value: 2,
expected: ovsdb.NewMutation("int", ovsdb.MutateOperationModulo, 2),
err: false,
},
{
name: "non-mutable",
column: "unmutable",
obj: testType{},
mutator: ovsdb.MutateOperationSubtract,
value: 2,
err: true,
},
{
name: "Add element to set ",
column: "set",
obj: testType{},
mutator: ovsdb.MutateOperationInsert,
value: []string{"foo"},
expected: ovsdb.NewMutation("set", ovsdb.MutateOperationInsert, testOvsSet(t, []string{"foo"})),
err: false,
},
{
name: "Delete element from set ",
column: "set",
obj: testType{},
mutator: ovsdb.MutateOperationDelete,
value: []string{"foo"},
expected: ovsdb.NewMutation("set", ovsdb.MutateOperationDelete, testOvsSet(t, []string{"foo"})),
err: false,
},
{
name: "Delete keys from map ",
column: "map",
obj: testType{},
mutator: ovsdb.MutateOperationDelete,
value: []string{"foo", "bar"},
expected: ovsdb.NewMutation("map", ovsdb.MutateOperationDelete, testOvsSet(t, []string{"foo", "bar"})),
err: false,
},
{
name: "Delete key value pairs from map ",
column: "map",
obj: testType{},
mutator: ovsdb.MutateOperationDelete,
value: map[string]string{"foo": "bar"},
expected: ovsdb.NewMutation("map", ovsdb.MutateOperationDelete, testOvsMap(t, map[string]string{"foo": "bar"})),
err: false,
},
{
name: "Insert elements in map ",
column: "map",
obj: testType{},
mutator: ovsdb.MutateOperationInsert,
value: map[string]string{"foo": "bar"},
expected: ovsdb.NewMutation("map", ovsdb.MutateOperationInsert, testOvsMap(t, map[string]string{"foo": "bar"})),
err: false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("newMutation%s", test.name), func(t *testing.T) {
info, err := NewInfo("TestTable", schema.Table("TestTable"), &test.obj)
assert.NoError(t, err)
mutation, err := mapper.NewMutation(info, test.column, test.mutator, test.value)
if test.err {
if err == nil {
t.Errorf("expected an error but got none")
}
} else {
if err != nil {
t.Error(err)
}
}
assert.Equalf(t, test.expected, mutation, "Mutation must match expected")
})
}
}
func testOvsSet(t *testing.T, set interface{}) ovsdb.OvsSet {
oSet, err := ovsdb.NewOvsSet(set)
assert.Nil(t, err)
return oSet
}
func testOvsMap(t *testing.T, set interface{}) ovsdb.OvsMap {
oMap, err := ovsdb.NewOvsMap(set)
assert.Nil(t, err)
return oMap
}
|