Skip to content

Commit

Permalink
Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorel-35 committed Apr 24, 2021
1 parent 0006693 commit b16927c
Show file tree
Hide file tree
Showing 2 changed files with 383 additions and 0 deletions.
231 changes: 231 additions & 0 deletions file/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,237 @@ protocols:
`
)

func Test_sortKey(t *testing.T) {
tests := []struct {
name string
sortable sortable
expectedKey string
}{
{
sortable: &FService{
Service: kong.Service{
Name: kong.String("my-service"),
ID: kong.String("my-id"),
},
},
expectedKey: "my-service",
},
{
sortable: &FService{
Service: kong.Service{
ID: kong.String("my-id"),
},
},
expectedKey: "my-id",
},
{
sortable: FService{},
expectedKey: "",
}, {
sortable: &FRoute{
Route: kong.Route{
Name: kong.String("my-route"),
ID: kong.String("my-id"),
},
},
expectedKey: "my-route",
},
{
sortable: FRoute{
Route: kong.Route{
ID: kong.String("my-id"),
},
},
expectedKey: "my-id",
},
{
sortable: FRoute{},
expectedKey: "",
}, {
sortable: FUpstream{
Upstream: kong.Upstream{
Name: kong.String("my-upstream"),
ID: kong.String("my-id"),
},
},
expectedKey: "my-upstream",
},
{
sortable: FUpstream{
Upstream: kong.Upstream{
ID: kong.String("my-id"),
},
},
expectedKey: "my-id",
},
{
sortable: FUpstream{},
expectedKey: "",
}, {
sortable: FTarget{
Target: kong.Target{
Target: kong.String("my-target"),
ID: kong.String("my-id"),
},
},
expectedKey: "my-target",
},
{
sortable: FTarget{
Target: kong.Target{
ID: kong.String("my-id"),
},
},
expectedKey: "my-id",
},
{
sortable: FTarget{},
expectedKey: "",
}, {
sortable: FCertificate{
Cert: kong.String("my-certificate"),
ID: kong.String("my-id"),
},
expectedKey: "my-certificate",
},
{
sortable: FCertificate{
ID: kong.String("my-id"),
},
expectedKey: "my-id",
},
{
sortable: FCertificate{},
expectedKey: "",
}, {
sortable: FCACertificate{
CACertificate: kong.CACertificate{
Cert: kong.String("my-ca-certificate"),
ID: kong.String("my-id"),
},
},
expectedKey: "my-ca-certificate",
},
{
sortable: FCACertificate{
CACertificate: kong.CACertificate{
ID: kong.String("my-id"),
},
},
expectedKey: "my-id",
},
{
sortable: FCACertificate{},
expectedKey: "",
},
{
sortable: FPlugin{
Plugin: kong.Plugin{
Name: kong.String("my-plugin"),
ID: kong.String("my-id"),
},
},
expectedKey: "my-plugin",
},
{
sortable: FPlugin{
Plugin: kong.Plugin{
Name: kong.String("my-plugin"),
ID: kong.String("my-id"),
Consumer: &kong.Consumer{
ID: kong.String("my-consumer-id"),
},
},
},
expectedKey: "my-pluginmy-consumer-id",
},
{
sortable: FPlugin{
Plugin: kong.Plugin{
Name: kong.String("my-plugin"),
ID: kong.String("my-id"),
Route: &kong.Route{
ID: kong.String("my-route-id"),
},
},
},
expectedKey: "my-pluginmy-route-id",
},
{
sortable: FPlugin{
Plugin: kong.Plugin{
Name: kong.String("my-plugin"),
ID: kong.String("my-id"),
Service: &kong.Service{
ID: kong.String("my-service-id"),
},
},
},
expectedKey: "my-pluginmy-service-id",
},

{
sortable: FPlugin{
Plugin: kong.Plugin{
ID: kong.String("my-id"),
},
},
expectedKey: "my-id",
},
{
sortable: FPlugin{},
expectedKey: "",
},
{
sortable: &FConsumer{
Consumer: kong.Consumer{
Username: kong.String("my-consumer"),
ID: kong.String("my-id"),
},
},
expectedKey: "my-consumer",
},
{
sortable: &FConsumer{
Consumer: kong.Consumer{
ID: kong.String("my-id"),
},
},
expectedKey: "my-id",
},
{
sortable: FConsumer{},
expectedKey: "",
},
{
sortable: &FServicePackage{
Name: kong.String("my-service-package"),
ID: kong.String("my-id"),
},
expectedKey: "my-service-package",
},
{
sortable: &FServicePackage{
ID: kong.String("my-id"),
},
expectedKey: "my-id",
},
{
sortable: FServicePackage{},
expectedKey: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key := tt.sortable.sortKey()
if key != tt.expectedKey {
t.Errorf("Expected %v, but is %v", tt.expectedKey, key)
}
})
}

}

func TestPluginUnmarshalYAML(t *testing.T) {
var p FPlugin
assert := assert.New(t)
Expand Down
152 changes: 152 additions & 0 deletions file/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,158 @@ func captureOutput(f func()) string {
return <-out
}

func Test_compareOrder(t *testing.T) {
tests := []struct {
name string
sortable1 sortable
sortable2 sortable
expected bool
}{
{
sortable1: &FService{
Service: kong.Service{
Name: kong.String("my-service-1"),
ID: kong.String("my-id-1"),
},
},
sortable2: &FService{
Service: kong.Service{
Name: kong.String("my-service-2"),
ID: kong.String("my-id-2"),
},
},
expected: true,
},

{
sortable1: &FRoute{
Route: kong.Route{
Name: kong.String("my-route-1"),
ID: kong.String("my-id-1"),
},
},
sortable2: &FRoute{
Route: kong.Route{
Name: kong.String("my-route-2"),
ID: kong.String("my-id-2"),
},
},
expected: true,
},

{
sortable1: FUpstream{
Upstream: kong.Upstream{
Name: kong.String("my-upstream-1"),
ID: kong.String("my-id-1"),
},
},
sortable2: FUpstream{
Upstream: kong.Upstream{
Name: kong.String("my-upstream-2"),
ID: kong.String("my-id-2"),
},
},
expected: true,
},

{
sortable1: FTarget{
Target: kong.Target{
Target: kong.String("my-target-1"),
ID: kong.String("my-id-1"),
},
},
sortable2: FTarget{
Target: kong.Target{
Target: kong.String("my-target-2"),
ID: kong.String("my-id-2"),
},
},
expected: true,
},

{
sortable1: FCertificate{
Cert: kong.String("my-certificate-1"),
ID: kong.String("my-id-1"),
},
sortable2: FCertificate{
Cert: kong.String("my-certificate-2"),
ID: kong.String("my-id-2"),
},
expected: true,
},

{
sortable1: FCACertificate{
CACertificate: kong.CACertificate{
Cert: kong.String("my-ca-certificate-1"),
ID: kong.String("my-id-1"),
},
},
sortable2: FCACertificate{
CACertificate: kong.CACertificate{
Cert: kong.String("my-ca-certificate-2"),
ID: kong.String("my-id-2"),
},
},
expected: true,
},

{
sortable1: FPlugin{
Plugin: kong.Plugin{
Name: kong.String("my-plugin-1"),
ID: kong.String("my-id-1"),
},
},
sortable2: FPlugin{
Plugin: kong.Plugin{
Name: kong.String("my-plugin-2"),
ID: kong.String("my-id-2"),
},
},
expected: true,
},

{
sortable1: &FConsumer{
Consumer: kong.Consumer{
Username: kong.String("my-consumer-1"),
ID: kong.String("my-id-2"),
},
},
sortable2: &FConsumer{
Consumer: kong.Consumer{
Username: kong.String("my-consumer-2"),
ID: kong.String("my-id-2"),
},
},
expected: true,
},

{
sortable1: &FServicePackage{
Name: kong.String("my-service-package-1"),
ID: kong.String("my-id-1"),
},
sortable2: &FServicePackage{
Name: kong.String("my-service-package-2"),
ID: kong.String("my-id-2"),
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if compareOrder(tt.sortable1, tt.sortable2) != tt.expected {
t.Errorf("Expected %v, but isn't", tt.expected)
}
})
}
}

func TestWriteKongStateToStdoutEmptyState(t *testing.T) {
var ks, _ = state.NewKongState()
var filename = "-"
Expand Down

0 comments on commit b16927c

Please sign in to comment.