9
9
)
10
10
from openapi_core .schema .schemas .models import Schema
11
11
12
+ from six import b , u
13
+
12
14
13
15
class TestSchemaIteritems (object ):
14
16
@@ -77,13 +79,22 @@ def test_string_format_date(self):
77
79
78
80
assert result == datetime .date (2018 , 1 , 2 )
79
81
82
+ def test_string_format_datetime (self ):
83
+ schema = Schema ('string' , schema_format = 'date-time' )
84
+ value = '2018-01-02T00:00:00'
85
+
86
+ result = schema .unmarshal (value )
87
+
88
+ assert result == datetime .datetime (2018 , 1 , 2 , 0 , 0 , 0 )
89
+
90
+ @pytest .mark .xfail (reason = "No custom formats support atm" )
80
91
def test_string_format_custom (self ):
81
92
custom_format = 'custom'
82
93
schema = Schema ('string' , schema_format = custom_format )
83
94
value = 'x'
84
95
85
96
with mock .patch .dict (
86
- Schema .FORMAT_CALLABLE_GETTER ,
97
+ Schema .STRING_FORMAT_CAST_CALLABLE_GETTER ,
87
98
{custom_format : lambda x : x + '-custom' },
88
99
):
89
100
result = schema .unmarshal (value )
@@ -95,17 +106,17 @@ def test_string_format_unknown(self):
95
106
schema = Schema ('string' , schema_format = unknown_format )
96
107
value = 'x'
97
108
98
- result = schema .unmarshal (value )
99
-
100
- assert result == 'x'
109
+ with pytest .raises (OpenAPISchemaError ):
110
+ schema .unmarshal (value )
101
111
112
+ @pytest .mark .xfail (reason = "No custom formats support atm" )
102
113
def test_string_format_invalid_value (self ):
103
114
custom_format = 'custom'
104
115
schema = Schema ('string' , schema_format = custom_format )
105
116
value = 'x'
106
117
107
118
with mock .patch .dict (
108
- Schema .FORMAT_CALLABLE_GETTER ,
119
+ Schema .STRING_FORMAT_CAST_CALLABLE_GETTER ,
109
120
{custom_format : mock .Mock (side_effect = ValueError ())},
110
121
), pytest .raises (
111
122
InvalidSchemaValue , message = 'Failed to format value'
@@ -191,7 +202,7 @@ def test_boolean(self, value):
191
202
192
203
assert result == value
193
204
194
- @pytest .mark .parametrize ('value' , [1 , 3.14 , 'true' , [True , False ]])
205
+ @pytest .mark .parametrize ('value' , [1 , 3.14 , u ( 'true' ) , [True , False ]])
195
206
def test_boolean_invalid (self , value ):
196
207
schema = Schema ('boolean' )
197
208
@@ -213,7 +224,7 @@ def test_array(self, value):
213
224
214
225
assert result == value
215
226
216
- @pytest .mark .parametrize ('value' , [False , 1 , 3.14 , 'true' ])
227
+ @pytest .mark .parametrize ('value' , [False , 1 , 3.14 , u ( 'true' ) ])
217
228
def test_array_invalid (self , value ):
218
229
schema = Schema ('array' )
219
230
@@ -228,7 +239,7 @@ def test_integer(self, value):
228
239
229
240
assert result == value
230
241
231
- @pytest .mark .parametrize ('value' , [False , 3.14 , 'true' , [1 , 2 ]])
242
+ @pytest .mark .parametrize ('value' , [False , 3.14 , u ( 'true' ) , [1 , 2 ]])
232
243
def test_integer_invalid (self , value ):
233
244
schema = Schema ('integer' )
234
245
@@ -250,21 +261,93 @@ def test_number_invalid(self, value):
250
261
with pytest .raises (InvalidSchemaValue ):
251
262
schema .validate (value )
252
263
253
- @pytest .mark .parametrize ('value' , ['true' , b'true' ])
264
+ @pytest .mark .parametrize ('value' , [u ( 'true' ), ])
254
265
def test_string (self , value ):
255
266
schema = Schema ('string' )
256
267
257
268
result = schema .validate (value )
258
269
259
270
assert result == value
260
271
261
- @pytest .mark .parametrize ('value' , [False , 1 , 3.14 , [1 , 3 ]])
272
+ @pytest .mark .parametrize ('value' , [b ( 'test' ), False , 1 , 3.14 , [1 , 3 ]])
262
273
def test_string_invalid (self , value ):
263
274
schema = Schema ('string' )
264
275
265
276
with pytest .raises (InvalidSchemaValue ):
266
277
schema .validate (value )
267
278
279
+ @pytest .mark .parametrize ('value' , [
280
+ b ('true' ), u ('test' ), False , 1 , 3.14 , [1 , 3 ],
281
+ datetime .datetime (1989 , 1 , 2 ),
282
+ ])
283
+ def test_string_format_date_invalid (self , value ):
284
+ schema = Schema ('string' , schema_format = 'date' )
285
+
286
+ with pytest .raises (InvalidSchemaValue ):
287
+ schema .validate (value )
288
+
289
+ @pytest .mark .parametrize ('value' , [
290
+ datetime .date (1989 , 1 , 2 ), datetime .date (2018 , 1 , 2 ),
291
+ ])
292
+ def test_string_format_date (self , value ):
293
+ schema = Schema ('string' , schema_format = 'date' )
294
+
295
+ result = schema .validate (value )
296
+
297
+ assert result == value
298
+
299
+ @pytest .mark .parametrize ('value' , [
300
+ b ('true' ), u ('true' ), False , 1 , 3.14 , [1 , 3 ],
301
+ datetime .date (1989 , 1 , 2 ),
302
+ ])
303
+ def test_string_format_datetime_invalid (self , value ):
304
+ schema = Schema ('string' , schema_format = 'date-time' )
305
+
306
+ with pytest .raises (InvalidSchemaValue ):
307
+ schema .validate (value )
308
+
309
+ @pytest .mark .parametrize ('value' , [
310
+ datetime .datetime (1989 , 1 , 2 , 0 , 0 , 0 ),
311
+ datetime .datetime (2018 , 1 , 2 , 23 , 59 , 59 ),
312
+ ])
313
+ def test_string_format_datetime (self , value ):
314
+ schema = Schema ('string' , schema_format = 'date-time' )
315
+
316
+ result = schema .validate (value )
317
+
318
+ assert result == value
319
+
320
+ @pytest .mark .parametrize ('value' , [
321
+ u ('true' ), False , 1 , 3.14 , [1 , 3 ], datetime .date (1989 , 1 , 2 ),
322
+ datetime .datetime (1989 , 1 , 2 , 0 , 0 , 0 ),
323
+ ])
324
+ def test_string_format_binary_invalid (self , value ):
325
+ schema = Schema ('string' , schema_format = 'binary' )
326
+
327
+ with pytest .raises (InvalidSchemaValue ):
328
+ schema .validate (value )
329
+
330
+ @pytest .mark .parametrize ('value' , [
331
+ b ('stream' ), b ('text' ),
332
+ ])
333
+ def test_string_format_binary (self , value ):
334
+ schema = Schema ('string' , schema_format = 'binary' )
335
+
336
+ result = schema .validate (value )
337
+
338
+ assert result == value
339
+
340
+ @pytest .mark .parametrize ('value' , [
341
+ u ('test' ), b ('stream' ), datetime .date (1989 , 1 , 2 ),
342
+ datetime .datetime (1989 , 1 , 2 , 0 , 0 , 0 ),
343
+ ])
344
+ def test_string_format_unknown (self , value ):
345
+ unknown_format = 'unknown'
346
+ schema = Schema ('string' , schema_format = unknown_format )
347
+
348
+ with pytest .raises (OpenAPISchemaError ):
349
+ schema .validate (value )
350
+
268
351
@pytest .mark .parametrize ('value' , ['true' , False , 1 , 3.14 , [1 , 3 ]])
269
352
def test_object_not_an_object (self , value ):
270
353
schema = Schema ('object' )
0 commit comments