mongo_mapper_ign 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +35 -0
  4. data/Rakefile +37 -0
  5. data/bin/mmconsole +60 -0
  6. data/lib/mongo_mapper.rb +116 -0
  7. data/lib/mongo_mapper/document.rb +313 -0
  8. data/lib/mongo_mapper/embedded_document.rb +70 -0
  9. data/lib/mongo_mapper/plugins.rb +35 -0
  10. data/lib/mongo_mapper/plugins/associations.rb +114 -0
  11. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  13. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  14. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  15. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -0
  16. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +144 -0
  17. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +129 -0
  19. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  20. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  21. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  22. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
  23. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +69 -0
  24. data/lib/mongo_mapper/plugins/associations/proxy.rb +124 -0
  25. data/lib/mongo_mapper/plugins/callbacks.rb +240 -0
  26. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  27. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  28. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  29. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  30. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  31. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  32. data/lib/mongo_mapper/plugins/keys.rb +345 -0
  33. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  34. data/lib/mongo_mapper/plugins/modifiers.rb +107 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +24 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/persistence.rb +68 -0
  38. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  39. data/lib/mongo_mapper/plugins/rails.rb +57 -0
  40. data/lib/mongo_mapper/plugins/serialization.rb +91 -0
  41. data/lib/mongo_mapper/plugins/serialization/array.rb +56 -0
  42. data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +240 -0
  43. data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
  44. data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
  45. data/lib/mongo_mapper/plugins/validations.rb +46 -0
  46. data/lib/mongo_mapper/query.rb +143 -0
  47. data/lib/mongo_mapper/support.rb +218 -0
  48. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  49. data/lib/mongo_mapper/support/find.rb +77 -0
  50. data/lib/mongo_mapper/version.rb +3 -0
  51. data/mongo_mapper.gemspec +214 -0
  52. data/mongo_mapper_ign.gemspec +217 -0
  53. data/performance/read_write.rb +52 -0
  54. data/specs.watchr +51 -0
  55. data/test/NOTE_ON_TESTING +1 -0
  56. data/test/active_model_lint_test.rb +13 -0
  57. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  58. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  59. data/test/functional/associations/test_in_array_proxy.rb +325 -0
  60. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  61. data/test/functional/associations/test_many_documents_proxy.rb +536 -0
  62. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  63. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  64. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  65. data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
  66. data/test/functional/associations/test_one_proxy.rb +196 -0
  67. data/test/functional/test_associations.rb +44 -0
  68. data/test/functional/test_binary.rb +27 -0
  69. data/test/functional/test_callbacks.rb +151 -0
  70. data/test/functional/test_dirty.rb +163 -0
  71. data/test/functional/test_document.rb +1219 -0
  72. data/test/functional/test_embedded_document.rb +210 -0
  73. data/test/functional/test_identity_map.rb +507 -0
  74. data/test/functional/test_indexing.rb +44 -0
  75. data/test/functional/test_logger.rb +20 -0
  76. data/test/functional/test_modifiers.rb +394 -0
  77. data/test/functional/test_pagination.rb +93 -0
  78. data/test/functional/test_protected.rb +163 -0
  79. data/test/functional/test_string_id_compatibility.rb +67 -0
  80. data/test/functional/test_timestamps.rb +64 -0
  81. data/test/functional/test_userstamps.rb +28 -0
  82. data/test/functional/test_validations.rb +342 -0
  83. data/test/models.rb +227 -0
  84. data/test/support/custom_matchers.rb +37 -0
  85. data/test/support/timing.rb +16 -0
  86. data/test/test_helper.rb +64 -0
  87. data/test/unit/associations/test_base.rb +212 -0
  88. data/test/unit/associations/test_proxy.rb +105 -0
  89. data/test/unit/serializers/test_json_serializer.rb +202 -0
  90. data/test/unit/test_descendant_appends.rb +71 -0
  91. data/test/unit/test_document.rb +225 -0
  92. data/test/unit/test_dynamic_finder.rb +123 -0
  93. data/test/unit/test_embedded_document.rb +657 -0
  94. data/test/unit/test_keys.rb +185 -0
  95. data/test/unit/test_mongo_mapper.rb +118 -0
  96. data/test/unit/test_pagination.rb +160 -0
  97. data/test/unit/test_plugins.rb +50 -0
  98. data/test/unit/test_query.rb +374 -0
  99. data/test/unit/test_rails.rb +181 -0
  100. data/test/unit/test_rails_compatibility.rb +52 -0
  101. data/test/unit/test_serialization.rb +51 -0
  102. data/test/unit/test_support.rb +382 -0
  103. data/test/unit/test_time_zones.rb +39 -0
  104. data/test/unit/test_validations.rb +544 -0
  105. metadata +327 -0
