Skip to content

Commit

Permalink
Add test cases for all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Mar 18, 2019
1 parent 2e655ec commit dc80593
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 214 deletions.
57 changes: 45 additions & 12 deletions src/DropboxCLI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ function cmd_get(args)
# Compare content hash before downloading
need_download = true
if metadata.size == 0
open(destination, "w") do io
open(filename, "w") do io
truncate(io, 0)
end
need_download = false
elseif isfile(destination)
size = filesize(destination)
elseif isfile(filename)
size = filesize(filename)
if size == metadata.size
# Don't download if content hash matches
content = read(destination)
content = read(filename)
content_hash = calc_content_hash(content)
if content_hash == metadata.content_hash
@show "content hash matches; skipping download"
Expand All @@ -245,11 +245,11 @@ function cmd_get(args)
# TODO: Download only missing fraction
else
# Truncate if necessary
content = read(destination, metadata.size)
content = read(filename, metadata.size)
content_hash = calc_content_hash(content)
if content_hash == metadata.content_hash
@show "content hash matches; truncating local file and skipping download"
open(destination, "w") do io
open(filename, "w") do io
truncate(io, metadata.size)
end
need_download = false
Expand All @@ -261,7 +261,7 @@ function cmd_get(args)
if need_download
# TODO: download all files simultaneously
metadata, content = files_download(auth, source)
open(destination, "w") do io
open(filename, "w") do io
write(io, content)
end
end
Expand Down Expand Up @@ -445,6 +445,14 @@ function cmd_put(args)
isdir_destination = metadata isa FolderMetadata
end

if !isdir_destination && length(sources) != 1
# Multiple sources: Destination is not a directory
println("Destination directory \"$destination\" does not exist")
exit(1)
end

uploads = Tuple{String, String}[]

for source in sources

# Distinguish between files and directories
Expand Down Expand Up @@ -509,14 +517,39 @@ function cmd_put(args)

# TODO: touch Dropbox file if upload is skipped?
if need_upload
if content === nothing
content = read(source)
end
# TODO: upload all files simultaneously
metadata = files_upload(auth, filename, content)
# if content === nothing
# content = read(source)
# end
# metadata = files_upload(auth, filename, content)

# Upload all files simultaneously
# TODO: don't read files twice; instead, check content
# hash dynamically when the file is about to be uploaded.
# probably most elegant to use Julia's multi-tasking (not
# multi-threading) for this, i.e. to use a Channel instead
# of iterators.
push!(uploads, (filename, source))
end

end

# Create upload iterator for a single file
function make_upload_iter(upload::Tuple{String, String})::
Tuple{String, ContentIterator}

dst, src = upload
dst, ContentIterator([read(src)])
end

# Create upload iterator for several files
function make_uploads_iter(uploads::Vector{Tuple{String, String}})::
StatefulIterator{Tuple{String, ContentIterator}}

StatefulIterator{Tuple{String, ContentIterator}}(
make_upload_iter(upload) for upload in uploads)
end

metadatas = files_upload(auth, make_uploads_iter(uploads))
end


