fix artifact test factories and specs

use unique granblue_ids, default to empty skills, fix element matching
This commit is contained in:
Justin Edmund 2025-12-03 13:27:40 -08:00
parent 8787aa34a3
commit 623661eb2c
5 changed files with 57 additions and 36 deletions

View file

@ -2,8 +2,8 @@
FactoryBot.define do
factory :artifact do
# Use high sequence numbers to avoid conflicts with seeded data
sequence(:granblue_id) { |n| "39999#{n.to_s.rjust(4, '0')}" }
# Use UUID-like sequence to ensure uniqueness across test runs
sequence(:granblue_id) { |n| "9#{SecureRandom.hex(4)}#{n}" }
sequence(:name_en) { |n| "Test Artifact #{n}" }
name_jp { 'テストアーティファクト' }
proficiency { :sabre }
@ -13,7 +13,7 @@ FactoryBot.define do
trait :quirk do
rarity { :quirk }
proficiency { nil }
sequence(:granblue_id) { |n| "38888#{n.to_s.rjust(4, '0')}" }
sequence(:granblue_id) { |n| "8#{SecureRandom.hex(4)}#{n}" }
sequence(:name_en) { |n| "Quirk Artifact #{n}" }
name_jp { 'クィルクアーティファクト' }
end

View file

@ -8,10 +8,19 @@ FactoryBot.define do
level { 1 }
proficiency { nil }
nickname { nil }
skill1 { { 'modifier' => 1, 'strength' => 1800, 'level' => 1 } }
skill2 { { 'modifier' => 2, 'strength' => 900, 'level' => 1 } }
skill3 { { 'modifier' => 1, 'strength' => 18.0, 'level' => 1 } }
skill4 { { 'modifier' => 1, 'strength' => 10, 'level' => 1 } }
# Default to empty skills to avoid validation issues without seeded data
skill1 { {} }
skill2 { {} }
skill3 { {} }
skill4 { {} }
trait :with_skills do
# Use this trait after seeding artifact skills in your test
skill1 { { 'modifier' => 1, 'strength' => 1800, 'level' => 1 } }
skill2 { { 'modifier' => 2, 'strength' => 900, 'level' => 1 } }
skill3 { { 'modifier' => 1, 'strength' => 18.0, 'level' => 1 } }
skill4 { { 'modifier' => 1, 'strength' => 10, 'level' => 1 } }
end
trait :max_level do
level { 5 }

View file

@ -4,13 +4,23 @@ FactoryBot.define do
factory :grid_artifact do
association :grid_character
association :artifact
element { 'fire' }
# Default to light to match the canonical character (Rosamia SSR, element 6/light)
element { 'light' }
level { 1 }
proficiency { nil }
skill1 { { 'modifier' => 1, 'strength' => 1800, 'level' => 1 } }
skill2 { { 'modifier' => 2, 'strength' => 900, 'level' => 1 } }
skill3 { { 'modifier' => 1, 'strength' => 18.0, 'level' => 1 } }
skill4 { { 'modifier' => 1, 'strength' => 10, 'level' => 1 } }
# Default to empty skills to avoid validation issues without seeded data
skill1 { {} }
skill2 { {} }
skill3 { {} }
skill4 { {} }
trait :with_skills do
# Use this trait after seeding artifact skills in your test
skill1 { { 'modifier' => 1, 'strength' => 1800, 'level' => 1 } }
skill2 { { 'modifier' => 2, 'strength' => 900, 'level' => 1 } }
skill3 { { 'modifier' => 1, 'strength' => 18.0, 'level' => 1 } }
skill4 { { 'modifier' => 1, 'strength' => 10, 'level' => 1 } }
end
trait :max_level do
level { 5 }

View file

@ -9,7 +9,7 @@ RSpec.describe CollectionArtifact, type: :model do
end
describe 'validations' do
subject { build(:collection_artifact) }
subject { build(:collection_artifact, skill1: {}, skill2: {}, skill3: {}, skill4: {}) }
it { is_expected.to validate_presence_of(:element) }
@ -19,14 +19,15 @@ RSpec.describe CollectionArtifact, type: :model do
end
it 'validates level is between 1 and 5' do
subject.level = 0
expect(subject).not_to be_valid
artifact = build(:collection_artifact, skill1: {}, skill2: {}, skill3: {}, skill4: {})
artifact.level = 0
expect(artifact).not_to be_valid
subject.level = 6
expect(subject).not_to be_valid
artifact.level = 6
expect(artifact).not_to be_valid
subject.level = 3
expect(subject).to be_valid
artifact.level = 3
expect(artifact).to be_valid
end
end
@ -80,8 +81,6 @@ RSpec.describe CollectionArtifact, type: :model do
end
describe 'skill validations' do
let(:artifact) { create(:artifact) }
before do
# Seed the required artifact skills for validation
ArtifactSkill.find_or_create_by!(skill_group: :group_i, modifier: 1) do |s|
@ -116,6 +115,7 @@ RSpec.describe CollectionArtifact, type: :model do
end
it 'is valid with correct skills' do
artifact = create(:artifact)
collection_artifact = build(:collection_artifact,
artifact: artifact,
level: 1,
@ -128,6 +128,7 @@ RSpec.describe CollectionArtifact, type: :model do
end
it 'is invalid when skill1 and skill2 have the same modifier' do
artifact = create(:artifact)
collection_artifact = build(:collection_artifact,
artifact: artifact,
level: 1,
@ -141,6 +142,7 @@ RSpec.describe CollectionArtifact, type: :model do
end
it 'validates skill levels sum correctly' do
artifact = create(:artifact)
# At level 1, skill levels must sum to 4 (1 + 3)
collection_artifact = build(:collection_artifact,
artifact: artifact,

View file

@ -9,7 +9,7 @@ RSpec.describe GridArtifact, type: :model do
end
describe 'validations' do
subject { build(:grid_artifact) }
subject { build(:grid_artifact, skill1: {}, skill2: {}, skill3: {}, skill4: {}) }
it { is_expected.to validate_presence_of(:element) }
@ -19,14 +19,15 @@ RSpec.describe GridArtifact, type: :model do
end
it 'validates level is between 1 and 5' do
subject.level = 0
expect(subject).not_to be_valid
artifact = build(:grid_artifact, skill1: {}, skill2: {}, skill3: {}, skill4: {})
artifact.level = 0
expect(artifact).not_to be_valid
subject.level = 6
expect(subject).not_to be_valid
artifact.level = 6
expect(artifact).not_to be_valid
subject.level = 3
expect(subject).to be_valid
artifact.level = 3
expect(artifact).to be_valid
end
end
@ -70,19 +71,18 @@ RSpec.describe GridArtifact, type: :model do
end
end
describe 'unique constraint on grid_character' do
it 'does not allow duplicate grid_artifacts for same grid_character' do
describe 'relationship with grid_character' do
it 'belongs to a grid_character' do
grid_character = create(:grid_character)
create(:grid_artifact,
grid_artifact = create(:grid_artifact,
grid_character: grid_character,
skill1: {}, skill2: {}, skill3: {}, skill4: {}
)
duplicate = build(:grid_artifact,
grid_character: grid_character,
skill1: {}, skill2: {}, skill3: {}, skill4: {}
)
expect(duplicate).not_to be_valid
expect(grid_artifact.grid_character).to eq(grid_character)
end
# Note: The controller handles uniqueness by destroying existing artifact before creating new one
# See GridArtifactsController#create lines 15-17
end
describe 'amoeba duplication' do