test/test_embedded.rb in mongoo-0.4.10 vs test/test_embedded.rb in mongoo-0.5.0
- old
+ new
@@ -1,42 +1,45 @@
require 'helper'
class Book < Mongoo::Base
- attribute "title", :type => :string
-
- attribute "chapters", :type => :hash
- attribute "authors", :type => :hash
- attribute "sample_chapter", :type => :hash
- attribute "purchases", :type => :hash
-
- embeds_one "sample_chapter", :as => "sample_chapter", :class => "Book::Chapter"
- embeds_many "chapters", :as => "chapters", :class => 'Book::Chapter'
- embeds_many "authors", :as => "authors", :class => 'Book::Author'
- embeds_many "purchases", :as => "purchases", :class => 'Book::Purchase'
+ describe do |d|
+ d.attribute "title", :type => :string
+ d.embeds_one "sample_chapter", :class => "Book::Chapter"
+ d.embeds_many "chapters", :class => 'Book::Chapter'
+ d.embeds_many "authors", :class => 'Book::Author'
+ d.embeds_many "purchases", :class => 'Book::Purchase'
+ end
end
class Book::Chapter < Mongoo::Embedded::Base
- attribute "title", :type => :string
+ describe do |d|
+ d.attribute "title", :type => :string
+ end
end
class Book::Author < Mongoo::Embedded::Base
- attribute "first_name", :type => :string
- attribute "last_name", :type => :string
+ describe do |d|
+ d.attribute "first_name", :type => :string
+ d.attribute "last_name", :type => :string
+ end
end
class Book::Purchase < Mongoo::Embedded::Base
- attribute "payment_type", :type => :string
+ describe do |d|
+ d.attribute "payment_type", :type => :string
+ d.embeds_one "customer", :class => 'Book::Purchase::Customer'
+ end
- attribute "customer", :type => :hash
- embeds_one "customer", :as => "customer", :class => 'Book::Purchase::Customer'
-
validates_presence_of :payment_type
end
class Book::Purchase::Customer < Mongoo::Embedded::Base
- attribute "name", :type => :string
- attribute "phone", :type => :string
+ describe do |d|
+ d.attribute "name", :type => :string
+ d.attribute "phone", :type => :string
+ end
+
validates_presence_of :name
validates_presence_of :phone
end
class TestEmbedded < Test::Unit::TestCase
@@ -66,42 +69,42 @@
should "be able to work with a single embedded doc" do
b = Book.new(title: "BASE Jumping Basics")
b.sample_chapter = Book::Chapter.new(b, {})
b.sample_chapter.title = "Understanding the Risks"
- assert_equal "Understanding the Risks", b.g('sample_chapter')['title']
+ assert_equal "Understanding the Risks", b.g('embedded_sample_chapter')['title']
b.insert!
b = Book.find_one(b.id)
assert_equal "Understanding the Risks", b.sample_chapter.title
- assert_equal "Understanding the Risks", b.g('sample_chapter')['title']
+ assert_equal "Understanding the Risks", b.g('embedded_sample_chapter')['title']
end
should "validate embedded docs and can have nested embeds" do
b = Book.new(title: "BASE Jumping Basics")
b.insert!
purchase_id = BSON::ObjectId.new.to_s
b.purchases[purchase_id] = b.purchases.build({})
assert !b.valid?
- assert_equal({:"purchases.#{purchase_id}.payment_type"=>["can't be blank"]}, b.errors)
+ assert_equal({:"embedded_purchases.#{purchase_id}.payment_type"=>["can't be blank"]}, b.errors)
b.purchases[purchase_id].payment_type = "Cash"
assert b.valid?
b.update!
b = Book.find_one(b.id)
assert_equal "Cash", b.purchases[purchase_id].payment_type
assert_nil b.purchases[purchase_id].customer
b.purchases[purchase_id].customer = Book::Purchase::Customer.new(b.purchases[purchase_id], name: "Jiminy")
assert_equal "Jiminy", b.purchases[purchase_id].customer.name
assert !b.valid?
- assert_equal({:"purchases.#{purchase_id}.customer.phone"=>["can't be blank"]}, b.errors)
+ assert_equal({:"embedded_purchases.#{purchase_id}.embedded_customer.phone"=>["can't be blank"]}, b.errors)
b.purchases[purchase_id].customer.phone = "123"
assert b.valid?
b.update!
b = Book.find_one(b.id)
assert_equal "Jiminy", b.purchases[purchase_id].customer.name
b.purchases[purchase_id].customer = nil
- assert_equal [[:unset, "purchases.#{purchase_id}.customer", 1]], b.changelog
+ assert_equal [[:unset, "embedded_purchases.#{purchase_id}.embedded_customer", 1]], b.changelog
b.update!
b = Book.find_one(b.id)
assert_nil b.purchases[purchase_id].customer
assert_equal [], b.changelog
end
\ No newline at end of file