Skip to content

Commit 9d682b1

Browse files
committed
test: Small improvements to specs
1 parent 130aa46 commit 9d682b1

2 files changed

Lines changed: 17 additions & 25 deletions

File tree

spec/requests/file_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def unprocessable
9898
put blob.service_url_for_direct_upload, params: data, headers: { "Content-Type" => "text/plain" }
9999

100100
expect(response).to have_http_status(:no_content)
101-
expect(data).to eq blob.download
101+
expect(blob.download).to eq data
102102
end
103103

104104
it "uses blob direct upload with mismatched content type" do

spec/service/active_storage/service/db_service_spec.rb

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110

111111
before { upload }
112112

113-
it { is_expected.to be_truthy }
113+
it { is_expected.to be true }
114114

115115
it "deletes the file" do
116116
expect { delete }.to change(ActiveStorageDB::File, :count).from(1).to(0)
@@ -119,17 +119,23 @@
119119
context "when the attachment is not found" do
120120
subject(:delete) { service.delete("#{key}!") }
121121

122-
it { is_expected.to be_falsey }
122+
it { is_expected.to be false }
123123
end
124124
end
125125

126126
describe ".delete_prefixed" do
127127
subject(:delete_prefixed) { service.delete_prefixed(key[0..10]) }
128128

129-
before { upload }
129+
let(:other_file) { create(:active_storage_db_file, ref: "unrelated_prefix_file") }
130+
131+
before do
132+
other_file
133+
upload
134+
end
130135

131-
it "deletes the files" do
132-
expect { delete_prefixed }.to change(ActiveStorageDB::File, :count).from(1).to(0)
136+
it "deletes only matching files", :aggregate_failures do
137+
expect { delete_prefixed }.to change(ActiveStorageDB::File, :count).by(-1)
138+
expect(ActiveStorageDB::File.find_by(ref: "unrelated_prefix_file")).to be_present
133139
end
134140

135141
context "when no files match the prefix" do
@@ -151,8 +157,6 @@
151157
context "with an existing file" do
152158
before { upload }
153159

154-
after { service.delete(key) }
155-
156160
it "downloads the data" do
157161
expect(download).to eq fixture_data
158162
end
@@ -167,9 +171,9 @@
167171
it "yields multiple chunks when data exceeds chunk size", :aggregate_failures do
168172
chunks = []
169173
service.download(key) { |chunk| chunks << chunk }
170-
# fixture_data is a PNG (~100 bytes), chunk size is 100 bytes,
171-
# so we expect at least 1 chunk yielded via the streaming path
172-
expect(chunks).not_to be_empty
174+
# fixture_data is 126 bytes, chunk size is 100 bytes,
175+
# so we expect exactly 2 chunks via the streaming path
176+
expect(chunks.size).to eq 2
173177
expect(chunks.join).to eq fixture_data
174178
end
175179
end
@@ -192,8 +196,6 @@
192196

193197
before { upload }
194198

195-
after { service.delete(key) }
196-
197199
it { is_expected.to eq fixture_data[range] }
198200

199201
context "when the attachment is not found" do
@@ -206,14 +208,12 @@
206208
describe ".exist?" do
207209
subject { service.exist?(key) }
208210

209-
it { is_expected.to be_falsey }
211+
it { is_expected.to be false }
210212

211213
context "when a file is uploaded" do
212214
before { upload }
213215

214-
after { service.delete(key) }
215-
216-
it { is_expected.to be_truthy }
216+
it { is_expected.to be true }
217217
end
218218
end
219219

@@ -226,17 +226,13 @@
226226
describe ".upload" do
227227
it "uploads the data" do
228228
expect(upload).to be_a ActiveStorageDB::File
229-
ensure
230-
service.delete(key)
231229
end
232230

233231
context "with the checksum" do
234232
let(:upload) { service.upload(key, StringIO.new(fixture_data), checksum: checksum) }
235233

236234
it "uploads the data" do
237235
expect(upload).to be_a ActiveStorageDB::File
238-
ensure
239-
service.delete(key)
240236
end
241237
end
242238

@@ -256,8 +252,6 @@
256252
context "with a duplicate key" do
257253
before { upload }
258254

259-
after { service.delete(key) }
260-
261255
it "raises a uniqueness error" do
262256
expect {
263257
service.upload(key, StringIO.new(fixture_data))
@@ -286,8 +280,6 @@
286280
end
287281
end
288282

289-
after { service.delete(key) }
290-
291283
it { is_expected.to start_with "#{url_options[:protocol]}#{url_options[:host]}" }
292284

293285
it "generates a token that contains the expected fields" do

0 commit comments

Comments
 (0)