tests for has_many

This commit is contained in:
Dylan Knutson
2025-07-24 05:22:55 +00:00
parent ad8ae7945b
commit 4249329fa3
3 changed files with 78 additions and 11 deletions

View File

@@ -894,6 +894,54 @@ RSpec.describe HasAuxTable do
expect(ModelECustom.where(owned_aux_to_aux: @e1)).to eq([])
end
end
describe "has_many association" do
it "works between base and base" do
# e1.pk_base_id -> e2.fk_base_id
@e1.owned_base_to_base_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_base_id).to eq(1)
expect(@e2.fk_base_id).to eq(1)
expect(ModelECustom.where(owned_base_to_base_many: @e2)).to eq(
[@e1]
)
expect(ModelECustom.where(owned_base_to_base_many: @e1)).to eq([])
end
it "works between base and aux" do
# e1.pk_base_id -> e2.fk_aux_id
@e1.owned_base_to_aux_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_base_id).to eq(1)
expect(@e2.fk_aux_id).to eq(1)
expect(ModelECustom.where(owned_base_to_aux_many: @e2)).to eq([@e1])
expect(ModelECustom.where(owned_base_to_aux_many: @e1)).to eq([])
end
it "works between aux and base" do
# e1.pk_aux_id -> e2.fk_base_id
@e1.owned_aux_to_base_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_aux_id).to eq(3)
expect(@e2.fk_base_id).to eq(3)
expect(ModelECustom.where(owned_aux_to_base_many: @e2)).to eq([@e1])
expect(ModelECustom.where(owned_aux_to_base_many: @e1)).to eq([])
end
it "works between aux and aux" do
# e1.pk_aux_id -> e2.fk_aux_id
@e1.owned_aux_to_aux_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_aux_id).to eq(3)
expect(@e2.fk_aux_id).to eq(3)
expect(ModelECustom.where(owned_aux_to_aux_many: @e2)).to eq([@e1])
expect(ModelECustom.where(owned_aux_to_aux_many: @e1)).to eq([])
end
end
end
end

View File

@@ -465,4 +465,24 @@ class ModelECustom < ModelE
class_name: "ModelECustom",
primary_key: :pk_aux_id,
foreign_key: :fk_aux_id
has_many :owned_base_to_base_many,
class_name: "ModelECustom",
primary_key: :pk_base_id,
foreign_key: :fk_base_id
has_many :owned_base_to_aux_many,
class_name: "ModelECustom",
primary_key: :pk_base_id,
foreign_key: :fk_aux_id
has_many :owned_aux_to_base_many,
class_name: "ModelECustom",
primary_key: :pk_aux_id,
foreign_key: :fk_base_id
has_many :owned_aux_to_aux_many,
class_name: "ModelECustom",
primary_key: :pk_aux_id,
foreign_key: :fk_aux_id
end