-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type CompilerTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *CompilerTestSuite) TestCompile() { | ||
tests := []struct { | ||
name string | ||
expectedErr string | ||
erdSlugPlural string | ||
erdVersion string | ||
schema string | ||
}{ | ||
{ | ||
name: "ok", | ||
schema: `{ | ||
"$id": "v1.canada-geese.test-ex-1", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Canada Goose", | ||
"type": "object", | ||
"unique": [ | ||
"firstName", | ||
"lastName" | ||
], | ||
"required": [ | ||
"firstName", | ||
"lastName" | ||
], | ||
"properties": { | ||
"firstName": { | ||
"type": "string", | ||
"description": "The goose's first name.", | ||
"ui": { | ||
"hide": true | ||
} | ||
}, | ||
"lastName": { | ||
"type": "string", | ||
"description": "The goose's last name." | ||
}, | ||
"age": { | ||
"description": "Age in years which must be equal to or greater than zero.", | ||
"type": "integer", | ||
"minimum": 0 | ||
} | ||
} | ||
}`, | ||
expectedErr: "", | ||
erdSlugPlural: "canada-geese", | ||
erdVersion: "v1alpha1", | ||
}, | ||
{ | ||
name: "test schema with required missing", | ||
schema: ` | ||
{ | ||
"$id": "v1.hello-world.extension-example.governor.equinixmetal.com", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"unique": [ | ||
"name" | ||
], | ||
"properties": { | ||
"name": { | ||
"default": "world", | ||
"description": "hello, name", | ||
"type": "string" | ||
} | ||
}, | ||
"title": "Greeting", | ||
"type": "object" | ||
} | ||
`, | ||
expectedErr: `cannot apply unique constraint when "required" is not provided`, | ||
erdSlugPlural: "greetings", | ||
erdVersion: "v1alpha1", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
compiler := NewCompiler( | ||
"extension-validator", tt.erdSlugPlural, tt.erdVersion, | ||
WithUniqueConstraint( | ||
context.Background(), | ||
nil, nil, nil, | ||
), | ||
) | ||
|
||
_, err := compiler.Compile(tt.schema) | ||
|
||
if tt.expectedErr != "" { | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), tt.expectedErr) | ||
} else { | ||
s.Require().NoError(err) | ||
} | ||
} | ||
} | ||
|
||
func TestCompilerSuite(t *testing.T) { | ||
suite.Run(t, new(CompilerTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters