mongo_mapper-unstable 2010.3.8 → 2010.06.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/README.rdoc +4 -8
  2. data/bin/mmconsole +1 -1
  3. data/examples/keys.rb +37 -0
  4. data/examples/plugins.rb +41 -0
  5. data/examples/querying.rb +35 -0
  6. data/examples/scopes.rb +52 -0
  7. data/lib/mongo_mapper/connection.rb +83 -0
  8. data/lib/mongo_mapper/document.rb +11 -329
  9. data/lib/mongo_mapper/embedded_document.rb +9 -38
  10. data/lib/mongo_mapper/exceptions.rb +30 -0
  11. data/lib/mongo_mapper/extensions/array.rb +19 -0
  12. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  13. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  14. data/lib/mongo_mapper/extensions/date.rb +25 -0
  15. data/lib/mongo_mapper/extensions/float.rb +14 -0
  16. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  17. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  18. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  19. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  20. data/lib/mongo_mapper/extensions/object.rb +27 -0
  21. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  22. data/lib/mongo_mapper/extensions/set.rb +20 -0
  23. data/lib/mongo_mapper/extensions/string.rb +18 -0
  24. data/lib/mongo_mapper/extensions/time.rb +29 -0
  25. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  26. data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
  27. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
  28. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
  29. data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
  30. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
  31. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
  32. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
  33. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
  34. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
  35. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
  36. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
  37. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  38. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
  39. data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
  40. data/lib/mongo_mapper/plugins/associations.rb +14 -22
  41. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  42. data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
  43. data/lib/mongo_mapper/plugins/clone.rb +10 -4
  44. data/lib/mongo_mapper/plugins/descendants.rb +3 -2
  45. data/lib/mongo_mapper/plugins/dirty.rb +1 -0
  46. data/lib/mongo_mapper/plugins/document.rb +41 -0
  47. data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
  48. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  49. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  50. data/lib/mongo_mapper/plugins/equality.rb +4 -10
  51. data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
  52. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  53. data/lib/mongo_mapper/plugins/inspect.rb +1 -0
  54. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  55. data/lib/mongo_mapper/plugins/keys.rb +85 -110
  56. data/lib/mongo_mapper/plugins/logger.rb +1 -0
  57. data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
  58. data/lib/mongo_mapper/plugins/pagination.rb +5 -15
  59. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  60. data/lib/mongo_mapper/plugins/protected.rb +9 -1
  61. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  62. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  63. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  64. data/lib/mongo_mapper/plugins/rails.rb +6 -1
  65. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  66. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  67. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  68. data/lib/mongo_mapper/plugins/serialization.rb +5 -4
  69. data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
  70. data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
  71. data/lib/mongo_mapper/plugins/validations.rb +9 -5
  72. data/lib/mongo_mapper/plugins.rb +1 -20
  73. data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
  74. data/lib/mongo_mapper/version.rb +4 -0
  75. data/lib/mongo_mapper.rb +71 -128
  76. data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
  77. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
  78. data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
  79. data/test/functional/associations/test_in_array_proxy.rb +7 -9
  80. data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
  81. data/test/functional/associations/test_many_documents_proxy.rb +186 -64
  82. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
  83. data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
  84. data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
  85. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  86. data/test/functional/associations/test_one_proxy.rb +70 -49
  87. data/test/functional/test_accessible.rb +168 -0
  88. data/test/functional/test_associations.rb +11 -11
  89. data/test/functional/test_binary.rb +5 -5
  90. data/test/functional/test_caching.rb +76 -0
  91. data/test/functional/test_callbacks.rb +104 -34
  92. data/test/functional/test_dirty.rb +16 -16
  93. data/test/functional/test_document.rb +12 -924
  94. data/test/functional/test_dynamic_querying.rb +75 -0
  95. data/test/functional/test_embedded_document.rb +88 -8
  96. data/test/functional/test_identity_map.rb +41 -43
  97. data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
  98. data/test/functional/test_logger.rb +1 -1
  99. data/test/functional/test_modifiers.rb +275 -181
  100. data/test/functional/test_pagination.rb +13 -15
  101. data/test/functional/test_protected.rb +25 -11
  102. data/test/functional/test_querying.rb +873 -0
  103. data/test/functional/test_safe.rb +76 -0
  104. data/test/functional/test_sci.rb +230 -0
  105. data/test/functional/test_scopes.rb +171 -0
  106. data/test/functional/test_string_id_compatibility.rb +11 -11
  107. data/test/functional/test_timestamps.rb +0 -2
  108. data/test/functional/test_userstamps.rb +0 -1
  109. data/test/functional/test_validations.rb +44 -31
  110. data/test/models.rb +18 -17
  111. data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
  112. data/test/test_helper.rb +59 -16
  113. data/test/unit/associations/test_base.rb +47 -42
  114. data/test/unit/associations/test_proxy.rb +15 -15
  115. data/test/unit/serializers/test_json_serializer.rb +29 -29
  116. data/test/unit/test_clone.rb +69 -0
  117. data/test/unit/test_descendant_appends.rb +3 -3
  118. data/test/unit/test_document.rb +49 -67
  119. data/test/unit/test_dynamic_finder.rb +53 -51
  120. data/test/unit/test_embedded_document.rb +19 -38
  121. data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
  122. data/test/unit/test_key.rb +185 -0
  123. data/test/unit/test_keys.rb +29 -147
  124. data/test/unit/test_mongo_mapper.rb +3 -48
  125. data/test/unit/test_pagination.rb +1 -150
  126. data/test/unit/test_rails.rb +77 -19
  127. data/test/unit/test_rails_compatibility.rb +12 -12
  128. data/test/unit/test_serialization.rb +5 -5
  129. data/test/unit/test_time_zones.rb +9 -9
  130. data/test/unit/test_validations.rb +46 -46
  131. metadata +157 -155
  132. data/.gitignore +0 -10
  133. data/Rakefile +0 -55
  134. data/VERSION +0 -1
  135. data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
  136. data/lib/mongo_mapper/query.rb +0 -130
  137. data/lib/mongo_mapper/support.rb +0 -215
  138. data/mongo_mapper.gemspec +0 -196
  139. data/performance/read_write.rb +0 -52
  140. data/specs.watchr +0 -51
  141. data/test/support/custom_matchers.rb +0 -55
  142. data/test/support/timing.rb +0 -16
  143. data/test/unit/test_query.rb +0 -340
