|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "reflect" |
5 | 6 | "testing" |
6 | 7 |
|
| 8 | + "github.com/kong/go-database-reconciler/pkg/diff" |
7 | 9 | "github.com/kong/go-database-reconciler/pkg/dump" |
8 | 10 | "github.com/kong/go-database-reconciler/pkg/file" |
| 11 | + "github.com/kong/go-database-reconciler/pkg/state" |
| 12 | + "github.com/kong/go-kong/kong" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
9 | 15 | ) |
10 | 16 |
|
11 | 17 | func TestDetermineSelectorTag(t *testing.T) { |
@@ -96,3 +102,64 @@ func TestDetermineSelectorTag(t *testing.T) { |
96 | 102 | }) |
97 | 103 | } |
98 | 104 | } |
| 105 | + |
| 106 | +func TestPerformDiff_JSONOutput(t *testing.T) { |
| 107 | + // Reset global jsonOutput to a known state |
| 108 | + jsonOutput = diff.JSONOutputObject{} |
| 109 | + // This is initialized in syncMain() in the actual application, |
| 110 | + // but we need to set it up here for testing |
| 111 | + jsonOutput.Changes = diff.EntityChanges{ |
| 112 | + Creating: []diff.EntityState{}, |
| 113 | + Updating: []diff.EntityState{}, |
| 114 | + Deleting: []diff.EntityState{}, |
| 115 | + DroppedCreations: []diff.EntityState{}, |
| 116 | + DroppedUpdates: []diff.EntityState{}, |
| 117 | + DroppedDeletions: []diff.EntityState{}, |
| 118 | + } |
| 119 | + |
| 120 | + currentState, err := state.NewKongState() |
| 121 | + require.NoError(t, err) |
| 122 | + |
| 123 | + // mock target state |
| 124 | + targetState, err := state.NewKongState() |
| 125 | + require.NoError(t, err) |
| 126 | + service := state.Service{ |
| 127 | + Service: kong.Service{ |
| 128 | + ID: kong.String("service-1"), |
| 129 | + Name: kong.String("Service 1"), |
| 130 | + }, |
| 131 | + } |
| 132 | + err = targetState.Services.Add(service) |
| 133 | + require.NoError(t, err) |
| 134 | + |
| 135 | + // Calling performDiff with dry=true to avoid actual API calls |
| 136 | + totalOps, err := performDiff( |
| 137 | + context.Background(), |
| 138 | + currentState, |
| 139 | + targetState, |
| 140 | + true, // dry mode |
| 141 | + 1, // parallelism |
| 142 | + 0, // delay |
| 143 | + nil, // client (not used in dry mode) |
| 144 | + false, // isKonnect |
| 145 | + true, // enabled Json output |
| 146 | + ApplyTypeFull, |
| 147 | + ) |
| 148 | + |
| 149 | + require.NoError(t, err) |
| 150 | + assert.Equal(t, 1, totalOps) |
| 151 | + |
| 152 | + // Verify jsonOutput is populated correctly |
| 153 | + assert.Equal(t, int32(1), jsonOutput.Summary.Creating) |
| 154 | + assert.Equal(t, int32(0), jsonOutput.Summary.Updating) |
| 155 | + assert.Equal(t, int32(0), jsonOutput.Summary.Deleting) |
| 156 | + assert.Equal(t, int32(1), jsonOutput.Summary.Total) |
| 157 | + |
| 158 | + // Verify changes are populated |
| 159 | + assert.Len(t, jsonOutput.Changes.Creating, 1) |
| 160 | + assert.Len(t, jsonOutput.Changes.Updating, 0) |
| 161 | + assert.Len(t, jsonOutput.Changes.Deleting, 0) |
| 162 | + assert.Len(t, jsonOutput.Changes.DroppedCreations, 0) |
| 163 | + assert.Len(t, jsonOutput.Changes.DroppedUpdates, 0) |
| 164 | + assert.Len(t, jsonOutput.Changes.DroppedDeletions, 0) |
| 165 | +} |
0 commit comments