Skip to content

Commit 4144c48

Browse files
committed
Add tests
1 parent d445835 commit 4144c48

File tree

4 files changed

+187
-3
lines changed

4 files changed

+187
-3
lines changed

src/transforms/unit.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Unit(1 => u"km", :b => u"K", "c" => u"s")
2323
Unit([2, 3] => u"cm")
2424
Unit([:a, :c] => u"cm")
2525
Unit(["a", "c"] => u"cm")
26-
Unit(r"abc" => u"km")
26+
Unit(r"[abc]" => u"km")
2727
```
2828
"""
2929
struct Unit <: StatelessFeatureTransform
@@ -39,6 +39,10 @@ Unit(pairs::Pair...) = Unit(collect(selector.(first.(pairs))), collect(last.(pai
3939

4040
isrevertible(::Type{<:Unit}) = true
4141

42+
_uconvert(u, x) = _uconvert(nonmissingtype(eltype(x)), u, x)
43+
_uconvert(::Type, _, x) = (x, nothing)
44+
_uconvert(::Type{Q}, u, x) where {Q<:AbstractQuantity} = (uconvert.(u, x), unit(Q))
45+
4246
function applyfeat(transform::Unit, feat, prep)
4347
cols = Tables.columns(feat)
4448
names = Tables.columnnames(cols)
@@ -55,8 +59,7 @@ function applyfeat(transform::Unit, feat, prep)
5559
x = Tables.getcolumn(cols, name)
5660
if haskey(unitdict, name)
5761
u = unitdict[name]
58-
y = uconvert.(u, x)
59-
(y, unit(eltype(x)))
62+
_uconvert(u, x)
6063
else
6164
(x, nothing)
6265
end

test/shows.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@
257257
@test iostr == "Unitify transform"
258258
end
259259

260+
@testset "Unit" begin
261+
T = Unit(:a => u"m", [:b, :c] => u"s")
262+
263+
# compact mode
264+
iostr = sprint(show, T)
265+
@test iostr == "Unit(selectors: ColumnSelector[:a, [:b, :c]], units: Units[m, s])"
266+
267+
# full mode
268+
iostr = sprint(show, MIME("text/plain"), T)
269+
@test iostr == """
270+
Unit transform
271+
├─ selectors: ColumnSelectors.ColumnSelector[:a, [:b, :c]]
272+
└─ units: Unitful.Units[m, s]"""
273+
end
274+
260275
@testset "Map" begin
261276
fun = (a, b) -> 2a + b
262277
T = Map(:a => sin, [:a, :b] => fun => :c)

test/transforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ transformfiles = [
1515
"dropconstant.jl",
1616
"absoluteunits.jl",
1717
"unitify.jl",
18+
"unit.jl",
1819
"map.jl",
1920
"replace.jl",
2021
"coalesce.jl",

test/transforms/unit.jl

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
@testset "Unit" begin
2+
@test isrevertible(Unit(u"m"))
3+
4+
a = [2.7, 2.9, 2.2, 1.4, 1.8, 3.3] * u"m"
5+
b = [300, 500, missing, 800, missing, 400] * u"cm"
6+
c = [8, 2, 5, 7, 9, 4] * u"km"
7+
d = [0.3, 0.1, 0.9, 0.2, 0.7, 0.4]
8+
e = ["no", "no", "yes", "yes", "no", "yes"]
9+
t = Table(; a, b, c, d, e)
10+
11+
T = Unit(u"m")
12+
n, c = apply(T, t)
13+
@test unit(eltype(n.a)) == u"m"
14+
@test unit(eltype(n.b)) == u"m"
15+
@test unit(eltype(n.c)) == u"m"
16+
@test eltype(n.d) <: Float64
17+
@test eltype(n.e) <: String
18+
tₒ = revert(T, n, c)
19+
@test unit(eltype(tₒ.a)) == u"m"
20+
@test unit(eltype(tₒ.b)) == u"cm"
21+
@test unit(eltype(tₒ.c)) == u"km"
22+
@test all(isapprox.(t.a, tₒ.a))
23+
@test all(isapprox.(skipmissing(t.b), skipmissing(tₒ.b)))
24+
@test all(isapprox.(t.c, tₒ.c))
25+
@test t.d == tₒ.d
26+
@test t.e == tₒ.e
27+
28+
a = [2.7, 2.9, 2.2, 1.4, 1.8, 3.3] * u"m"
29+
b = [300, 500, missing, 800, missing, 400] * u"cm"
30+
c = [8, 2, 5, 7, 9, 4] * u"km"
31+
d = [29.1, missing, 29.2, missing, 28.4, 26.4] * u"°C"
32+
e = [0.9, 0.4, 0.5, 0.1, 0.3, 0.6] * u"kg"
33+
f = 0.5u"ppm" * e
34+
t = Table(; a, b, c, d, e, f)
35+
36+
T = Unit(4 => u"K")
37+
n, c = apply(T, t)
38+
@test unit(eltype(n.a)) == u"m"
39+
@test unit(eltype(n.b)) == u"cm"
40+
@test unit(eltype(n.c)) == u"km"
41+
@test unit(eltype(n.d)) == u"K"
42+
@test unit(eltype(n.e)) == u"kg"
43+
@test unit(eltype(n.f)) == u"kg * ppm"
44+
tₒ = revert(T, n, c)
45+
@test unit(eltype(tₒ.d)) == u"°C"
46+
@test t.a == tₒ.a
47+
@test isequal(t.b, tₒ.b)
48+
@test t.c == tₒ.c
49+
@test all(isapprox.(skipmissing(t.d), skipmissing(tₒ.d)))
50+
@test t.e == tₒ.e
51+
@test t.f == tₒ.f
52+
53+
T = Unit(:e => u"g")
54+
n, c = apply(T, t)
55+
@test unit(eltype(n.a)) == u"m"
56+
@test unit(eltype(n.b)) == u"cm"
57+
@test unit(eltype(n.c)) == u"km"
58+
@test unit(eltype(n.d)) == u"°C"
59+
@test unit(eltype(n.e)) == u"g"
60+
@test unit(eltype(n.f)) == u"kg * ppm"
61+
tₒ = revert(T, n, c)
62+
@test unit(eltype(tₒ.e)) == u"kg"
63+
@test t.a == tₒ.a
64+
@test isequal(t.b, tₒ.b)
65+
@test t.c == tₒ.c
66+
@test isequal(t.d, tₒ.d)
67+
@test all(isapprox.(t.e, tₒ.e))
68+
@test t.f == tₒ.f
69+
70+
T = Unit("f" => u"kg")
71+
n, c = apply(T, t)
72+
@test unit(eltype(n.a)) == u"m"
73+
@test unit(eltype(n.b)) == u"cm"
74+
@test unit(eltype(n.c)) == u"km"
75+
@test unit(eltype(n.d)) == u"°C"
76+
@test unit(eltype(n.e)) == u"kg"
77+
@test unit(eltype(n.f)) == u"kg"
78+
tₒ = revert(T, n, c)
79+
@test unit(eltype(tₒ.f)) == u"kg * ppm"
80+
@test t.a == tₒ.a
81+
@test isequal(t.b, tₒ.b)
82+
@test t.c == tₒ.c
83+
@test isequal(t.d, tₒ.d)
84+
@test t.e == tₒ.e
85+
@test all(isapprox.(t.f, tₒ.f))
86+
87+
T = Unit([1, 2, 3] => u"m")
88+
n, c = apply(T, t)
89+
@test unit(eltype(n.a)) == u"m"
90+
@test unit(eltype(n.b)) == u"m"
91+
@test unit(eltype(n.c)) == u"m"
92+
@test unit(eltype(n.d)) == u"°C"
93+
@test unit(eltype(n.e)) == u"kg"
94+
@test unit(eltype(n.f)) == u"kg * ppm"
95+
tₒ = revert(T, n, c)
96+
@test unit(eltype(tₒ.a)) == u"m"
97+
@test unit(eltype(tₒ.b)) == u"cm"
98+
@test unit(eltype(tₒ.c)) == u"km"
99+
@test all(isapprox.(t.a, tₒ.a))
100+
@test all(isapprox.(skipmissing(t.b), skipmissing(tₒ.b)))
101+
@test all(isapprox.(t.c, tₒ.c))
102+
@test isequal(t.d, tₒ.d)
103+
@test t.e == tₒ.e
104+
@test t.f == tₒ.f
105+
106+
T = Unit([:a, :b, :c] => u"cm")
107+
n, c = apply(T, t)
108+
@test unit(eltype(n.a)) == u"cm"
109+
@test unit(eltype(n.b)) == u"cm"
110+
@test unit(eltype(n.c)) == u"cm"
111+
@test unit(eltype(n.d)) == u"°C"
112+
@test unit(eltype(n.e)) == u"kg"
113+
@test unit(eltype(n.f)) == u"kg * ppm"
114+
tₒ = revert(T, n, c)
115+
@test unit(eltype(tₒ.a)) == u"m"
116+
@test unit(eltype(tₒ.b)) == u"cm"
117+
@test unit(eltype(tₒ.c)) == u"km"
118+
@test all(isapprox.(t.a, tₒ.a))
119+
@test all(isapprox.(skipmissing(t.b), skipmissing(tₒ.b)))
120+
@test all(isapprox.(t.c, tₒ.c))
121+
@test isequal(t.d, tₒ.d)
122+
@test t.e == tₒ.e
123+
@test t.f == tₒ.f
124+
125+
T = Unit(["a", "b", "c"] => u"km")
126+
n, c = apply(T, t)
127+
@test unit(eltype(n.a)) == u"km"
128+
@test unit(eltype(n.b)) == u"km"
129+
@test unit(eltype(n.c)) == u"km"
130+
@test unit(eltype(n.d)) == u"°C"
131+
@test unit(eltype(n.e)) == u"kg"
132+
@test unit(eltype(n.f)) == u"kg * ppm"
133+
tₒ = revert(T, n, c)
134+
@test unit(eltype(tₒ.a)) == u"m"
135+
@test unit(eltype(tₒ.b)) == u"cm"
136+
@test unit(eltype(tₒ.c)) == u"km"
137+
@test all(isapprox.(t.a, tₒ.a))
138+
@test all(isapprox.(skipmissing(t.b), skipmissing(tₒ.b)))
139+
@test all(isapprox.(t.c, tₒ.c))
140+
@test isequal(t.d, tₒ.d)
141+
@test t.e == tₒ.e
142+
@test t.f == tₒ.f
143+
144+
T = Unit(r"[abc]" => u"m")
145+
n, c = apply(T, t)
146+
@test unit(eltype(n.a)) == u"m"
147+
@test unit(eltype(n.b)) == u"m"
148+
@test unit(eltype(n.c)) == u"m"
149+
@test unit(eltype(n.d)) == u"°C"
150+
@test unit(eltype(n.e)) == u"kg"
151+
@test unit(eltype(n.f)) == u"kg * ppm"
152+
tₒ = revert(T, n, c)
153+
@test unit(eltype(tₒ.a)) == u"m"
154+
@test unit(eltype(tₒ.b)) == u"cm"
155+
@test unit(eltype(tₒ.c)) == u"km"
156+
@test all(isapprox.(t.a, tₒ.a))
157+
@test all(isapprox.(skipmissing(t.b), skipmissing(tₒ.b)))
158+
@test all(isapprox.(t.c, tₒ.c))
159+
@test isequal(t.d, tₒ.d)
160+
@test t.e == tₒ.e
161+
@test t.f == tₒ.f
162+
163+
# error: cannot create Unit transform without arguments
164+
@test_throws ArgumentError Unit()
165+
end

0 commit comments

Comments
 (0)