Skip to content

Commit

Permalink
additional tests to improve coverage (#2560)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored Nov 27, 2020
1 parent 86a5ee6 commit 3689047
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 14 deletions.
11 changes: 10 additions & 1 deletion test/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ end
@test_throws ArgumentError df .+ 1 .+ df2
end

@testset "broadcasting expansion" begin
@testset "broadcasting data frames" begin
df1 = DataFrame(x=1, y=2)
df2 = DataFrame(x=[1, 11], y=[2, 12])
@test df1 .+ df2 == DataFrame(x=[2, 12], y=[4, 14])
Expand All @@ -91,6 +91,15 @@ end
dfv = view(df, 1:1, 1:2)
df .-= dfv
@test df == DataFrame(x=[0, 10], y=[0, 10])

@test DataFrame() .+ DataFrame() == DataFrame()
@test_throws ArgumentError DataFrame(a=1, b=1) .+ DataFrame(b=1, a=1)

df = DataFrame(a=1, b=2)
@test_throws ArgumentError df .= DataFrame(b=1, a=2)
@test_throws ArgumentError df .= DataFrame(a=1, c=2)
@test_throws ArgumentError df[!, [:a, :b]] .= DataFrame(b=1, a=2)
@test_throws ArgumentError df[!, [:a, :b]] .= DataFrame(a=1, c=2)
end

@testset "broadcasting of AbstractDataFrame objects corner cases" begin
Expand Down
31 changes: 31 additions & 0 deletions test/cat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ end
@testset "vcat thrown exceptions" begin
df1 = DataFrame(A = 1:3, B = 1:3)
df2 = DataFrame(A = 1:3)

# wrong cols argument
@test_throws ArgumentError vcat(df1, df1, cols=:unions)
# right missing 1 column
err = @test_throws ArgumentError vcat(df1, df2)
@test err.value.msg == "column(s) B are missing from argument(s) 2"
Expand Down Expand Up @@ -547,4 +550,32 @@ end
reduce(vcat, [x, y]) == reduce(vcat, (x, y))
end

@testset "reduce vcat corner cases" begin
df = DataFrame(a=1, b=2)
df1 = @view df[:, :]
df2 = @view df[:, 1:2]
@test typeof(df1) != typeof(df2)

@test_throws ArgumentError reduce(vcat, ())
@test reduce(vcat, DataFrame[]) == DataFrame()
@test reduce(vcat, SubDataFrame[]) == DataFrame()
@test reduce(vcat, AbstractDataFrame[]) == DataFrame()

@test reduce(vcat, [df]) == df
@test reduce(vcat, [df1]) == df
@test reduce(vcat, [df2]) == df
@test reduce(vcat, [df, df]) == DataFrame(a=[1, 1], b=[2, 2])
@test reduce(vcat, [df, df1]) == DataFrame(a=[1, 1], b=[2, 2])
@test reduce(vcat, [df, df2]) == DataFrame(a=[1, 1], b=[2, 2])
@test reduce(vcat, [df1, df2]) == DataFrame(a=[1, 1], b=[2, 2])

@test reduce(vcat, (df,)) == df
@test reduce(vcat, (df1,)) == df
@test reduce(vcat, (df2,)) == df
@test reduce(vcat, (df, df)) == DataFrame(a=[1, 1], b=[2, 2])
@test reduce(vcat, (df, df1)) == DataFrame(a=[1, 1], b=[2, 2])
@test reduce(vcat, (df, df2)) == DataFrame(a=[1, 1], b=[2, 2])
@test reduce(vcat, (df1, df2)) == DataFrame(a=[1, 1], b=[2, 2])
end

end # module
7 changes: 4 additions & 3 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const ≅ = isequal
@test df2.x1 === vecvec[1]
@test df2.x2 === vecvec[2]

@test_throws ArgumentError DataFrame([[1, 2]], :autos)
@test_throws ArgumentError DataFrame([1 2], :autos)

for copycolsarg in (true, false)
@test df == DataFrame(vecvec, :auto, copycols=copycolsarg)
@test df == DataFrame(collect(Any, vecvec), :auto, copycols=copycolsarg)
Expand Down Expand Up @@ -156,13 +159,11 @@ end
DataFrame("x1" => zeros(3), "x2" => ones(3)) ==
DataFrame("x1" => zeros(3), "x2" => ones(3))

df = DataFrame([:x1 => zeros(3), :x2 => ones(3)])
@inferred DataFrame(:x1 => zeros(3), :x2 => ones(3))
df = DataFrame([:x1 => zeros(3), :x2 => ones(3)])
@test size(df, 1) == 3
@test size(df, 2) == 2
@test isequal(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.0, 1.0, 1.0]))
@test isapprox(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.0, 1.0, 1.0]))
@test isapprox(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.000000010000, 1.0, 1.0]))