@@ -1,173 +1,55 @@
1
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
2
+ require 'models'
21
3
 
22
4
  class KeyTest < Test::Unit::TestCase
23
5
  include MongoMapper::Plugins::Keys
24
6
 
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)
7
+ context ".key?(:symbol)" do
8
+ should "be true if document has key" do
9
+ Address.key?(:city).should be_true
44
10
  end
45
11
 
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
12
+ should "be false if document does not have key" do
13
+ Address.key?(:foo).should be_false
68
14
  end
69
15
  end
70
16
 
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)
17
+ context ".key?('string')" do
18
+ should "be true if document has key" do
19
+ Address.key?('city').should be_true
74
20
  end
75
21
 
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
22
+ should "be false if document does not have key" do
23
+ Address.key?('foo').should be_false
99
24
  end
100
25
  end
101
26
 
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
27
+ context ".new (from database)" do
28
+ setup do
29
+ @klass = Doc do
30
+ key :user, Hash
120
31
 
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
32
+ def user=(user)
33
+ super(:id => user.id, :name => user.name)
34
+ end
35
+ end
126
36
 
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 == {}
37
+ user_class = Struct.new(:id, :name)
38
+ @klass.create(:user => user_class.new(1, 'John Nunemaker'))
132
39
  end
133
40
 
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
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'
144
46
  end
145
47
  end
146
48
  end
147
49
 
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
-
169
- should "work with procs" do
170
- Key.new(:foo, String, :default => lambda { return 'hello world' }).get(nil).should == "hello world"
50
+ context ".load" do
51
+ should "return nil if argument is nil" do
52
+ Doc().load(nil).should be_nil
171
53
  end
172
54
  end
173
55
  end # KeyTest
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class Address; end
4
4
 
5
- class MongoMapperTest < Test::Unit::TestCase
5
+ class MongoMapperTest < Test::Unit::TestCase
6
6
  should "be able to write and read connection" do
7
7
  conn = Mongo::Connection.new
