Skip to content

Commit 2f7ccb4

Browse files
author
Christopher Doris
committed
test(numpydates): add tests for defaultunit and add more methods for period types
1 parent 955ebee commit 2f7ccb4

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/NumpyDates/Unit.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,43 @@ end
7474
function defaultunit(::Dates.DateTime)
7575
MILLISECONDS
7676
end
77+
78+
function defaultunit(::Dates.Year)
79+
YEARS
80+
end
81+
82+
function defaultunit(::Dates.Month)
83+
MONTHS
84+
end
85+
86+
function defaultunit(::Dates.Week)
87+
WEEKS
88+
end
89+
90+
function defaultunit(::Dates.Day)
91+
DAYS
92+
end
93+
94+
function defaultunit(::Dates.Hour)
95+
HOURS
96+
end
97+
98+
function defaultunit(::Dates.Minute)
99+
MINUTES
100+
end
101+
102+
function defaultunit(::Dates.Second)
103+
SECONDS
104+
end
105+
106+
function defaultunit(::Dates.Millisecond)
107+
MILLISECONDS
108+
end
109+
110+
function defaultunit(::Dates.Microsecond)
111+
MICROSECONDS
112+
end
113+
114+
function defaultunit(::Dates.Nanosecond)
115+
NANOSECONDS
116+
end

test/NumpyDates.jl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,71 @@ end
747747
@test_throws Exception Dates.Date(nat3)
748748
end
749749
end
750+
751+
@testitem "defaultunit" begin
752+
using Dates
753+
using PythonCall: NumpyDates
754+
755+
# Helper: normalize any defaultunit result to a unitpair tuple
756+
norm(u) = NumpyDates.unitpair(u)
757+
758+
# 1) Primitives and Dates.* types
759+
cases = [
760+
# fallback Any (e.g. AbstractString)
761+
("hello", :ns),
762+
763+
# Dates.Date / Dates.DateTime
764+
(Date(2000, 1, 2), :D),
765+
(DateTime(2000, 1, 2, 3, 4, 5), :ms),
766+
767+
# Dates.Periods => exact unit mapping
768+
(Year(1), :Y),
769+
(Month(1), :M),
770+
(Week(1), :W),
771+
(Day(1), :D),
772+
(Hour(1), :h),
773+
(Minute(1), :m),
774+
(Second(1), :s),
775+
(Millisecond(1), :ms),
776+
(Microsecond(1), :us),
777+
(Nanosecond(1), :ns),
778+
]
779+
780+
@testset "$x -> $u" for (x, u) in cases
781+
@test norm(NumpyDates.defaultunit(x)) == norm(u)
782+
end
783+
784+
# 2) (Inline)DateTime64 should return their own unitpair
785+
@testset "AbstractDateTime64 instances" begin
786+
# DateTime64
787+
dt_s = NumpyDates.DateTime64(0, :s)
788+
dt_ms = NumpyDates.DateTime64(0, :ms)
789+
@test norm(NumpyDates.defaultunit(dt_s)) == NumpyDates.unitpair(dt_s)
790+
@test norm(NumpyDates.defaultunit(dt_ms)) == NumpyDates.unitpair(dt_ms)
791+
792+
# InlineDateTime64 typed
793+
idt_typed = NumpyDates.InlineDateTime64{NumpyDates.SECONDS}(0)
794+
@test norm(NumpyDates.defaultunit(idt_typed)) == NumpyDates.unitpair(idt_typed)
795+
796+
# InlineDateTime64 dynamic
797+
idt_dyn = NumpyDates.InlineDateTime64(0, :us)
798+
@test norm(NumpyDates.defaultunit(idt_dyn)) == NumpyDates.unitpair(idt_dyn)
799+
end
800+
801+
# 3) (Inline)TimeDelta64 should return their own unitpair
802+
@testset "AbstractTimeDelta64 instances" begin
803+
# TimeDelta64
804+
td_ns = NumpyDates.TimeDelta64(0, :ns)
805+
td_ps = NumpyDates.TimeDelta64(0, :ps)
806+
@test norm(NumpyDates.defaultunit(td_ns)) == NumpyDates.unitpair(td_ns)
807+
@test norm(NumpyDates.defaultunit(td_ps)) == NumpyDates.unitpair(td_ps)
808+
809+
# InlineTimeDelta64 typed
810+
itd_typed = NumpyDates.InlineTimeDelta64{NumpyDates.MINUTES}(0)
811+
@test norm(NumpyDates.defaultunit(itd_typed)) == NumpyDates.unitpair(itd_typed)
812+
813+
# InlineTimeDelta64 dynamic
814+
itd_dyn = NumpyDates.InlineTimeDelta64(0, :as)
815+
@test norm(NumpyDates.defaultunit(itd_dyn)) == NumpyDates.unitpair(itd_dyn)
816+
end
817+
end

0 commit comments

Comments
 (0)