Skip to content

Commit 89fa3f9

Browse files
p-mongoneilshwekyp
authored
MONGOID-5080 Add tests for locating inverses (#5210)
* MONGOID-5275 add failing test * MONGOID-5275 attempt a fix * MONGOID-5275 another potential fix * MONGOID-5275 add back name_with_module * MONGOID-5275 go back to comparing classes * MONGOID-5275 next on no class found * MONGOID-5275 remove unused code * MONGOID-5275 add touch tests * MONGOID-5275 add module into relation class name * remove leftover commented code * MONGOID-5275 include short docstring * move test * add docstring * add a test which does not use a class name which also exists on top level * add test escaping namespacing * add test when inverse is not found * reinstate comment * test unqualified class_name * test does not work yet * revert everything other than tests relevant to MONGOID-5080 * mark pending * mark pending correctly Co-authored-by: Neil Shweky <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 343b666 commit 89fa3f9

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

spec/mongoid/association/embedded/embeds_many/proxy_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "spec_helper"
4+
require_relative '../embeds_many_models.rb'
45

56
describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
67

spec/mongoid/association/embedded/embeds_many_models.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,61 @@ class EmmOrder
104104

105105
embedded_in :user, class_name: 'EmmUser'
106106
end
107+
108+
module EmmSpec
109+
# There is also a top-level Car class defined.
110+
class Car
111+
include Mongoid::Document
112+
113+
embeds_many :doors
114+
end
115+
116+
class Door
117+
include Mongoid::Document
118+
119+
embedded_in :car
120+
end
121+
122+
class Tank
123+
include Mongoid::Document
124+
125+
embeds_many :guns
126+
embeds_many :emm_turrets
127+
# This association references a model that is not in our module,
128+
# and it does not define class_name hence Mongoid will not be able to
129+
# figure out the inverse for this association.
130+
embeds_many :emm_hatches
131+
132+
# class_name is intentionally unqualified, references a class in the
133+
# same module. Rails permits class_name to be unqualified like this.
134+
embeds_many :launchers, class_name: 'Launcher'
135+
end
136+
137+
class Gun
138+
include Mongoid::Document
139+
140+
embedded_in :tank
141+
end
142+
143+
class Launcher
144+
include Mongoid::Document
145+
146+
# class_name is intentionally unqualified.
147+
embedded_in :tank, class_name: 'Tank'
148+
end
149+
end
150+
151+
# This is intentionally on top level.
152+
class EmmTurret
153+
include Mongoid::Document
154+
155+
embedded_in :tank, class_name: 'EmmSpec::Tank'
156+
end
157+
158+
# This is intentionally on top level.
159+
class EmmHatch
160+
include Mongoid::Document
161+
162+
# No :class_name option on this association intentionally.
163+
embedded_in :tank
164+
end

spec/mongoid/association/embedded/embeds_many_spec.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "spec_helper"
4+
require_relative 'embeds_many_models'
45

56
describe Mongoid::Association::Embedded::EmbedsMany do
67

@@ -861,4 +862,71 @@ class OtherEmbeddedObject; end
861862
expect(association.path(double( :_parent => true))).to be_a(Mongoid::Atomic::Paths::Embedded::Many)
862863
end
863864
end
865+
866+
context "when the document and embedded document's klass is in a submodule" do
867+
868+
let(:tank) { EmmSpec::Tank.create! }
869+
let(:gun) { tank.guns.create! }
870+
871+
let(:inverse_assoc) { gun._association.inverse_association }
872+
873+
it "has the correct inverses" do
874+
pending 'MONGOID-5080'
875+
876+
inverse_assoc.should be_a(Mongoid::Association::Embedded::EmbeddedIn)
877+
inverse_assoc.name.should == :tank
878+
end
879+
880+
context "when embedded association is not namespaced but has class_name" do
881+
882+
let(:turret) { tank.emm_turrets.create! }
883+
884+
let(:inverse_assoc) { turret._association.inverse_association }
885+
886+
it "has the correct inverses" do
887+
inverse_assoc.should be_a(Mongoid::Association::Embedded::EmbeddedIn)
888+
inverse_assoc.name.should == :tank
889+
end
890+
end
891+
892+
context "when embedded association is not namespaced and lacks class_name" do
893+
894+
let(:hatch) { tank.emm_hatches.create! }
895+
896+
let(:inverse_assoc) { hatch._association.inverse_association }
897+
898+
it "does not find the inverse" do
899+
inverse_assoc.should be nil
900+
end
901+
end
902+
903+
context "when the class names exist on top level and namespaced" do
904+
905+
let(:car) { EmmSpec::Car.create! }
906+
let(:door) { car.doors.create! }
907+
908+
let(:inverse_assoc) { door._association.inverse_association }
909+
910+
it "has the correct inverses" do
911+
pending 'MONGOID-5080'
912+
913+
inverse_assoc.should be_a(Mongoid::Association::Embedded::EmbeddedIn)
914+
inverse_assoc.name.should == :car
915+
end
916+
end
917+
918+
context "when the association has an unqualified class_name in same module" do
919+
920+
let(:launcher) { tank.launchers.create! }
921+
922+
let(:inverse_assoc) { launcher._association.inverse_association }
923+
924+
it "has the correct inverses" do
925+
pending 'unqualified class_name arguments do not work per MONGOID-5080'
926+
927+
inverse_assoc.should be_a(Mongoid::Association::Embedded::EmbeddedIn)
928+
inverse_assoc.name.should == :tank
929+
end
930+
end
931+
end
864932
end

0 commit comments

Comments
 (0)