forked from deepnoodle-ai/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable_container_test.go
More file actions
119 lines (104 loc) · 2.66 KB
/
variable_container_test.go
File metadata and controls
119 lines (104 loc) · 2.66 KB
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
package workflow
import (
"testing"
"github.com/deepnoodle-ai/workflow/internal/require"
)
func TestGeneratePatches(t *testing.T) {
t.Run("no changes", func(t *testing.T) {
original := map[string]any{
"a": 1,
"b": "hello",
}
modified := map[string]any{
"a": 1,
"b": "hello",
}
patches := generatePatches(original, modified)
require.Len(t, patches, 0)
})
t.Run("add new variable", func(t *testing.T) {
original := map[string]any{"a": 1}
modified := map[string]any{
"a": 1,
"b": "new",
}
patches := generatePatches(original, modified)
require.Len(t, patches, 1)
require.Equal(t, "b", patches[0].Variable())
require.Equal(t, "new", patches[0].Value())
require.False(t, patches[0].Delete())
})
t.Run("modify existing variable", func(t *testing.T) {
original := map[string]any{
"a": 1,
"b": "old",
}
modified := map[string]any{
"a": 2,
"b": "new",
}
patches := generatePatches(original, modified)
require.Len(t, patches, 2)
var aPatch, bPatch *patch
for i := range patches {
switch patches[i].Variable() {
case "a":
aPatch = &patches[i]
case "b":
bPatch = &patches[i]
}
}
require.NotNil(t, aPatch)
require.NotNil(t, bPatch)
require.Equal(t, 2, aPatch.Value())
require.False(t, aPatch.Delete())
require.Equal(t, "new", bPatch.Value())
require.False(t, bPatch.Delete())
})
t.Run("delete variable", func(t *testing.T) {
original := map[string]any{
"a": 1,
"b": "delete_me",
}
modified := map[string]any{"a": 1}
patches := generatePatches(original, modified)
require.Len(t, patches, 1)
require.Equal(t, "b", patches[0].Variable())
require.Nil(t, patches[0].Value())
require.True(t, patches[0].Delete())
})
t.Run("mixed operations", func(t *testing.T) {
original := map[string]any{
"keep": "unchanged",
"modify": "old_value",
"delete": "remove_me",
}
modified := map[string]any{
"keep": "unchanged",
"modify": "new_value",
"add": "brand_new",
}
patches := generatePatches(original, modified)
require.Len(t, patches, 3)
var modifyPatch, addPatch, deletePatch *patch
for i := range patches {
switch patches[i].Variable() {
case "modify":
modifyPatch = &patches[i]
case "add":
addPatch = &patches[i]
case "delete":
deletePatch = &patches[i]
}
}
require.NotNil(t, modifyPatch)
require.Equal(t, "new_value", modifyPatch.Value())
require.False(t, modifyPatch.Delete())
require.NotNil(t, addPatch)
require.Equal(t, "brand_new", addPatch.Value())
require.False(t, addPatch.Delete())
require.NotNil(t, deletePatch)
require.Nil(t, deletePatch.Value())
require.True(t, deletePatch.Delete())
})
}