@@ -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
@@ -123,11 +132,19 @@ def test_time_parsing(value, result):
123
132
(1_549_316_052_104 , datetime (2019 , 2 , 4 , 21 , 34 , 12 , 104_000 , tzinfo = timezone .utc )), # nowish in ms
124
133
(1_549_316_052_104_324 , datetime (2019 , 2 , 4 , 21 , 34 , 12 , 104_324 , tzinfo = timezone .utc )), # nowish in μs
125
134
(1_549_316_052_104_324_096 , datetime (2019 , 2 , 4 , 21 , 34 , 12 , 104_324 , tzinfo = timezone .utc )), # nowish in ns
135
+ ('infinity' , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
136
+ ('inf' , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
137
+ ('inf ' , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
138
+ (1e50 , datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
139
+ (float ('inf' ), datetime (9999 , 12 , 31 , 23 , 59 , 59 , 999999 )),
140
+ ('-infinity' , datetime (1 , 1 , 1 , 0 , 0 )),
141
+ ('-inf' , datetime (1 , 1 , 1 , 0 , 0 )),
142
+ ('nan' , ValueError ),
126
143
],
127
144
)
128
145
def test_datetime_parsing (value , result ):
129
- if result == errors . DateTimeError :
130
- with pytest .raises (errors . DateTimeError ):
146
+ if type ( result ) == type and issubclass ( result , Exception ) :
147
+ with pytest .raises (result ):
131
148
parse_datetime (value )
132
149
else :
133
150
assert parse_datetime (value ) == result
@@ -251,3 +268,24 @@ class Model(BaseModel):
251
268
'type' : 'value_error.unicodedecode' ,
252
269
'msg' : "'utf-8' codec can't decode byte 0x81 in position 0: invalid start byte" ,
253
270
}
271
+
272
+
273
+ def test_nan ():
274
+ class Model (BaseModel ):
275
+ dt : datetime
276
+ d : date
277
+
278
+ with pytest .raises (ValidationError ) as exc_info :
279
+ Model (dt = 'nan' , d = 'nan' )
280
+ assert exc_info .value .errors () == [
281
+ {
282
+ 'loc' : ('dt' ,),
283
+ 'msg' : 'cannot convert float NaN to integer' ,
284
+ 'type' : 'value_error' ,
285
+ },
286
+ {
287
+ 'loc' : ('d' ,),
288
+ 'msg' : 'cannot convert float NaN to integer' ,
289
+ 'type' : 'value_error' ,
290
+ },
291
+ ]
0 commit comments