8
8
  MongoMapper.connection = conn
@@ -89,7 +89,7 @@ class MongoMapperTest < Test::Unit::TestCase
89
89
  Mongo::DB.any_instance.expects(:authenticate).with('john', 'secret')
90
90
  MongoMapper.connect('development')
91
91
  end
92
-
92
+
93
93
  should "raise error for invalid scheme" do
94
94
  MongoMapper.config = {
95
95
  'development' => {'uri' => 'mysql://127.0.0.1:5336/foo'}
@@ -103,53 +103,8 @@ class MongoMapperTest < Test::Unit::TestCase
103
103
  config, logger = mock('config'), mock('logger')
104
104
  MongoMapper.expects(:config=).with(config)
105
105
  MongoMapper.expects(:connect).with('development', :logger => logger)
106
- MongoMapper.expects(:handle_passenger_forking).never
106
+ MongoMapper.expects(:handle_passenger_forking).once
107
107
  MongoMapper.setup(config, 'development', :logger => logger)
108
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
-
119
-
120
- context "use_time_zone?" do
121
- should "be true if Time.zone set" do
122
- Time.zone = 'Hawaii'
123
- MongoMapper.use_time_zone?.should be_true
124
- Time.zone = nil
125
- end
126
-
127
- should "be false if Time.zone not set" do
128
- MongoMapper.use_time_zone?.should be_false
129
- end
130
- end
131
-
132
- context "time_class" do
133
- should "be Time.zone if using time zones" do
134
- Time.zone = 'Hawaii'
135
- MongoMapper.time_class.should == Time.zone
136
- Time.zone = nil
137
- end
138
-
139
- should "be Time if not using time zones" do
140
- MongoMapper.time_class.should == Time
141
- end
142
- end
143
-
144
- context "normalize_object_id" do
145
- should "turn string into object id" do
146
- id = Mongo::ObjectID.new
147
- MongoMapper.normalize_object_id(id.to_s).should == id
148
- end
149
-
150
- should "leave object id alone" do
151
- id = Mongo::ObjectID.new
152
- MongoMapper.normalize_object_id(id).should == id
153
- end
154
109
  end
155
110
  end
@@ -4,157 +4,8 @@ class PaginationTest < Test::Unit::TestCase
4
4
  should "default per_page to 25" do
5
5
  Doc().per_page.should == 25
6
6
  end
7
-
7
+
8
8
  should "allow overriding per_page" do
9
9
  Doc() { def self.per_page; 1 end }.per_page.should == 1
10
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
11
  end
@@ -7,7 +7,7 @@ class TestRails < Test::Unit::TestCase
7
7
  key :foo, String
8
8
  end
9
9
  end
10
-
10
+
11
11
  context "Class methods" do
12
12
  should "alias has_many to many" do
13
13
  @klass.should respond_to(:has_many)
@@ -16,7 +16,7 @@ class TestRails < Test::Unit::TestCase
16
16
  should "alias has_one to one" do
17
17
  @klass.should respond_to(:has_one)
18
18
  end
19
-
19
+
20
20
  should "have column names" do
21
21
  @klass.column_names.sort.should == ['_id', 'foo']
22
22
  end
@@ -25,48 +25,76 @@ class TestRails < Test::Unit::TestCase
25
25
  @klass.human_name.should == 'Post'
26
26
  end
27
27
  end
28
-
28
+
29
29
  context "Instance methods" do
30
30
  setup do
31
31
  @klass.class_eval do
32
32
  def bar=(value)
33
33
  write_attribute(:foo, value)
34
34
  end
35
-
35
+
36
36
  def bar_before_typecast
37
37
  read_attribute_before_typecast(:foo)
38
38
  end
39
-
39
+
40
40
  def bar
41
41
  read_attribute(:foo)
42
42
  end
43
43
  end
44
44
  end
45
-
45
+
46
46
  should "alias new_record? to new?" do
47
47
  @klass.new.should be_new_record
48
48
  end
49
-
49
+
50
50
  should "be able to read key with read_attribute" do
51
51
  @klass.new(:foo => 'Bar').bar.should == 'Bar'
52
52
  end
53
-
53
+
54
54
  should "be able to read key before typecast with read_attribute_before_typecast" do
55
55
  @klass.new(:foo => 21).bar_before_typecast.should == 21
56
56
  @klass.new(:foo => 21).bar.should == '21'
57
57
  end
58
-
58
+
59
59
  should "be able to write key with write_attribute" do
60
60
  @klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
61
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
62
90
  end
63
91
  end
64
-
92
+
65
93
  context "EmbeddedDocument" do
66
94
  setup do
67
95
  @klass = EDoc('Post') { key :foo, String }
68
96
  end
69
-
97
+
70
98
  context "Class methods" do
71
99
  should "alias has_many to many" do
72
100
  @klass.should respond_to(:has_many)
@@ -75,7 +103,7 @@ class TestRails < Test::Unit::TestCase
75
103
  should "alias has_one to one" do
76
104
  @klass.should respond_to(:has_one)
77
105
  end
78
-
106
+
79
107
  should "have column names" do
80
108
  @klass.column_names.sort.should == ['_id', 'foo']
81
109
  end
@@ -84,40 +112,70 @@ class TestRails < Test::Unit::TestCase
84
112
  @klass.human_name.should == 'Post'
85
113
  end
86
114
  end
87
-
115
+
88
116
  context "Instance methods" do
89
117
  setup do
90
118
  @klass.class_eval do
91
119
  def bar=(value)
92
120
  write_attribute(:foo, value)
93
121
  end
94
-
122
+
95
123
  def bar_before_typecast
96
124
  read_attribute_before_typecast(:foo)
97
125
  end
98
-
126
+
99
127
  def bar
100
128
  read_attribute(:foo)
101
129
  end
102
130
  end
103
131
  end
104
-
132
+
105
133
  should "alias new_record? to new?" do
106
134
  @klass.new.should be_new_record
107
135
  end
108
-
136
+
109
137
  should "be able to read key with read_attribute" do
110
138
  @klass.new(:foo => 'Bar').bar.should == 'Bar'
111
139
  end
112
-
140
+
113
141
  should "be able to read key before typecast with read_attribute_before_typecast" do
114
142
  @klass.new(:foo => 21).bar_before_typecast.should == 21
115
143
  @klass.new(:foo => 21).bar.should == '21'
116
144
  end
117
-
145
+
118
146
  should "be able to write key with write_attribute" do
119
147
  @klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
120
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
121
179
  end
122
180
  end
123
181
  end
@@ -4,13 +4,13 @@ class TestRailsCompatibility < Test::Unit::TestCase
4
4
  class BigStuff
5
5
  include MongoMapper::Document
6
6
  end
7
-
7
+
8
8
  class Item
9
9
  include MongoMapper::EmbeddedDocument
10
10
  key :for_all, String
11
11
  end
12
12
 
13
- class FirstItem < Item
13
+ class FirstItem < Item
14
14
  key :first_only, String
15
15
  many :second_items
16
16
  end
@@ -18,32 +18,32 @@ class TestRailsCompatibility < Test::Unit::TestCase
18
18
  class SecondItem < Item
19
19
  key :second_only, String
20
20
  end
21
-
22
- context "EmbeddedDocument" do
21
+
22
+ context "EmbeddedDocument" do
23
23
  should "alias many to has_many" do
24
24
  FirstItem.should respond_to(:has_many)
25
25
  end
26
-
26
+
27
27
  should "alias one to has_one" do
28
28
  FirstItem.should respond_to(:has_one)
29
29
  end
30
-
30
+
31
31
  should "have column names" do
32
- Item.column_names.sort.should == ['_id', 'for_all']
33
- FirstItem.column_names.sort.should == ['_id', 'first_only', 'for_all']
34
- SecondItem.column_names.sort.should == ['_id', 'for_all', 'second_only']
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
35
  end
36
-
36
+
37
37
  should "alias new to new_record?" do
38
38
  instance = Item.new
39
39
  instance.new_record?.should == instance.new?
40
40
  end
41
-
41
+
42
42
  should "implement human_name" do
43
43
  Item.human_name.should == 'Item'
44
44
  end
45
45
  end
46
-
46
+
47
47
  context "Document" do
48
48
  should "implement human_name" do
49
49
  BigStuff.human_name.should == 'Big Stuff'