Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Spec files', multiparts' #to_h and transfers' #to_h and #to_json
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoFleming committed Mar 4, 2019
1 parent 73bfb10 commit 5242b54
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
8 changes: 1 addition & 7 deletions lib/we_transfer/remote_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ module RemoteFile
class FileMismatchError < StandardError; end
class NoIoError < StandardError; end

class Multipart < ::Ks.strict(:chunks, :chunk_size)
def to_h
%i[chunks chunk_size].each_with_object({}) do |prop, memo|
memo[prop] = send(prop)
end
end
end
class Multipart < ::Ks.strict(:chunks, :chunk_size); end

def self.upgrade(files_response:, transfer:)
files_response.each do |file_response|
Expand Down
69 changes: 69 additions & 0 deletions spec/we_transfer/transfer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,73 @@

it "url?"
end

describe "#to_h" do
let(:transfer) { described_class.new(message: 'test transfer') }
let(:file_stub_1) { [instance_double(WeTransfer::WeTransferFile, name: 'foo', size: 8)] }

it "has keys and values for id, state, url, message and files" do
allow(file_stub_1)
.to receive(:to_h)
.and_return('fake-file-to_h')

allow(transfer)
.to receive(:files)
.and_return([file_stub_1])

allow(transfer)
.to receive(:id)
.and_return('fake-id')

allow(transfer)
.to receive(:state)
.and_return('fake-state')

allow(transfer)
.to receive(:url)
.and_return('fake-url')

expected = {
id: "fake-id",
state: "fake-state",
url: "fake-url",
message: "test transfer",
files: ["fake-file-to_h"]
}

expect(transfer.to_h)
.to match(expected)
end

it "calls :to_h on all files" do
file_stub_2 = instance_double(WeTransfer::WeTransferFile, name: 'bar', size: 8)

allow(transfer)
.to receive(:files)
.and_return([file_stub_1, file_stub_2])

expect(file_stub_1)
.to receive(:to_h)

expect(file_stub_2)
.to receive(:to_h)

transfer.to_h
end
end

describe "#to_json" do
it "converts the results of #to_h" do
transfer = described_class.new(message: 'test transfer')

transfer_hash = { "foo" => "bar" }

allow(transfer)
.to receive(:to_h)
.and_return(transfer_hash)

expect(JSON.parse(transfer.to_json))
.to eq(transfer_hash)
end
end
end
41 changes: 41 additions & 0 deletions spec/we_transfer/we_transfer_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,45 @@
.to eq %|{"name":"test file","size":8}|
end
end

describe "#to_h" do
let(:file) { described_class.new(name: 'foo', size: 8) }
let(:multipart_stub) { instance_double(WeTransfer::RemoteFile::Multipart) }

it "has keys and values for id, name, size and multipart" do
allow(file)
.to receive(:multipart)
.and_return(multipart_stub)

allow(file)
.to receive(:id)
.and_return('fake-id')

allow(multipart_stub)
.to receive(:to_h)
.and_return('fake-multipart')

expected = {
id: 'fake-id',
multipart: 'fake-multipart',
size: 8,
name: 'foo'

}

expect(file.to_h)
.to match(expected)
end

it "calls :to_h on multipart" do
allow(file)
.to receive(:multipart)
.and_return(multipart_stub)

expect(multipart_stub)
.to receive(:to_h)

file.to_h
end
end
end

0 comments on commit 5242b54

Please sign in to comment.