Skip to content

Commit

Permalink
added additional tests for new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhostettler committed May 23, 2024
1 parent 8d63dc1 commit 61f5243
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 59 deletions.
30 changes: 30 additions & 0 deletions generate/avro_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,33 @@ func TestBuildAvroHelperFunctions(t *testing.T) {
}
}
}

func TestStructToFilename(t *testing.T) {
tests := []struct {
description string
structName string
wantPath string
}{
{
description: "Simple",
structName: "Simple",
wantPath: "simple.go",
},
{
description: "CamelCase",
structName: "CamelCase",
wantPath: "camelCase.go",
},
{
description: "Snake_Case",
structName: "Snake_Case",
wantPath: "snake_Case.go",
},
}

for _, test := range tests {
if got := structToFilename(test.structName); got != test.wantPath {
t.Errorf("Test %q - got unexpected path %q", test.description, got)
}
}
}
6 changes: 6 additions & 0 deletions generate/avro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func TestBuildAvroSchemaFile(t *testing.T) {
goPath: "./avro_test_data/times.go",
wantPath: "./avro_test_data/times.avsc.out",
},
{
description: "Nested duplicate struct",
name: "Duplicate",
goPath: "./avro_test_data/duplicate.go",
wantPath: "./avro_test_data/duplicate.avsc.out",
},
}

for _, test := range tests {
Expand Down
9 changes: 9 additions & 0 deletions generate/avro_test_data/basic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions generate/avro_test_data/duplicate.avsc.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"name": "Duplicate",
"type": "record",
"fields": [
{
"name": "AvroWriteTime",
"doc": "The timestamp when this avro data is written. Useful for identifying the newest row of data sharing keys.",
"type": "long",
"logicalType": "timestamp-millis"
},
{
"name": "AvroDeleted",
"doc": "This is set to true when the Avro data is recording a delete in the source data.",
"default": false,
"type": "boolean"
},
{
"name": "height",
"namespace": "Basic",
"type": [
"null",
"long"
]
},
{
"name": "visible",
"namespace": "Basic",
"type": "boolean",
"default": false
},
{
"name": "width",
"namespace": "Basic",
"type": [
"null",
"double"
]
},
{
"name": "caption",
"namespace": "OneOfType",
"type": "string"
},
{
"name": "credit",
"namespace": "OneOfType",
"type": "string"
},
{
"name": "cutline",
"namespace": "OneOfType",
"type": [
"null",
"string"
]
},
{
"name": "datePhotoTaken",
"namespace": "OneOfType",
"type": {
"type": "long",
"logicalType": "timestamp-millis"
}
},
{
"name": "orientation",
"namespace": "OneOfType",
"type": "string"
},
{
"name": "type",
"namespace": "OneOfType",
"type": "string"
},
{
"name": "anotherField",
"type": [
"null",
"string"
]
}
]
}
10 changes: 10 additions & 0 deletions generate/avro_test_data/duplicate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions generate/avro_test_data/oneOfType.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 0 additions & 48 deletions generate/generate_test_data/generated/complex_no_nested.go

This file was deleted.

11 changes: 0 additions & 11 deletions generate/generate_test_data/generated/targeted.go

This file was deleted.

107 changes: 107 additions & 0 deletions jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,113 @@ func TestSchemaFromFile(t *testing.T) {
}
}

func TestSchemaFromFileNoFlatten(t *testing.T) {
tests := []struct {
description string
oneOfType string
schemaPath string
want *Schema
wantErr bool
}{
{
description: "Nested allOf",
oneOfType: "subtype",
schemaPath: "./test_data/allOf.json",
want: &Schema{
Instance: Instance{
Properties: map[string]json.RawMessage{
"aField": json.RawMessage(`{
"type": "string"
}`),
"bField": json.RawMessage(`{
"type": "integer"
}`),
"cField": json.RawMessage(`{
"type": "boolean"
}`),
"gField": json.RawMessage(`{
"type": "string"
}`),
"hField": json.RawMessage(`{
"type": "string"
}`),
},
},
},
},
{
description: "basic",
oneOfType: "subtype",
schemaPath: "./test_data/basic.json",
want: &Schema{
Instance: Instance{
Properties: map[string]json.RawMessage{
"aField": json.RawMessage(`{
"type": "string"
}`),
"bField": json.RawMessage(`{
"type": "integer"
}`),
"cField": json.RawMessage(`{
"type": "boolean"
}`),
},
},
},
},
{
description: "Nested allOf, no oneOfType provided",
oneOfType: "",
schemaPath: "./test_data/allOf.json",
want: &Schema{
Instance: Instance{
Properties: map[string]json.RawMessage{
"aField": json.RawMessage(`{
"type": "string"
}`),
"bField": json.RawMessage(`{
"type": "integer"
}`),
"cField": json.RawMessage(`{
"type": "boolean"
}`),
},
},
},
},
}
for _, test := range tests {
got, err := SchemaFromFileNoFlatten(test.schemaPath, test.oneOfType)

var gotProperties, wantProperties []byte
if !test.wantErr && got != nil {
gotProperties, err = json.MarshalIndent(got.Properties, "", " ")
if err != nil {
t.Errorf("Test %q - failed to marshal got.Properties: %v", test.description, err)
}
wantProperties, err = json.MarshalIndent(test.want.Properties, "", " ")
if err != nil {
t.Errorf("Test %q - failed to marshal test.want.Properties: %v", test.description, err)
}
}

switch {
case test.wantErr && err != nil:
continue
case test.wantErr && err == nil:
t.Errorf("Test %q - got nil error want error", test.description)
case !test.wantErr && err != nil:
t.Errorf("Test %q - got error: %v", test.description, err)
case !reflect.DeepEqual(gotProperties, wantProperties):
t.Errorf("Test %q - got Properties\n%s\nwant\n%s", test.description, gotProperties, wantProperties)
case !reflect.DeepEqual(got.Items, test.want.Items):
t.Errorf("Test %q - got Items\n%s\nwant\n%s", test.description, got.Items, test.want.Items)
case !reflect.DeepEqual(got.Required, test.want.Required):
t.Errorf("Test %q - got Required\n%s\nwant\n%s", test.description, got.Required, test.want.Required)
}
}
}

func TestMappings(t *testing.T) {
tests := []struct {
description string
Expand Down
14 changes: 14 additions & 0 deletions jsonschema/test_data/allOf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"allOf": [
{
"$ref": "./basic.json"
}
],
"oneOf": [
{
"$ref": "./subtype.json"
}
]
}
16 changes: 16 additions & 0 deletions jsonschema/test_data/basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"additionalProperties": false,
"description": "basic",
"type": "object",
"properties": {
"aField": {
"type": "string"
},
"bField": {
"type": "integer"
},
"cField": {
"type": "boolean"
}
}
}
18 changes: 18 additions & 0 deletions jsonschema/test_data/subtype.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"additionalProperties": false,
"description": "subtype",
"type": "object",
"allOf": [
{
"$ref": "./subtypeAllOf.json"
}
],
"properties": {
"gField": {
"type": "string"
},
"hField": {
"type": "string"
}
}
}
Loading

0 comments on commit 61f5243

Please sign in to comment.