Skip to content

Commit 506b836

Browse files
authored
Fixes evaluating empty value from repository when syncing API (#42)
1 parent 86f4aa0 commit 506b836

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"domain": {
3+
"group": [
4+
{
5+
"name": "Release 1",
6+
"description": "Showcase configuration",
7+
"activated": true,
8+
"config": [
9+
{
10+
"key": "MY_SWITCHER_1",
11+
"activated": false,
12+
"strategies": [
13+
{
14+
"strategy": "VALUE_VALIDATION",
15+
"activated": false,
16+
"operation": "EXIST",
17+
"values": [
18+
"user_1"
19+
]
20+
}
21+
],
22+
"components": [
23+
"switcher-playground"
24+
]
25+
},
26+
{
27+
"key": "MY_SWITCHER_2",
28+
"description": "",
29+
"activated": false,
30+
"strategies": [],
31+
"components": [
32+
"switcher-playground"
33+
]
34+
},
35+
{
36+
"key": "MY_SWITCHER_3",
37+
"description": "",
38+
"activated": true,
39+
"strategies": [],
40+
"components": [
41+
"benchmark"
42+
]
43+
}
44+
]
45+
}
46+
]
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"domain": {
3+
"group": [
4+
{
5+
"name": "Release 1",
6+
"description": "Showcase configuration",
7+
"activated": true,
8+
"config": [
9+
{
10+
"key": "MY_SWITCHER_1",
11+
"description": "My first switcher",
12+
"activated": true,
13+
"strategies": [
14+
{
15+
"strategy": "VALUE_VALIDATION",
16+
"operation": "EXIST",
17+
"values": [
18+
"user_1"
19+
]
20+
}
21+
],
22+
"components": [
23+
"switcher-playground"
24+
]
25+
},
26+
{
27+
"key": "MY_SWITCHER_2",
28+
"description": "",
29+
"activated": false,
30+
"strategies": [],
31+
"components": [
32+
"switcher-playground"
33+
]
34+
},
35+
{
36+
"key": "MY_SWITCHER_3",
37+
"description": "",
38+
"activated": true,
39+
"strategies": [],
40+
"components": [
41+
"benchmark"
42+
]
43+
}
44+
]
45+
}
46+
]
47+
}
48+
}

src/core/comparator.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,24 @@ func checkComponentsDiff(leftConfig model.Config, rightConfig model.Config, left
200200
}
201201

202202
func compareAndUpdateBool(left *bool, right *bool, diffFound bool, modelDiffFound **bool) bool {
203-
if *left != *right {
203+
// Bool are required and will assume right is equal to left if right is nil
204+
// E.g. when Respository (right) has not been set, it will assume the value from the left (API)
205+
if right == nil {
206+
right = new(bool)
207+
*right = *left
208+
diffFound = true
209+
*modelDiffFound = right
210+
} else if *left != *right {
204211
diffFound = true
205212
*modelDiffFound = right
206213
}
214+
207215
return diffFound
208216
}
209217

210218
func compareAndUpdateString(left string, right string, diffFound bool, modelDiffFound *string) bool {
211-
if left != right {
219+
// Strings are optional and will only evaluate if right is not empty
220+
if right != "" && left != right {
212221
diffFound = true
213222
*modelDiffFound = right
214223
}

src/core/comparator_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,36 @@ func TestCheckConfigSnapshot(t *testing.T) {
198198
]}`, utils.ToJsonFromObject(actual))
199199
})
200200

201+
t.Run("Should return changes in config - missing description assume not changed", func(t *testing.T) {
202+
// Given
203+
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)
204+
jsonRepo := utils.ReadJsonFromFile("../../resources/fixtures/comparator/changed_config_missing_description.json")
205+
fromApi := c.NewSnapshotFromJson([]byte(jsonApi))
206+
fromRepo := c.NewSnapshotFromJson([]byte(jsonRepo))
207+
208+
// Test Check/Merge changes
209+
diffChanged := c.CheckSnapshotDiff(fromApi, fromRepo, CHANGED)
210+
diffNew := c.CheckSnapshotDiff(fromRepo, fromApi, NEW)
211+
diffDeleted := c.CheckSnapshotDiff(fromApi, fromRepo, DELETED)
212+
actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted})
213+
214+
assert.NotNil(t, actual)
215+
assert.JSONEq(t, `{
216+
"changes": [
217+
{
218+
"action": "CHANGED",
219+
"diff": "CONFIG",
220+
"path": [
221+
"Release 1",
222+
"MY_SWITCHER_1"
223+
],
224+
"content": {
225+
"activated": false
226+
}
227+
}
228+
]}`, utils.ToJsonFromObject(actual))
229+
})
230+
201231
t.Run("Should return changes in config after calling RemoveDeleted", func(t *testing.T) {
202232
// Given
203233
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)
@@ -367,6 +397,37 @@ func TestCheckStrategySnapshot(t *testing.T) {
367397
]}`, utils.ToJsonFromObject(actual))
368398
})
369399

400+
t.Run("Should return changes in strategy - missing activated assume is changed", func(t *testing.T) {
401+
// Given
402+
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)
403+
jsonRepo := utils.ReadJsonFromFile("../../resources/fixtures/comparator/changed_strategy_missing_activated.json")
404+
fromApi := c.NewSnapshotFromJson([]byte(jsonApi))
405+
fromRepo := c.NewSnapshotFromJson([]byte(jsonRepo))
406+
407+
// Test Check/Merge changes
408+
diffChanged := c.CheckSnapshotDiff(fromApi, fromRepo, CHANGED)
409+
diffNew := c.CheckSnapshotDiff(fromRepo, fromApi, NEW)
410+
diffDeleted := c.CheckSnapshotDiff(fromApi, fromRepo, DELETED)
411+
actual := c.MergeResults([]model.DiffResult{diffChanged, diffNew, diffDeleted})
412+
413+
assert.NotNil(t, actual)
414+
assert.JSONEq(t, `{
415+
"changes": [
416+
{
417+
"action": "CHANGED",
418+
"diff": "STRATEGY",
419+
"path": [
420+
"Release 1",
421+
"MY_SWITCHER_1",
422+
"VALUE_VALIDATION"
423+
],
424+
"content": {
425+
"activated": false
426+
}
427+
}
428+
]}`, utils.ToJsonFromObject(actual))
429+
})
430+
370431
t.Run("Should return new strategy", func(t *testing.T) {
371432
// Given
372433
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)

0 commit comments

Comments
 (0)