Skip to content

Commit

Permalink
Fix bugs, increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cjprybol committed Sep 27, 2017
1 parent 5f8f065 commit 36faf96
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Expand Up @@ -11,11 +11,11 @@ notifications:
git:
depth: 99999999

## uncomment the following lines to allow failures on nightly julia
## (tests will run but not make your overall status red)
#matrix:
# allow_failures:
# - julia: nightly
# uncomment the following lines to allow failures on nightly julia
# (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia: nightly

## uncomment and modify the following lines to manually install system packages
#addons:
Expand Down
2 changes: 1 addition & 1 deletion src/helperfunctions.jl
Expand Up @@ -26,7 +26,7 @@ function getintdict{T}(arg::Dict{String, T}, numcols::Int, colnames::Vector{Stri
if all(k -> in(k, colnames), keys(arg))
return Dict(findfirst(colnames, k) => v for (k,v) in arg)
else
k = findfirst(filter(k -> in(k, colnames), keys(arg)))
k = first(filter(k -> !in(k, colnames), collect(keys(arg))))
throw(ArgumentError("""
user-provided column name $k does not match any parsed or user-provided column names.
"""))
Expand Down
2 changes: 1 addition & 1 deletion src/parsesource.jl
Expand Up @@ -71,7 +71,7 @@ function parsesource(source, delim, quotes, escape, comment, encodings, header,
isquoted .|= quoted
end

if currentline == 0
if currentline == 0 || eof(source) && (size(rawstrings, 2) == 0)
return Any[], colnames
elseif linesparsedfortypedetection < typedetectrows
rawstrings = rawstrings[1:linesparsedfortypedetection, :]
Expand Down
60 changes: 59 additions & 1 deletion test/runtests.jl
Expand Up @@ -31,6 +31,18 @@ end
3.0 3.0 3.0]
end

@testset "DataFrame" begin
s =
"""
header
data
"""
@test DataFrame(uCSV.read(IOBuffer(s), header=1)) == DataFrame([["data"]], [:header])
@test DataFrame(uCSV.read(IOBuffer(s), header=1, skiprows=1:1)) == DataFrame([[]], [:header])
@test DataFrame(uCSV.read(IOBuffer(s), skiprows=1:1)) == DataFrame([["data"]])
@test DataFrame(uCSV.read(IOBuffer(s), skiprows=1:2)) == DataFrame()
end

@testset "Mixed Type Matrix" begin
s =
"""
Expand Down Expand Up @@ -89,6 +101,41 @@ end
@test uCSV.read(IOBuffer(s), escape='\\', trimwhitespace=false)[1][1][1] == " s s "
@test uCSV.read(IOBuffer(s), trimwhitespace=true)[1][1][1] == "s s\\"
@test uCSV.read(IOBuffer(s))[1][1][1] == s

s =
"""
" s s\\ " ,"other text"
"""
@test uCSV.read(IOBuffer(s), quotes='"', escape='\\', trimwhitespace=true)[1][1][1] == " s s "
@test uCSV.read(IOBuffer(s), quotes='"', escape='\\', trimwhitespace=false)[1][1][1] == " s s "
end

@testset "errors" begin
s =
"""
1,2,3
"""
e = @test_throws ArgumentError uCSV.read(IOBuffer(s), types=Dict("col2" => Float64))
@test e.value.msg == "One of the following user-supplied arguments:\n 1. types\n 2. isnullable\n 3. iscategorical\n 4. colparsers\nwas provided as a Dict with String keys that cannot be mapped to column indices because column names have either not been provided or have not been parsed.\n"

s =
"""
c1,c2,c3
1,2,3
"""
e = @test_throws ArgumentError uCSV.read(IOBuffer(s), header=1, types=Dict("col2" => Float64))
@test e.value.msg == "user-provided column name col2 does not match any parsed or user-provided column names.\n"

s =
"""
1,2,3
,,
"""
e = @test_throws ErrorException uCSV.read(IOBuffer(s), encodings=Dict{String,Any}("" => null))
@test e.value.msg == "Error parsing field \"\" in row 2, column 1.\nUnable to push value null to column of type Int64\nPossible fixes may include:\n 1. set `typedetectrows` to a value >= 2\n 2. manually specify the element-type of column 1 via the `types` argument\n 3. manually specify a parser for column 1 via the `parsers` argument\n 4. if the value is null, setting the `isnullable` argument\n"

e = @test_throws ArgumentError uCSV.read(IOBuffer(s), header=["col1"])
@test e.value.msg == "user-provided header String[\"col1\"] has 1 columns, but 3 were detected the in dataset.\n"
end

@testset "Ford Fiesta (Ford examples from Wikipedia page)" begin
Expand Down Expand Up @@ -600,7 +647,18 @@ end
@test header == ["a", "b", "c"]
end

@testset "Malformed Row Errors" begin
@testset "Malformed Rows" begin
s = "col1,col2,\"col3\n\""
data, header = uCSV.read(IOBuffer(s), quotes='"')
@test data == Any[["col1"],
["col2"],
["col3\n"]]
@test header == Vector{String}()

data, header = uCSV.read(IOBuffer(s), quotes='"', header=1)
@test data == Any[]
@test header == ["col1", "col2", "col3\n"]

s =
"""
A;B;C
Expand Down

0 comments on commit 36faf96

Please sign in to comment.