Skip to content

Commit c53f3de

Browse files
committed
group tests with @testset
1 parent 86bf3b1 commit c53f3de

17 files changed

+176
-169
lines changed

test/cat.jl

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
module TestCat
2-
using Base.Test
3-
using DataFrames
4-
5-
#
6-
# hcat
7-
#
8-
1+
@testset "concatenation" begin
92
nvint = NullableArray(Nullable{Int}[1, 2, Nullable(), 4])
103
nvstr = NullableArray(Nullable{String}["one", "two", Nullable(), "four"])
114

5+
null_df = DataFrame(Int, 0, 0)
6+
df = DataFrame(Int, 4, 3)
127
df2 = DataFrame(Any[nvint, nvstr])
138
df3 = DataFrame(Any[nvint])
149
df4 = convert(DataFrame, [1:4 1:4])
1510
df5 = DataFrame(Any[NullableArray([1,2,3,4]), nvstr])
1611

12+
@testset "hcat()" begin
1713
dfh = hcat(df3, df4)
1814
@test size(dfh, 2) == 3
1915
@test names(dfh) == [:x1, :x1_1, :x2]
@@ -27,14 +23,9 @@ module TestCat
2723
@test isequal(dfh3, DataFrames.hcat!(DataFrame(), df3, df4, df5))
2824

2925
@test isequal(df2, DataFrames.hcat!(df2))
26+
end
3027

31-
#
32-
# vcat
33-
#
34-
35-
null_df = DataFrame(Int, 0, 0)
36-
df = DataFrame(Int, 4, 3)
37-
28+
@testset "broadcasting" begin
3829
# Assignment of rows
3930
df[1, :] = df[1, :]
4031
df[1:2, :] = df[1:2, :]
@@ -71,7 +62,9 @@ module TestCat
7162
# vector broadcasting assignment of subframes
7263
df[1:2, 1:2] = [3,2]
7364
df[[true,false,false,true], 2:3] = [2,3]
65+
end
7466

67+
@testset "vcat()" begin
7568
vcat([])
7669
vcat(null_df)
7770
vcat(null_df, null_df)
@@ -149,3 +142,5 @@ module TestCat
149142
# vcat should be able to concatenate different implementations of AbstractDataFrame (PR #944)
150143
@test isequal(vcat(sub(DataFrame(A=1:3),2),DataFrame(A=4:5)), DataFrame(A=[2,4,5]))
151144
end
145+
146+
end

test/contrasts.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
module TestContrasts
2-
3-
using Base.Test
4-
using DataFrames
5-
1+
@testset "Contrasts" begin
62

73
d = DataFrame(x = CategoricalVector([:a, :b, :c, :a, :a, :b]))
84

95
mf = ModelFrame(Formula(nothing, :x), d)
106

