github.com/kaptinlin/jsonschema@v0.4.6/tests/multipleOf_test.go (about) 1 package tests 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/goccy/go-json" 10 "github.com/kaptinlin/jsonschema" 11 ) 12 13 // TestMultipleOfForTestSuite executes the multipleOf validation tests for Schema Test Suite. 14 func TestMultipleOfForTestSuite(t *testing.T) { 15 testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/multipleOf.json") 16 } 17 18 // TestFloatOverflowForTestSuite executes the floatOverflow validation tests for Schema Test Suite. 19 func TestFloatOverflowForTestSuite(t *testing.T) { 20 testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/optional/float-overflow.json") 21 } 22 23 func TestSchemaWithMultipleOf(t *testing.T) { 24 testCases := []struct { 25 name string 26 schemaJSON string 27 expectedSchema jsonschema.Schema 28 }{ 29 { 30 name: "Multiple of integer", 31 schemaJSON: `{ 32 "$schema": "https://json-schema.org/draft/2020-12/schema", 33 "multipleOf": 2 34 }`, 35 expectedSchema: jsonschema.Schema{ 36 Schema: "https://json-schema.org/draft/2020-12/schema", 37 MultipleOf: jsonschema.NewRat(2), 38 }, 39 }, 40 { 41 name: "Multiple of decimal", 42 schemaJSON: `{ 43 "$schema": "https://json-schema.org/draft/2020-12/schema", 44 "multipleOf": 1.5 45 }`, 46 expectedSchema: jsonschema.Schema{ 47 Schema: "https://json-schema.org/draft/2020-12/schema", 48 MultipleOf: jsonschema.NewRat(1.5), 49 }, 50 }, 51 { 52 name: "Multiple of small number", 53 schemaJSON: `{ 54 "$schema": "https://json-schema.org/draft/2020-12/schema", 55 "multipleOf": 0.0001 56 }`, 57 expectedSchema: jsonschema.Schema{ 58 Schema: "https://json-schema.org/draft/2020-12/schema", 59 MultipleOf: jsonschema.NewRat(0.0001), 60 }, 61 }, 62 } 63 64 // Use ptrFloat64 and ptrString to ensure they are being used 65 _ = ptrFloat64(1.0) 66 _ = ptrString("test") 67 68 // Start the test server 69 server := startTestServer() 70 defer stopTestServer(server) 71 72 for _, tc := range testCases { 73 t.Run(tc.name, func(t *testing.T) { 74 var schema jsonschema.Schema 75 err := json.Unmarshal([]byte(tc.schemaJSON), &schema) 76 require.NoError(t, err, "Unmarshalling failed unexpectedly") 77 assert.Equal(t, tc.expectedSchema.ID, schema.ID) 78 assert.Equal(t, tc.expectedSchema.Schema, schema.Schema) 79 assert.Equal(t, tc.expectedSchema.Type, schema.Type) 80 81 // Now test marshaling back to JSON 82 marshaledJSON, err := json.Marshal(schema) 83 require.NoError(t, err, "Marshalling failed unexpectedly") 84 85 // Unmarshal marshaled JSON to verify it matches the original schema object 86 var reUnmarshaledSchema jsonschema.Schema 87 err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema) 88 require.NoError(t, err, "Unmarshalling the marshaled JSON failed") 89 assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original") 90 91 // Check if the marshaled JSON matches the original JSON input 92 assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON") 93 }) 94 } 95 }