Skip to content

Commit b949573

Browse files
author
Christopher Doris
committed
test(numpydates): add test for show(datetime64)
1 parent 34adabc commit b949573

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/NumpyDates/InlineDateTime64.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ function InlineDateTime64{U}(
3636
InlineDateTime64{U}(value(DateTime64(v, U)))
3737
end
3838

39+
function InlineDateTime64{U}(v::Integer) where {U}
40+
InlineDateTime64{U}(convert(Int, v))
41+
end
42+
3943
function InlineDateTime64{U}(
4044
v::AbstractString,
4145
f::Union{AbstractString,Dates.DateFormat},
@@ -50,6 +54,10 @@ function InlineDateTime64(
5054
InlineDateTime64{unitparam(u)}(v)
5155
end
5256

57+
function InlineDateTime64(v::Integer, u::UnitArg)
58+
InlineDateTime64{unitparam(u)}(v)
59+
end
60+
5361
function InlineDateTime64(
5462
v::AbstractString,
5563
f::Union{AbstractString,Dates.DateFormat},

test/NumpyDates.jl

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@testitem "Date -> DateTime64" begin
1+
@testitem "DateTime64 from Date" begin
22
using Dates
33
using PythonCall: NumpyDates
44

@@ -100,7 +100,7 @@
100100
end
101101
end
102102

103-
@testitem "String -> DateTime64" begin
103+
@testitem "DateTime64 from String" begin
104104
using Dates
105105
using PythonCall: NumpyDates
106106

@@ -199,7 +199,7 @@ end
199199
end
200200
end
201201

202-
@testitem "String and DateFormat -> DateTime64" begin
202+
@testitem "DateTime64 from String and DateFormat" begin
203203
using Dates
204204
using PythonCall: NumpyDates
205205

@@ -237,7 +237,7 @@ end
237237
end
238238
end
239239

240-
@testitem "AbstractDateTime64 -> DateTime64" begin
240+
@testitem "DateTime64 from AbstractDateTime64" begin
241241
using Dates
242242
using PythonCall: NumpyDates
243243

@@ -255,7 +255,7 @@ end
255255
@test NumpyDates.unitpair(z) == NumpyDates.unitpair(:s)
256256
end
257257

258-
@testitem "Integer -> DateTime64" begin
258+
@testitem "DateTime64 from Integer" begin
259259
using Dates
260260
using PythonCall: NumpyDates
261261

@@ -279,7 +279,7 @@ end
279279
@test Dates.DateTime(s2) == DateTime(1970, 1, 1, 1, 0, 0)
280280
end
281281

282-
@testitem "DateTime -> DateTime64/InlineDateTime64" begin
282+
@testitem "DateTime64 from DateTime" begin
283283
using Dates
284284
using PythonCall: NumpyDates
285285

@@ -385,3 +385,63 @@ end
385385
@test NumpyDates.unitpair(inline_dyn) == NumpyDates.unitpair(usym)
386386
end
387387
end
388+
389+
@testitem "DateTime64 show" begin
390+
using Dates
391+
using PythonCall: NumpyDates
392+
393+
# Helper to get "showvalue" form by setting :typeinfo to the concrete type
394+
function showvalue_string(x)
395+
io = IOBuffer()
396+
show(IOContext(io, :typeinfo => typeof(x)), x)
397+
String(take!(io))
398+
end
399+
400+
# Helper to get the default Base.show output (with type wrapper)
401+
function show_string(x)
402+
io = IOBuffer()
403+
show(io, x)
404+
String(take!(io))
405+
end
406+
407+
# Cases: (kind, value, unit_symbol, expected_showvalue_string)
408+
cases = [
409+
# 1) Days-aligned: prints Date in value form
410+
(Date(1999, 12, 31), :D, "\"1999-12-31\"", "DAYS"),
411+
412+
# 2) Seconds-aligned: prints DateTime in value form
413+
(DateTime(1999, 12, 31, 23, 59, 59), :s, "\"1999-12-31T23:59:59\"", "SECONDS"),
414+
415+
# 3) Sub-millisecond units: fall back to raw integer in value form
416+
(1, :us, "1", "MICROSECONDS"),
417+
(1, :ns, "1", "NANOSECONDS"),
418+
419+
# 4) Calendar units (years/months): value form shows truncated Date
420+
(Date(2000, 2, 29), :Y, "\"2000-01-01\"", "YEARS"),
421+
(Date(1999, 12, 31), :M, "\"1999-12-01\"", "MONTHS"),
422+
423+
# 5) NaT
424+
("NaT", :D, "\"NaT\"", "DAYS"),
425+
]
426+
427+
@testset "$v $usym" for (v, usym, expected_val, ustr) in cases
428+
# Construct DateTime64
429+
x = NumpyDates.DateTime64(v, usym)
430+
431+
# showvalue checks
432+
s_val = showvalue_string(x)
433+
@test s_val == expected_val
434+
435+
# default show checks
436+
s_def = show_string(x)
437+
@test s_def == "PythonCall.NumpyDates.DateTime64($expected_val, $ustr)"
438+
439+
# and again with InlineDateTime64
440+
x2 = NumpyDates.InlineDateTime64(v, usym)
441+
s_val2 = showvalue_string(x2)
442+
@test s_val2 == expected_val
443+
s_def2 = show_string(x2)
444+
@test s_def2 ==
445+
"PythonCall.NumpyDates.InlineDateTime64{PythonCall.NumpyDates.$ustr}($expected_val)"
446+
end
447+
end

0 commit comments

Comments
 (0)