7+
@testset "Dummy" begin
118
# Dummy coded contrasts by default:
129
@test ModelMatrix(mf).m == [1 0 0
1310
1 1 0
@@ -20,7 +17,9 @@ mf = ModelFrame(Formula(nothing, :x), d)
2017
mmm = ModelMatrix(mf).m
2118
setcontrasts!(mf, x = DummyCoding())
2219
@test ModelMatrix(mf).m == mmm
20+
end
2321

22+
@testset "Effects" begin
2423
setcontrasts!(mf, x = EffectsCoding())
2524
@test ModelMatrix(mf).m == [1 -1 -1
2625
1 1 0
@@ -50,7 +49,6 @@ setcontrasts!(mf, x = EffectsCoding(levels = [:c, :b, :a]))
5049
1 1 0]
5150
@test coefnames(mf) == ["(Intercept)"; "x: b"; "x: a"]
5251

53-
5452
# change levels and base level of contrast
5553
setcontrasts!(mf, x = EffectsCoding(levels = [:c, :b, :a], base = :a))
5654
@test ModelMatrix(mf).m == [1 -1 -1
@@ -61,6 +59,7 @@ setcontrasts!(mf, x = EffectsCoding(levels = [:c, :b, :a], base = :a))
6159
1 0 1]
6260
@test coefnames(mf) == ["(Intercept)"; "x: c"; "x: b"]
6361

62+
@testset "Helmert" begin
6463
# Helmert coded contrasts
6564
setcontrasts!(mf, x = HelmertCoding())
6665
@test ModelMatrix(mf).m == [1 -1 -1
@@ -70,7 +69,9 @@ setcontrasts!(mf, x = HelmertCoding())
7069
1 -1 -1
7170
1 1 -1]
7271
@test coefnames(mf) == ["(Intercept)"; "x: b"; "x: c"]
72+
end
7373

74+
@testset "Incorrect input data" begin
7475
# Mismatching types of data and contrasts levels throws an error:
7576
@test_throws ArgumentError setcontrasts!(mf, x = EffectsCoding(levels = ["a", "b", "c"]))
7677

@@ -91,6 +92,7 @@ mf_missing = ModelFrame(Formula(nothing, :x), d, contrasts = Dict(:x => EffectsC
9192
@test_throws ArgumentError setcontrasts!(mf, x = EffectsCoding(levels = [:a, :b, :c, :d]))
9293
# Asking for base level that's not found in data
9394
@test_throws ArgumentError setcontrasts!(mf, x = EffectsCoding(base = :e))
95+
end
9496

9597
# Manually specified contrasts
9698
contrasts = [0 1
@@ -110,5 +112,6 @@ setcontrasts!(mf, x = ContrastsCoding(contrasts))
110112

111113
# contrasts types must be instaniated
112114
@test_throws ArgumentError setcontrasts!(mf, x = DummyCoding)
115+
end
113116

114117
end

test/data.jl

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
1-
module TestData
2-
importall Base # so that we get warnings for conflicts
3-
using Base.Test
4-
using DataFrames
5-
using Compat.String
6-
7-
#test_group("NullableArray creation")
8-
nvint = NullableArray(Nullable{Int}[1, 2, Nullable(), 4])
9-
nvint2 = NullableArray(5:8)
10-
nvint3 = NullableArray(5:8)
11-
nvflt = NullableArray(Nullable{Float64}[1.0, 2.0, Nullable(), 4.0])
12-
nvstr = NullableArray(Nullable{String}["one", "two", Nullable(), "four"])
13-
dvdict = NullableArray(Dict, 4) # for issue #199
14-
15-
#test_group("constructors")
16-
df1 = DataFrame(Any[nvint, nvstr], [:Ints, :Strs])
17-
df2 = DataFrame(Any[nvint, nvstr])
18-
df3 = DataFrame(Any[nvint])
19-
df4 = convert(DataFrame, [1:4 1:4])
20-
df5 = DataFrame(Any[NullableArray([1,2,3,4]), nvstr])
21-
df6 = DataFrame(Any[nvint, nvint, nvstr], [:A, :B, :C])
22-
df7 = DataFrame(x = nvint, y = nvstr)
23-
@test size(df7) == (4, 2)
24-
@test isequal(df7[:x], nvint)
25-
26-
#test_group("description functions")
27-
@test size(df6, 1) == 4
28-
@test size(df6, 2) == 3
29-
@test names(df6) == [:A, :B, :C]
30-
@test names(df2) == [:x1, :x2]
31-
@test names(df7) == [:x, :y]
32-
33-
#test_group("ref")
1+
@testset "Assorted DataFrame tests" begin # TODO redistribute/remove?
2+
3+
# NullableArray creation
4+
nvint = NullableArray(Nullable{Int}[1, 2, Nullable(), 4])
5+
nvint2 = NullableArray(5:8)
6+
nvint3 = NullableArray(5:8)
7+
nvflt = NullableArray(Nullable{Float64}[1.0, 2.0, Nullable(), 4.0])
8+
nvstr = NullableArray(Nullable{String}["one", "two", Nullable(), "four"])
9+
dvdict = NullableArray(Dict, 4) # for issue #199
10+
11+
# constructors
12+
df1 = DataFrame(Any[nvint, nvstr], [:Ints, :Strs])
13+
df2 = DataFrame(Any[nvint, nvstr])
14+
df3 = DataFrame(Any[nvint])
15+
df4 = convert(DataFrame, [1:4 1:4])
16+
df5 = DataFrame(Any[NullableArray([1,2,3,4]), nvstr])
17+
df6 = DataFrame(Any[nvint, nvint, nvstr], [:A, :B, :C])
18+
df7 = DataFrame(x = nvint, y = nvstr)
19+
@test size(df7) == (4, 2)
20+
@test isequal(df7[:x], nvint)
21+
22+
# description functions
23+
@test size(df6, 1) == 4
24+
@test size(df6, 2) == 3
25+
@test names(df6) == [:A, :B, :C]
26+
@test names(df2) == [:x1, :x2]
27+
@test names(df7) == [:x, :y]
28+
29+
@testset "ref" begin
3430
@test isequal(df6[2, 3], Nullable("two"))
3531
@test isnull(df6[3, 3])
3632
@test isequal(df6[2, :C], Nullable("two"))
@@ -41,8 +37,9 @@ module TestData
4137
@test size(df6[1:2, 1:2]) == (2, 2)
4238
@test size(head(df6,2)) == (2, 3)
4339
# lots more to do
40+
end
4441

45-
#test_group("assign")
42+
@testset "assign" begin
4643
df6[3] = NullableArray(["un", "deux", "troix", "quatre"])
4744
@test isequal(df6[1, 3], Nullable("un"))
4845
df6[:B] = [4, 3, 2, 1]
@@ -52,12 +49,13 @@ module TestData
5249
delete!(df6, :D)
5350
@test names(df6) == [:A, :B, :C]
5451
@test size(df6, 2) == 3
52+
end
5553

56-
#test_group("null handling")
54+
@testset "null handling" begin
5755
@test nrow(df5[complete_cases(df5), :]) == 3
56+
end
5857

59-
#test_context("SubDataFrames")
60-
58+
@testset "SubDataFrame" begin
6159
#test_group("constructors")
6260
# single index is rows
6361
sdf6a = sub(df6, 1)
@@ -69,11 +67,12 @@ module TestData
6967

7068
#test_group("ref")
7169
@test isequal(sdf6a[1,2], Nullable(4))
70+
end
7271

7372
#test_context("Within")
7473
#test_group("Associative")
7574

76-
#test_group("DataFrame")
75+
@testset "Grouping" begin
7776
srand(1)
7877
N = 20
7978
#Cast to Int64 as rand() behavior differs between Int32/64
@@ -83,7 +82,6 @@ module TestData
8382
d4 = NullableArray(randn(N))
8483
df7 = DataFrame(Any[d1, d2, d3], [:d1, :d2, :d3])
8584

86-
#test_group("groupby")
8785
gd = groupby(df7, :d1)
8886
@test isa(gd, GroupedDataFrame)
8987
@test length(gd) == 2
@@ -133,8 +131,9 @@ module TestData
133131
@test ggd[1][2, :d4] == "c"
134132
@test ggd[2][1, :d3] == "a"
135133
@test ggd[2][1, :d4] == "d"
134+
end
136135

137-
#test_group("reshape")
136+
@testset "reshape" begin
138137
d1 = DataFrame(a = repeat([1:3;], inner = [4]),
139138
b = repeat([1:4;], inner = [3]),
140139
c = randn(12),
@@ -175,9 +174,9 @@ module TestData
175174
@test isequal(d1us[:a], d1[:a])
176175
@test isequal(d1us2[:d], d1[:d])
177176
@test isequal(d1us2[:3], d1[:d])
177+
end
178178

179-
180-
179+
@testset "join" begin
181180
d2 = DataFrame(id1 = [:a, :a, :a, :b],
182181
id2 = [:A, :B, :B, :B],
183182
id3 = [:t, :f, :t, :f],
@@ -225,22 +224,23 @@ module TestData
225224
m4 = join(df1, df2, on = :a, kind = :outer)
226225
@test isequal(m4[:a], NullableArray([1, 2, 3, 4]))
227226

228-
# test with nulls (issue #185)
229-
df1 = DataFrame()
230-
df1[:A] = NullableArray(Nullable{Compat.ASCIIString}["a", "b", "a", Nullable()])
231-
df1[:B] = NullableArray([1, 2, 1, 3])
227+
@testset "test with nulls (issue #185)" begin
228+
df1 = DataFrame()
229+
df1[:A] = NullableArray(Nullable{Compat.ASCIIString}["a", "b", "a", Nullable()])
230+
df1[:B] = NullableArray([1, 2, 1, 3])
232231

233-
df2 = DataFrame()
234-
df2[:A] = NullableArray(Nullable{Compat.ASCIIString}["a", Nullable(), "c"])
235-
df2[:C] = NullableArray([1, 2, 4])
232+
df2 = DataFrame()
233+
df2[:A] = NullableArray(Nullable{Compat.ASCIIString}["a", Nullable(), "c"])
234+
df2[:C] = NullableArray([1, 2, 4])
236235

237-
m1 = join(df1, df2, on = :A)
238-
@test size(m1) == (3,3)
239-
@test isequal(m1[:A], NullableArray(Nullable{Compat.ASCIIString}["a","a",Nullable()]))
236+
m1 = join(df1, df2, on = :A)
237+
@test size(m1) == (3,3)
238+
@test isequal(m1[:A], NullableArray(Nullable{Compat.ASCIIString}["a","a",Nullable()]))
240239

241-
m2 = join(df1, df2, on = :A, kind = :outer)
242-
@test size(m2) == (5,3)
243-
@test isequal(m2[:A], NullableArray(Nullable{Compat.ASCIIString}["a","b","a",Nullable(),"c"]))
240+
m2 = join(df1, df2, on = :A, kind = :outer)
241+
@test size(m2) == (5,3)
242+
@test isequal(m2[:A], NullableArray(Nullable{Compat.ASCIIString}["a","b","a",Nullable(),"c"]))
243+
end
244244

245245
srand(1)
246246
df1 = DataFrame(
@@ -265,7 +265,9 @@ module TestData
265265
# NullableArray(Nullable{String}["x", "x", "y", "y",
266266
# "x", "x", "x", "x", "x", "y",
267267
# Nullable(), "y"])
268+
end
268269

270+
@testset "unique" begin
269271
srand(1)
270272
function spltdf(d)
271273
d[:x1] = map(x -> get(x)[1], d[:a])
@@ -313,3 +315,5 @@ module TestData
313315
unique!(df, [1, 3])
314316
@test isequal(df, df1)
315317
end
318+
319+
end

0 commit comments

Comments
 (0)