df = DataFrame(:type => [], :begin => [])
@test propertynames(df) == [:type, :begin]
Expand Down
12 changes: 12 additions & 0 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ end
@test fun(view(df, 1:2, 1:2), view=true) isa SubDataFrame
@test fun(view(df, 1:2, 1:2), view=true) == fun(view(df, 1:2, 1:2))
end
@test_throws ArgumentError dropmissing(df, view=true, disallowmissing=true)
end

@testset "merge" begin
Expand Down Expand Up @@ -535,4 +536,15 @@ end
@test eltype(df.b) <: String
end

@testset "isapprox" begin
df = DataFrame([:x1 => zeros(3), :x2 => ones(3)])
@test isapprox(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.0, 1.0, 1.0]))
@test isapprox(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.000000010000, 1.0, 1.0]))
@test_throws DimensionMismatch isapprox(DataFrame(a=1), DataFrame(a=[1,2]))
@test_throws ArgumentError isapprox(DataFrame(a=1), DataFrame(b=1))
@test !isapprox(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.1, 1.0, 1.0]))
@test !isapprox(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.1, 1.0, 1.0]), atol=0.09)
@test isapprox(df, DataFrame(x1 = [0.0, 0.0, 0.0], x2 = [1.1, 1.0, 1.0]), atol=0.11)
end

end # module
9 changes: 9 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ end
df = DataFrame(c=1:3)
insertcols!(df, 1, :a => Ref(1), :b => fill(1))
@test df == DataFrame(a=[1, 1, 1], b=[1, 1, 1], c=1:3)

df = DataFrame(a=1)
@test insertcols!(df, "a" => 2, makeunique=true) == DataFrame(a=1, a_1=2)
end

@testset "unsupported insertcols!" begin
Expand Down Expand Up @@ -555,6 +558,7 @@ end

@testset "delete!" begin
df = DataFrame(a=[1, 2], b=[3.0, 4.0])
@test_throws BoundsError delete!(df, [true, true, true])
@test delete!(df, 1) === df
@test df == DataFrame(a=[2], b=[4.0])

Expand Down Expand Up @@ -669,6 +673,8 @@ end
DataFrame(variable=:a, min=1, min2=1, max2=2, max=2)

@test_throws ArgumentError describe(df, :mean, :all)
@test_throws ArgumentError describe(df, :min, :min)
@test_throws ArgumentError describe(df, :minimum)
end

@testset "append!" begin
Expand Down Expand Up @@ -924,6 +930,9 @@ end
DataFrame(a=[1, missing, missing, missing], b=[missing, 1, 1, 1],
x=[1:3; missing])
end

@test_throws ArgumentError push!(DataFrame(), (a=1, b=2), cols=:unions)
@test_throws ArgumentError push!(DataFrame(), Dict('a'=>1, 'b'=>2), cols=:union)
end

