Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing tests for orderable embedded docs #74

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 232 additions & 23 deletions spec/integration/embedded_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,243 @@ def positions
it 'sets proper position while creation' do
expect(positions).to eq([[1, 2], [1, 2, 3]])
end

context '#move_to' do

it 'move_to! moves an item returned by a query to position' do
parent = EmbedsOrderable.first
child1 = parent.embedded_orderables.where(position: 1).first
child2 = parent.embedded_orderables.where(position: 2).first
child1.move_to!(2)
expect(child1.reload.position).to eq(2)
expect(child2.reload.position).to eq(1)
it 'move_to! moves an item returned by a query to position' do
parent = EmbedsOrderable.first
child1 = parent.embedded_orderables.where(position: 1).first
child2 = parent.embedded_orderables.where(position: 2).first
child1.move_to!(2)
expect(child1.reload.position).to eq(2)
expect(child2.reload.position).to eq(1)
end

it 'move_to moves an item returned by a query to position when saving the parent' do
parent = EmbedsOrderable.first
child1 = parent.embedded_orderables.where(position: 1).first
child2 = parent.embedded_orderables.where(position: 2).first
child1.move_to(2)
parent.save!
expect(child1.reload.position).to eq(2)
expect(child2.reload.position).to eq(1)
end

it 'move_to= moves an item returned by a query to position when saving the parent' do
parent = EmbedsOrderable.first
child1 = parent.embedded_orderables.where(position: 1).first
child2 = parent.embedded_orderables.where(position: 2).first
child1.move_to = 2
parent.save!
expect(child1.reload.position).to eq(2)
expect(child2.reload.position).to eq(1)
end

it 'moves an embedded item to top position' do
parent = EmbedsOrderable.first
emb1 = parent.embedded_orderables.where(position: 1).first
emb2 = parent.embedded_orderables.where(position: 2).first
emb2.move_to!(1)

[emb1, emb2, parent].each(&:reload)
# TODO: needs to sort order
# expect(parent.embedded_orderables).to eq([emb2, emb1])
expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2])
expect(emb1.position).to eq(2)
expect(emb2.position).to eq(1)
end

it 'moves an embedded item to top position by symbol' do
parent = EmbedsOrderable.first
emb1 = parent.embedded_orderables.where(position: 1).first
emb2 = parent.embedded_orderables.where(position: 2).first
emb2.move_to!(:top)

[emb1, emb2, parent].each(&:reload)
# TODO: needs to sort order
# expect(parent.embedded_orderables).to eq([emb2, emb1])
expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2])
expect(emb1.position).to eq(2)
expect(emb2.position).to eq(1)
end

it 'moves an embedded item above top position' do
parent = EmbedsOrderable.first
emb1 = parent.embedded_orderables.where(position: 1).first
emb2 = parent.embedded_orderables.where(position: 2).first
emb2.move_to!(0)

[emb1, emb2, parent].each(&:reload)
# TODO: needs to sort order; remove sort from position
# expect(parent.embedded_orderables).to eq([emb2, emb1])
expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2])
expect(emb1.position).to eq(2)
expect(emb2.position).to eq(1)
end

it 'moves an embedded item to middle position' do
parent = EmbedsOrderable.last
emb1 = parent.embedded_orderables.where(position: 1).first
emb2 = parent.embedded_orderables.where(position: 2).first
emb3 = parent.embedded_orderables.where(position: 3).first
emb3.move_to!(2)

[emb1, emb2, emb3, parent].each(&:reload)
# TODO: needs to sort order
# expect(parent.embedded_orderables).to eq([emb1, emb3, emb2])
expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2, 3])
expect(emb1.position).to eq(1)
expect(emb2.position).to eq(3)
expect(emb3.position).to eq(2)
end

it 'moves an embedded item to bottom position' do
parent = EmbedsOrderable.first
emb1 = parent.embedded_orderables.where(position: 1).first
emb2 = parent.embedded_orderables.where(position: 2).first
emb1.move_to!(2)

[emb1, emb2, parent].each(&:reload)
# TODO: needs to sort order
# expect(parent.embedded_orderables).to eq([emb2, emb1])
expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2])
expect(emb1.position).to eq(2)
expect(emb2.position).to eq(1)
end

