|
32 | 32 | end |
33 | 33 | end |
34 | 34 |
|
35 | | - describe '#csv' do |
36 | | - let (:csv_io) { export.csv } |
| 35 | + describe '#download' do |
| 36 | + let (:download_io) { export.download } |
37 | 37 |
|
38 | 38 | describe 'when file is ready' do |
39 | 39 | before do |
40 | 40 | stub_request(:get, export.url).to_return( |
41 | 41 | status: 200, |
42 | | - body: api_fixture('exports/id/get/csv_download/200', ext: :csv), |
43 | | - headers: { 'Content-Type' => 'text/csv' } |
| 42 | + body: File.binread(File.expand_path('../../fixtures/exports/id/get/csv_download/200.csv.gz', __dir__)), |
| 43 | + headers: { 'Content-Type' => 'application/octet-stream' } |
44 | 44 | ) |
45 | 45 | end |
46 | 46 | it 'does not raise an error' do |
47 | | - expect { export.csv }.to_not raise_error |
| 47 | + expect { export.download }.to_not raise_error |
48 | 48 | end |
49 | | - it 'returs a readable object' do |
50 | | - expect(csv_io).to be_kind_of(Down::ChunkedIO) |
51 | | - expect(csv_io.read).to be_kind_of(String) |
| 49 | + it 'returns a readable object' do |
| 50 | + expect(download_io).to be_kind_of(Down::ChunkedIO) |
| 51 | + expect(download_io.read).to be_kind_of(String) |
52 | 52 | end |
53 | | - it 'has csv caption' do |
54 | | - expect(csv_io.read).to start_with('Date/Time (UTC),Source,DID,Destination') |
| 53 | + it 'returns gzip data' do |
| 54 | + magic = download_io.read(2) |
| 55 | + expect(magic.bytes).to eq([0x1f, 0x8b]) |
55 | 56 | end |
56 | 57 | end |
57 | 58 |
|
|
61 | 62 | end |
62 | 63 | it 'does not call the api' do |
63 | 64 | WebMock.reset! |
64 | | - expect { export.csv }.to_not raise_error |
| 65 | + expect { export.download }.to_not raise_error |
65 | 66 | expect(a_request(:any, /.*/)).to_not have_been_made |
66 | 67 | end |
67 | | - it 'returs nil' do |
68 | | - expect(csv_io).to be_nil |
| 68 | + it 'returns nil' do |
| 69 | + expect(download_io).to be_nil |
69 | 70 | end |
70 | 71 | end |
71 | 72 |
|
|
74 | 75 | stub_request(:get, export.url).to_return(status: 404, body: '') |
75 | 76 | end |
76 | 77 | it 'raises a NotFound error' do |
77 | | - expect { export.csv }.to raise_error(Down::ClientError) |
| 78 | + expect { export.download }.to raise_error(Down::ClientError) |
78 | 79 | end |
79 | 80 | end |
80 | 81 |
|
81 | 82 | end |
82 | 83 | end |
83 | 84 |
|
| 85 | + describe '#csv' do |
| 86 | + let (:export) do |
| 87 | + stub_didww_request(:get, "/exports/#{id}").to_return( |
| 88 | + status: 200, |
| 89 | + body: api_fixture('exports/id/get/without_includes/200'), |
| 90 | + headers: json_api_headers |
| 91 | + ) |
| 92 | + client.exports.find(id).first |
| 93 | + end |
| 94 | + |
| 95 | + describe 'when file is ready' do |
| 96 | + before do |
| 97 | + stub_request(:get, export.url).to_return( |
| 98 | + status: 200, |
| 99 | + body: File.binread(File.expand_path('../../fixtures/exports/id/get/csv_download/200.csv.gz', __dir__)), |
| 100 | + headers: { 'Content-Type' => 'application/octet-stream' } |
| 101 | + ) |
| 102 | + end |
| 103 | + it 'does not raise an error' do |
| 104 | + expect { export.csv }.to_not raise_error |
| 105 | + end |
| 106 | + it 'returns a StringIO' do |
| 107 | + expect(export.csv).to be_kind_of(StringIO) |
| 108 | + end |
| 109 | + it 'has csv caption' do |
| 110 | + expect(export.csv.read).to start_with('Date/Time Start (UTC)') |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + describe 'when url is empty' do |
| 115 | + before do |
| 116 | + export.url = nil |
| 117 | + end |
| 118 | + it 'returns nil' do |
| 119 | + expect(export.csv).to be_nil |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
84 | 124 | context 'when Export does not exist' do |
85 | 125 | it 'raises a NotFound error' do |
86 | 126 | stub_didww_request(:get, "/exports/#{id}").to_return( |
|
0 commit comments