36
36
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
37
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
38
# SOFTWARE.
39
-
40
39
import datetime
41
40
42
- from . import CPyExtType
41
+ from . import CPyExtType , CPyExtTestCase , CPyExtFunction , unhandled_error_compare
43
42
44
43
__dir__ = __file__ .rpartition ("/" )[0 ]
45
44
46
45
47
- class TestDateTime (object ):
48
-
46
+ class TestPyDateTime (CPyExtTestCase ):
47
+
48
+ def compile_module (self , name ):
49
+ type (self ).mro ()[1 ].__dict__ ["test_%s" % name ].create_module (name )
50
+ super ().compile_module (name )
51
+
52
+ test_PyDateTime_GET_YEAR = CPyExtFunction (
53
+ lambda args : args [0 ].year ,
54
+ lambda : (
55
+ (datetime .date (year = 2023 , month = 2 , day = 12 ),),
56
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
57
+ ),
58
+ code = '#include "datetime.h"' ,
59
+ resultspec = "i" ,
60
+ argspec = 'O' ,
61
+ arguments = ("PyObject* datetime" ,),
62
+ cmpfunc = unhandled_error_compare ,
63
+ )
64
+
65
+ test_PyDateTime_GET_MONTH = CPyExtFunction (
66
+ lambda args : args [0 ].month ,
67
+ lambda : (
68
+ (datetime .date (year = 2023 , month = 2 , day = 12 ),),
69
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
70
+ ),
71
+ code = '#include "datetime.h"' ,
72
+ resultspec = "i" ,
73
+ argspec = 'O' ,
74
+ arguments = ("PyObject* datetime" ,),
75
+ cmpfunc = unhandled_error_compare ,
76
+ )
77
+
78
+ test_PyDateTime_GET_DAY = CPyExtFunction (
79
+ lambda args : args [0 ].day ,
80
+ lambda : (
81
+ (datetime .date (year = 2023 , month = 2 , day = 12 ),),
82
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
83
+ ),
84
+ code = '#include "datetime.h"' ,
85
+ resultspec = "i" ,
86
+ argspec = 'O' ,
87
+ arguments = ("PyObject* datetime" ,),
88
+ cmpfunc = unhandled_error_compare ,
89
+ )
90
+
91
+ test_PyDateTime_DATE_GET_HOUR = CPyExtFunction (
92
+ lambda args : args [0 ].hour ,
93
+ lambda : (
94
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
95
+ ),
96
+ code = '#include "datetime.h"' ,
97
+ resultspec = "i" ,
98
+ argspec = 'O' ,
99
+ arguments = ("PyObject* datetime" ,),
100
+ cmpfunc = unhandled_error_compare ,
101
+ )
102
+
103
+ test_PyDateTime_DATE_GET_MINUTE = CPyExtFunction (
104
+ lambda args : args [0 ].minute ,
105
+ lambda : (
106
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
107
+ ),
108
+ code = '#include "datetime.h"' ,
109
+ resultspec = "i" ,
110
+ argspec = 'O' ,
111
+ arguments = ("PyObject* datetime" ,),
112
+ cmpfunc = unhandled_error_compare ,
113
+ )
114
+
115
+ test_PyDateTime_DATE_GET_SECOND = CPyExtFunction (
116
+ lambda args : args [0 ].second ,
117
+ lambda : (
118
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
119
+ ),
120
+ code = '#include "datetime.h"' ,
121
+ resultspec = "i" ,
122
+ argspec = 'O' ,
123
+ arguments = ("PyObject* datetime" ,),
124
+ cmpfunc = unhandled_error_compare ,
125
+ )
126
+
127
+ test_PyDateTime_DATE_GET_MICROSECOND = CPyExtFunction (
128
+ lambda args : args [0 ].microsecond ,
129
+ lambda : (
130
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
131
+ ),
132
+ code = '#include "datetime.h"' ,
133
+ resultspec = "i" ,
134
+ argspec = 'O' ,
135
+ arguments = ("PyObject* datetime" ,),
136
+ cmpfunc = unhandled_error_compare ,
137
+ )
138
+
139
+ test_PyDateTime_DATE_GET_FOLD = CPyExtFunction (
140
+ lambda args : args [0 ].fold ,
141
+ lambda : (
142
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
143
+ ),
144
+ code = '#include "datetime.h"' ,
145
+ resultspec = "i" ,
146
+ argspec = 'O' ,
147
+ arguments = ("PyObject* datetime" ,),
148
+ cmpfunc = unhandled_error_compare ,
149
+ )
150
+
151
+ test_PyDateTime_DATE_GET_TZINFO = CPyExtFunction (
152
+ lambda args : args [0 ].tzinfo ,
153
+ lambda : (
154
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
155
+ (datetime .datetime (year = 2023 , month = 2 , day = 12 , hour = 3 , minute = 14 , second = 8 , microsecond = 9 , tzinfo = datetime .timezone .utc ),),
156
+ ),
157
+ code = '#include "datetime.h"' ,
158
+ resultspec = "O" ,
159
+ argspec = 'O' ,
160
+ arguments = ("PyObject* datetime" ,),
161
+ cmpfunc = unhandled_error_compare ,
162
+ )
163
+
164
+ test_PyDateTime_TIME_GET_HOUR = CPyExtFunction (
165
+ lambda args : args [0 ].hour ,
166
+ lambda : (
167
+ (datetime .time (hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
168
+ ),
169
+ code = '#include "datetime.h"' ,
170
+ resultspec = "i" ,
171
+ argspec = 'O' ,
172
+ arguments = ("PyObject* datetime" ,),
173
+ cmpfunc = unhandled_error_compare ,
174
+ )
175
+
176
+ test_PyDateTime_TIME_GET_MINUTE = CPyExtFunction (
177
+ lambda args : args [0 ].minute ,
178
+ lambda : (
179
+ (datetime .time (hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
180
+ ),
181
+ code = '#include "datetime.h"' ,
182
+ resultspec = "i" ,
183
+ argspec = 'O' ,
184
+ arguments = ("PyObject* datetime" ,),
185
+ cmpfunc = unhandled_error_compare ,
186
+ )
187
+
188
+ test_PyDateTime_TIME_GET_SECOND = CPyExtFunction (
189
+ lambda args : args [0 ].second ,
190
+ lambda : (
191
+ (datetime .time (hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
192
+ ),
193
+ code = '#include "datetime.h"' ,
194
+ resultspec = "i" ,
195
+ argspec = 'O' ,
196
+ arguments = ("PyObject* datetime" ,),
197
+ cmpfunc = unhandled_error_compare ,
198
+ )
199
+
200
+ test_PyDateTime_TIME_GET_MICROSECOND = CPyExtFunction (
201
+ lambda args : args [0 ].microsecond ,
202
+ lambda : (
203
+ (datetime .time (hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
204
+ ),
205
+ code = '#include "datetime.h"' ,
206
+ resultspec = "i" ,
207
+ argspec = 'O' ,
208
+ arguments = ("PyObject* datetime" ,),
209
+ cmpfunc = unhandled_error_compare ,
210
+ )
211
+
212
+ test_PyDateTime_TIME_GET_FOLD = CPyExtFunction (
213
+ lambda args : args [0 ].fold ,
214
+ lambda : (
215
+ (datetime .time (hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
216
+ ),
217
+ code = '#include "datetime.h"' ,
218
+ resultspec = "i" ,
219
+ argspec = 'O' ,
220
+ arguments = ("PyObject* datetime" ,),
221
+ cmpfunc = unhandled_error_compare ,
222
+ )
223
+
224
+ test_PyDateTime_TIME_GET_TZINFO = CPyExtFunction (
225
+ lambda args : args [0 ].tzinfo ,
226
+ lambda : (
227
+ (datetime .time (hour = 3 , minute = 14 , second = 8 , microsecond = 9 ),),
228
+ (datetime .time (hour = 3 , minute = 14 , second = 8 , microsecond = 9 , tzinfo = datetime .timezone .utc ),),
229
+ ),
230
+ code = '#include "datetime.h"' ,
231
+ resultspec = "O" ,
232
+ argspec = 'O' ,
233
+ arguments = ("PyObject* datetime" ,),
234
+ cmpfunc = unhandled_error_compare ,
235
+ )
236
+
237
+ test_PyDateTime_DELTA_GET_DAYS = CPyExtFunction (
238
+ lambda args : args [0 ].days ,
239
+ lambda : (
240
+ (datetime .timedelta (days = 3 , seconds = 8 , microseconds = 9 ),),
241
+ ),
242
+ code = '#include "datetime.h"' ,
243
+ resultspec = "i" ,
244
+ argspec = 'O' ,
245
+ arguments = ("PyObject* datetime" ,),
246
+ cmpfunc = unhandled_error_compare ,
247
+ )
248
+
249
+ test_PyDateTime_DELTA_GET_SECONDS = CPyExtFunction (
250
+ lambda args : args [0 ].seconds ,
251
+ lambda : (
252
+ (datetime .timedelta (days = 3 , seconds = 8 , microseconds = 9 ),),
253
+ ),
254
+ code = '#include "datetime.h"' ,
255
+ resultspec = "i" ,
256
+ argspec = 'O' ,
257
+ arguments = ("PyObject* datetime" ,),
258
+ cmpfunc = unhandled_error_compare ,
259
+ )
260
+
261
+ test_PyDateTime_DELTA_GET_MICROSECONDS = CPyExtFunction (
262
+ lambda args : args [0 ].microseconds ,
263
+ lambda : (
264
+ (datetime .timedelta (days = 3 , seconds = 8 , microseconds = 9 ),),
265
+ ),
266
+ code = '#include "datetime.h"' ,
267
+ resultspec = "i" ,
268
+ argspec = 'O' ,
269
+ arguments = ("PyObject* datetime" ,),
270
+ cmpfunc = unhandled_error_compare ,
271
+ )
272
+
273
+
274
+ class TestDateTime (object ):
275
+
49
276
def test_date_type (self ):
50
277
TestDate = CPyExtType ("TestDate" ,
51
278
"""
@@ -77,7 +304,7 @@ def test_datetime_type(self):
77
304
)
78
305
tester = TestDateTime ()
79
306
assert tester .getDateTimeType () == datetime .datetime
80
-
307
+
81
308
def test_time_type (self ):
82
309
TestTime = CPyExtType ("TestTime" ,
83
310
"""
@@ -93,7 +320,7 @@ def test_time_type(self):
93
320
)
94
321
tester = TestTime ()
95
322
assert tester .getTimeType () == datetime .time
96
-
323
+
97
324
def test_timedelta_type (self ):
98
325
TestTimeDelta = CPyExtType ("TestTimeDelta" ,
99
326
"""
@@ -109,7 +336,7 @@ def test_timedelta_type(self):
109
336
)
110
337
tester = TestTimeDelta ()
111
338
assert tester .getTimeDeltaType () == datetime .timedelta
112
-
339
+
113
340
def test_tzinfo_type (self ):
114
341
TestTZInfo = CPyExtType ("TestTZInfo" ,
115
342
"""
@@ -125,7 +352,7 @@ def test_tzinfo_type(self):
125
352
)
126
353
tester = TestTZInfo ()
127
354
assert tester .getTZInfoType () == datetime .tzinfo
128
-
355
+
129
356
def test_timezone (self ):
130
357
TestTimezone = CPyExtType ("TestTimezone" ,
131
358
"""
@@ -158,7 +385,7 @@ def test_date_from_date(self):
158
385
)
159
386
tester = TestDateFromDate ()
160
387
assert tester .getDate () == datetime .date (1 , 1 , 1 )
161
-
388
+
162
389
def test_datetime_from_date_and_time (self ):
163
390
TestDateTimeFromDateAndTime = CPyExtType ("TestDateTimeFromDateAndTime" ,
164
391
"""
@@ -174,7 +401,7 @@ def test_datetime_from_date_and_time(self):
174
401
)
175
402
tester = TestDateTimeFromDateAndTime ()
176
403
assert tester .getDateTime () == datetime .datetime (1 , 1 , 1 , 1 , 1 , 1 , 1 )
177
-
404
+
178
405
def test_time_from_time (self ):
179
406
TestTimeFromTime = CPyExtType ("TestTimeFromTime" ,
180
407
"""
@@ -189,7 +416,7 @@ def test_time_from_time(self):
189
416
includes = '#include "datetime.h"' ,
190
417
)
191
418
tester = TestTimeFromTime ()
192
- tester .getTime () == datetime .time (1 , 1 , 1 , 1 )
419
+ assert tester .getTime () == datetime .time (1 , 1 , 1 , 1 )
193
420
194
421
def test_delta_from_delta (self ):
195
422
TestDeltaFromDelta = CPyExtType ("TestDeltaFromDelta" ,
@@ -205,9 +432,9 @@ def test_delta_from_delta(self):
205
432
includes = '#include "datetime.h"' ,
206
433
)
207
434
tester = TestDeltaFromDelta ()
208
-
435
+
209
436
assert tester .getDelta () == datetime .timedelta (1 , 1 , 1 )
210
-
437
+
211
438
def test_timezone_from_timezone (self ):
212
439
TestTimezoneFromTimezone = CPyExtType ("TestTimezoneFromTimezone" ,
213
440
"""
@@ -224,8 +451,8 @@ def test_timezone_from_timezone(self):
224
451
includes = '#include "datetime.h"' ,
225
452
)
226
453
tester = TestTimezoneFromTimezone ()
227
- assert tester .getTZ () == datetime .timezone (datetime .timedelta (0 ,0 ,1 ,0 ), "CET" )
228
-
454
+ assert tester .getTZ () == datetime .timezone (datetime .timedelta (0 ,0 ,1 ,0 ), "CET" )
455
+
229
456
def test_time_from_time_and_fold (self ):
230
457
TestTimeFromTimeAndFold = CPyExtType ("TestTimeFromTimeAndFold" ,
231
458
"""
@@ -241,7 +468,7 @@ def test_time_from_time_and_fold(self):
241
468
)
242
469
tester = TestTimeFromTimeAndFold ()
243
470
assert tester .getTime () == datetime .time (1 , 1 , 1 , 1 , None , fold = True )
244
-
471
+
245
472
def test_datetime_from_date_and_time_and_fold (self ):
246
473
TestDateTimeFromDateAndTimeAndFold = CPyExtType ("TestDateTimeFromDateAndTimeAndFold" ,
247
474
"""
@@ -257,7 +484,7 @@ def test_datetime_from_date_and_time_and_fold(self):
257
484
)
258
485
tester = TestDateTimeFromDateAndTimeAndFold ()
259
486
assert tester .getDateTime () == datetime .datetime (1 , 1 , 1 , 1 , 1 , 1 , 1 , None , fold = True )
260
-
487
+
261
488
def test_date_from_timestamp (self ):
262
489
TestDateFromTimestamp = CPyExtType ("TestDateFromTimestamp" ,
263
490
"""
@@ -274,7 +501,7 @@ def test_date_from_timestamp(self):
274
501
tester = TestDateFromTimestamp ()
275
502
ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
276
503
assert tester .getDate (int (ts )) == datetime .date .fromtimestamp (int (ts ))
277
-
504
+
278
505
def test_datetime_from_timestamp (self ):
279
506
TestDatetimeFromTimestamp = CPyExtType ("TestDatetimeFromTimestamp" ,
280
507
"""
@@ -291,7 +518,7 @@ def test_datetime_from_timestamp(self):
291
518
tester = TestDatetimeFromTimestamp ()
292
519
ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
293
520
assert tester .getDatetime (int (ts )) == datetime .datetime .fromtimestamp (int (ts ))
294
-
521
+
295
522
def test_datetime_from_timestamp_and_tz (self ):
296
523
TestDatetimeFromTimestamp = CPyExtType ("TestDatetimeFromTimestamp" ,
297
524
"""
@@ -309,7 +536,7 @@ def test_datetime_from_timestamp_and_tz(self):
309
536
ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
310
537
tz = datetime .timezone (datetime .timedelta (hours = 3 ))
311
538
assert tester .getDatetime (int (ts ), tz ) == datetime .datetime .fromtimestamp (int (ts ), tz )
312
-
539
+
313
540
def test_datetime_from_timestamp_and_tz_kwd (self ):
314
541
TestDatetimeFromTimestamp = CPyExtType ("TestDatetimeFromTimestamp" ,
315
542
"""
@@ -326,7 +553,7 @@ def test_datetime_from_timestamp_and_tz_kwd(self):
326
553
tester = TestDatetimeFromTimestamp ()
327
554
ts = datetime .datetime (1995 , 4 , 12 ).timestamp ()
328
555
tz = datetime .timezone (datetime .timedelta (hours = 3 ))
329
- assert tester .getDatetime (int (ts ), tz = tz ) == datetime .datetime .fromtimestamp (int (ts ), tz = tz )
556
+ assert tester .getDatetime (int (ts ), tz = tz ) == datetime .datetime .fromtimestamp (int (ts ), tz = tz )
330
557
331
558
def test_write_and_invoke_member (self ):
332
559
TestWriteAndInvokeMemeber = CPyExtType ("TestWriteAndInvokeMemeber" ,
@@ -349,4 +576,4 @@ def test_write_and_invoke_member(self):
349
576
includes = '#include "datetime.h"' ,
350
577
)
351
578
tester = TestWriteAndInvokeMemeber ()
352
- assert tester .getDate () == "foo"
579
+ assert tester .getDate () == "foo"
0 commit comments