Expand Down
2 changes: 1 addition & 1 deletion src/DropboxSDK.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function post_rpc(auth::Authorization,
@goto retry
end

println("Error $(ex.status): $(res["error_summary"])")
# println("Error $(ex.status): $(res["error_summary"])")
return Error(res)
end
end
Expand Down
203 changes: 2 additions & 201 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,213 +2,14 @@ using Dates
using Test
using UUIDs

using DropboxSDK



const timestamp = Dates.format(now(UTC), dateformat"yyyy-mm-ddTHH:MM:SS.sss")
const uuid = UUIDs.uuid4()
const folder = "test-$timestamp-$uuid"
println("Using folder \"$folder\" for testing")

filename(entry) =
entry.path_display === nothing ? entry.name : entry.path_display



@testset "Get authorization" begin
global auth = get_authorization()
@test auth isa Authorization
end

@testset "Get current account" begin
account = users_get_current_account(auth)
first = account.name.given_name
last = account.name.surname
display = account.name.display_name
# println(" account: name: $first $last ($display)")
@test first == "Erik"
@test last == "Schnetter"
@test display == "Erik Schnetter (PI)"
end

@testset "Get space usage" begin
usage = users_get_space_usage(auth)
used = usage.used
# println(" usage: $(round(Int, used / 1.0e9)) GByte")
@test used isa Integer
@test used >= 0
end

@testset "List folder" begin
entries = files_list_folder(auth, "", recursive=true)
# for (i,entry) in enumerate(entries)
# println(" $i: $(filename(entry))")
# end
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == 0
end

@testset "Create folder" begin
files_create_folder(auth, "/$folder")
entries = files_list_folder(auth, "", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == 1
@test count(entry -> entry.path_display == "/$folder", entries) == 1
end

@testset "Upload file" begin
metadata =
files_upload(auth, "/$folder/file", Vector{UInt8}("Hello, World!\n"))
@test metadata isa FileMetadata
@test metadata.size == length("Hello, World!\n")
entries = files_list_folder(auth, "/$folder", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == 2
@test count(entry -> entry.path_display == "/$folder", entries) == 1
@test count(entry -> entry.path_display == "/$folder/file", entries) == 1
end

@testset "Download file" begin
metadata, content = files_download(auth, "/$folder/file")
@test metadata.path_display == "/$folder/file"
@test String(content) == "Hello, World!\n"
end

@testset "Get file metadata" begin
metadata = files_get_metadata(auth, "/$folder/file")
@test metadata.path_display == "/$folder/file"
@test (metadata.content_hash ==
calc_content_hash(Vector{UInt8}("Hello, World!\n")))
end

@testset "Upload empty file" begin
metadata = files_upload(auth, "/$folder/file0", Vector{UInt8}(""))
@test metadata isa FileMetadata
@test metadata.size == 0
entries = files_list_folder(auth, "/$folder", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == 3
@test count(entry -> entry.path_display == "/$folder", entries) == 1
@test count(entry -> entry.path_display == "/$folder/file0", entries) == 1
end

@testset "Download empty file" begin
metadata, content = files_download(auth, "/$folder/file0")
@test metadata.path_display == "/$folder/file0"
@test String(content) == ""
end

@testset "Upload file in chunks" begin
content = map(Vector{UInt8}, ["Hello, ","World!\n"])
metadata = files_upload(auth, "/$folder/file1", ContentIterator(content))
@test metadata isa FileMetadata
@test metadata.size == length("Hello, World!\n")
entries = files_list_folder(auth, "/$folder", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == 4
@test count(entry -> entry.path_display == "/$folder", entries) == 1
@test count(entry -> entry.path_display == "/$folder/file1", entries) == 1
end

@testset "Download file" begin
metadata, content = files_download(auth, "/$folder/file1")
@test metadata.path_display == "/$folder/file1"
@test String(content) == "Hello, World!\n"
end

@testset "Upload empty file in chunks" begin
content = map(Vector{UInt8}, String[])
metadata = files_upload(auth, "/$folder/file2", ContentIterator(content))
@test metadata isa FileMetadata
@test metadata.size == 0
entries = files_list_folder(auth, "/$folder", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == 5
@test count(entry -> entry.path_display == "/$folder", entries) == 1
@test count(entry -> entry.path_display == "/$folder/file2", entries) == 1
end

@testset "Download empty file" begin
metadata, content = files_download(auth, "/$folder/file2")
@test metadata.path_display == "/$folder/file2"
@test String(content) == ""
end

const numfiles = 4
@testset "Upload several files" begin
chunk = Vector{UInt8}("Hello, World!\n")
contents = StatefulIterator{Tuple{String, ContentIterator}}(
("/$folder/files$i", ContentIterator(Iterators.repeated(chunk, i)))
for i in 0:numfiles-1)
metadatas = files_upload(auth, contents)
@test length(metadatas) == numfiles
@test all(metadata isa FileMetadata for metadata in metadatas)
@test all(metadata.size == (i-1) * length("Hello, World!\n")
for (i,metadata) in enumerate(metadatas))
entries = files_list_folder(auth, "/$folder", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == numfiles + 5
@test count(entry -> entry.path_display == "/$folder", entries) == 1
for i in 0:numfiles-1
@test count(entry -> entry.path_display == "/$folder/files$i",
entries) == 1
end
end

@testset "Download files" begin
for i in 0:numfiles-1
metadata, content = files_download(auth, "/$folder/files$i")
@test metadata.path_display == "/$folder/files$i"
@test String(content) == repeat("Hello, World!\n", i)
end
end

@testset "Upload zero files" begin
contents = StatefulIterator{Tuple{String, ContentIterator}}(
Tuple{String, ContentIterator}[]
)
metadatas = files_upload(auth, contents)
@test isempty(metadatas)
entries = files_list_folder(auth, "/$folder", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == numfiles + 5
end

@testset "Delete folder" begin
files_delete(auth, "/$folder")
entries = files_list_folder(auth, "", recursive=true)
@test count(entry -> startswith(entry.path_display, "/$folder"),
entries) == 0
end


function runcmd(args::Cmd)::Vector{String}
julia = Base.julia_cmd()
lines = String[]
open(`$julia ../bin/dbftp.jl $args`) do io
skipcount = 0
for line in eachline(io)
if skipcount > 0
skipcount -= 1
continue
elseif startswith(line, "Julia Dropbox client")
skipcount = 1
continue
else
push!(lines, line)
end
end
end
lines
end



@testset "Commands" begin
lines = runcmd(`version`)
@test length(lines) == 1
m = match(r"^Version\s+(.*)", lines[1])
@test m !== nothing
version = VersionNumber(m.captures[1])
end
#TODO include("testsdk.jl")
include("testcli.jl")

0 comments on commit dc80593

Please sign in to comment.