@testset "rename" begin
Expand Down
30 changes: 20 additions & 10 deletions test/dataframerow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ ref_df = DataFrame(a=Union{Int, Missing}[1, 2, 3, 1, 2, 2],
@test DataFrameRow(sdf, 2, :) == sdf[2, :] == view(sdf, 2, :)
@test DataFrameRow(df, 3, 2:3) === df[3, 2:3]
@test view(df, 3, 2:3) === df[3, 2:3]
@test_throws ArgumentError DataFrameRow(df, 1, :a)
@test_throws ArgumentError DataFrameRow(df, 1, 1)
@test_throws BoundsError DataFrameRow(df, 1, 1:10)
@test_throws BoundsError DataFrameRow(df, 1, [1:10;])
@test_throws BoundsError DataFrameRow(df, 100, 1:2)
@test_throws BoundsError DataFrameRow(df, 100, [1:2;])
@test_throws BoundsError DataFrameRow(df, 100, :)
@test_throws BoundsError DataFrameRow(df, 100)
@test_throws ArgumentError DataFrameRow(df, true, 1:2)
@test_throws ArgumentError DataFrameRow(df, true)
for x in (df, sdf)
@test_throws ArgumentError DataFrameRow(x, 1, :a)
@test_throws ArgumentError DataFrameRow(x, 1, 1)
@test_throws BoundsError DataFrameRow(x, 1, 1:10)
@test_throws BoundsError DataFrameRow(x, 1, [1:10;])
@test_throws BoundsError DataFrameRow(x, 100, 1:2)
@test_throws BoundsError DataFrameRow(x, 100, [1:2;])
@test_throws BoundsError DataFrameRow(x, 100, :)
@test_throws BoundsError DataFrameRow(x, 100)
@test_throws ArgumentError DataFrameRow(x, true, 1:2)
@test_throws ArgumentError DataFrameRow(x, true)
end
@test_throws ArgumentError DataFrameRow(sdf, true, 1:2)
end

Expand Down Expand Up @@ -121,6 +123,8 @@ end
df = deepcopy(ref_df)
df2 = DataFrame(a = [1, 2, 3])

@test !isequal(DataFrameRow(df, 1, :), DataFrameRow(df2, 1, :))
@test isequal(DataFrame(a=missing)[1, :], DataFrame(a=missing)[1, :])
@test DataFrameRow(df, 1, :) != DataFrameRow(df2, 1, :)
@test DataFrameRow(df, 1, [:a]) == DataFrameRow(df2, 1, :)
@test DataFrameRow(df, 1, [:a]) == DataFrameRow(df2, big(1), :)
Expand Down Expand Up @@ -396,6 +400,7 @@ end

@test push!(df, df[1, :]) == DataFrame(x=[1, 1], y=[2, 2])
@test push!(df, df[1, [2, 1]], cols=:setequal) == DataFrame(x=[1, 1, 1], y=[2, 2, 2])
@test_throws ArgumentError push!(df, df[1, [2, 1]], cols=:setequals)

push!(df, df[1, [2, 1]], cols=:intersect)
@test df == DataFrame(x=[1, 1, 1, 1], y=[2, 2, 2, 2])
Expand Down Expand Up @@ -577,4 +582,9 @@ end
end
end

@testset "broadcasting" begin
r = DataFrame(a=1)[1, :]
@test_throws ArgumentError r .+ 1
end

end # module
48 changes: 48 additions & 0 deletions test/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,8 @@ end

@testset "iteration protocol" begin
gd = groupby_checked(DataFrame(A = [:A, :A, :B, :B], B = 1:4), :A)
@test IndexStyle(gd) == IndexLinear()
@test IndexStyle(typeof(gd)) == IndexLinear()
count = 0
for v in gd
count += 1
Expand Down Expand Up @@ -1471,6 +1473,10 @@ end

@test gd[1] == DataFrame(a=[:A, :A], b=[1, 1], c=[1, 4])

@test gd[Any[Dict("a" => :A, "b" => 1)]] == gd[[Dict("a" => :A, "b" => 1)]] ==
gd[[(a=:A, b=1)]]
@test haskey(gd, Dict("a" => :A, "b" => 1))

@test map(NamedTuple, keys(gd))
[(a=:A, b=1), (a=:B, b=1), (a=missing, b=1), (a=:A, b=2), (a=:B, b=2), (a=missing, b=2)]

Expand Down Expand Up @@ -3224,4 +3230,46 @@ end
end
end

@testset "corner cases of wrong transformation" begin
df = DataFrame(id=[1, 1, 2, 3, 3, 1], x=1:6)
gdf = groupby_checked(df, :id)
@test_throws ArgumentError combine(gdf, :x, :x)
@test_throws ErrorException combine(gdf, :x => (x -> Dict("a" => [1])) => AsTable)
@test_throws ErrorException combine(gdf, :x => (x -> Dict(:a => 1)) => AsTable)
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 1 ? Ref(1) : [1])
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 2 ? Ref(1) : [1])
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 1 ? (a=1, b=2) : (a=1,))
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 1 ? (a=1, b=2) : (a=1, c=2))
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 1 ? (a=[1], b=[2]) : (a=[1],))
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 1 ? (a=[1], b=[2]) : (a=[1], c=[2]))
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 1 ? (a=[1], b=[2]) : (a=1,))
@test_throws ArgumentError combine(gdf, sdf -> sdf.id[1] == 2 ? (a=[1], b=[2]) : (a=1,))
@test_throws ArgumentError combine(gdf, :id => (x -> x[1] == 1 ? [[1, 2]] : [[1]]) => AsTable)
@test_throws ArgumentError combine(gdf, :id => (x -> x[1] == 1 ? [[1]] : [[1]]) => [:a, :b])
@test_throws ArgumentError combine(gdf, :x, :id => (x -> fill([1], length(x))) => [:x])
@test select(gdf, [:x], :id => (x -> fill([1], length(x))) => [:x]) ==
DataFrame(id=df.id, x=1)
@test_throws ArgumentError select(gdf, x -> [1, 2])

df = DataFrame(id=[1, 1, 2, 3, 3, 1], x=categorical(1:6, ordered=true))
gdf = groupby_checked(df, :id)
@test combine(gdf, :x => minimum => :x) == df[[1, 3, 4], :]
end

@testset "select and transform! tests with function as first argument" begin
df = DataFrame(id=[1, 1, 2, 3, 3, 1], x=1:6)
gdf = groupby_checked(df, :id)
df2 = select(sdf -> sdf.id .* sdf.x, gdf)
@test df2 == DataFrame(id=df.id, x1=df.id .* df.x)
select!(sdf -> sdf.id .* sdf.x, gdf)
@test df == df2

