github.com/kaptinlin/jsonschema@v0.4.6/tests/exclusiveMinimum_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/goccy/go-json"
     7  	"github.com/kaptinlin/jsonschema"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  // TestExclusiveMinimumForTestSuite executes the exclusiveMinimum validation tests for Schema Test Suite.
    13  func TestExclusiveMinimumForTestSuite(t *testing.T) {
    14  	testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/exclusiveMinimum.json")
    15  }
    16  
    17  func TestSchemaWithExclusiveMinimum(t *testing.T) {
    18  	testCases := []struct {
    19  		name           string
    20  		schemaJSON     string
    21  		expectedSchema jsonschema.Schema
    22  	}{
    23  		{
    24  			name: "ExclusiveMinimum validation",
    25  			schemaJSON: `{
    26  				"$schema": "https://json-schema.org/draft/2020-12/schema",
    27  				"exclusiveMinimum": 1.1
    28  			}`,
    29  			expectedSchema: jsonschema.Schema{
    30  				Schema:           "https://json-schema.org/draft/2020-12/schema",
    31  				ExclusiveMinimum: jsonschema.NewRat(1.1),
    32  			},
    33  		},
    34  	}
    35  
    36  	for _, tc := range testCases {
    37  		t.Run(tc.name, func(t *testing.T) {
    38  			var schema jsonschema.Schema
    39  			err := json.Unmarshal([]byte(tc.schemaJSON), &schema)
    40  			require.NoError(t, err, "Unmarshalling failed unexpectedly")
    41  			assert.Equal(t, tc.expectedSchema.ID, schema.ID)
    42  			assert.Equal(t, tc.expectedSchema.Schema, schema.Schema)
    43  			assert.Equal(t, tc.expectedSchema.Type, schema.Type)
    44  
    45  			// Now test marshaling back to JSON
    46  			marshaledJSON, err := json.Marshal(schema)
    47  			require.NoError(t, err, "Marshalling failed unexpectedly")
    48  
    49  			// Unmarshal marshaled JSON to verify it matches the original schema object
    50  			var reUnmarshaledSchema jsonschema.Schema
    51  			err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema)
    52  			require.NoError(t, err, "Unmarshalling the marshaled JSON failed")
    53  			assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original")
    54  
    55  			// Check if the marshaled JSON matches the original JSON input
    56  			assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON")
    57  		})
    58  	}
    59  }