it 'moves an embedded item to bottom position by symbol' do
parent = EmbedsOrderable.first
emb1 = parent.embedded_orderables.where(position: 1).first
emb2 = parent.embedded_orderables.where(position: 2).first
emb1.move_to!(:bottom)

[emb1, emb2, parent].each(&:reload)
# TODO: needs to sort order
# expect(parent.embedded_orderables).to eq([emb2, emb1])
expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2])
expect(emb1.position).to eq(2)
expect(emb2.position).to eq(1)
end

it 'moves an embedded item below bottom position' do
parent = EmbedsOrderable.first
emb1 = parent.embedded_orderables.where(position: 1).first
emb2 = parent.embedded_orderables.where(position: 2).first
emb1.move_to!(3)

[emb1, emb2, parent].each(&:reload)
# TODO: needs to sort order; remove sort from position
# expect(parent.embedded_orderables).to eq([emb2, emb1])
expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2])
expect(emb1.position).to eq(2)
expect(emb2.position).to eq(1)
end
end

it 'move_to moves an item returned by a query to position when saving the parent' do
parent = EmbedsOrderable.first
child1 = parent.embedded_orderables.where(position: 1).first
child2 = parent.embedded_orderables.where(position: 2).first
child1.move_to(2)
parent.save!
expect(child1.reload.position).to eq(2)
expect(child2.reload.position).to eq(1)
context '#save! on parent' do
let!(:parent) { EmbedsOrderable.first }
let!(:emb1) { parent.embedded_orderables.where(position: 1).first }
let!(:emb2) { parent.embedded_orderables.where(position: 2).first }

it 'saves new item with position set to top' do
emb3 = parent.embedded_orderables.build
emb3.position = 1
parent.save!
# expect(parent.embedded_orderables).to eq([emb3, emb1, emb2])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set above top' do
emb3 = parent.embedded_orderables.build
emb3.position = 2
parent.save!
# expect(parent.embedded_orderables).to eq([emb3, emb1, emb2])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set to middle' do
emb3 = parent.embedded_orderables.build
emb3.position = 2
parent.save!
# expect(parent.embedded_orderables).to eq([emb1, emb3, emb2])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set to bottom' do
emb3 = parent.embedded_orderables.build
emb3.position = 3
parent.save!
# expect(parent.embedded_orderables).to eq([emb1, emb2, emb3])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set below bottom' do
emb3 = parent.embedded_orderables.build
emb3.position = 4
parent.save!
# expect(parent.embedded_orderables).to eq([emb1, emb2, emb3])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with nil position' do
parent.embedded_orderables.build
parent.save!
# expect(parent.embedded_orderables).to eq([emb1, emb2, emb3])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end
end

it 'move_to= moves an item returned by a query to position when saving the parent' do
parent = EmbedsOrderable.first
child1 = parent.embedded_orderables.where(position: 1).first
child2 = parent.embedded_orderables.where(position: 2).first
child1.move_to = 2
parent.save!
expect(child1.reload.position).to eq(2)
expect(child2.reload.position).to eq(1)
context '#save! on child' do
let!(:parent) { EmbedsOrderable.first }
let!(:emb1) { parent.embedded_orderables.where(position: 1).first }
let!(:emb2) { parent.embedded_orderables.where(position: 2).first }

it 'saves new item with position set to top' do
emb3 = parent.embedded_orderables.build
emb3.position = 1
emb3.save!
# expect(parent.embedded_orderables).to eq([emb3, emb1, emb2])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set above top' do
emb3 = parent.embedded_orderables.build
emb3.position = 2
emb3.save!
# expect(parent.embedded_orderables).to eq([emb3, emb1, emb2])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set to middle' do
emb3 = parent.embedded_orderables.build
emb3.position = 2
emb3.save!
# expect(parent.embedded_orderables).to eq([emb1, emb3, emb2])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set to bottom' do
emb3 = parent.embedded_orderables.build
emb3.position = 3
emb3.save!
# expect(parent.embedded_orderables).to eq([emb1, emb2, emb3])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with position set below bottom' do
emb3 = parent.embedded_orderables.build
emb3.position = 4
emb3.save!
# expect(parent.embedded_orderables).to eq([emb1, emb2, emb3])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end

it 'saves new item with nil position' do
emb3 = parent.embedded_orderables.build
emb3.save!
# expect(parent.embedded_orderables).to eq([emb1, emb2, emb3])
expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3]
end
end
end

Expand Down
Loading