From 3faefef599455632abd743e09bb6efb8d24aca0e Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 8 Nov 2023 02:01:39 +0900 Subject: [PATCH] Failing specs for embedded doc position updating --- spec/integration/embedded_spec.rb | 190 ++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/spec/integration/embedded_spec.rb b/spec/integration/embedded_spec.rb index 5c00d62..ef1b9eb 100644 --- a/spec/integration/embedded_spec.rb +++ b/spec/integration/embedded_spec.rb @@ -187,6 +187,196 @@ def positions expect(child2.reload.position).to eq(1) end end + + context '#save! on parent' do + let!(:parent) { EmbedsOrderable.last } + let!(:child1) { parent.embedded_orderables.first } + let!(:child2) { parent.embedded_orderables.second } + let!(:child3) { parent.embedded_orderables.third } + + it 'sets existing item to top' do + child3.position = 1 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 1] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 1] + end + + it 'sets existing item above top' do + child3.position = 0 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 0] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 1] + end + + it 'sets existing item to middle' do + child3.position = 2 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 2] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 3, 2] + end + + it 'sets existing item to bottom' do + child1.position = 3 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [3, 2, 3] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [3, 1, 2] + end + + it 'sets existing item below bottom' do + child1.position = 4 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [4, 2, 3] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [3, 1, 2] + end + + it 'sets existing item with nil position' do + child2.position = nil + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 4, 3] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 3, 2] + end + + it 'saves new item with position set to top' do + child4 = parent.embedded_orderables.build + child4.position = 1 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + end + + it 'saves new item with position set above top' do + child4 = parent.embedded_orderables.build + child4.position = 0 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + end + + it 'saves new item with position set to middle' do + child4 = parent.embedded_orderables.build + child4.position = 2 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 3, 4, 2] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 3, 4, 2] + end + + it 'saves new item with position set to bottom' do + child4 = parent.embedded_orderables.build + child4.position = 4 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 3, 5] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + end + + it 'saves new item with position set below bottom' do + child4 = parent.embedded_orderables.build + child4.position = 5 + parent.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + end + + it 'saves new item with nil position' do + parent.embedded_orderables.build + parent.save! + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + end + end + + context '#save! on child' do + let!(:parent) { EmbedsOrderable.last } + let!(:child1) { parent.embedded_orderables.first } + let!(:child2) { parent.embedded_orderables.second } + let!(:child3) { parent.embedded_orderables.third } + + it 'sets existing item to top' do + child3.position = 1 + child3.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 1] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 1] + end + + it 'sets existing item above top' do + child3.position = 0 + child3.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 0] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 1] + end + + it 'sets existing item to middle' do + child3.position = 2 + child3.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 2] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 3, 2] + end + + it 'sets existing item to bottom' do + child1.position = 3 + child1.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [3, 2, 3] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [3, 1, 2] + end + + it 'sets existing item below bottom' do + child1.position = 4 + child1.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [4, 2, 3] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [3, 1, 2] + end + + it 'sets existing item with nil position' do + child2.position = nil + child2.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 4, 3] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 3, 2] + end + + it 'saves new item with position set to top' do + child4 = parent.embedded_orderables.build + child4.position = 1 + child4.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + end + + it 'saves new item with position set above top' do + child4 = parent.embedded_orderables.build + child4.position = 0 + child4.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [2, 3, 4, 1] + end + + it 'saves new item with position set to middle' do + child4 = parent.embedded_orderables.build + child4.position = 2 + child4.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 3, 4, 2] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 3, 4, 2] + end + + it 'saves new item with position set to bottom' do + child4 = parent.embedded_orderables.build + child4.position = 4 + child4.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 3, 5] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + end + + it 'saves new item with position set below bottom' do + child4 = parent.embedded_orderables.build + child4.position = 5 + child4.save! + # expect(parent.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + end + + it 'saves new item with nil position' do + child4 = parent.embedded_orderables.build + child4.save! + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3, 4] + end + end end context 'with transactions' do