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

Commit 5242b54

Browse files
committed
Spec files', multiparts' #to_h and transfers' #to_h and #to_json
1 parent 73bfb10 commit 5242b54

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

lib/we_transfer/remote_file.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ module RemoteFile
33
class FileMismatchError < StandardError; end
44
class NoIoError < StandardError; end
55

6-
class Multipart < ::Ks.strict(:chunks, :chunk_size)
7-
def to_h
8-
%i[chunks chunk_size].each_with_object({}) do |prop, memo|
9-
memo[prop] = send(prop)
10-
end
11-
end
12-
end
6+
class Multipart < ::Ks.strict(:chunks, :chunk_size); end
137

148
def self.upgrade(files_response:, transfer:)
159
files_response.each do |file_response|

spec/we_transfer/transfer_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,73 @@
492492

493493
it "url?"
494494
end
495+
496+
describe "#to_h" do
497+
let(:transfer) { described_class.new(message: 'test transfer') }
498+
let(:file_stub_1) { [instance_double(WeTransfer::WeTransferFile, name: 'foo', size: 8)] }
499+
500+
it "has keys and values for id, state, url, message and files" do
501+
allow(file_stub_1)
502+
.to receive(:to_h)
503+
.and_return('fake-file-to_h')
504+
505+
allow(transfer)
506+
.to receive(:files)
507+
.and_return([file_stub_1])
508+
509+
allow(transfer)
510+
.to receive(:id)
511+
.and_return('fake-id')
512+
513+
allow(transfer)
514+
.to receive(:state)
515+
.and_return('fake-state')
516+
517+
allow(transfer)
518+
.to receive(:url)
519+
.and_return('fake-url')
520+
521+
expected = {
522+
id: "fake-id",
523+
state: "fake-state",
524+
url: "fake-url",
525+
message: "test transfer",
526+
files: ["fake-file-to_h"]
527+
}
528+
529+
expect(transfer.to_h)
530+
.to match(expected)
531+
end
532+
533+
it "calls :to_h on all files" do
534+
file_stub_2 = instance_double(WeTransfer::WeTransferFile, name: 'bar', size: 8)
535+
536+
allow(transfer)
537+
.to receive(:files)
538+
.and_return([file_stub_1, file_stub_2])
539+
540+
expect(file_stub_1)
541+
.to receive(:to_h)
542+
543+
expect(file_stub_2)
544+
.to receive(:to_h)
545+
546+
transfer.to_h
547+
end
548+
end
549+
550+
describe "#to_json" do
551+
it "converts the results of #to_h" do
552+
transfer = described_class.new(message: 'test transfer')
553+
554+
transfer_hash = { "foo" => "bar" }
555+
556+
allow(transfer)
557+
.to receive(:to_h)
558+
.and_return(transfer_hash)
559+
560+
expect(JSON.parse(transfer.to_json))
561+
.to eq(transfer_hash)
562+
end
563+
end
495564
end

spec/we_transfer/we_transfer_file_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,45 @@
5959
.to eq %|{"name":"test file","size":8}|
6060
end
6161
end
62+
63+
describe "#to_h" do
64+
let(:file) { described_class.new(name: 'foo', size: 8) }
65+
let(:multipart_stub) { instance_double(WeTransfer::RemoteFile::Multipart) }
66+
67+
it "has keys and values for id, name, size and multipart" do
68+
allow(file)
69+
.to receive(:multipart)
70+
.and_return(multipart_stub)
71+
72+
allow(file)
73+
.to receive(:id)
74+
.and_return('fake-id')
75+
76+
allow(multipart_stub)
77+
.to receive(:to_h)
78+
.and_return('fake-multipart')
79+
80+
expected = {
81+
id: 'fake-id',
82+
multipart: 'fake-multipart',
83+
size: 8,
84+
name: 'foo'
85+
86+
}
87+
88+
expect(file.to_h)
89+
.to match(expected)
90+
end
91+
92+
it "calls :to_h on multipart" do
93+
allow(file)
94+
.to receive(:multipart)
95+
.and_return(multipart_stub)
96+
97+
expect(multipart_stub)
98+
.to receive(:to_h)
99+
100+
file.to_h
101+
end
102+
end
62103
end

0 commit comments

Comments
 (0)