test/test_embedded.rb in mongoo-0.5.5 vs test/test_embedded.rb in mongoo-0.5.6

- old
+ new

@@ -238,6 +238,60 @@ end assert_equal 1, c.addresses.size assert_equal [address], c.addresses.to_a end + + should "be able to change an element in an embedded array doc" do + c = Customer.new(name: "Ben") + c.insert! + + address = c.addresses.build(street: "123 Street", city: "Metropolis") + c.mod! do |m| + m.push 'addresses', address + end + + assert_equal address, c.addresses.first + assert_equal address, c.addresses[0] + assert_equal 1, c.addresses.size + + addr = c.addresses[0] + addr.street = "456 Street" + + assert_equal [[:set, "addresses", [{"street"=>"456 Street", "city"=>"Metropolis"}]]], c.changelog + end + + should "be able to do a custom update with modifiers" do + c = Customer.new(name: "Ben") + c.insert! + + addresses = [] + %w(123 456 789).each do |s| + addresses << c.addresses.build(street: "123 Street", city: "Metropolis") + end + c.mod! do |m| + m.push_all 'addresses', addresses + end + + 2.times do + assert_equal addresses[0], c.addresses.first + assert_equal addresses[2], c.addresses.last + assert_equal 3, c.addresses.size + c = Customer.find_one(c.id) + end + + resp = c.raw_update do |u| + u.updates = { "$push" => { "addresses" => { "street" => "1 E Main St", "city" => "NY" } } } + u.opts = { safe: true } + u.will_change do |doc, res| + doc.addresses.raw << { "street" => "1 E Main St", "city" => "NY" } + doc.persisted_mongohash["addresses"] = doc.addresses.raw + end + end + + 2.times do + assert_equal 4, c.addresses.size + assert_equal "NY", c.addresses.last.city + c = Customer.find_one(c.id) + end + end end \ No newline at end of file