From 61f524364605e2f992144e4c4ada033bef6f4fe7 Mon Sep 17 00:00:00 2001 From: Adam Hostettler Date: Thu, 23 May 2024 15:44:02 -0400 Subject: [PATCH] added additional tests for new changes --- generate/avro_helpers_test.go | 30 +++++ generate/avro_test.go | 6 + generate/avro_test_data/basic.go | 9 ++ generate/avro_test_data/duplicate.avsc.out | 83 ++++++++++++++ generate/avro_test_data/duplicate.go | 10 ++ generate/avro_test_data/oneOfType.go | 18 +++ .../generated/complex_no_nested.go | 48 -------- .../generate_test_data/generated/targeted.go | 11 -- jsonschema/schema_test.go | 107 ++++++++++++++++++ jsonschema/test_data/allOf.json | 14 +++ jsonschema/test_data/basic.json | 16 +++ jsonschema/test_data/subtype.json | 18 +++ jsonschema/test_data/subtypeAllOf.json | 16 +++ 13 files changed, 327 insertions(+), 59 deletions(-) create mode 100644 generate/avro_test_data/basic.go create mode 100644 generate/avro_test_data/duplicate.avsc.out create mode 100644 generate/avro_test_data/duplicate.go create mode 100644 generate/avro_test_data/oneOfType.go delete mode 100644 generate/generate_test_data/generated/complex_no_nested.go delete mode 100644 generate/generate_test_data/generated/targeted.go create mode 100644 jsonschema/test_data/allOf.json create mode 100644 jsonschema/test_data/basic.json create mode 100644 jsonschema/test_data/subtype.json create mode 100644 jsonschema/test_data/subtypeAllOf.json diff --git a/generate/avro_helpers_test.go b/generate/avro_helpers_test.go index 85393d9..dcd72fc 100644 --- a/generate/avro_helpers_test.go +++ b/generate/avro_helpers_test.go @@ -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) + } + } +} diff --git a/generate/avro_test.go b/generate/avro_test.go index 21fc9e3..fb77feb 100644 --- a/generate/avro_test.go +++ b/generate/avro_test.go @@ -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 { diff --git a/generate/avro_test_data/basic.go b/generate/avro_test_data/basic.go new file mode 100644 index 0000000..3afe8ad --- /dev/null +++ b/generate/avro_test_data/basic.go @@ -0,0 +1,9 @@ +package avro_test_data + +// Code generated by github.com/GannettDigital/jstransform; DO NOT EDIT. + +type Basic struct { + Height int64 `json:"height,omitempty"` + Visible bool `json:"visible,omitempty"` + Width float64 `json:"width,omitempty"` +} diff --git a/generate/avro_test_data/duplicate.avsc.out b/generate/avro_test_data/duplicate.avsc.out new file mode 100644 index 0000000..f015282 --- /dev/null +++ b/generate/avro_test_data/duplicate.avsc.out @@ -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" + ] + } + ] +} diff --git a/generate/avro_test_data/duplicate.go b/generate/avro_test_data/duplicate.go new file mode 100644 index 0000000..e1160ae --- /dev/null +++ b/generate/avro_test_data/duplicate.go @@ -0,0 +1,10 @@ +package avro_test_data + +// Code generated by github.com/GannettDigital/jstransform; DO NOT EDIT. + +type Duplicate struct { + Basic + OneOfType + + AnotherField string `json:"anotherField,omitempty"` +} diff --git a/generate/avro_test_data/oneOfType.go b/generate/avro_test_data/oneOfType.go new file mode 100644 index 0000000..cae3064 --- /dev/null +++ b/generate/avro_test_data/oneOfType.go @@ -0,0 +1,18 @@ +package avro_test_data + +// Code generated by github.com/GannettDigital/jstransform; DO NOT EDIT. + +import "time" + +type OneOfType struct { + Basic + + Caption string `json:"caption"` + Credit string `json:"credit"` + Cutline string `json:"cutline,omitempty"` + DatePhotoTaken time.Time `json:"datePhotoTaken"` + Orientation string `json:"orientation"` + // a type + Type string `json:"type"` + // Universal Resource Locator +} diff --git a/generate/generate_test_data/generated/complex_no_nested.go b/generate/generate_test_data/generated/complex_no_nested.go deleted file mode 100644 index d7ed00c..0000000 --- a/generate/generate_test_data/generated/complex_no_nested.go +++ /dev/null @@ -1,48 +0,0 @@ -package generated - -// Code generated by github.com/GannettDigital/jstransform; DO NOT EDIT. - -import "time" - -type Complex_no_nested struct { - Caption string `json:"caption"` - Credit string `json:"credit"` - // The available cropped images - Crops []Complex_no_nestedCrops `json:"crops"` - Cutline string `json:"cutline,omitempty"` - DatePhotoTaken time.Time `json:"datePhotoTaken"` - Orientation string `json:"orientation"` - OriginalSize Complex_no_nestedOriginalSize `json:"originalSize"` - // a type - Type string `json:"type"` - // Universal Resource Locator - URL Complex_no_nestedURL `json:"URL"` -} - -type Complex_no_nestedCrops struct { - Height float64 `json:"height"` - Name string `json:"name"` - // full path to the cropped image file - Path string `json:"path"` - // a long - // multi-line description - RelativePath string `json:"relativePath"` - Width float64 `json:"width"` -} - -type Complex_no_nestedOriginalSize struct { - Height float64 `json:"height"` - Width float64 `json:"width"` -} - -type Complex_no_nestedURL struct { - // The full Canonical URL - Absolute string `json:"absolute"` - Meta Complex_no_nestedURLMeta `json:"meta,omitempty"` - Publish string `json:"publish"` -} - -type Complex_no_nestedURLMeta struct { - Description string `json:"description"` - SiteName string `json:"siteName"` -} diff --git a/generate/generate_test_data/generated/targeted.go b/generate/generate_test_data/generated/targeted.go deleted file mode 100644 index 567fcb2..0000000 --- a/generate/generate_test_data/generated/targeted.go +++ /dev/null @@ -1,11 +0,0 @@ -package generated - -// Code generated by github.com/GannettDigital/jstransform; DO NOT EDIT. - -type Targeted struct { - AnotherField string `json:"anotherField,omitempty"` - // The available cropped images - Crops []Complex_no_nestedCrops `json:"crops,omitempty"` - // Universal Resource Locator - URL Complex_no_nestedURL `json:"URL,omitempty"` -} diff --git a/jsonschema/schema_test.go b/jsonschema/schema_test.go index e4e6cb8..60c2257 100644 --- a/jsonschema/schema_test.go +++ b/jsonschema/schema_test.go @@ -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 diff --git a/jsonschema/test_data/allOf.json b/jsonschema/test_data/allOf.json new file mode 100644 index 0000000..279d876 --- /dev/null +++ b/jsonschema/test_data/allOf.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "allOf": [ + { + "$ref": "./basic.json" + } + ], + "oneOf": [ + { + "$ref": "./subtype.json" + } + ] +} diff --git a/jsonschema/test_data/basic.json b/jsonschema/test_data/basic.json new file mode 100644 index 0000000..cb0b7a7 --- /dev/null +++ b/jsonschema/test_data/basic.json @@ -0,0 +1,16 @@ +{ + "additionalProperties": false, + "description": "basic", + "type": "object", + "properties": { + "aField": { + "type": "string" + }, + "bField": { + "type": "integer" + }, + "cField": { + "type": "boolean" + } + } +} \ No newline at end of file diff --git a/jsonschema/test_data/subtype.json b/jsonschema/test_data/subtype.json new file mode 100644 index 0000000..2acf7f8 --- /dev/null +++ b/jsonschema/test_data/subtype.json @@ -0,0 +1,18 @@ +{ + "additionalProperties": false, + "description": "subtype", + "type": "object", + "allOf": [ + { + "$ref": "./subtypeAllOf.json" + } + ], + "properties": { + "gField": { + "type": "string" + }, + "hField": { + "type": "string" + } + } +} \ No newline at end of file diff --git a/jsonschema/test_data/subtypeAllOf.json b/jsonschema/test_data/subtypeAllOf.json new file mode 100644 index 0000000..c23897f --- /dev/null +++ b/jsonschema/test_data/subtypeAllOf.json @@ -0,0 +1,16 @@ +{ + "additionalProperties": false, + "description": "subtypeAllOf", + "type": "object", + "properties": { + "dField": { + "type": "integer" + }, + "eField": { + "type": "string" + }, + "fField": { + "type": "string" + } + } +} \ No newline at end of file