Skip to content

Commit 4cfca57

Browse files
committed
feat: add model specs
1 parent 7683903 commit 4cfca57

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

app/validators/parts_validators.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ def validate_each(record, attribute, value)
1515
unless value.is_a?(String) && value.match(/\A[^0-9]*\z/)
1616
record.errors.add(:parts, "must be a string and not contain numbers")
1717
end
18-
19-
parsed_parts = parse_parts(value)
20-
if parsed_parts.empty?
21-
record.errors.add(:parts, "contains invalid parts")
18+
if value.empty?
19+
record.parts = ""
2220
else
23-
record.parts = parsed_parts
21+
parsed_parts = parse_parts(value)
22+
if parsed_parts[:invalid].length > 0
23+
record.errors.add(:parts, "contains invalid parts")
24+
else
25+
record.parts = parsed_parts[:valid]
26+
end
2427
end
2528
end
2629

2730
def parse_parts(parts_to_parse)
28-
parts_to_parse.split(", ").reject { |part| part.empty? || !POSSIBLE_PARTS.include?(part) }.join(", ")
31+
parts = parts_to_parse.split(", ").partition { |part| POSSIBLE_PARTS.include?(part) }
32+
{ valid: parts[0].join(", "), invalid: parts[1].join(", ") }
2933
end
3034
end

spec/requests/bodies_controller_spec.rb

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
valid_attributes_list.map { |attributes| Body.create!(attributes) }
1111
}
1212

13-
let(:invalid_attributes) {
14-
{ parts: "" }
15-
}
16-
1713
describe "GET #index" do
1814
it "returns the created bodies" do
1915
get :index

spec/requests/bodies_model_spec.rb

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Body, type: :model do
4+
subject { described_class.new(parts: parts) }
5+
6+
context 'with valid parts' do
7+
let(:parts) { 'front-torso, front-thigh-right' }
8+
9+
it 'is valid' do
10+
expect(subject).to be_valid
11+
end
12+
end
13+
14+
context 'with invalid parts' do
15+
let(:parts) { 'invalid-part, another-invalid-part' }
16+
17+
it 'is not valid' do
18+
puts subject
19+
expect(subject).not_to be_valid
20+
expect(subject.errors[:parts]).to include("contains invalid parts")
21+
end
22+
end
23+
24+
context 'with parts containing numbers' do
25+
let(:parts) { 'front-torso1, front-thigh-right' }
26+
27+
it 'is not valid' do
28+
expect(subject).not_to be_valid
29+
expect(subject.errors[:parts]).to include("must be a string and not contain numbers")
30+
end
31+
end
32+
33+
context 'with parts as a non-string' do
34+
let(:parts) { 123 }
35+
36+
it 'is not valid' do
37+
expect(subject).not_to be_valid
38+
expect(subject.errors[:parts]).to include("must be a string and not contain numbers")
39+
end
40+
end
41+
42+
context 'with a mixture of valid and invalid parts' do
43+
let(:parts) { 'front-torso, invalid-part' }
44+
45+
it 'is not valid' do
46+
expect(subject).not_to be_valid
47+
expect(subject.errors[:parts]).to include("contains invalid parts")
48+
end
49+
end
50+
51+
context 'with empty parts' do
52+
let(:parts) { '' }
53+
54+
it 'is valid' do
55+
expect(subject).to be_valid
56+
end
57+
end
58+
end

spec/requests/bodies_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99

1010
let(:invalid_attributes) {
11-
{ parts: "" }
11+
{ parts: "invalid-torso, invalid-thigh-right" }
1212
}
1313

1414
let(:body) { Body.create! valid_attributes }

0 commit comments

Comments
 (0)