@@ -42,11 +42,20 @@ def create_tz(minutes):
42
42
(1_549_316_052_104 , date (2019 , 2 , 4 )), # nowish in ms
43
43
(1_549_316_052_104_324 , date (2019 , 2 , 4 )), # nowish in μs
44
44
(1_549_316_052_104_324_096 , date (2019 , 2 , 4 )), # nowish in ns
45
+ ('infinity' , date (9999 , 12 , 31 )),
46
+ ('inf' , date (9999 , 12 , 31 )),
47
+ (float ('inf' ), date (9999 , 12 , 31 )),
48
+ ('infinity ' , date (9999 , 12 , 31 )),
49
+ (int ('1' + '0' * 100 ), date (9999 , 12 , 31 )),
50
+ (1e1000 , date (9999 , 12 , 31 )),
51
+ ('-infinity' , date (1 , 1 , 1 )),
52
+ ('-inf' , date (1 , 1 , 1 )),
53
+ ('nan' , ValueError ),
45
54
],
46
55
)
47
56
def test_date_parsing (value , result ):
48
- if result == errors . DateError :
49
- with pytest .raises (errors . DateError ):
57
+ if type ( result ) == type and issubclass ( result , Exception ) :
58
+ with pytest .raises (result ):
50
59
parse_date (value )
51
60
else :
52
61
assert parse_date (value ) == result
@@ -114,11 +123,19 @@ def test_time_parsing(value, result):
114
123
(1_549_316_052_104 , datetime (2019 , 2 , 4 , 21 , 34 , 12 , 104_000 , tzinfo = timezone .utc )), # nowish in ms
115
124
(1_549_316_052_104_324 , datetime (2019 , 2 , 4 , 21 , 34 , 12 , 104_324 , tzinfo = timezone .utc )), # nowish in μs
116
125
(1_549_316_052_104_324_096 , datetime (2019 , 2 , 4 , 21 , 34 , 12 , 104_324 , tzinfo = timezone .utc )), # nowish in ns
126
+ ('infinity' , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
127
+ ('inf' , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
128
+ ('inf ' , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
129
+ (1e50 , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
130
+ (float ('inf' ), datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
131
+ ('-infinity' , datetime (1 , 1 , 1 , 0 , 0 )),
132
+ ('-inf' , datetime (1 , 1 , 1 , 0 , 0 )),
133
+ ('nan' , ValueError ),
117
134
],
118
135
)
119
136
def test_datetime_parsing (value , result ):
120
- if result == errors . DateTimeError :
121
- with pytest .raises (errors . DateTimeError ):
137
+ if type ( result ) == type and issubclass ( result , Exception ) :
138
+ with pytest .raises (result ):
122
139
parse_datetime (value )
123
140
else :
124
141
assert parse_datetime (value ) == result
@@ -242,3 +259,24 @@ class Model(BaseModel):
242
259
'type' : 'value_error.unicodedecode' ,
243
260
'msg' : "'utf-8' codec can't decode byte 0x81 in position 0: invalid start byte" ,
244
261
}
262
+
263
+
264
+ def test_nan ():
265
+ class Model (BaseModel ):
266
+ dt : datetime
267
+ d : date
268
+
269
+ with pytest .raises (ValidationError ) as exc_info :
270
+ Model (dt = 'nan' , d = 'nan' )
271
+ assert exc_info .value .errors () == [
272
+ {
273
+ 'loc' : ('dt' ,),
274
+ 'msg' : 'cannot convert float NaN to integer' ,
275
+ 'type' : 'value_error' ,
276
+ },
277
+ {
278
+ 'loc' : ('d' ,),
279
+ 'msg' : 'cannot convert float NaN to integer' ,
280
+ 'type' : 'value_error' ,
281
+ },
282
+ ]
0 commit comments