items_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. package spec
  15. import (
  16. "encoding/json"
  17. "testing"
  18. "github.com/go-openapi/swag"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. var items = Items{
  22. Refable: Refable{Ref: MustCreateRef("Dog")},
  23. CommonValidations: CommonValidations{
  24. Maximum: float64Ptr(100),
  25. ExclusiveMaximum: true,
  26. ExclusiveMinimum: true,
  27. Minimum: float64Ptr(5),
  28. MaxLength: int64Ptr(100),
  29. MinLength: int64Ptr(5),
  30. Pattern: "\\w{1,5}\\w+",
  31. MaxItems: int64Ptr(100),
  32. MinItems: int64Ptr(5),
  33. UniqueItems: true,
  34. MultipleOf: float64Ptr(5),
  35. Enum: []interface{}{"hello", "world"},
  36. },
  37. SimpleSchema: SimpleSchema{
  38. Type: "string",
  39. Format: "date",
  40. Items: &Items{
  41. Refable: Refable{Ref: MustCreateRef("Cat")},
  42. },
  43. CollectionFormat: "csv",
  44. Default: "8",
  45. },
  46. }
  47. const itemsJSON = `{
  48. "items": {
  49. "$ref": "Cat"
  50. },
  51. "$ref": "Dog",
  52. "maximum": 100,
  53. "minimum": 5,
  54. "exclusiveMaximum": true,
  55. "exclusiveMinimum": true,
  56. "maxLength": 100,
  57. "minLength": 5,
  58. "pattern": "\\w{1,5}\\w+",
  59. "maxItems": 100,
  60. "minItems": 5,
  61. "uniqueItems": true,
  62. "multipleOf": 5,
  63. "enum": ["hello", "world"],
  64. "type": "string",
  65. "format": "date",
  66. "collectionFormat": "csv",
  67. "default": "8"
  68. }`
  69. func TestIntegrationItems(t *testing.T) {
  70. var actual Items
  71. if assert.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual)) {
  72. assert.EqualValues(t, actual, items)
  73. }
  74. assertParsesJSON(t, itemsJSON, items)
  75. }
  76. func TestTypeNameItems(t *testing.T) {
  77. var nilItems Items
  78. assert.Equal(t, "", nilItems.TypeName())
  79. assert.Equal(t, "date", items.TypeName())
  80. assert.Equal(t, "", items.ItemsTypeName())
  81. nested := Items{
  82. SimpleSchema: SimpleSchema{
  83. Type: "array",
  84. Items: &Items{
  85. SimpleSchema: SimpleSchema{
  86. Type: "integer",
  87. Format: "int32",
  88. },
  89. },
  90. CollectionFormat: "csv",
  91. },
  92. }
  93. assert.Equal(t, "array", nested.TypeName())
  94. assert.Equal(t, "int32", nested.ItemsTypeName())
  95. simple := SimpleSchema{
  96. Type: "string",
  97. Items: nil,
  98. }
  99. assert.Equal(t, "string", simple.TypeName())
  100. assert.Equal(t, "", simple.ItemsTypeName())
  101. simple.Items = NewItems()
  102. simple.Type = "array"
  103. simple.Items.Type = "string"
  104. assert.Equal(t, "array", simple.TypeName())
  105. assert.Equal(t, "string", simple.ItemsTypeName())
  106. }
  107. func TestItemsBuilder(t *testing.T) {
  108. simple := SimpleSchema{
  109. Type: "array",
  110. Items: NewItems().
  111. Typed("string", "uuid").
  112. WithDefault([]string{"default-value"}).
  113. WithEnum([]string{"abc", "efg"}, []string{"hij"}).
  114. WithMaxItems(4).
  115. WithMinItems(1).
  116. UniqueValues(),
  117. }
  118. assert.Equal(t, SimpleSchema{
  119. Type: "array",
  120. Items: &Items{
  121. SimpleSchema: SimpleSchema{
  122. Type: "string",
  123. Format: "uuid",
  124. Default: []string{"default-value"},
  125. },
  126. CommonValidations: CommonValidations{
  127. Enum: []interface{}{[]string{"abc", "efg"}, []string{"hij"}},
  128. MinItems: swag.Int64(1),
  129. MaxItems: swag.Int64(4),
  130. UniqueItems: true,
  131. },
  132. },
  133. }, simple)
  134. }
  135. func TestJSONLookupItems(t *testing.T) {
  136. res, err := items.JSONLookup("$ref")
  137. if !assert.NoError(t, err) {
  138. t.FailNow()
  139. return
  140. }
  141. if assert.IsType(t, &Ref{}, res) {
  142. ref := res.(*Ref)
  143. assert.EqualValues(t, MustCreateRef("Dog"), *ref)
  144. }
  145. var max *float64
  146. res, err = items.JSONLookup("maximum")
  147. if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, max, res) {
  148. t.FailNow()
  149. return
  150. }
  151. max = res.(*float64)
  152. assert.Equal(t, float64(100), *max)
  153. var f string
  154. res, err = items.JSONLookup("collectionFormat")
  155. if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, f, res) {
  156. t.FailNow()
  157. return
  158. }
  159. f = res.(string)
  160. assert.Equal(t, "csv", f)
  161. res, err = items.JSONLookup("unknown")
  162. if !assert.Error(t, err) || !assert.Nil(t, res) {
  163. t.FailNow()
  164. return
  165. }
  166. }