/spec/functional/mongoid/relations/referenced/one_spec.rb

https://github.com/kuruma-gs/mongoid · Ruby · 568 lines · 421 code · 147 blank · 0 comment · 32 complexity · a3d206144f0b649e22f2b5e73d05ffc2 MD5 · raw file

  1. require "spec_helper"
  2. describe Mongoid::Relations::Referenced::One do
  3. before do
  4. [ Person, Game, Bar ].map(&:delete_all)
  5. end
  6. describe "#=" do
  7. context "when the relationship is an illegal embedded reference" do
  8. let(:game) do
  9. Game.new
  10. end
  11. let(:video) do
  12. Video.new
  13. end
  14. it "raises a mixed relation error" do
  15. expect {
  16. game.video = video
  17. }.to raise_error(Mongoid::Errors::MixedRelations)
  18. end
  19. end
  20. context "when the relation is not polymorphic" do
  21. context "when the parent is a new record" do
  22. let(:person) do
  23. Person.new
  24. end
  25. let(:game) do
  26. Game.new
  27. end
  28. before do
  29. person.game = game
  30. end
  31. it "sets the target of the relation" do
  32. person.game.target.should == game
  33. end
  34. it "sets the foreign key on the relation" do
  35. game.person_id.should == person.id
  36. end
  37. it "sets the base on the inverse relation" do
  38. game.person.should == person
  39. end
  40. it "sets the same instance on the inverse relation" do
  41. game.person.should eql(person)
  42. end
  43. it "does not save the target" do
  44. game.should_not be_persisted
  45. end
  46. end
  47. context "when the parent is not a new record" do
  48. let(:person) do
  49. Person.create(:ssn => "437-11-1112")
  50. end
  51. let(:game) do
  52. Game.new
  53. end
  54. before do
  55. person.game = game
  56. end
  57. it "sets the target of the relation" do
  58. person.game.target.should == game
  59. end
  60. it "sets the foreign key of the relation" do
  61. game.person_id.should == person.id
  62. end
  63. it "sets the base on the inverse relation" do
  64. game.person.should == person
  65. end
  66. it "sets the same instance on the inverse relation" do
  67. game.person.should eql(person)
  68. end
  69. it "saves the target" do
  70. game.should be_persisted
  71. end
  72. end
  73. end
  74. context "when the relation is polymorphic" do
  75. context "when the parent is a new record" do
  76. let(:bar) do
  77. Bar.new
  78. end
  79. let(:rating) do
  80. Rating.new
  81. end
  82. before do
  83. bar.rating = rating
  84. end
  85. it "sets the target of the relation" do
  86. bar.rating.target.should == rating
  87. end
  88. it "sets the foreign key on the relation" do
  89. rating.ratable_id.should == bar.id
  90. end
  91. it "sets the base on the inverse relation" do
  92. rating.ratable.should == bar
  93. end
  94. it "sets the same instance on the inverse relation" do
  95. rating.ratable.should eql(bar)
  96. end
  97. it "does not save the target" do
  98. rating.should_not be_persisted
  99. end
  100. end
  101. context "when the parent is not a new record" do
  102. let(:bar) do
  103. Bar.create
  104. end
  105. let(:rating) do
  106. Rating.new
  107. end
  108. before do
  109. bar.rating = rating
  110. end
  111. it "sets the target of the relation" do
  112. bar.rating.target.should == rating
  113. end
  114. it "sets the foreign key of the relation" do
  115. rating.ratable_id.should == bar.id
  116. end
  117. it "sets the base on the inverse relation" do
  118. rating.ratable.should == bar
  119. end
  120. it "sets the same instance on the inverse relation" do
  121. rating.ratable.should eql(bar)
  122. end
  123. it "saves the target" do
  124. rating.should be_persisted
  125. end
  126. end
  127. end
  128. end
  129. describe "#= nil" do
  130. context "when the relation is not polymorphic" do
  131. context "when the parent is a new record" do
  132. let(:person) do
  133. Person.new
  134. end
  135. let(:game) do
  136. Game.new
  137. end
  138. before do
  139. person.game = game
  140. person.game = nil
  141. end
  142. it "sets the relation to nil" do
  143. person.game.should be_nil
  144. end
  145. it "removed the inverse relation" do
  146. game.person.should be_nil
  147. end
  148. it "removes the foreign key value" do
  149. game.person_id.should be_nil
  150. end
  151. end
  152. context "when the parent is not a new record" do
  153. let(:person) do
  154. Person.create(:ssn => "437-11-1112")
  155. end
  156. let(:game) do
  157. Game.new
  158. end
  159. before do
  160. person.game = game
  161. person.game = nil
  162. end
  163. it "sets the relation to nil" do
  164. person.game.should be_nil
  165. end
  166. it "removed the inverse relation" do
  167. game.person.should be_nil
  168. end
  169. it "removes the foreign key value" do
  170. game.person_id.should be_nil
  171. end
  172. it "deletes the target from the database" do
  173. game.should be_destroyed
  174. end
  175. end
  176. end
  177. context "when the relation is polymorphic" do
  178. context "when the parent is a new record" do
  179. let(:bar) do
  180. Bar.new
  181. end
  182. let(:rating) do
  183. Rating.new
  184. end
  185. before do
  186. bar.rating = rating
  187. bar.rating = nil
  188. end
  189. it "sets the relation to nil" do
  190. bar.rating.should be_nil
  191. end
  192. it "removed the inverse relation" do
  193. rating.ratable.should be_nil
  194. end
  195. it "removes the foreign key value" do
  196. rating.ratable_id.should be_nil
  197. end
  198. end
  199. context "when the parent is not a new record" do
  200. let(:bar) do
  201. Bar.create
  202. end
  203. let(:rating) do
  204. Rating.new
  205. end
  206. before do
  207. bar.rating = rating
  208. bar.rating = nil
  209. end
  210. it "sets the relation to nil" do
  211. bar.rating.should be_nil
  212. end
  213. it "removed the inverse relation" do
  214. rating.ratable.should be_nil
  215. end
  216. it "removes the foreign key value" do
  217. rating.ratable_id.should be_nil
  218. end
  219. it "deletes the target from the database" do
  220. rating.should be_destroyed
  221. end
  222. end
  223. end
  224. end
  225. describe "#build_#\{name}" do
  226. context "when the relationship is an illegal embedded reference" do
  227. let(:game) do
  228. Game.new
  229. end
  230. it "raises a mixed relation error" do
  231. expect {
  232. game.build_video(:title => "Tron")
  233. }.to raise_error(Mongoid::Errors::MixedRelations)
  234. end
  235. end
  236. context "when the relation is not polymorphic" do
  237. context "when using object ids" do
  238. let(:person) do
  239. Person.create
  240. end
  241. let(:game) do
  242. person.build_game(:score => 50)
  243. end
  244. it "returns a new document" do
  245. game.score.should == 50
  246. end
  247. it "sets the foreign key on the document" do
  248. game.person_id.should == person.id
  249. end
  250. it "sets the inverse relation" do
  251. game.person.should == person
  252. end
  253. it "does not save the built document" do
  254. game.should_not be_persisted
  255. end
  256. end
  257. context "when providing no attributes" do
  258. let(:person) do
  259. Person.create
  260. end
  261. let(:game) do
  262. person.build_game
  263. end
  264. it "sets the foreign key on the document" do
  265. game.person_id.should == person.id
  266. end
  267. it "sets the inverse relation" do
  268. game.person.should == person
  269. end
  270. it "does not save the built document" do
  271. game.should_not be_persisted
  272. end
  273. end
  274. context "when providing nil attributes" do
  275. let(:person) do
  276. Person.create
  277. end
  278. let(:game) do
  279. person.build_game(nil)
  280. end
  281. it "sets the foreign key on the document" do
  282. game.person_id.should == person.id
  283. end
  284. it "sets the inverse relation" do
  285. game.person.should == person
  286. end
  287. it "does not save the built document" do
  288. game.should_not be_persisted
  289. end
  290. end
  291. end
  292. context "when the relation is polymorphic" do
  293. context "when using object ids" do
  294. let(:bar) do
  295. Bar.create
  296. end
  297. let(:rating) do
  298. bar.build_rating(:value => 5)
  299. end
  300. it "returns a new document" do
  301. rating.value.should == 5
  302. end
  303. it "sets the foreign key on the document" do
  304. rating.ratable_id.should == bar.id
  305. end
  306. it "sets the inverse relation" do
  307. rating.ratable.should == bar
  308. end
  309. it "does not save the built document" do
  310. rating.should_not be_persisted
  311. end
  312. end
  313. end
  314. end
  315. describe "#create_#\{name}" do
  316. context "when the relationship is an illegal embedded reference" do
  317. let(:game) do
  318. Game.new
  319. end
  320. it "raises a mixed relation error" do
  321. expect {
  322. game.create_video(:title => "Tron")
  323. }.to raise_error(Mongoid::Errors::MixedRelations)
  324. end
  325. end
  326. context "when the relation is not polymorphic" do
  327. let(:person) do
  328. Person.create
  329. end
  330. let(:game) do
  331. person.create_game(:score => 50)
  332. end
  333. it "returns a new document" do
  334. game.score.should == 50
  335. end
  336. it "sets the foreign key on the document" do
  337. game.person_id.should == person.id
  338. end
  339. it "sets the inverse relation" do
  340. game.person.should == person
  341. end
  342. it "saves the document" do
  343. game.should be_persisted
  344. end
  345. end
  346. context "when providing no attributes" do
  347. let(:person) do
  348. Person.create
  349. end
  350. let(:game) do
  351. person.create_game
  352. end
  353. it "sets the foreign key on the document" do
  354. game.person_id.should == person.id
  355. end
  356. it "sets the inverse relation" do
  357. game.person.should == person
  358. end
  359. it "saves the document" do
  360. game.should be_persisted
  361. end
  362. end
  363. context "when providing nil attributes" do
  364. let(:person) do
  365. Person.create
  366. end
  367. let(:game) do
  368. person.create_game(nil)
  369. end
  370. it "sets the foreign key on the document" do
  371. game.person_id.should == person.id
  372. end
  373. it "sets the inverse relation" do
  374. game.person.should == person
  375. end
  376. it "saves the document" do
  377. game.should be_persisted
  378. end
  379. end
  380. context "when the relation is polymorphic" do
  381. let(:bar) do
  382. Bar.create
  383. end
  384. let(:rating) do
  385. bar.create_rating(:value => 5)
  386. end
  387. it "returns a new document" do
  388. rating.value.should == 5
  389. end
  390. it "sets the foreign key on the document" do
  391. rating.ratable_id.should == bar.id
  392. end
  393. it "sets the inverse relation" do
  394. rating.ratable.should == bar
  395. end
  396. it "saves the document" do
  397. rating.should be_persisted
  398. end
  399. end
  400. end
  401. describe "#nullify" do
  402. let(:person) do
  403. Person.create(:ssn => "777-77-7777")
  404. end
  405. let!(:game) do
  406. person.create_game(:name => "Starcraft II")
  407. end
  408. before do
  409. person.game.nullify
  410. end
  411. it "removes the foreign key from the target" do
  412. game.person_id.should be_nil
  413. end
  414. it "removes the reference from the target" do
  415. game.person.should be_nil
  416. end
  417. it "removes the reference from the base" do
  418. person.game.should be_nil
  419. end
  420. end
  421. end