File size: 4,742 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package moduletools

import (
	"testing"

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

func TestObjectDiff(t *testing.T) {
	t.Run("strings are the same", func(t *testing.T) {
		objDiff := NewObjectDiff(nil).
			WithProp("sameStrings", "Some string", "Some string")

		assert.False(t, objDiff.IsChangedProp("sameStrings"))
	})

	t.Run("strings are different", func(t *testing.T) {
		objDiff := NewObjectDiff(nil).
			WithProp("differentStrings1", "Some string", "some string").
			WithProp("differentStrings2", "Some string", "Some string, but different")

		assert.True(t, objDiff.IsChangedProp("differentStrings1"))
		assert.True(t, objDiff.IsChangedProp("differentStrings2"))
	})

	t.Run("string slices are the same", func(t *testing.T) {
		objDiff := NewObjectDiff(nil).
			WithProp("sameStringSlices1", []string{"aa", "bb", "cc"}, []string{"aa", "bb", "cc"}).
			WithProp("sameStringSlices2", []string{"aa", "bb", "cc"}, []interface{}{"aa", "bb", "cc"}).
			WithProp("sameStringSlices3", []interface{}{"aa", "bb", "cc"}, []string{"aa", "bb", "cc"}).
			WithProp("sameStringSlices4", []interface{}{"aa", "bb", "cc"}, []interface{}{"aa", "bb", "cc"}).
			WithProp("sameStringSlices5", []string{}, []string{}).
			WithProp("sameStringSlices6", []string{}, []interface{}{}).
			WithProp("sameStringSlices7", []interface{}{}, []string{}).
			WithProp("sameStringSlices8", []interface{}{}, []interface{}{})

		for _, prop := range []string{
			"sameStringSlices1", "sameStringSlices2", "sameStringSlices3", "sameStringSlices4",
			"sameStringSlices5", "sameStringSlices6", "sameStringSlices7", "sameStringSlices8",
		} {
			assert.False(t, objDiff.IsChangedProp(prop))
		}
	})

	t.Run("string slices are different", func(t *testing.T) {
		objDiff := NewObjectDiff(nil).
			WithProp("differentStringSlices1", []string{"aa", "bb", "cc"}, []string{"aa", "bb", "cc", "dd"}).
			WithProp("differentStringSlices2", []string{"aa", "bb", "cc"}, []interface{}{"aa", "bb", "cc", "dd"}).
			WithProp("differentStringSlices3", []interface{}{"aa", "bb", "cc"}, []string{"aa", "bb", "cc", "dd"}).
			WithProp("differentStringSlices4", []interface{}{"aa", "bb", "cc"}, []interface{}{"aa", "bb", "cc", "dd"}).
			WithProp("differentStringSlices5", []string{"aa", "bb", "cc"}, []string{"cc", "bb", "aa"}).
			WithProp("differentStringSlices6", []string{"aa", "bb", "cc"}, []interface{}{"cc", "bb", "aa"}).
			WithProp("differentStringSlices7", []interface{}{"aa", "bb", "cc"}, []string{"cc", "bb", "aa"}).
			WithProp("differentStringSlices8", []interface{}{"aa", "bb", "cc"}, []interface{}{"cc", "bb", "aa"})

		for _, prop := range []string{
			"differentStringSlices1", "differentStringSlices2", "differentStringSlices3", "differentStringSlices4",
			"differentStringSlices5", "differentStringSlices6", "differentStringSlices7", "differentStringSlices8",
		} {
			assert.True(t, objDiff.IsChangedProp(prop))
		}
	})

	t.Run("nils are different", func(t *testing.T) {
		objDiff := NewObjectDiff(nil).
			WithProp("nil1", "some value", nil).
			WithProp("nil2", nil, "some value").
			WithProp("nil3", []string{"some value"}, nil).
			WithProp("nil4", nil, []string{"some value"})

		for _, prop := range []string{"nil1", "nil2", "nil3", "nil4"} {
			assert.True(t, objDiff.IsChangedProp(prop))
		}
	})

	t.Run("not set is the same", func(t *testing.T) {
		objDiff := NewObjectDiff(nil)

		assert.False(t, objDiff.IsChangedProp("notSet"))
	})

	t.Run("non strings are different", func(t *testing.T) {
		objDiff := NewObjectDiff(nil).
			WithProp("float1", 1.23, 1.23).
			WithProp("float2", 1.23, 1.234).
			WithProp("int1", 1, 1).
			WithProp("int2", 1, 2).
			WithProp("bool1", true, true).
			WithProp("bool2", true, false).
			WithProp("floatSlice1", []float64{1.23}, []float64{1.23}).
			WithProp("floatSlice2", []float64{1.23}, []float64{1.23, 1.234}).
			WithProp("intSlice1", []int64{1}, []int64{1}).
			WithProp("intSlice2", []int64{1}, []int64{1, 2}).
			WithProp("boolSlice1", []bool{false}, []bool{false}).
			WithProp("boolSlice2", []bool{false}, []bool{false, true})

		for _, prop := range []string{
			"float1", "float2", "int1", "int2", "bool1", "bool2",
			"floatSlice1", "floatSlice2", "intSlice1", "intSlice2", "boolSlice1", "boolSlice2",
		} {
			assert.True(t, objDiff.IsChangedProp(prop))
		}
	})
}