working on image feature

This commit is contained in:
2026-10-01 09:56:32 -06:00
parent 7d4e446d4a
commit 23d8154f17
26 changed files with 1259 additions and 57 deletions
+33
View File
@@ -1,4 +1,5 @@
require "rails_helper"
require "stringio"
RSpec.describe FuelEntry do
it "has a valid factory" do
@@ -57,4 +58,36 @@ RSpec.describe FuelEntry do
expect(entry).to be_valid
end
it "accepts multiple JPEG or PNG images" do
entry = build(:fuel_entry)
entry.images.attach(image_attachment("receipt-one.png", "image/png"))
entry.images.attach(image_attachment("receipt-two.jpg", "image/jpeg"))
expect(entry).to be_valid
end
it "rejects non-JPEG and non-PNG images" do
entry = build(:fuel_entry)
entry.images.attach(io: StringIO.new("not an image"), filename: "receipt.txt", content_type: "text/plain")
expect(entry).not_to be_valid
expect(entry.errors[:images]).to include("must be JPEG or PNG")
end
it "rejects images larger than 10 MB" do
entry = build(:fuel_entry)
entry.images.attach(io: StringIO.new("a" * (10.megabytes + 1)), filename: "large.png", content_type: "image/png")
expect(entry).not_to be_valid
expect(entry.errors[:images]).to include("must be 10 MB or smaller")
end
def image_attachment(filename, content_type)
{
io: StringIO.new("image content for #{filename}"),
filename: filename,
content_type: content_type
}
end
end
+33
View File
@@ -1,4 +1,5 @@
require "rails_helper"
require "stringio"
RSpec.describe MaintenanceEntry do
it "has a valid factory" do
@@ -50,4 +51,36 @@ RSpec.describe MaintenanceEntry do
expect(unknown_entry).not_to be_valid
expect(unknown_entry.errors[:name]).to be_present
end
it "accepts multiple JPEG or PNG images" do
entry = build(:maintenance_entry)
entry.images.attach(image_attachment("service-one.png", "image/png"))
entry.images.attach(image_attachment("service-two.jpg", "image/jpeg"))
expect(entry).to be_valid
end
it "rejects non-JPEG and non-PNG images" do
entry = build(:maintenance_entry)
entry.images.attach(io: StringIO.new("not an image"), filename: "service.txt", content_type: "text/plain")
expect(entry).not_to be_valid
expect(entry.errors[:images]).to include("must be JPEG or PNG")
end
it "rejects images larger than 10 MB" do
entry = build(:maintenance_entry)
entry.images.attach(io: StringIO.new("a" * (10.megabytes + 1)), filename: "large.png", content_type: "image/png")
expect(entry).not_to be_valid
expect(entry.errors[:images]).to include("must be 10 MB or smaller")
end
def image_attachment(filename, content_type)
{
io: StringIO.new("image content for #{filename}"),
filename: filename,
content_type: content_type
}
end
end