tpitale-mongo_mapper 0.6.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +10 -0
- data/LICENSE +20 -0
- data/README.rdoc +53 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/bin/mmconsole +60 -0
- data/lib/mongo_mapper/associations/base.rb +110 -0
- data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +26 -0
- data/lib/mongo_mapper/associations/belongs_to_proxy.rb +21 -0
- data/lib/mongo_mapper/associations/collection.rb +19 -0
- data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +26 -0
- data/lib/mongo_mapper/associations/many_documents_proxy.rb +115 -0
- data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +31 -0
- data/lib/mongo_mapper/associations/many_embedded_proxy.rb +54 -0
- data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
- data/lib/mongo_mapper/associations/proxy.rb +113 -0
- data/lib/mongo_mapper/associations.rb +70 -0
- data/lib/mongo_mapper/callbacks.rb +109 -0
- data/lib/mongo_mapper/dirty.rb +136 -0
- data/lib/mongo_mapper/document.rb +472 -0
- data/lib/mongo_mapper/dynamic_finder.rb +74 -0
- data/lib/mongo_mapper/embedded_document.rb +384 -0
- data/lib/mongo_mapper/finder_options.rb +133 -0
- data/lib/mongo_mapper/key.rb +36 -0
- data/lib/mongo_mapper/observing.rb +50 -0
- data/lib/mongo_mapper/pagination.rb +55 -0
- data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
- data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +27 -0
- data/lib/mongo_mapper/serialization.rb +54 -0
- data/lib/mongo_mapper/serializers/json_serializer.rb +92 -0
- data/lib/mongo_mapper/support.rb +206 -0
- data/lib/mongo_mapper/validations.rb +41 -0
- data/lib/mongo_mapper.rb +120 -0
- data/mongo_mapper.gemspec +173 -0
- data/specs.watchr +32 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
- data/test/functional/associations/test_many_documents_proxy.rb +387 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +192 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +18 -0
- data/test/functional/test_callbacks.rb +85 -0
- data/test/functional/test_dirty.rb +159 -0
- data/test/functional/test_document.rb +1235 -0
- data/test/functional/test_embedded_document.rb +135 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_pagination.rb +95 -0
- data/test/functional/test_rails_compatibility.rb +25 -0
- data/test/functional/test_string_id_compatibility.rb +72 -0
- data/test/functional/test_validations.rb +378 -0
- data/test/models.rb +271 -0
- data/test/support/custom_matchers.rb +55 -0
- data/test/support/timing.rb +16 -0
- data/test/test_helper.rb +27 -0
- data/test/unit/associations/test_base.rb +166 -0
- data/test/unit/associations/test_proxy.rb +91 -0
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_document.rb +204 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +718 -0
- data/test/unit/test_finder_options.rb +296 -0
- data/test/unit/test_key.rb +172 -0
- data/test/unit/test_mongo_mapper.rb +65 -0
- data/test/unit/test_observing.rb +101 -0
- data/test/unit/test_pagination.rb +113 -0
- data/test/unit/test_rails_compatibility.rb +49 -0
- data/test/unit/test_serializations.rb +52 -0
- data/test/unit/test_support.rb +342 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +503 -0
- metadata +235 -0
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class EmbeddedDocumentTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@document = Class.new do
|
7
|
+
include MongoMapper::Document
|
8
|
+
set_collection_name 'users'
|
9
|
+
|
10
|
+
key :first_name, String
|
11
|
+
key :last_name, String
|
12
|
+
end
|
13
|
+
@document.collection.remove
|
14
|
+
end
|
15
|
+
|
16
|
+
context "Saving a document with an embedded document" do
|
17
|
+
setup do
|
18
|
+
@document.class_eval do
|
19
|
+
key :foo, Address
|
20
|
+
end
|
21
|
+
|
22
|
+
@address = Address.new(:city => 'South Bend', :state => 'IN')
|
23
|
+
@doc = @document.new(:foo => @address)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "embed embedded document" do
|
27
|
+
@doc.save
|
28
|
+
@doc.foo.city.should == 'South Bend'
|
29
|
+
@doc.foo.state.should == 'IN'
|
30
|
+
|
31
|
+
doc = @doc.reload
|
32
|
+
doc.foo.city.should == 'South Bend'
|
33
|
+
doc.foo.state.should == 'IN'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "Instantiating single collection inherited embedded documents" do
|
38
|
+
setup do
|
39
|
+
@document = Class.new do
|
40
|
+
include MongoMapper::Document
|
41
|
+
key :message, Message
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
should "work" do
|
46
|
+
doc1 = @document.create(:message => Enter.new)
|
47
|
+
doc2 = @document.create(:message => Exit.new)
|
48
|
+
doc3 = @document.create(:message => Chat.new)
|
49
|
+
|
50
|
+
doc1.reload.message.class.should be(Enter)
|
51
|
+
doc2.reload.message.class.should be(Exit)
|
52
|
+
doc3.reload.message.class.should be(Chat)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "new?" do
|
57
|
+
setup do
|
58
|
+
@document.class_eval do
|
59
|
+
key :foo, Address
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
should "be new until document is saved" do
|
64
|
+
address = Address.new(:city => 'South Bend', :state => 'IN')
|
65
|
+
doc = @document.new(:foo => address)
|
66
|
+
address.new?.should == true
|
67
|
+
end
|
68
|
+
|
69
|
+
should "not be new after document is saved" do
|
70
|
+
address = Address.new(:city => 'South Bend', :state => 'IN')
|
71
|
+
doc = @document.new(:foo => address)
|
72
|
+
doc.save
|
73
|
+
doc.foo.new?.should == false
|
74
|
+
end
|
75
|
+
|
76
|
+
should "not be new when document is read back" do
|
77
|
+
address = Address.new(:city => 'South Bend', :state => 'IN')
|
78
|
+
doc = @document.new(:foo => address)
|
79
|
+
doc.save
|
80
|
+
|
81
|
+
doc = doc.reload
|
82
|
+
doc.foo.new?.should == false
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "save" do
|
87
|
+
should "save the root document" do
|
88
|
+
person = RealPerson.create
|
89
|
+
|
90
|
+
pet = Pet.new :name => 'sparky'
|
91
|
+
person.pets << pet
|
92
|
+
pet.save
|
93
|
+
|
94
|
+
person = person.reload
|
95
|
+
person.pets.first.should == pet
|
96
|
+
end
|
97
|
+
|
98
|
+
should "save new keys" do
|
99
|
+
person = RealPerson.new
|
100
|
+
person[:new_attribute] = 'foobar'
|
101
|
+
person.save
|
102
|
+
|
103
|
+
person = person.reload
|
104
|
+
person.new_attribute.should == 'foobar'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "update_attributes" do
|
109
|
+
should "save the root document" do
|
110
|
+
person = RealPerson.create
|
111
|
+
|
112
|
+
pet = Pet.new(:name => 'sparky')
|
113
|
+
person.pets << pet
|
114
|
+
pet.save
|
115
|
+
|
116
|
+
person = person.reload
|
117
|
+
pet = person.pets.first
|
118
|
+
pet.update_attributes :name => 'koda'
|
119
|
+
|
120
|
+
person = person.reload
|
121
|
+
person.pets.first._id.should == pet._id
|
122
|
+
person.pets.first.name.should == 'koda'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "update_attributes!" do
|
127
|
+
should "pass the attributes to self.attributes" do
|
128
|
+
person = RealPerson.create
|
129
|
+
attributes = { :foo => 'bar' }
|
130
|
+
person.expects(:attributes=).with(attributes)
|
131
|
+
person.expects(:save!)
|
132
|
+
person.update_attributes!(attributes)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LoggerTest < Test::Unit::TestCase
|
4
|
+
context "with connection that has logger" do
|
5
|
+
setup do
|
6
|
+
@output = StringIO.new
|
7
|
+
@logger = Logger.new(@output)
|
8
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => @logger)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "be able to get access to that logger" do
|
12
|
+
MongoMapper.logger.should == @logger
|
13
|
+
end
|
14
|
+
|
15
|
+
should "be able to log messages" do
|
16
|
+
MongoMapper.logger.debug 'testing'
|
17
|
+
@output.string.include?('testing').should be_true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PaginationTest < Test::Unit::TestCase
|
4
|
+
context "Paginating" do
|
5
|
+
setup do
|
6
|
+
@document = Class.new do
|
7
|
+
include MongoMapper::Document
|
8
|
+
set_collection_name 'users'
|
9
|
+
|
10
|
+
key :first_name, String
|
11
|
+
key :last_name, String
|
12
|
+
key :age, Integer
|
13
|
+
|
14
|
+
def self.per_page; 1 end
|
15
|
+
end
|
16
|
+
@document.collection.remove
|
17
|
+
|
18
|
+
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
19
|
+
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
20
|
+
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
21
|
+
end
|
22
|
+
|
23
|
+
should "return the total pages" do
|
24
|
+
result = @document.paginate(:per_page => 2, :page => 1)
|
25
|
+
result.total_pages.should == 2
|
26
|
+
end
|
27
|
+
|
28
|
+
should "return the total pages when defaulting to the document class per_page" do
|
29
|
+
result = @document.paginate(:page => 1)
|
30
|
+
result.total_pages.should == 3
|
31
|
+
end
|
32
|
+
|
33
|
+
should "return the total of records" do
|
34
|
+
result = @document.paginate(:per_page => 2, :page => 1)
|
35
|
+
result.total_entries.should == 3
|
36
|
+
end
|
37
|
+
|
38
|
+
should "return the items" do
|
39
|
+
result = @document.paginate(:per_page => 2, :page => 1, :order => 'first_name')
|
40
|
+
result.size.should == 2
|
41
|
+
result.should == [@doc1, @doc3]
|
42
|
+
end
|
43
|
+
|
44
|
+
should "accept conditions" do
|
45
|
+
result = @document.paginate({
|
46
|
+
:last_name => 'Nunemaker',
|
47
|
+
:order => "age DESC",
|
48
|
+
:per_page => 2,
|
49
|
+
:page => 1,
|
50
|
+
})
|
51
|
+
result.should == [@doc1, @doc3]
|
52
|
+
result.first.age.should == 27
|
53
|
+
|
54
|
+
result = @document.paginate({
|
55
|
+
:conditions => {:last_name => 'Nunemaker'},
|
56
|
+
:order => "age DESC",
|
57
|
+
:per_page => 2,
|
58
|
+
:page => 1} )
|
59
|
+
result.should == [@doc1, @doc3]
|
60
|
+
result.first.age.should == 27
|
61
|
+
end
|
62
|
+
|
63
|
+
should "withstand rigor" do
|
64
|
+
result = @document.paginate({
|
65
|
+
:per_page => 1,
|
66
|
+
:page => 1,
|
67
|
+
:order => 'age desc',
|
68
|
+
:last_name => 'Nunemaker'
|
69
|
+
})
|
70
|
+
result.should == [@doc1]
|
71
|
+
result.total_entries.should == 2
|
72
|
+
result.total_pages.should == 2
|
73
|
+
|
74
|
+
result = @document.paginate({
|
75
|
+
:per_page => 1,
|
76
|
+
:page => 2,
|
77
|
+
:order => 'age desc',
|
78
|
+
:last_name => 'Nunemaker'
|
79
|
+
})
|
80
|
+
result.should == [@doc3]
|
81
|
+
result.total_entries.should == 2
|
82
|
+
result.total_pages.should == 2
|
83
|
+
|
84
|
+
result = @document.paginate({
|
85
|
+
:per_page => 2,
|
86
|
+
:page => 1,
|
87
|
+
:order => 'age desc',
|
88
|
+
:last_name => 'Nunemaker'
|
89
|
+
})
|
90
|
+
result.should == [@doc1, @doc3]
|
91
|
+
result.total_entries.should == 2
|
92
|
+
result.total_pages.should == 1
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestRailsCompatibility < Test::Unit::TestCase
|
4
|
+
class Item
|
5
|
+
include MongoMapper::EmbeddedDocument
|
6
|
+
key :for_all, String
|
7
|
+
end
|
8
|
+
|
9
|
+
class Order
|
10
|
+
include MongoMapper::Document
|
11
|
+
many :items, :class_name => 'TestRailsCompatibility::Item'
|
12
|
+
key :order_only, String
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Document" do
|
16
|
+
setup do
|
17
|
+
Order.collection.remove
|
18
|
+
end
|
19
|
+
|
20
|
+
should "alias new to new_record?" do
|
21
|
+
instance = Order.new
|
22
|
+
instance.new_record?.should == instance.new?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class StringIdCompatibilityTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@note_class = Class.new do
|
6
|
+
include MongoMapper::EmbeddedDocument
|
7
|
+
key :_id, String
|
8
|
+
end
|
9
|
+
|
10
|
+
@task_class = Class.new do
|
11
|
+
include MongoMapper::Document
|
12
|
+
key :_id, String
|
13
|
+
key :project_id, String
|
14
|
+
belongs_to :project
|
15
|
+
end
|
16
|
+
|
17
|
+
@project_class = Class.new do
|
18
|
+
include MongoMapper::Document
|
19
|
+
key :_id, String
|
20
|
+
end
|
21
|
+
|
22
|
+
@task_class.belongs_to :project, :class => @project_class
|
23
|
+
@project_class.many :notes, :class => @note_class
|
24
|
+
@project_class.many :tasks, :class => @task_class, :foreign_key => 'project_id'
|
25
|
+
|
26
|
+
@project_class.collection.remove
|
27
|
+
@task_class.collection.remove
|
28
|
+
end
|
29
|
+
|
30
|
+
should "assign correct _id for documents" do
|
31
|
+
project = @project_class.create
|
32
|
+
project._id.should == project.id
|
33
|
+
project._id.should be_instance_of(String)
|
34
|
+
project.id.size.should == 24
|
35
|
+
lambda {
|
36
|
+
Mongo::ObjectID.from_string(project.id)
|
37
|
+
}.should_not raise_error
|
38
|
+
end
|
39
|
+
|
40
|
+
should "assign correct _id for embedded documents" do
|
41
|
+
note = @note_class.new
|
42
|
+
note.id.should == note._id
|
43
|
+
note.id.size.should == 24
|
44
|
+
end
|
45
|
+
|
46
|
+
should "find records" do
|
47
|
+
project = @project_class.create
|
48
|
+
@project_class.find(project.id).should == project
|
49
|
+
end
|
50
|
+
|
51
|
+
should "save embedded docs" do
|
52
|
+
n1 = @note_class.new
|
53
|
+
n2 = @note_class.new
|
54
|
+
n3 = @note_class.new
|
55
|
+
project = @project_class.create(:notes => [n1, n2, n3])
|
56
|
+
|
57
|
+
project = project.reload
|
58
|
+
project.notes.size.should == 3
|
59
|
+
project.notes.should == [n1, n2, n3]
|
60
|
+
end
|
61
|
+
|
62
|
+
should "be able to associate records" do
|
63
|
+
t1 = @task_class.new(:body => 'First task')
|
64
|
+
t2 = @task_class.new(:body => 'Second task')
|
65
|
+
t3 = @task_class.new(:body => 'Third task')
|
66
|
+
project = @project_class.create(:name => 'MM', :tasks => [t1, t2, t3])
|
67
|
+
|
68
|
+
project = project.reload
|
69
|
+
project.tasks.count.should == 3
|
70
|
+
project.tasks.should == [t1, t2, t3]
|
71
|
+
end
|
72
|
+
end
|