df = DataFrame(id=[1, 1, 2, 3, 3, 1], x=1:6)
gdf = groupby_checked(df, :id)
df2 = transform(sdf -> sdf.id .* sdf.x, gdf)
@test df2 == DataFrame(id=df.id, x=df.x, x1=df.id .* df.x)
transform!(sdf -> sdf.id .* sdf.x, gdf)
@test df == df2
end

end # module
1 change: 1 addition & 0 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using Test, DataFrames
@test parent(df[1, Not([])]) === df
@test_throws ArgumentError df[true, 1]
@test_throws ArgumentError df[true, 1:2]
@test_throws BoundsError df[5, "a"]

@test df[1:2, 1] == [1, 2]
@test df[1:2, 1:2] == DataFrame(a=1:2, b=4:5)
Expand Down
6 changes: 6 additions & 0 deletions test/join.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ anti = left[Bool[ismissing(x) for x in left.Job], [:ID, :Name]]
innerjoin(name, job, on = [:ID])

@test_throws ArgumentError innerjoin(name, job)
@test_throws ArgumentError innerjoin(name, job, on = :ID, matchmissing=:errors)

@test innerjoin(name, job, on = :ID) == inner
@test outerjoin(name, job, on = :ID) outer
Expand Down Expand Up @@ -757,6 +758,11 @@ end
df1 = DataFrame(id1=[1, 2, 3], id2=[1, 2, 3], x=1:3)
df2 = DataFrame(id1=[1, 2, 4], ID2=[1, 2, 4], x=1:3)

@test_throws ArgumentError innerjoin(df1, df2, on=:id1, renamecols=1=>1, makeunique=true)
@test_throws ArgumentError leftjoin(df1, df2, on=:id1, renamecols=1=>1, makeunique=true)
@test_throws ArgumentError rightjoin(df1, df2, on=:id1, renamecols=1=>1, makeunique=true)
@test_throws ArgumentError outerjoin(df1, df2, on=:id1, renamecols=1=>1, makeunique=true)

@test_throws ArgumentError innerjoin(df1, df2, on=:id1)
@test innerjoin(df1, df2, on=:id1, makeunique=true) ==
DataFrame(id1=[1, 2], id2=[1, 2], x=[1, 2], ID2=[1, 2], x_1=[1, 2])
Expand Down
7 changes: 7 additions & 0 deletions test/reshape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,11 @@ end
@test permutedims(DataFrame(a=String[], b=Float64[]), 1) == DataFrame(a=["b"])
end

@testset "stack view=true additional tests" begin
df = DataFrame(a=1:3, b=11:13, c=101:103)
sdf = stack(df, [:b, :c], view=true)
@test reverse(sdf.a) == reverse(copy(sdf.a))
@test IndexStyle(DataFrames.StackedVector) == IndexLinear()
end

end # module
17 changes: 17 additions & 0 deletions test/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1581,4 +1581,21 @@ end
end
end

@testset "selection special cases" begin
df = DataFrame(a=1)
@test_throws ArgumentError select(df, '1' => sum => "b")
@test_throws ArgumentError select(df, '1' => sum)
@test select(df, ["a", "a"] => (+) => "b") == DataFrame(b=2)
@test_throws ArgumentError combine(df, :a => (x -> (a=1, b=[2])) => AsTable)
@test_throws ArgumentError combine(:, df)
@test_throws ArgumentError select(:, df)
@test_throws ArgumentError select!(:, df)
@test_throws ArgumentError transform(:, df)
@test_throws ArgumentError transform!(:, df)
@test combine(df, :a => (x -> 1) => :x1, :a => (x -> [1, 2]) => :x2) ==
DataFrame(x1=1, x2=[1, 2])
@test isequal_coltyped(combine(df, :a => (x -> 1) => :x1, :a => (x -> []) => :x2),
DataFrame(x1=Int[], x2=[]))
end

end # module
14 changes: 14 additions & 0 deletions test/subdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,18 @@ end
@test view(s2, 1:1:2, :) == view(df, [1, 3], :)
end

@testset "SubDataFrame corner cases" begin
df = DataFrame(a=[1, 2], b=[3, 4])
@test_throws ArgumentError SubDataFrame(df, Integer[true], 1)
@test_throws ArgumentError SubDataFrame(df, [true], 1)

sdf = @view df[:, :]
@test_throws ArgumentError SubDataFrame(sdf, true, 1)
@test_throws ArgumentError SubDataFrame(sdf, true, :)
@test_throws ArgumentError SubDataFrame(sdf, Integer[true], 1)

# this is an error in Julia Base
SubDataFrame(sdf, Integer[true, true, true], :) == SubDataFrame(sdf, [1, 1, 1], :)
end

end # module

0 comments on commit 3689047

Please sign in to comment.