99)
1010from openapi_core .schema .schemas .models import Schema
1111
12+ from six import b , u
13+
1214
1315class TestSchemaIteritems (object ):
1416
@@ -77,13 +79,22 @@ def test_string_format_date(self):
7779
7880 assert result == datetime .date (2018 , 1 , 2 )
7981
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" )
8091 def test_string_format_custom (self ):
8192 custom_format = 'custom'
8293 schema = Schema ('string' , schema_format = custom_format )
8394 value = 'x'
8495
8596 with mock .patch .dict (
86- Schema .FORMAT_CALLABLE_GETTER ,
97+ Schema .STRING_FORMAT_CAST_CALLABLE_GETTER ,
8798 {custom_format : lambda x : x + '-custom' },
8899 ):
89100 result = schema .unmarshal (value )
@@ -95,17 +106,17 @@ def test_string_format_unknown(self):
95106 schema = Schema ('string' , schema_format = unknown_format )
96107 value = 'x'
97108
98- result = schema .unmarshal (value )
99-
100- assert result == 'x'
109+ with pytest .raises (OpenAPISchemaError ):
110+ schema .unmarshal (value )
101111
112+ @pytest .mark .xfail (reason = "No custom formats support atm" )
102113 def test_string_format_invalid_value (self ):
103114 custom_format = 'custom'
104115 schema = Schema ('string' , schema_format = custom_format )
105116 value = 'x'
106117
107118 with mock .patch .dict (
108- Schema .FORMAT_CALLABLE_GETTER ,
119+ Schema .STRING_FORMAT_CAST_CALLABLE_GETTER ,
109120 {custom_format : mock .Mock (side_effect = ValueError ())},
110121 ), pytest .raises (
111122 InvalidSchemaValue , message = 'Failed to format value'
@@ -191,7 +202,7 @@ def test_boolean(self, value):
191202
192203 assert result == value
193204
194- @pytest .mark .parametrize ('value' , [1 , 3.14 , 'true' , [True , False ]])
205+ @pytest .mark .parametrize ('value' , [1 , 3.14 , u ( 'true' ) , [True , False ]])
195206 def test_boolean_invalid (self , value ):
196207 schema = Schema ('boolean' )
197208
@@ -213,7 +224,7 @@ def test_array(self, value):
213224
214225 assert result == value
215226
216- @pytest .mark .parametrize ('value' , [False , 1 , 3.14 , 'true' ])
227+ @pytest .mark .parametrize ('value' , [False , 1 , 3.14 , u ( 'true' ) ])
217228 def test_array_invalid (self , value ):
218229 schema = Schema ('array' )
219230
@@ -228,7 +239,7 @@ def test_integer(self, value):
228239
229240 assert result == value
230241
231- @pytest .mark .parametrize ('value' , [False , 3.14 , 'true' , [1 , 2 ]])
242+ @pytest .mark .parametrize ('value' , [False , 3.14 , u ( 'true' ) , [1 , 2 ]])
232243 def test_integer_invalid (self , value ):
233244 schema = Schema ('integer' )
234245
@@ -250,21 +261,93 @@ def test_number_invalid(self, value):
250261 with pytest .raises (InvalidSchemaValue ):
251262 schema .validate (value )
252263
253- @pytest .mark .parametrize ('value' , ['true' , b'true' ])
264+ @pytest .mark .parametrize ('value' , [u ( 'true' ), ])
254265 def test_string (self , value ):
255266 schema = Schema ('string' )
256267
257268 result = schema .validate (value )
258269
259270 assert result == value
260271
261- @pytest .mark .parametrize ('value' , [False , 1 , 3.14 , [1 , 3 ]])
272+ @pytest .mark .parametrize ('value' , [b ( 'test' ), False , 1 , 3.14 , [1 , 3 ]])
262273 def test_string_invalid (self , value ):
263274 schema = Schema ('string' )
264275
265276 with pytest .raises (InvalidSchemaValue ):
266277 schema .validate (value )
267278
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+
268351 @pytest .mark .parametrize ('value' , ['true' , False , 1 , 3.14 , [1 , 3 ]])
269352 def test_object_not_an_object (self , value ):
270353 schema = Schema ('object' )
0 commit comments