@@ -0,0 +1,185 @@
1
+ require 'test_helper'
2
+
3
+ class Address
4
+ include MongoMapper::EmbeddedDocument
5
+
6
+ key :address, String
7
+ key :city, String
8
+ key :state, String
9
+ key :zip, Integer
10
+ end
11
+
12
+ class FooType < Struct.new(:bar)
13
+ def self.to_mongo(value)
14
+ 'to_mongo'
15
+ end
16
+
17
+ def self.from_mongo(value)
18
+ 'from_mongo'
19
+ end
20
+ end
21
+
22
+ class KeyTest < Test::Unit::TestCase
23
+ include MongoMapper::Plugins::Keys
24
+
25
+ context ".key?" do
26
+ should "be true if document has key" do
27
+ Address.key?(:city).should be_true
28
+ Address.key?('city').should be_true
29
+ end
30
+
31
+ should "be false if document does not have key" do
32
+ Address.key?(:foo).should be_false
33
+ Address.key?('foo').should be_false
34
+ end
35
+ end
36
+
37
+ context "Initializing a new key" do
38
+ should "allow setting the name" do
39
+ Key.new(:foo, String).name.should == 'foo'
40
+ end
41
+
42
+ should "allow setting the type" do
43
+ Key.new(:foo, Integer).type.should be(Integer)
44
+ end
45
+
46
+ should "allow setting options" do
47
+ Key.new(:foo, Integer, :required => true).options[:required].should be(true)
48
+ end
49
+
50
+ should "default options to {}" do
51
+ Key.new(:foo, Integer, nil).options.should == {}
52
+ end
53
+
54
+ should "symbolize option keys" do
55
+ Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
56
+ end
57
+
58
+ should "work with just name" do
59
+ key = Key.new(:foo)
60
+ key.name.should == 'foo'
61
+ end
62
+
63
+ should "work with name and type" do
64
+ key = Key.new(:foo, String)
65
+ key.name.should == 'foo'
66
+ key.type.should == String
67
+ end
68
+
69
+ should "work with name, type, and options" do
70
+ key = Key.new(:foo, String, :required => true)
71
+ key.name.should == 'foo'
72
+ key.type.should == String
73
+ key.options[:required].should be_true
74
+ end
75
+
76
+ should "work with name and options" do
77
+ key = Key.new(:foo, :required => true)
78
+ key.name.should == 'foo'
79
+ key.options[:required].should be_true
80
+ end
81
+ end
82
+
83
+ context "A key" do
84
+ should "be equal to another key with same name and type" do
85
+ Key.new(:name, String).should == Key.new(:name, String)
86
+ end
87
+
88
+ should "not be equal to another key with different name" do
89
+ Key.new(:name, String).should_not == Key.new(:foo, String)
90
+ end
91
+
92
+ should "not be equal to another key with different type" do
93
+ Key.new(:name, String).should_not == Key.new(:name, Integer)
94
+ end
95
+
96
+ should "know if it is a embedded_document" do
97
+ Key.new(:name, EDoc()).embeddable?.should be_true
98
+ end
99
+
100
+ should "know if it is not a embedded_document" do
101
+ Key.new(:name, String).embeddable?.should be_false
102
+ end
103
+
104
+ should "know if it is a number" do
105
+ Key.new(:age, Integer).number?.should be_true
106
+ Key.new(:age, Float).number?.should be_true
107
+ end
108
+
109
+ should "know if it is not a number" do
110
+ Key.new(:age, String).number?.should be_false
111
+ end
112
+ end
113
+
114
+ context "setting a value with a custom type" do
115
+ should "correctly typecast" do
116
+ key = Key.new(:foo, FooType)
117
+ key.set("something").should == 'to_mongo'
118
+ end
119
+
120
+ should "correctly typecast if object of that type is given" do
121
+ key = Key.new(:foo, FooType)
122
+ key.set(FooType.new('something')).should == 'to_mongo'
123
+ end
124
+ end
125
+
126
+ context "getting a value with a custom type" do
127
+ should "use #from_mongo to convert back to custom type" do
128
+ key = Key.new(:foo, FooType)
129
+ key.get('something').should == 'from_mongo'
130
+ end
131
+ end
132
+
133
+ context "getting a value" do
134
+ should "work with a type" do
135
+ key = Key.new(:foo, String)
136
+ key.get('bar').should == 'bar'
137
+ end
138
+
139
+ should "work without type" do
140
+ key = Key.new(:foo)
141
+ key.get([1, '2']).should == [1, '2']
142
+ key.get(false).should == false
143
+ key.get({}).should == {}
144
+ end
145
+
146
+ context "for a embedded_document" do
147
+ should "default to nil" do
148
+ key = Key.new(:foo, Address)
149
+ key.get(nil).should be_nil
150
+ end
151
+
152
+ should "return instance if instance" do
153
+ address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
154
+ key = Key.new(:foo, Address)
155
+ key.get(address).should == address
156
+ end
157
+ end
158
+ end
159
+
160
+ context "getting a value with a default set" do
161
+ setup do
162
+ @key = Key.new(:foo, String, :default => 'baz')
163
+ end
164
+
165
+ should "return default value if value nil" do
166
+ @key.get(nil).should == 'baz'
167
+ end
168
+
169
+ should "return value if not blank" do
170
+ @key.get('foobar').should == 'foobar'
171
+ end
172
+
173
+ should "work with Boolean type and false value" do
174
+ Key.new(:active, Boolean, :default => false).get(nil).should be_false
175
+ end
176
+
177
+ should "work with Boolean type and true value" do
178
+ Key.new(:active, Boolean, :default => true).get(nil).should be_true
179
+ end
180
+
181
+ should "work with procs" do
182
+ Key.new(:foo, String, :default => lambda { return 'hello world' }).get(nil).should == "hello world"
183
+ end
184
+ end
185
+ end # KeyTest
@@ -0,0 +1,118 @@
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).never
107
+ MongoMapper.setup(config, 'development', :logger => logger)
108
+ end
109
+
110
+ should "handle passenger if option present" do
111
+ config, logger = mock('config'), mock('logger')
112
+ MongoMapper.expects(:config=).with(config)
113
+ MongoMapper.expects(:connect).with('development', :logger => logger)
114
+ MongoMapper.expects(:handle_passenger_forking)
115
+ MongoMapper.setup(config, 'development', :logger => logger, :passenger => true)
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,160 @@
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
+
12
+ context "Pagination proxy" do
13
+ include MongoMapper::Plugins::Pagination
14
+
15
+ should "respond_to? correctly on proxy readers" do
16
+ proxy = Proxy.new(25, 10, 4)
17
+ proxy.respond_to?(:subject).should be_true
18
+ proxy.respond_to?(:total_entries).should be_true
19
+ proxy.respond_to?(:per_page).should be_true
20
+ proxy.respond_to?(:current_page).should be_true
21
+ proxy.respond_to?(:limit).should be_true
22
+ proxy.respond_to?(:total_pages).should be_true
23
+ proxy.respond_to?(:out_of_bounds?).should be_true
24
+ proxy.respond_to?(:previous_page).should be_true
25
+ proxy.respond_to?(:next_page).should be_true
26
+ proxy.respond_to?(:skip).should be_true
27
+ proxy.respond_to?(:offset).should be_true
28
+
29
+ # make sure it doesnt respond true to everything
30
+ proxy.respond_to?(:blahblahblah).should be_false
31
+ end
32
+
33
+ should "respond_to? correctly on proxy writers" do
34
+ proxy = Proxy.new(25, 10, 4)
35
+ proxy.respond_to?(:subject=).should be_true
36
+ end
37
+
38
+ should "should have accessors for subject" do
39
+ subject = [1,2,3,4,5]
40
+ collection = Proxy.new(25, 2)
41
+ collection.subject = subject
42
+ collection.subject.should == subject
43
+ end
44
+
45
+ should "delegate any methods not defined to the subject" do
46
+ subject = [1,2,3,4,5]
47
+ collection = Proxy.new(25, 2, 10)
48
+ collection.subject = subject
49
+ collection.size.should == 5
50
+ collection.each_with_index do |value, i|
51
+ value.should == subject[i]
52
+ end
53
+ collection[0..2].should == [1,2,3]
54
+ collection.class.should == Array
55
+ end
56
+
57
+ should "should respond_to? correctly for methods defined on the subject" do
58
+ subject = [1,2,3,4,5]
59
+ def subject.blahblah
60
+ "BLAHBLAH"
61
+ end
62
+ collection = Proxy.new(25, 2, 10)
63
+ collection.subject = subject
64
+ collection.respond_to?(:blahblah).should be_true
65
+ end
66
+
67
+ should "return correct value for total_entries" do
68
+ Proxy.new(25, 2, 10).total_entries.should == 25
69
+ Proxy.new('25', 2, 10).total_entries.should == 25
70
+ end
71
+
72
+ should "return correct value for per_page" do
73
+ Proxy.new(25, 2, 10).per_page.should == 10
74
+ Proxy.new(25, 2, '10').per_page.should == 10
75
+ end
76
+
77
+ should "alias limit to per_page" do
78
+ Proxy.new(100, 1, 300).limit.should == 300
79
+ end
80
+
81
+ should "set per_page to 25 if nil or blank" do
82
+ Proxy.new(25, 2).per_page.should == 25
83
+ Proxy.new(25, 2, '').per_page.should == 25
84
+ end
85
+
86
+ should "return correct value for current_page" do
87
+ Proxy.new(25, 2, 10).current_page.should == 2
88
+ Proxy.new(25, '2', 10).current_page.should == 2
89
+ end
90
+
91
+ should "not allow value less than 1 for current page" do
92
+ Proxy.new(25, -1).current_page.should == 1
93
+ end
94
+
95
+ should "automatically calculate total_pages from total_entries and per page" do
96
+ Proxy.new(25, 2, 10).total_pages.should == 3
97
+ end
98
+
99
+ should "know how many records to skip" do
100
+ Proxy.new(25, 2, 10).skip.should == 10
101
+ end
102
+
103
+ should "alias offset to skip" do
104
+ Proxy.new(25, 2, 10).offset.should == 10
105
+ end
106
+
107
+ should "return true for === Array" do
108
+ collection = Proxy.new(25, 2, 10)
109
+ collection.subject = [1, 2]
110
+ collection.should === Array
111
+ end
112
+
113
+ context "previous_page" do
114
+ should "be nil if current page 1" do
115
+ Proxy.new(25, 1, 10).previous_page.should be_nil
116
+ end
117
+
118
+ should "be one less than current page if current is > 1" do
119
+ Proxy.new(25, 2, 10).previous_page.should == 1
120
+ end
121
+ end
122
+
123
+ context "next_page" do
124
+ should "be nil if current page is last page" do
125
+ Proxy.new(25, 3, 10).next_page.should be_nil
126
+ end
127
+
128
+ should "work for any page that is not last" do
129
+ Proxy.new(25, 1, 10).next_page.should == 2
130
+ Proxy.new(25, 2, 10).next_page.should == 3
131
+ end
132
+ end
133
+
134
+ context "previous_page" do
135
+ should "be nil if current page is first page" do
136
+ Proxy.new(25, 1, 10).previous_page.should be_nil
137
+ end
138
+
139
+ should "work for any page other than first" do
140
+ Proxy.new(25, 2, 10).previous_page.should == 1
141
+ Proxy.new(25, 3, 10).previous_page.should == 2
142
+ end
143
+ end
144
+
145
+ context "out_of_bounds?" do
146
+ should "be true if current_page is greater than total_pages" do
147
+ Proxy.new(25, 10, 4).out_of_bounds?.should be_true
148
+ end
149
+
150
+ should "be false if current_page is less than total_pages" do
151
+ Proxy.new(25, 10, 1).out_of_bounds?.should be_false
152
+ Proxy.new(25, 2, 10).out_of_bounds?.should be_false
153
+ end
154
+
155
+ should "be false if current_page is equal to total_pages" do
156
+ Proxy.new(25, 3, 10).out_of_bounds?.should be_false
157
+ end
158
+ end
159
+ end
160
+ end