Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
losisin committed Oct 26, 2023
1 parent 272d990 commit c420149
Showing 1 changed file with 102 additions and 75 deletions.
177 changes: 102 additions & 75 deletions schema_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
package main

import (
"bufio"
"fmt"
"encoding/json"
"io/ioutil"

Check failure on line 5 in schema_test.go

View workflow job for this annotation

GitHub Actions / Test

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"os"
"os/exec"
"strings"
"reflect"
"testing"

"github.com/losisin/go-jsonschema-generator"
)

func TestMultiStringFlagString(t *testing.T) {
// Initialize a multiStringFlag instance.
flag := multiStringFlag{"value1", "value2", "value3"}

// Call the String method.
result := flag.String()

// Define the expected result.
expected := "value1, value2, value3"

// Check if the result matches the expected value.
if result != expected {
t.Errorf("String() method returned %s, expected %s", result, expected)
}
}

func TestMultiStringFlagSet(t *testing.T) {
// Initialize a multiStringFlag instance.
flag := multiStringFlag{}

// Call the Set method with a sample value.
value := "value1,value2,value3"
err := flag.Set(value)

// Check for any errors returned by the Set method.
if err != nil {
t.Errorf("Set() method returned an error: %v", err)
}

// Define the expected flag value after calling Set.
expected := multiStringFlag{"value1", "value2", "value3"}

// Check if the flag value matches the expected value.
if !reflect.DeepEqual(flag, expected) {
t.Errorf("Set() method set the flag to %v, expected %v", flag, expected)
}
}

func TestReadAndUnmarshalYAML(t *testing.T) {
yamlContent := []byte("key1: value1\nkey2: value2\n")
yamlFilePath := "test.yaml"
Expand All @@ -17,106 +56,94 @@ func TestReadAndUnmarshalYAML(t *testing.T) {
t.Fatalf("Error creating a temporary YAML file: %v", err)
}
defer os.Remove(yamlFilePath)

var target map[string]interface{}
err = readAndUnmarshalYAML(yamlFilePath, &target)
if err != nil {
t.Errorf("Error reading and unmarshaling YAML: %v", err)
}

if len(target) != 2 {
t.Errorf("Expected target map length to be 2, but got %d", len(target))
}

if target["key1"] != "value1" {
t.Errorf("target map should contain key1 with value 'value1'")
}

if target["key2"] != "value2" {
t.Errorf("target map should contain key2 with value 'value2'")
}
}

func TestMergeMaps(t *testing.T) {
mapA := map[string]interface{}{
"key1": "value1",
"key2": "value2",
tests := []struct {
a, b, expected map[string]interface{}
}{
{
a: map[string]interface{}{"key1": "value1"},
b: map[string]interface{}{"key2": "value2"},
expected: map[string]interface{}{"key1": "value1", "key2": "value2"},
},
{
a: map[string]interface{}{"key1": "value1"},
b: map[string]interface{}{"key1": "value2"},
expected: map[string]interface{}{"key1": "value2"},
},
{
a: map[string]interface{}{
"key1": map[string]interface{}{"subkey1": "value1"},
},
b: map[string]interface{}{
"key1": map[string]interface{}{"subkey2": "value2"},
},
expected: map[string]interface{}{
"key1": map[string]interface{}{"subkey1": "value1", "subkey2": "value2"},
},
},
}

mapB := map[string]interface{}{
"key2": "newvalue2",
"key3": "value3",
for i, test := range tests {
result := mergeMaps(test.a, test.b)
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("Test case %d failed. Expected: %v, Got: %v", i+1, test.expected, result)
}
}
}

merged := mergeMaps(mapA, mapB)
func TestPrintMap(t *testing.T) {
// Create a temporary file for testing
tmpFile := "test_output.json"
defer os.Remove(tmpFile)

if len(merged) != 3 {
t.Errorf("Expected merged map length to be 3, but got %d", len(merged))
}
// Create a variable to store the mock YAML data
var yamlData map[string]interface{}

if merged["key1"] != "value1" {
t.Errorf("Merged map should contain key1 from mapA")
// Mock the output of readAndUnmarshalYAML
err := readAndUnmarshalYAML("values.yaml", &yamlData)
if err != nil {
t.Fatalf("Failed to mock YAML data: %v", err)
}

if merged["key2"] != "newvalue2" {
t.Errorf("Merged map should contain key2 from mapB")
}
// Convert the YAML data to JSON
// jsonData, err := convertYAMLToJSON(yamlData)
// if err != nil {
// t.Fatalf("Failed to convert YAML to JSON: %v", err)
// }
s := &jsonschema.Document{}
s.Read(yamlData)

if merged["key3"] != "value3" {
t.Errorf("Merged map should contain key3 from mapB")
// Call the printMap function to write the JSON data to the temporary file
err = printMap(s, tmpFile)
if err != nil {
t.Fatalf("printMap failed: %v", err)
}
}

func TestMainWithDynamicFlags(t *testing.T) {
testCases := []struct {
draftVersion int
expectedString string
}{
{4, `"$schema": "http://json-schema.org/draft-04/schema#",`},
{6, `"$schema": "http://json-schema.org/draft-06/schema#",`},
{7, `"$schema": "http://json-schema.org/draft-07/schema#",`},
{2019, `"$schema": "https://json-schema.org/draft/2019-09/schema",`},
{2020, `"$schema": "https://json-schema.org/draft/2020-12/schema",`},
// Read the contents of the temporary file
fileData, err := ioutil.ReadFile(tmpFile)
if err != nil {
t.Fatalf("Failed to read temporary file: %v", err)
}

for _, testCase := range testCases {
// Run schema.go with the specified draft version flag.
cmd := exec.Command("go", "run", "schema.go", "--input=values.yaml", fmt.Sprintf("--draft=%d", testCase.draftVersion))

// Capture the command's output (stdout and stderr).
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Command execution failed: %v\nOutput:\n%s", err, cmdOutput)
}

// Check the exit status to ensure it ran successfully.
if cmd.ProcessState.ExitCode() != 0 {
t.Fatalf("Command execution returned a non-zero exit code: %d\nOutput:\n%s", cmd.ProcessState.ExitCode(), cmdOutput)
}

// Now, read and inspect the contents of values.schema.json.
file, err := os.Open("values.schema.json")
if err != nil {
t.Fatalf("Failed to open values.schema.json: %v", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
lineNumber := 1

for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if lineNumber == 2 {
if line != testCase.expectedString {
t.Errorf("Expected line 2 to be:\n%s\nGot:\n%s", testCase.expectedString, line)
}
break
}
lineNumber++
}

if err := scanner.Err(); err != nil {
t.Fatalf("Error reading values.schema.json: %v", err)
}
// Verify that the output is valid JSON
var outputJSON interface{}
if err := json.Unmarshal(fileData, &outputJSON); err != nil {
t.Errorf("Output is not valid JSON: %v", err)
}
}

0 comments on commit c420149

Please sign in to comment.