Skip to content

Commit fc5c8da

Browse files
committed
test: added unit test for json output
1 parent 46eae72 commit fc5c8da

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

cmd/common_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package cmd
22

33
import (
4+
"context"
45
"reflect"
56
"testing"
67

8+
"github.com/kong/go-database-reconciler/pkg/diff"
79
"github.com/kong/go-database-reconciler/pkg/dump"
810
"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"
915
)
1016

1117
func TestDetermineSelectorTag(t *testing.T) {
@@ -96,3 +102,64 @@ func TestDetermineSelectorTag(t *testing.T) {
96102
})
97103
}
98104
}
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

Comments
 (0)