-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbdd_schema_compilation_test.go
More file actions
136 lines (109 loc) · 3.13 KB
/
bdd_schema_compilation_test.go
File metadata and controls
136 lines (109 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package jsonschema
import (
"fmt"
"os"
)
// Schema compilation step methods
func (ctx *JSONSchemaBDDTestContext) iCompileASchemaFromAJSONString() error {
if ctx.service == nil {
return fmt.Errorf("jsonschema service not available")
}
// Create a temporary schema file
schemaString := `{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name"]
}`
// Write to temporary file
tmpFile, err := os.CreateTemp("", "schema-*.json")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
defer tmpFile.Close()
_, err = tmpFile.WriteString(schemaString)
if err != nil {
return fmt.Errorf("failed to write schema: %w", err)
}
ctx.tempFile = tmpFile.Name()
schema, err := ctx.service.CompileSchema(ctx.tempFile)
if err != nil {
ctx.lastError = err
return fmt.Errorf("failed to compile schema: %w", err)
}
ctx.compiledSchema = schema
return nil
}
func (ctx *JSONSchemaBDDTestContext) theSchemaShouldBeCompiledSuccessfully() error {
if ctx.compiledSchema == nil {
return fmt.Errorf("schema was not compiled")
}
if ctx.lastError != nil {
return fmt.Errorf("schema compilation failed: %v", ctx.lastError)
}
return nil
}
func (ctx *JSONSchemaBDDTestContext) iHaveACompiledSchemaForUserData() error {
return ctx.iCompileASchemaFromAJSONString()
}
func (ctx *JSONSchemaBDDTestContext) iHaveACompiledSchema() error {
return ctx.iCompileASchemaFromAJSONString()
}
func (ctx *JSONSchemaBDDTestContext) iTryToCompileAnInvalidSchema() error {
if ctx.service == nil {
return fmt.Errorf("jsonschema service not available")
}
invalidSchemaString := `{"type": "invalid_type"}` // Invalid schema type
// Write to temporary file
tmpFile, err := os.CreateTemp("", "invalid-schema-*.json")
if err != nil {
return fmt.Errorf("failed to create temp file: %w", err)
}
defer tmpFile.Close()
_, err = tmpFile.WriteString(invalidSchemaString)
if err != nil {
return fmt.Errorf("failed to write schema: %w", err)
}
_, err = ctx.service.CompileSchema(tmpFile.Name())
if err != nil {
ctx.lastError = err
}
return nil
}
func (ctx *JSONSchemaBDDTestContext) aSchemaCompilationErrorShouldBeReturned() error {
if ctx.lastError == nil {
return fmt.Errorf("expected schema compilation error but got none")
}
// Check that error message contains useful information
errMsg := ctx.lastError.Error()
if errMsg == "" {
return fmt.Errorf("schema compilation error message is empty")
}
return nil
}
func (ctx *JSONSchemaBDDTestContext) iCompileAValidSchema() error {
schemaJSON := `{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "number"}
},
"required": ["name"]
}`
// Create temporary file for schema
tempFile, err := os.CreateTemp("", "test-schema-*.json")
if err != nil {
return err
}
defer tempFile.Close()
ctx.tempFile = tempFile.Name()
if _, err := tempFile.WriteString(schemaJSON); err != nil {
return err
}
// Compile the schema
ctx.compiledSchema, ctx.lastError = ctx.service.CompileSchema(ctx.tempFile)
return nil
}