test/unit/test_document.rb in mongo_mapper-unstable-2009.12.30 vs test/unit/test_document.rb in mongo_mapper-unstable-2010.1.4
- old
+ new
@@ -2,15 +2,11 @@
require 'models'
class DocumentTest < Test::Unit::TestCase
context "The Document Class" do
setup do
- @document = Class.new do
- include MongoMapper::Document
- set_collection_name 'test'
- end
- @document.collection.remove
+ @document = Doc()
end
should "have logger method" do
@document.logger.should == MongoMapper.logger
@document.logger.should be_instance_of(Logger)
@@ -38,14 +34,11 @@
should "allow setting a different database without affecting the default" do
@document.set_database_name 'test2'
@document.database_name.should == 'test2'
@document.database.name.should == 'test2'
- another_document = Class.new do
- include MongoMapper::Document
- set_collection_name 'test'
- end
+ another_document = Doc()
another_document.database.should == MongoMapper.database
end
should "default collection name to class name tableized" do
class ::Item
@@ -70,10 +63,38 @@
should "allow setting the collection name" do
@document.set_collection_name('foobar')
@document.collection.should be_instance_of(Mongo::Collection)
@document.collection.name.should == 'foobar'
end
+
+ should 'allow extensions to Document to be appended' do
+ module Extension; def test_this_extension; end end
+ MongoMapper::Document.append_extensions(Extension)
+ article = Doc()
+ article.should respond_to(:test_this_extension)
+ end
+
+ should 'add appended extensions to classes that include Document before they are added' do
+ module Extension; def test_this_extension; end end
+ article = Doc()
+ MongoMapper::Document.append_extensions(Extension)
+ article.should respond_to(:test_this_extension)
+ end
+
+ should 'allow inclusions to Document to be appended' do
+ module Inclusion; def test_this_inclusion; end end
+ MongoMapper::Document.append_inclusions(Inclusion)
+ article = Doc()
+ article.new.should respond_to(:test_this_inclusion)
+ end
+
+ should 'add appended inclusions to classes that include Document before they are added' do
+ module Inclusion; def test_this_inclusion; end end
+ article = Doc()
+ MongoMapper::Document.append_inclusions(Inclusion)
+ article.new.should respond_to(:test_this_inclusion)
+ end
end # Document class
context "Documents that inherit from other documents" do
should "default collection name to inherited class" do
Message.collection_name.should == 'messages'
@@ -94,18 +115,14 @@
end
end
context "An instance of a document" do
setup do
- @document = Class.new do
- include MongoMapper::Document
- set_collection_name 'test'
-
+ @document = Doc do
key :name, String
key :age, Integer
end
- @document.collection.remove
end
should "have access to logger" do
doc = @document.new
doc.logger.should == @document.logger
@@ -135,17 +152,13 @@
should "have a nil _root_document" do
@document.new._root_document.should be_nil
end
should "set self to the root document on embedded documents" do
- klass = Class.new do
- include MongoMapper::Document
- end
+ klass = Doc()
+ pets = EDoc()
- pets = Class.new do
- include MongoMapper::EmbeddedDocument
- end
klass.many :pets, :class => pets
doc = klass.new(:pets => [{}])
doc.pets.first._root_document.should == doc
end
@@ -181,24 +194,21 @@
context "equality" do
setup do
@oid = Mongo::ObjectID.new
end
+
should "be equal if id and class are the same" do
(@document.new('_id' => @oid) == @document.new('_id' => @oid)).should be(true)
end
should "not be equal if class same but id different" do
(@document.new('_id' => @oid) == @document.new('_id' => Mongo::ObjectID.new)).should be(false)
end
should "not be equal if id same but class different" do
- @another_document = Class.new do
- include MongoMapper::Document
- set_collection_name 'test'
- end
-
- (@document.new('_id' => @oid) == @another_document.new('_id' => @oid)).should be(false)
+ another_document = Doc()
+ (@document.new('_id' => @oid) == another_document.new('_id' => @oid)).should be(false)
end
end
end # instance of a document
end # DocumentTest