mongo_mapper-rails3 0.7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/.gitignore +10 -0
  2. data/Gemfile +15 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +60 -0
  5. data/Rakefile +58 -0
  6. data/VERSION +1 -0
  7. data/bin/mmconsole +60 -0
  8. data/lib/mongo_mapper.rb +131 -0
  9. data/lib/mongo_mapper/document.rb +439 -0
  10. data/lib/mongo_mapper/embedded_document.rb +68 -0
  11. data/lib/mongo_mapper/plugins.rb +30 -0
  12. data/lib/mongo_mapper/plugins/associations.rb +106 -0
  13. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  14. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  15. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  16. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  17. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
  18. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +141 -0
  19. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  20. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +120 -0
  21. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  22. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  23. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  24. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  25. data/lib/mongo_mapper/plugins/associations/proxy.rb +119 -0
  26. data/lib/mongo_mapper/plugins/callbacks.rb +87 -0
  27. data/lib/mongo_mapper/plugins/clone.rb +14 -0
  28. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  29. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  30. data/lib/mongo_mapper/plugins/equality.rb +24 -0
  31. data/lib/mongo_mapper/plugins/identity_map.rb +124 -0
  32. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  33. data/lib/mongo_mapper/plugins/keys.rb +310 -0
  34. data/lib/mongo_mapper/plugins/logger.rb +19 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +26 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/protected.rb +46 -0
  38. data/lib/mongo_mapper/plugins/rails.rb +46 -0
  39. data/lib/mongo_mapper/plugins/serialization.rb +50 -0
  40. data/lib/mongo_mapper/plugins/validations.rb +88 -0
  41. data/lib/mongo_mapper/query.rb +130 -0
  42. data/lib/mongo_mapper/support.rb +217 -0
  43. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  44. data/lib/mongo_mapper/support/find.rb +77 -0
  45. data/mongo_mapper-rails3.gemspec +208 -0
  46. data/performance/read_write.rb +52 -0
  47. data/specs.watchr +51 -0
  48. data/test/NOTE_ON_TESTING +1 -0
  49. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  50. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  51. data/test/functional/associations/test_in_array_proxy.rb +321 -0
  52. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  53. data/test/functional/associations/test_many_documents_proxy.rb +453 -0
  54. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  55. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  56. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  57. data/test/functional/associations/test_one_proxy.rb +161 -0
  58. data/test/functional/test_associations.rb +44 -0
  59. data/test/functional/test_binary.rb +27 -0
  60. data/test/functional/test_callbacks.rb +81 -0
  61. data/test/functional/test_dirty.rb +163 -0
  62. data/test/functional/test_document.rb +1244 -0
  63. data/test/functional/test_embedded_document.rb +125 -0
  64. data/test/functional/test_identity_map.rb +508 -0
  65. data/test/functional/test_logger.rb +20 -0
  66. data/test/functional/test_modifiers.rb +252 -0
  67. data/test/functional/test_pagination.rb +93 -0
  68. data/test/functional/test_protected.rb +161 -0
  69. data/test/functional/test_string_id_compatibility.rb +67 -0
  70. data/test/functional/test_validations.rb +329 -0
  71. data/test/models.rb +232 -0
  72. data/test/support/custom_matchers.rb +55 -0
  73. data/test/support/timing.rb +16 -0
  74. data/test/test_helper.rb +59 -0
  75. data/test/unit/associations/test_base.rb +207 -0
  76. data/test/unit/associations/test_proxy.rb +105 -0
  77. data/test/unit/serializers/test_json_serializer.rb +189 -0
  78. data/test/unit/test_descendant_appends.rb +71 -0
  79. data/test/unit/test_document.rb +231 -0
  80. data/test/unit/test_dynamic_finder.rb +123 -0
  81. data/test/unit/test_embedded_document.rb +663 -0
  82. data/test/unit/test_keys.rb +169 -0
  83. data/test/unit/test_lint.rb +8 -0
  84. data/test/unit/test_mongo_mapper.rb +125 -0
  85. data/test/unit/test_pagination.rb +160 -0
  86. data/test/unit/test_plugins.rb +51 -0
  87. data/test/unit/test_query.rb +334 -0
  88. data/test/unit/test_rails.rb +123 -0
  89. data/test/unit/test_rails_compatibility.rb +57 -0
  90. data/test/unit/test_serialization.rb +51 -0
  91. data/test/unit/test_support.rb +362 -0
  92. data/test/unit/test_time_zones.rb +39 -0
  93. data/test/unit/test_validations.rb +557 -0
  94. metadata +344 -0
