thorsson-mongo_mapper 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +27 -0
  3. data/bin/mmconsole +60 -0
  4. data/examples/keys.rb +40 -0
  5. data/examples/modifiers/set.rb +25 -0
  6. data/examples/plugins.rb +41 -0
  7. data/examples/querying.rb +35 -0
  8. data/examples/scopes.rb +52 -0
  9. data/lib/mongo_mapper.rb +79 -0
  10. data/lib/mongo_mapper/connection.rb +83 -0
  11. data/lib/mongo_mapper/document.rb +41 -0
  12. data/lib/mongo_mapper/embedded_document.rb +31 -0
  13. data/lib/mongo_mapper/exceptions.rb +27 -0
  14. data/lib/mongo_mapper/extensions/array.rb +19 -0
  15. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  16. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  17. data/lib/mongo_mapper/extensions/date.rb +25 -0
  18. data/lib/mongo_mapper/extensions/float.rb +14 -0
  19. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  20. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  21. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  22. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  23. data/lib/mongo_mapper/extensions/object.rb +27 -0
  24. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  25. data/lib/mongo_mapper/extensions/set.rb +20 -0
  26. data/lib/mongo_mapper/extensions/string.rb +18 -0
  27. data/lib/mongo_mapper/extensions/time.rb +29 -0
  28. data/lib/mongo_mapper/plugins.rb +16 -0
  29. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  30. data/lib/mongo_mapper/plugins/associations.rb +97 -0
  31. data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
  32. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
  33. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
  34. data/lib/mongo_mapper/plugins/associations/collection.rb +22 -0
  35. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
  36. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
  37. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  38. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +110 -0
  39. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  40. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  41. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  42. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  43. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  44. data/lib/mongo_mapper/plugins/associations/proxy.rb +126 -0
  45. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  46. data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
  47. data/lib/mongo_mapper/plugins/clone.rb +19 -0
  48. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  49. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  50. data/lib/mongo_mapper/plugins/document.rb +41 -0
  51. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  52. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  53. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  54. data/lib/mongo_mapper/plugins/equality.rb +17 -0
  55. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  56. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  57. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  58. data/lib/mongo_mapper/plugins/keys.rb +309 -0
  59. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  60. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  61. data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
  62. data/lib/mongo_mapper/plugins/pagination.rb +14 -0
  63. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  64. data/lib/mongo_mapper/plugins/protected.rb +53 -0
  65. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  66. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  67. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  68. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  69. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  70. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  71. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  72. data/lib/mongo_mapper/plugins/serialization.rb +76 -0
  73. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  74. data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
  75. data/lib/mongo_mapper/plugins/validations.rb +50 -0
  76. data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
  77. data/lib/mongo_mapper/version.rb +4 -0
  78. data/test/_NOTE_ON_TESTING +1 -0
  79. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  80. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  81. data/test/functional/associations/test_in_array_proxy.rb +319 -0
  82. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  83. data/test/functional/associations/test_many_documents_proxy.rb +575 -0
  84. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  85. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  86. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  87. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  88. data/test/functional/associations/test_one_proxy.rb +182 -0
  89. data/test/functional/test_accessible.rb +168 -0
  90. data/test/functional/test_associations.rb +44 -0
  91. data/test/functional/test_binary.rb +27 -0
  92. data/test/functional/test_caching.rb +76 -0
  93. data/test/functional/test_callbacks.rb +151 -0
  94. data/test/functional/test_dirty.rb +163 -0
  95. data/test/functional/test_document.rb +253 -0
  96. data/test/functional/test_dynamic_querying.rb +75 -0
  97. data/test/functional/test_embedded_document.rb +210 -0
  98. data/test/functional/test_identity_map.rb +506 -0
  99. data/test/functional/test_indexes.rb +42 -0
  100. data/test/functional/test_logger.rb +20 -0
  101. data/test/functional/test_modifiers.rb +416 -0
  102. data/test/functional/test_pagination.rb +91 -0
  103. data/test/functional/test_protected.rb +175 -0
  104. data/test/functional/test_querying.rb +873 -0
  105. data/test/functional/test_safe.rb +76 -0
  106. data/test/functional/test_sci.rb +230 -0
  107. data/test/functional/test_scopes.rb +171 -0
  108. data/test/functional/test_string_id_compatibility.rb +67 -0
  109. data/test/functional/test_timestamps.rb +62 -0
  110. data/test/functional/test_userstamps.rb +27 -0
  111. data/test/functional/test_validations.rb +342 -0
  112. data/test/models.rb +233 -0
  113. data/test/test_active_model_lint.rb +13 -0
  114. data/test/test_helper.rb +104 -0
  115. data/test/unit/associations/test_base.rb +212 -0
  116. data/test/unit/associations/test_proxy.rb +105 -0
  117. data/test/unit/serializers/test_json_serializer.rb +202 -0
  118. data/test/unit/test_clone.rb +69 -0
  119. data/test/unit/test_descendant_appends.rb +71 -0
  120. data/test/unit/test_document.rb +213 -0
  121. data/test/unit/test_dynamic_finder.rb +125 -0
  122. data/test/unit/test_embedded_document.rb +644 -0
  123. data/test/unit/test_extensions.rb +380 -0
  124. data/test/unit/test_key.rb +185 -0
  125. data/test/unit/test_keys.rb +55 -0
  126. data/test/unit/test_mongo_mapper.rb +110 -0
  127. data/test/unit/test_pagination.rb +11 -0
  128. data/test/unit/test_plugins.rb +50 -0
  129. data/test/unit/test_rails.rb +181 -0
  130. data/test/unit/test_rails_compatibility.rb +52 -0
  131. data/test/unit/test_serialization.rb +51 -0
  132. data/test/unit/test_time_zones.rb +39 -0
  133. data/test/unit/test_validations.rb +544 -0
  134. metadata +316 -0
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class KeyTest < Test::Unit::TestCase
5
+ include MongoMapper::Plugins::Keys
6
+
7
+ context ".key?(:symbol)" do
8
+ should "be true if document has key" do
9
+ Address.key?(:city).should be_true
10
+ end
11
+
12
+ should "be false if document does not have key" do
13
+ Address.key?(:foo).should be_false
14
+ end
15
+ end
16
+
17
+ context ".key?('string')" do
18
+ should "be true if document has key" do
19
+ Address.key?('city').should be_true
20
+ end
21
+
22
+ should "be false if document does not have key" do
23
+ Address.key?('foo').should be_false
24
+ end
25
+ end
26
+
27
+ context ".new (from database)" do
28
+ setup do
29
+ @klass = Doc do
30
+ key :user, Hash
31
+
32
+ def user=(user)
33
+ super(:id => user.id, :name => user.name)
34
+ end
35
+ end
36
+
37
+ user_class = Struct.new(:id, :name)
38
+ @klass.create(:user => user_class.new(1, 'John Nunemaker'))
39
+ end
40
+
41
+ should "use []= for keys instead of public writer" do
42
+ assert_nothing_raised do
43
+ doc = @klass.first
44
+ doc.user['id'].should == 1
45
+ doc.user['name'].should == 'John Nunemaker'
46
+ end
47
+ end
48
+ end
49
+
50
+ context ".load" do
51
+ should "return nil if argument is nil" do
52
+ Doc().load(nil).should be_nil
53
+ end
54
+ end
55
+ end # KeyTest
@@ -0,0 +1,110 @@
1
+ require 'test_helper'
2
+
3
+ class Address; end
4
+
5
+ class MongoMapperTest < Test::Unit::TestCase
6
+ should "be able to write and read connection" do
7
+ conn = Mongo::Connection.new
8
+ MongoMapper.connection = conn
9
+ MongoMapper.connection.should == conn
10
+ end
11
+
12
+ should "default connection to new mongo ruby driver" do
13
+ MongoMapper.connection = nil
14
+ MongoMapper.connection.should be_instance_of(Mongo::Connection)
15
+ end
16
+
17
+ should "be able to write and read default database" do
18
+ MongoMapper.database = 'test'
19
+ MongoMapper.database.should be_instance_of(Mongo::DB)
20
+ MongoMapper.database.name.should == 'test'
21
+ end
22
+
23
+ should "have document not found error" do
24
+ lambda {
25
+ MongoMapper::DocumentNotFound
26
+ }.should_not raise_error
27
+ end
28
+
29
+ should "be able to read/write config" do
30
+ config = {
31
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'},
32
+ 'production' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test-prod'}
33
+ }
34
+ MongoMapper.config = config
35
+ MongoMapper.config.should == config
36
+ end
37
+
38
+ context "connecting to environment from config" do
39
+ should "work without authentication" do
40
+ MongoMapper.config = {
41
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
42
+ }
43
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
44
+ MongoMapper.expects(:database=).with('test')
45
+ Mongo::DB.any_instance.expects(:authenticate).never
46
+ MongoMapper.connect('development')
47
+ end
48
+
49
+ should "work without authentication using uri" do
50
+ MongoMapper.config = {
51
+ 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
52
+ }
53
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
54
+ MongoMapper.expects(:database=).with('test')
55
+ Mongo::DB.any_instance.expects(:authenticate).never
56
+ MongoMapper.connect('development')
57
+ end
58
+
59
+ should "work with options" do
60
+ MongoMapper.config = {
61
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
62
+ }
63
+ connection, logger = mock('connection'), mock('logger')
64
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
65
+ MongoMapper.connect('development', :logger => logger)
66
+ end
67
+
68
+ should "work with options using uri" do
69
+ MongoMapper.config = {
70
+ 'development' => {'uri' => 'mongodb://127.0.0.1:27017/test'}
71
+ }
72
+ connection, logger = mock('connection'), mock('logger')
73
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
74
+ MongoMapper.connect('development', :logger => logger)
75
+ end
76
+
77
+ should "work with authentication" do
78
+ MongoMapper.config = {
79
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
80
+ }
81
+ Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
82
+ MongoMapper.connect('development')
83
+ end
84
+
85
+ should "work with authentication using uri" do
86
+ MongoMapper.config = {
87
+ 'development' => {'uri' => 'mongodb://john:secret@127.0.0.1:27017/test'}
88
+ }
89
+ Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
90
+ MongoMapper.connect('development')
91
+ end
92
+
93
+ should "raise error for invalid scheme" do
94
+ MongoMapper.config = {
95
+ 'development' => {'uri' => 'mysql://127.0.0.1:5336/foo'}
96
+ }
97
+ assert_raises(MongoMapper::InvalidScheme) { MongoMapper.connect('development') }
98
+ end
99
+ end
100
+
101
+ context "setup" do
102
+ should "work as shortcut for setting config, environment and options" do
103
+ config, logger = mock('config'), mock('logger')
104
+ MongoMapper.expects(:config=).with(config)
105
+ MongoMapper.expects(:connect).with('development', :logger => logger)
106
+ MongoMapper.expects(:handle_passenger_forking).once
107
+ MongoMapper.setup(config, 'development', :logger => logger)
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class PaginationTest < Test::Unit::TestCase
4
+ should "default per_page to 25" do
5
+ Doc().per_page.should == 25
6
+ end
7
+
8
+ should "allow overriding per_page" do
9
+ Doc() { def self.per_page; 1 end }.per_page.should == 1
10
+ end
11
+ end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ module MyPlugin
4
+ def self.configure(model)
5
+ model.class_eval { attr_accessor :from_configure }
6
+ end
7
+
8
+ module ClassMethods
9
+ def class_foo
10
+ 'class_foo'
11
+ end
12
+ end
13
+
14
+ module InstanceMethods
15
+ def instance_foo
16
+ 'instance_foo'
17
+ end
18
+ end
19
+ end
20
+
21
+ class PluginsTest < Test::Unit::TestCase
22
+ context "plugin" do
23
+ setup do
24
+ @document = Class.new do
25
+ extend MongoMapper::Plugins
26
+ plugin MyPlugin
27
+ end
28
+ end
29
+
30
+ should "include instance methods" do
31
+ @document.new.instance_foo.should == 'instance_foo'
32
+ end
33
+
34
+ should "extend class methods" do
35
+ @document.class_foo.should == 'class_foo'
36
+ end
37
+
38
+ should "pass model to configure" do
39
+ @document.new.should respond_to(:from_configure)
40
+ end
41
+
42
+ should "default plugins to empty array" do
43
+ Class.new { extend MongoMapper::Plugins }.plugins.should == []
44
+ end
45
+
46
+ should "add plugin to plugins" do
47
+ @document.plugins.should include(MyPlugin)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,181 @@
1
+ require 'test_helper'
2
+
3
+ class TestRails < Test::Unit::TestCase
4
+ context "Document" do
5
+ setup do
6
+ @klass = Doc('Post') do
7
+ key :foo, String
8
+ end
9
+ end
10
+
11
+ context "Class methods" do
12
+ should "alias has_many to many" do
13
+ @klass.should respond_to(:has_many)
14
+ end
15
+
16
+ should "alias has_one to one" do
17
+ @klass.should respond_to(:has_one)
18
+ end
19
+
20
+ should "have column names" do
21
+ @klass.column_names.sort.should == ['_id', 'foo']
22
+ end
23
+
24
+ should "implement human_name" do
25
+ @klass.human_name.should == 'Post'
26
+ end
27
+ end
28
+
29
+ context "Instance methods" do
30
+ setup do
31
+ @klass.class_eval do
32
+ def bar=(value)
33
+ write_attribute(:foo, value)
34
+ end
35
+
36
+ def bar_before_typecast
37
+ read_attribute_before_typecast(:foo)
38
+ end
39
+
40
+ def bar
41
+ read_attribute(:foo)
42
+ end
43
+ end
44
+ end
45
+
46
+ should "alias new_record? to new?" do
47
+ @klass.new.should be_new_record
48
+ end
49
+
50
+ should "be able to read key with read_attribute" do
51
+ @klass.new(:foo => 'Bar').bar.should == 'Bar'
52
+ end
53
+
54
+ should "be able to read key before typecast with read_attribute_before_typecast" do
55
+ @klass.new(:foo => 21).bar_before_typecast.should == 21
56
+ @klass.new(:foo => 21).bar.should == '21'
57
+ end
58
+
59
+ should "be able to write key with write_attribute" do
60
+ @klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
61
+ end
62
+
63
+ context '#to_param' do
64
+ should "be nil if not persisted" do
65
+ @klass.new.tap do |doc|
66
+ doc.to_param.should be_nil
67
+ end
68
+ end
69
+
70
+ should "array representation of id if persisted" do
71
+ @klass.create.tap do |doc|
72
+ doc.to_param.should == doc.id.to_s
73
+ end
74
+ end
75
+ end
76
+
77
+ context '#to_key' do
78
+ should "be nil if not persisted" do
79
+ @klass.new.tap do |doc|
80
+ doc.to_key.should be_nil
81
+ end
82
+ end
83
+
84
+ should "array representation of id if persisted" do
85
+ @klass.create.tap do |doc|
86
+ doc.to_key.should == [doc.id]
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ context "EmbeddedDocument" do
94
+ setup do
95
+ @klass = EDoc('Post') { key :foo, String }
96
+ end
97
+
98
+ context "Class methods" do
99
+ should "alias has_many to many" do
100
+ @klass.should respond_to(:has_many)
101
+ end
102
+
103
+ should "alias has_one to one" do
104
+ @klass.should respond_to(:has_one)
105
+ end
106
+
107
+ should "have column names" do
108
+ @klass.column_names.sort.should == ['_id', 'foo']
109
+ end
110
+
111
+ should "implement human_name" do
112
+ @klass.human_name.should == 'Post'
113
+ end
114
+ end
115
+
116
+ context "Instance methods" do
117
+ setup do
118
+ @klass.class_eval do
119
+ def bar=(value)
120
+ write_attribute(:foo, value)
121
+ end
122
+
123
+ def bar_before_typecast
124
+ read_attribute_before_typecast(:foo)
125
+ end
126
+
127
+ def bar
128
+ read_attribute(:foo)
129
+ end
130
+ end
131
+ end
132
+
133
+ should "alias new_record? to new?" do
134
+ @klass.new.should be_new_record
135
+ end
136
+
137
+ should "be able to read key with read_attribute" do
138
+ @klass.new(:foo => 'Bar').bar.should == 'Bar'
139
+ end
140
+
141
+ should "be able to read key before typecast with read_attribute_before_typecast" do
142
+ @klass.new(:foo => 21).bar_before_typecast.should == 21
143
+ @klass.new(:foo => 21).bar.should == '21'
144
+ end
145
+
146
+ should "be able to write key with write_attribute" do
147
+ @klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
148
+ end
149
+
150
+ context '#to_param' do
151
+ should "be nil if not persisted" do
152
+ @klass.new.tap do |doc|
153
+ doc.to_param.should be_nil
154
+ end
155
+ end
156
+
157
+ should "array representation of id if persisted" do
158
+ @klass.new.tap do |doc|
159
+ doc.expects(:persisted?).returns(true)
160
+ doc.to_param.should == doc.id.to_s
161
+ end
162
+ end
163
+ end
164
+
165
+ context '#to_key' do
166
+ should "be nil if not persisted" do
167
+ @klass.new.tap do |doc|
168
+ doc.to_key.should be_nil
169
+ end
170
+ end
171
+
172
+ should "array representation of id if persisted" do
173
+ @klass.new.tap do |doc|
174
+ doc.expects(:persisted?).returns(true)
175
+ doc.to_key.should == [doc.id]
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class TestRailsCompatibility < Test::Unit::TestCase
4
+ class BigStuff
5
+ include MongoMapper::Document
6
+ end
7
+
8
+ class Item
9
+ include MongoMapper::EmbeddedDocument
10
+ key :for_all, String
11
+ end
12
+
13
+ class FirstItem < Item
14
+ key :first_only, String
15
+ many :second_items
16
+ end
17
+
18
+ class SecondItem < Item
19
+ key :second_only, String
20
+ end
21
+
22
+ context "EmbeddedDocument" do
23
+ should "alias many to has_many" do
24
+ FirstItem.should respond_to(:has_many)
25
+ end
26
+
27
+ should "alias one to has_one" do
28
+ FirstItem.should respond_to(:has_one)
29
+ end
30
+
31
+ should "have column names" do
32
+ Item.column_names.sort.should == ['_id', '_type', 'for_all']
33
+ FirstItem.column_names.sort.should == ['_id', '_type', 'first_only', 'for_all']
34
+ SecondItem.column_names.sort.should == ['_id', '_type', 'for_all', 'second_only']
35
+ end
36
+
37
+ should "alias new to new_record?" do
38
+ instance = Item.new
39
+ instance.new_record?.should == instance.new?
40
+ end
41
+
42
+ should "implement human_name" do
43
+ Item.human_name.should == 'Item'
44
+ end
45
+ end
46
+
47
+ context "Document" do
48
+ should "implement human_name" do
49
+ BigStuff.human_name.should == 'Big Stuff'
50
+ end
51
+ end
52
+ end