Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: Add test coverage for JSON/YAML utilities #773

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions internal/util/jsonyamlutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// Copyright 2023 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// NOTE: This file is for stubbing out client code for proof of concept
// purposes. It will / should be removed in the future.
// Until then, it is not covered by unit tests and should not be used
// It does make a good example of how to use the generated client code
// for others to use as a reference.

package util_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/stacklok/mediator/internal/util"
)

func TestConvertYAMLToJSON(t *testing.T) {
t.Parallel()

tests := []struct {
name string
yamlCase string
wantW string
wantErr bool
}{
{
name: "simple yaml",
yamlCase: "foo: bar",
wantW: "{\"foo\":\"bar\"}\n",
wantErr: false,
},
{
name: "complex yaml",
yamlCase: `---
foo: bar
bar:
- foo
- bar
- baz
`,
wantW: "{\"bar\":[\"foo\",\"bar\",\"baz\"],\"foo\":\"bar\"}\n",
wantErr: false,
},
{
name: "invalid yaml",
yamlCase: "\tThis is invalid yaml",
wantW: "",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := util.ConvertYamlToJson(tt.yamlCase)

if tt.wantErr {
assert.Error(t, err, "expected error")
return
}

assert.NoError(t, err, "unexpected error")
assert.Equal(t, tt.wantW, string(got), "unexpected output")
})
}
}

func TestConvertJSONToYAML(t *testing.T) {
t.Parallel()

tests := []struct {
name string
jsonCase string
wantW string
wantErr bool
}{
{
name: "simple yaml",
jsonCase: "{\"foo\":\"bar\"}\n",
wantW: "foo: bar\n",
wantErr: false,
},
{
name: "complex yaml",
jsonCase: "{\"bar\":[\"foo\",\"bar\",\"baz\"],\"foo\":\"bar\"}\n",
wantW: `bar:
- foo
- bar
- baz
foo: bar
`,
wantErr: false,
},
{
name: "invalid yaml",
jsonCase: "This is invalid JSON",
wantW: "",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := util.ConvertJsonToYaml([]byte(tt.jsonCase))

if tt.wantErr {
assert.Error(t, err, "expected error")
return
}

assert.NoError(t, err, "unexpected error")
assert.Equal(t, tt.wantW, string(got), "unexpected output")
})
}
}