@@ -0,0 +1,169 @@
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 "Initializing a new key" do
26
+ should "allow setting the name" do
27
+ Key.new(:foo, String).name.should == 'foo'
28
+ end
29
+
30
+ should "allow setting the type" do
31
+ Key.new(:foo, Integer).type.should be(Integer)
32
+ end
33
+
34
+ should "allow setting options" do
35
+ Key.new(:foo, Integer, :required => true).options[:required].should be(true)
36
+ end
37
+
38
+ should "default options to {}" do
39
+ Key.new(:foo, Integer, nil).options.should == {}
40
+ end
41
+
42
+ should "symbolize option keys" do
43
+ Key.new(:foo, Integer, 'required' => true).options[:required].should be(true)
44
+ end
45
+
46
+ should "work with just name" do
47
+ key = Key.new(:foo)
48
+ key.name.should == 'foo'
49
+ end
50
+
51
+ should "work with name and type" do
52
+ key = Key.new(:foo, String)
53
+ key.name.should == 'foo'
54
+ key.type.should == String
55
+ end
56
+
57
+ should "work with name, type, and options" do
58
+ key = Key.new(:foo, String, :required => true)
59
+ key.name.should == 'foo'
60
+ key.type.should == String
61
+ key.options[:required].should be_true
62
+ end
63
+
64
+ should "work with name and options" do
65
+ key = Key.new(:foo, :required => true)
66
+ key.name.should == 'foo'
67
+ key.options[:required].should be_true
68
+ end
69
+ end
70
+
71
+ context "A key" do
72
+ should "be equal to another key with same name and type" do
73
+ Key.new(:name, String).should == Key.new(:name, String)
74
+ end
75
+
76
+ should "not be equal to another key with different name" do
77
+ Key.new(:name, String).should_not == Key.new(:foo, String)
78
+ end
79
+
80
+ should "not be equal to another key with different type" do
81
+ Key.new(:name, String).should_not == Key.new(:name, Integer)
82
+ end
83
+
84
+ should "know if it is a embedded_document" do
85
+ Key.new(:name, EDoc()).embeddable?.should be_true
86
+ end
87
+
88
+ should "know if it is not a embedded_document" do
89
+ Key.new(:name, String).embeddable?.should be_false
90
+ end
91
+
92
+ should "know if it is a number" do
93
+ Key.new(:age, Integer).number?.should be_true
94
+ Key.new(:age, Float).number?.should be_true
95
+ end
96
+
97
+ should "know if it is not a number" do
98
+ Key.new(:age, String).number?.should be_false
99
+ end
100
+ end
101
+
102
+ context "setting a value with a custom type" do
103
+ should "correctly typecast" do
104
+ key = Key.new(:foo, FooType)
105
+ key.set("something").should == 'to_mongo'
106
+ end
107
+
108
+ should "correctly typecast if object of that type is given" do
109
+ key = Key.new(:foo, FooType)
110
+ key.set(FooType.new('something')).should == 'to_mongo'
111
+ end
112
+ end
113
+
114
+ context "getting a value with a custom type" do
115
+ should "use #from_mongo to convert back to custom type" do
116
+ key = Key.new(:foo, FooType)
117
+ key.get('something').should == 'from_mongo'
118
+ end
119
+ end
120
+
121
+ context "getting a value" do
122
+ should "work with a type" do
123
+ key = Key.new(:foo, String)
124
+ key.get('bar').should == 'bar'
125
+ end
126
+
127
+ should "work without type" do
128
+ key = Key.new(:foo)
129
+ key.get([1, '2']).should == [1, '2']
130
+ key.get(false).should == false
131
+ key.get({}).should == {}
132
+ end
133
+
134
+ context "for a embedded_document" do
135
+ should "default to nil" do
136
+ key = Key.new(:foo, Address)
137
+ key.get(nil).should be_nil
138
+ end
139
+
140
+ should "return instance if instance" do
141
+ address = Address.new(:city => 'South Bend', :state => 'IN', :zip => 46544)
142
+ key = Key.new(:foo, Address)
143
+ key.get(address).should == address
144
+ end
145
+ end
146
+ end
147
+
148
+ context "getting a value with a default set" do
149
+ setup do
150
+ @key = Key.new(:foo, String, :default => 'baz')
151
+ end
152
+
153
+ should "return default value if value nil" do
154
+ @key.get(nil).should == 'baz'
155
+ end
156
+
157
+ should "return value if not blank" do
158
+ @key.get('foobar').should == 'foobar'
159
+ end
160
+
161
+ should "work with Boolean type and false value" do
162
+ Key.new(:active, Boolean, :default => false).get(nil).should be_false
163
+ end
164
+
165
+ should "work with Boolean type and true value" do
166
+ Key.new(:active, Boolean, :default => true).get(nil).should be_true
167
+ end
168
+ end
169
+ end # KeyTest
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class TestLint < Test::Unit::TestCase
4
+ def model
5
+ Doc().new.to_model
6
+ end
7
+ include ActiveModel::Lint::Tests
8
+ end
@@ -0,0 +1,125 @@
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
+
35
+ MongoMapper.config = config
36
+ MongoMapper.config.should == config
37
+ end
38
+
39
+ context "connecting to environment from config" do
40
+ should "work without authentication" do
41
+ MongoMapper.config = {
42
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
43
+ }
44
+
45
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
46
+ MongoMapper.expects(:database=).with('test')
47
+ Mongo::DB.any_instance.expects(:authenticate).never
48
+ MongoMapper.connect('development')
49
+ end
50
+
51
+ should "work with options" do
52
+ MongoMapper.config = {
53
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
54
+ }
55
+
56
+ connection, logger = mock('connection'), mock('logger')
57
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, :logger => logger)
58
+ MongoMapper.connect('development', :logger => logger)
59
+ end
60
+
61
+ should "work with authentication" do
62
+ MongoMapper.config = {
63
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test', 'username' => 'john', 'password' => 'secret'}
64
+ }
65
+
66
+ Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
67
+ MongoMapper.connect('development')
68
+ end
69
+ end
70
+
71
+ context "setup" do
72
+ should "work as shortcut for setting config, environment and options" do
73
+ config, logger = mock('config'), mock('logger')
74
+ MongoMapper.expects(:config=).with(config)
75
+ MongoMapper.expects(:connect).with('development', :logger => logger)
76
+ MongoMapper.expects(:handle_passenger_forking).never
77
+ MongoMapper.setup(config, 'development', :logger => logger)
78
+ end
79
+
80
+ should "handle passenger if option present" do
81
+ config, logger = mock('config'), mock('logger')
82
+ MongoMapper.expects(:config=).with(config)
83
+ MongoMapper.expects(:connect).with('development', :logger => logger)
84
+ MongoMapper.expects(:handle_passenger_forking)
85
+ MongoMapper.setup(config, 'development', :logger => logger, :passenger => true)
86
+ end
87
+ end
88
+
89
+
90
+ context "use_time_zone?" do
91
+ should "be true if Time.zone set" do
92
+ Time.zone = 'Hawaii'
93
+ MongoMapper.use_time_zone?.should be_true
94
+ Time.zone = nil
95
+ end
96
+
97
+ should "be false if Time.zone not set" do
98
+ MongoMapper.use_time_zone?.should be_false
99
+ end
100
+ end
101
+
102
+ context "time_class" do
103
+ should "be Time.zone if using time zones" do
104
+ Time.zone = 'Hawaii'
105
+ MongoMapper.time_class.should == Time.zone
106
+ Time.zone = nil
107
+ end
108
+
109
+ should "be Time if not using time zones" do
110
+ MongoMapper.time_class.should == Time
111
+ end
112
+ end
113
+
114
+ context "normalize_object_id" do
115
+ should "turn string into object id" do
116
+ id = Mongo::ObjectID.new
117
+ MongoMapper.normalize_object_id(id.to_s).should == id
118
+ end
119
+
120
+ should "leave object id alone" do
121
+ id = Mongo::ObjectID.new
122
+ MongoMapper.normalize_object_id(id).should == id
123
+ end
124
+ end
125
+ 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