87 lines
2.7 KiB
Ruby
87 lines
2.7 KiB
Ruby
require "rails_helper"
|
|
require "stringio"
|
|
|
|
RSpec.describe MaintenanceEntry do
|
|
it "has a valid factory" do
|
|
expect(build(:maintenance_entry)).to be_valid
|
|
end
|
|
|
|
it "requires all fields" do
|
|
entry = build(
|
|
:maintenance_entry,
|
|
vehicle: nil,
|
|
updated_by_user: nil,
|
|
name: nil,
|
|
notes: nil,
|
|
odometer: nil,
|
|
cost: nil,
|
|
date: nil,
|
|
ensure_matching_schedule: false
|
|
)
|
|
|
|
expect(entry).not_to be_valid
|
|
expect(entry.errors.attribute_names).to include(:vehicle, :updated_by_user, :name, :notes, :odometer, :cost, :date)
|
|
end
|
|
|
|
it "requires valid numeric values and allows zero cost" do
|
|
invalid_entry = build(:maintenance_entry, odometer: -1, cost: -1)
|
|
no_cost_entry = build(:maintenance_entry, cost: 0)
|
|
|
|
expect(invalid_entry).not_to be_valid
|
|
expect(invalid_entry.errors[:odometer]).to be_present
|
|
expect(invalid_entry.errors[:cost]).to be_present
|
|
expect(no_cost_entry).to be_valid
|
|
end
|
|
|
|
it "rejects future dates" do
|
|
entry = build(:maintenance_entry, date: Date.current + 1.day)
|
|
|
|
expect(entry).not_to be_valid
|
|
expect(entry.errors[:date]).to be_present
|
|
end
|
|
|
|
it "requires name to match a vehicle maintenance schedule case-insensitively" do
|
|
vehicle = create(:vehicle)
|
|
create(:maintenance_schedule, vehicle: vehicle, maintenance_type: "Oil Change")
|
|
|
|
matching_entry = build(:maintenance_entry, vehicle: vehicle, name: "oil change")
|
|
unknown_entry = build(:maintenance_entry, vehicle: vehicle, name: "Tire Rotation", ensure_matching_schedule: false)
|
|
|
|
expect(matching_entry).to be_valid
|
|
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
|