File: mutation_test.go

package info (click to toggle)
golang-github-ovn-org-libovsdb 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,440 kB
  • sloc: makefile: 52; sh: 14
file content (114 lines) | stat: -rw-r--r-- 2,759 bytes parent folder | download | duplicates (2)
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
package ovsdb

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestMutationMarshalUnmarshalJSON(t *testing.T) {
	tests := []struct {
		name     string
		mutation Mutation
		want     string
		wantErr  bool
	}{
		{
			"test delete",
			Mutation{"foo", MutateOperationDelete, "bar"},
			`[ "foo", "delete", "bar" ]`,
			false,
		},
		{
			"test insert",
			Mutation{"foo", MutateOperationInsert, "bar"},
			`[ "foo", "insert", "bar" ]`,
			false,
		},
		{
			"test add",
			Mutation{"foo", MutateOperationAdd, "bar"},
			`[ "foo", "+=", "bar" ]`,
			false,
		},
		{
			"test subtract",
			Mutation{"foo", MutateOperationSubtract, "bar"},
			`[ "foo", "-=", "bar" ]`,
			false,
		},
		{
			"test multiply",
			Mutation{"foo", MutateOperationMultiply, "bar"},
			`[ "foo", "*=", "bar" ]`,
			false,
		},
		{
			"test divide",
			Mutation{"foo", MutateOperationDivide, "bar"},
			`[ "foo", "/=", "bar" ]`,
			false,
		},
		{
			"test modulo",
			Mutation{"foo", MutateOperationModulo, "bar"},
			`[ "foo", "%=", "bar" ]`,
			false,
		},
		{
			"test uuid",
			Mutation{"foo", MutateOperationInsert, UUID{GoUUID: "foo"}},
			`[ "foo", "insert", ["named-uuid", "foo"] ]`,
			false,
		},
		{
			"test set",
			Mutation{"foo", MutateOperationInsert, OvsSet{GoSet: []interface{}{"foo", "bar", "baz"}}},
			`[ "foo", "insert", ["set",["foo", "bar", "baz"]] ]`,
			false,
		},
		{
			"test map",
			Mutation{"foo", MutateOperationInsert, OvsMap{GoMap: map[interface{}]interface{}{"foo": "bar", "baz": "quux"}}},
			`[ "foo", "insert", ["map",[["foo", "bar"], ["baz", "quux"]]]]`,
			false,
		},
		{
			"test uuid set",
			Mutation{"foo", MutateOperationInsert, OvsSet{GoSet: []interface{}{UUID{GoUUID: "foo"}, UUID{GoUUID: "bar"}}}},
			`[ "foo", "insert", ["set",[["named-uuid", "foo"], ["named-uuid", "bar"]]] ]`,
			false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := json.Marshal(tt.mutation)
			if err != nil {
				t.Fatal(err)
			}
			// testing JSON equality is flaky for ovsdb notated maps
			// it's safe to skip this as we test from json->object later
			if tt.name != "test map" {
				assert.JSONEq(t, tt.want, string(got))
			}
			var c Mutation
			if err := json.Unmarshal(got, &c); err != nil {
				t.Fatal(err)
			}
			assert.Equal(t, tt.mutation.Column, c.Column)
			assert.Equal(t, tt.mutation.Mutator, c.Mutator)
			v := reflect.TypeOf(tt.mutation.Value)
			vv := reflect.ValueOf(c.Value)
			if !vv.IsValid() {
				t.Fatalf("c.Value is empty: %v", c.Value)
			}
			assert.Equal(t, v, vv.Type())
			assert.Equal(t, tt.mutation.Value, vv.Convert(v).Interface())
			if vv.Kind() == reflect.String {
				assert.Equal(t, tt.mutation.Value, vv.String())
			}
		})
	}
}