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

     1  package tests
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/goccy/go-json"
    11  	"github.com/kaptinlin/jsonschema"
    12  )
    13  
    14  // TestConstForTestSuite executes the const validation tests for Schema Test Suite.
    15  func TestConstForTestSuite(t *testing.T) {
    16  	testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/const.json")
    17  }
    18  
    19  func TestConstValueUnmarshalJSON(t *testing.T) {
    20  	tests := []struct {
    21  		input    string
    22  		expected *jsonschema.ConstValue
    23  		err      error
    24  	}{
    25  		{`null`, &jsonschema.ConstValue{Value: nil, IsSet: true}, nil},
    26  		{`123`, &jsonschema.ConstValue{Value: 123.0, IsSet: true}, nil}, // JSON numbers are decoded as float64 by default
    27  		{`"hello"`, &jsonschema.ConstValue{Value: "hello", IsSet: true}, nil},
    28  		{``, nil, &json.SyntaxError{}}, // Expecting syntax error due to empty string
    29  	}
    30  
    31  	for _, tt := range tests {
    32  		var cv jsonschema.ConstValue
    33  		err := json.Unmarshal([]byte(tt.input), &cv)
    34  		if err != nil {
    35  			if tt.err == nil || reflect.TypeOf(err) != reflect.TypeOf(tt.err) {
    36  				t.Errorf("UnmarshalJSON(%s) error = %v, wantErr %v", tt.input, err, tt.err)
    37  			}
    38  			continue
    39  		}
    40  		if !reflect.DeepEqual(&cv, tt.expected) {
    41  			t.Errorf("UnmarshalJSON(%s) = %+v, want %+v", tt.input, cv, tt.expected)
    42  		}
    43  	}
    44  }
    45  
    46  func TestUnmarshalJSON(t *testing.T) {
    47  	tests := []struct {
    48  		name     string
    49  		jsonStr  string
    50  		expected *jsonschema.Schema
    51  		wantErr  bool
    52  	}{
    53  		{
    54  			name:    "Const set to null",
    55  			jsonStr: `{"const": null}`,
    56  			expected: &jsonschema.Schema{
    57  				Const: &jsonschema.ConstValue{
    58  					Value: nil,
    59  					IsSet: true,
    60  				},
    61  			},
    62  			wantErr: false,
    63  		},
    64  		{
    65  			name:    "Const set to an integer",
    66  			jsonStr: `{"const": 42}`,
    67  			expected: &jsonschema.Schema{
    68  				Const: &jsonschema.ConstValue{
    69  					Value: float64(42), // JSON unmarshals numbers into float64 by default
    70  					IsSet: true,
    71  				},
    72  			},
    73  			wantErr: false,
    74  		},
    75  		{
    76  			name:    "Const set to a string",
    77  			jsonStr: `{"const": "hello"}`,
    78  			expected: &jsonschema.Schema{
    79  				Const: &jsonschema.ConstValue{
    80  					Value: "hello",
    81  					IsSet: true,
    82  				},
    83  			},
    84  			wantErr: false,
    85  		},
    86  		{
    87  			name:     "Const not set",
    88  			jsonStr:  `{}`,
    89  			expected: &jsonschema.Schema{},
    90  			wantErr:  false,
    91  		},
    92  	}
    93  
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			var schema jsonschema.Schema
    97  			err := json.Unmarshal([]byte(tt.jsonStr), &schema)
    98  			if (err != nil) != tt.wantErr {
    99  				t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
   100  			}
   101  			if !reflect.DeepEqual(&schema, tt.expected) {
   102  				t.Errorf("UnmarshalJSON() got = %v, want %v", schema, tt.expected)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestConstValidation(t *testing.T) {
   109  	testCases := []struct {
   110  		name           string
   111  		schemaJSON     string
   112  		expectedSchema jsonschema.Schema
   113  	}{
   114  		{
   115  			name: "Const string",
   116  			schemaJSON: `{
   117  				"$schema": "https://json-schema.org/draft/2020-12/schema",
   118  				"const": "test"
   119  			}`,
   120  			expectedSchema: jsonschema.Schema{
   121  				Schema: "https://json-schema.org/draft/2020-12/schema",
   122  				Const: &jsonschema.ConstValue{
   123  					Value: "test",
   124  					IsSet: true,
   125  				},
   126  			},
   127  		},
   128  		{
   129  			name: "Const number",
   130  			schemaJSON: `{
   131  				"$schema": "https://json-schema.org/draft/2020-12/schema",
   132  				"const": 42
   133  			}`,
   134  			expectedSchema: jsonschema.Schema{
   135  				Schema: "https://json-schema.org/draft/2020-12/schema",
   136  				Const: &jsonschema.ConstValue{
   137  					Value: float64(42),
   138  					IsSet: true,
   139  				},
   140  			},
   141  		},
   142  		{
   143  			name: "Const null",
   144  			schemaJSON: `{
   145  				"$schema": "https://json-schema.org/draft/2020-12/schema",
   146  				"const": null
   147  			}`,
   148  			expectedSchema: jsonschema.Schema{
   149  				Schema: "https://json-schema.org/draft/2020-12/schema",
   150  				Const: &jsonschema.ConstValue{
   151  					Value: nil,
   152  					IsSet: true,
   153  				},
   154  			},
   155  		},
   156  	}
   157  
   158  	for _, tc := range testCases {
   159  		t.Run(tc.name, func(t *testing.T) {
   160  			var schema jsonschema.Schema
   161  			err := json.Unmarshal([]byte(tc.schemaJSON), &schema)
   162  			require.NoError(t, err, "Unmarshalling failed unexpectedly")
   163  			assert.Equal(t, tc.expectedSchema.ID, schema.ID)
   164  			assert.Equal(t, tc.expectedSchema.Schema, schema.Schema)
   165  			assert.Equal(t, tc.expectedSchema.Const, schema.Const)
   166  
   167  			// Now test marshaling back to JSON
   168  			marshaledJSON, err := json.Marshal(schema)
   169  			require.NoError(t, err, "Marshalling failed unexpectedly")
   170  
   171  			// Unmarshal marshaled JSON to verify it matches the original schema object
   172  			var reUnmarshaledSchema jsonschema.Schema
   173  			err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema)
   174  			require.NoError(t, err, "Unmarshalling the marshaled JSON failed")
   175  			assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original")
   176  
   177  			// Check if the marshaled JSON matches the original JSON input
   178  			assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON")
   179  		})
   180  	}
   181  }