k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/items_test.go (about)

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package spec
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting"
    24  )
    25  
    26  var items = Items{
    27  	Refable: Refable{Ref: MustCreateRef("Dog")},
    28  	CommonValidations: CommonValidations{
    29  		Maximum:          float64Ptr(100),
    30  		ExclusiveMaximum: true,
    31  		ExclusiveMinimum: true,
    32  		Minimum:          float64Ptr(5),
    33  		MaxLength:        int64Ptr(100),
    34  		MinLength:        int64Ptr(5),
    35  		Pattern:          "\\w{1,5}\\w+",
    36  		MaxItems:         int64Ptr(100),
    37  		MinItems:         int64Ptr(5),
    38  		UniqueItems:      true,
    39  		MultipleOf:       float64Ptr(5),
    40  		Enum:             []interface{}{"hello", "world"},
    41  	},
    42  	SimpleSchema: SimpleSchema{
    43  		Type:   "string",
    44  		Format: "date",
    45  		Items: &Items{
    46  			Refable: Refable{Ref: MustCreateRef("Cat")},
    47  		},
    48  		CollectionFormat: "csv",
    49  		Default:          "8",
    50  	},
    51  }
    52  
    53  const itemsJSON = `{
    54  	"items": {
    55  		"$ref": "Cat"
    56  	},
    57    "$ref": "Dog",
    58    "maximum": 100,
    59    "minimum": 5,
    60    "exclusiveMaximum": true,
    61    "exclusiveMinimum": true,
    62    "maxLength": 100,
    63    "minLength": 5,
    64    "pattern": "\\w{1,5}\\w+",
    65    "maxItems": 100,
    66    "minItems": 5,
    67    "uniqueItems": true,
    68    "multipleOf": 5,
    69    "enum": ["hello", "world"],
    70    "type": "string",
    71    "format": "date",
    72  	"collectionFormat": "csv",
    73  	"default": "8"
    74  }`
    75  
    76  func TestIntegrationItems(t *testing.T) {
    77  	var actual Items
    78  	if assert.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual)) {
    79  		assert.EqualValues(t, actual, items)
    80  	}
    81  
    82  	assertParsesJSON(t, itemsJSON, items)
    83  }
    84  
    85  func TestItemsRoundTrip(t *testing.T) {
    86  	cases := []jsontesting.RoundTripTestCase{
    87  		{
    88  			// Show at least one field from each embededd struct sitll allows
    89  			// roundtrips successfully
    90  			Name: "UnmarshalEmbedded",
    91  			JSON: `{
    92  				"$ref": "/components/my.cool.Schema",
    93  				"pattern": "x-^",
    94  				"type": "string",
    95  				"x-framework": "swagger-go"
    96  			  }`,
    97  			Object: &Items{
    98  				Refable{MustCreateRef("/components/my.cool.Schema")},
    99  				CommonValidations{
   100  					Pattern: "x-^",
   101  				},
   102  				SimpleSchema{
   103  					Type: "string",
   104  				},
   105  				VendorExtensible{Extensions{
   106  					"x-framework": "swagger-go",
   107  				}},
   108  			},
   109  		}, {
   110  			Name:   "BasicCase",
   111  			JSON:   itemsJSON,
   112  			Object: &items,
   113  		},
   114  	}
   115  
   116  	for _, tcase := range cases {
   117  		t.Run(tcase.Name, func(t *testing.T) {
   118  			require.NoError(t, tcase.RoundTripTest(&Items{}))
   119  		})
   120  	}
   121  }