...

Source file src/github.com/go-openapi/spec/items_test.go

Documentation: github.com/go-openapi/spec

     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/go-openapi/swag"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    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  	require.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual))
    79  	assert.EqualValues(t, actual, items)
    80  
    81  	assertParsesJSON(t, itemsJSON, items)
    82  }
    83  
    84  func TestTypeNameItems(t *testing.T) {
    85  	var nilItems Items
    86  	assert.Equal(t, "", nilItems.TypeName())
    87  
    88  	assert.Equal(t, "date", items.TypeName())
    89  	assert.Equal(t, "", items.ItemsTypeName())
    90  
    91  	nested := Items{
    92  		SimpleSchema: SimpleSchema{
    93  			Type: "array",
    94  			Items: &Items{
    95  				SimpleSchema: SimpleSchema{
    96  					Type:   "integer",
    97  					Format: "int32",
    98  				},
    99  			},
   100  			CollectionFormat: "csv",
   101  		},
   102  	}
   103  
   104  	assert.Equal(t, "array", nested.TypeName())
   105  	assert.Equal(t, "int32", nested.ItemsTypeName())
   106  
   107  	simple := SimpleSchema{
   108  		Type:  "string",
   109  		Items: nil,
   110  	}
   111  
   112  	assert.Equal(t, "string", simple.TypeName())
   113  	assert.Equal(t, "", simple.ItemsTypeName())
   114  
   115  	simple.Items = NewItems()
   116  	simple.Type = "array"
   117  	simple.Items.Type = "string"
   118  
   119  	assert.Equal(t, "array", simple.TypeName())
   120  	assert.Equal(t, "string", simple.ItemsTypeName())
   121  }
   122  
   123  func TestItemsBuilder(t *testing.T) {
   124  	simple := SimpleSchema{
   125  		Type: "array",
   126  		Items: NewItems().
   127  			Typed("string", "uuid").
   128  			WithDefault([]string{"default-value"}).
   129  			WithEnum([]string{"abc", "efg"}, []string{"hij"}).
   130  			WithMaxItems(4).
   131  			WithMinItems(1).
   132  			UniqueValues(),
   133  	}
   134  
   135  	assert.Equal(t, SimpleSchema{
   136  		Type: "array",
   137  		Items: &Items{
   138  			SimpleSchema: SimpleSchema{
   139  				Type:    "string",
   140  				Format:  "uuid",
   141  				Default: []string{"default-value"},
   142  			},
   143  			CommonValidations: CommonValidations{
   144  				Enum:        []interface{}{[]string{"abc", "efg"}, []string{"hij"}},
   145  				MinItems:    swag.Int64(1),
   146  				MaxItems:    swag.Int64(4),
   147  				UniqueItems: true,
   148  			},
   149  		},
   150  	}, simple)
   151  }
   152  
   153  func TestJSONLookupItems(t *testing.T) {
   154  	res, err := items.JSONLookup("$ref")
   155  	require.NoError(t, err)
   156  	require.NotNil(t, res)
   157  	require.IsType(t, &Ref{}, res)
   158  
   159  	var ok bool
   160  	ref, ok := res.(*Ref)
   161  	require.True(t, ok)
   162  	assert.EqualValues(t, MustCreateRef("Dog"), *ref)
   163  
   164  	var max *float64
   165  	res, err = items.JSONLookup("maximum")
   166  	require.NoError(t, err)
   167  	require.NotNil(t, res)
   168  	require.IsType(t, max, res)
   169  
   170  	max, ok = res.(*float64)
   171  	require.True(t, ok)
   172  	assert.InDelta(t, float64(100), *max, epsilon)
   173  
   174  	var f string
   175  	res, err = items.JSONLookup("collectionFormat")
   176  	require.NoError(t, err)
   177  	require.NotNil(t, res)
   178  	require.IsType(t, f, res)
   179  
   180  	f, ok = res.(string)
   181  	require.True(t, ok)
   182  	assert.Equal(t, "csv", f)
   183  
   184  	res, err = items.JSONLookup("unknown")
   185  	require.Error(t, err)
   186  	require.Nil(t, res)
   187  }
   188  
   189  func TestItemsWithValidation(t *testing.T) {
   190  	i := new(Items).WithValidations(CommonValidations{MaxLength: swag.Int64(15)})
   191  	assert.EqualValues(t, swag.Int64(15), i.MaxLength)
   192  }
   193  

View as plain text