jonbell-mongo_mapper 0.8.6
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/UPGRADES +7 -0
- data/bin/mmconsole +60 -0
- data/examples/attr_accessible.rb +22 -0
- data/examples/attr_protected.rb +22 -0
- data/examples/cache_key.rb +24 -0
- data/examples/custom_types.rb +24 -0
- data/examples/identity_map.rb +33 -0
- data/examples/identity_map/automatic.rb +8 -0
- data/examples/identity_map/middleware.rb +14 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/safe.rb +43 -0
- data/examples/scopes.rb +52 -0
- data/examples/validating/embedded_docs.rb +29 -0
- data/lib/mongo_mapper.rb +79 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +41 -0
- data/lib/mongo_mapper/embedded_document.rb +31 -0
- data/lib/mongo_mapper/exceptions.rb +27 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins.rb +15 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations.rb +97 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +109 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +139 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
- data/lib/mongo_mapper/plugins/clone.rb +22 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +124 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
- data/lib/mongo_mapper/plugins/equality.rb +17 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +313 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +59 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
- data/lib/mongo_mapper/plugins/pagination.rb +14 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +53 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +76 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
- data/lib/mongo_mapper/plugins/validations.rb +50 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
- data/lib/mongo_mapper/version.rb +4 -0
- data/rails/init.rb +19 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
- data/test/functional/associations/test_in_array_proxy.rb +319 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +615 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +81 -0
- data/test/functional/associations/test_one_proxy.rb +181 -0
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +151 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +253 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +210 -0
- data/test/functional/test_identity_map.rb +514 -0
- data/test/functional/test_indexes.rb +42 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +416 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +175 -0
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_userstamps.rb +27 -0
- data/test/functional/test_validations.rb +342 -0
- data/test/models.rb +233 -0
- data/test/test_active_model_lint.rb +13 -0
- data/test/test_helper.rb +100 -0
- data/test/unit/associations/test_base.rb +212 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +217 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +213 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +644 -0
- data/test/unit/test_extensions.rb +376 -0
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +89 -0
- data/test/unit/test_mongo_mapper.rb +110 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +181 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +564 -0
- metadata +337 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class KeyTest < Test::Unit::TestCase
|
5
|
+
include MongoMapper::Plugins::Keys
|
6
|
+
|
7
|
+
context ".new with no id and _id of type integer" do
|
8
|
+
should "not error" do
|
9
|
+
lambda {
|
10
|
+
klass = Doc() do
|
11
|
+
key :_id, Integer
|
12
|
+
end
|
13
|
+
# No sensible default id for integer, people better pass them in if they user this
|
14
|
+
klass.new.id.should be_nil
|
15
|
+
}.should_not raise_error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context ".new with no id and _id of type string" do
|
20
|
+
should "not error" do
|
21
|
+
lambda {
|
22
|
+
klass = Doc() do
|
23
|
+
key :_id, String
|
24
|
+
end
|
25
|
+
klass.new.id.should_not be_nil
|
26
|
+
}.should_not raise_error
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context ".new with no id and _id of type object id" do
|
31
|
+
should "not error" do
|
32
|
+
lambda {
|
33
|
+
klass = Doc() do
|
34
|
+
key :_id, ObjectId
|
35
|
+
end
|
36
|
+
klass.new.should_not be_nil
|
37
|
+
}.should_not raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context ".key?(:symbol)" do
|
42
|
+
should "be true if document has key" do
|
43
|
+
Address.key?(:city).should be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
should "be false if document does not have key" do
|
47
|
+
Address.key?(:foo).should be_false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context ".key?('string')" do
|
52
|
+
should "be true if document has key" do
|
53
|
+
Address.key?('city').should be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
should "be false if document does not have key" do
|
57
|
+
Address.key?('foo').should be_false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context ".new (from database)" do
|
62
|
+
setup do
|
63
|
+
@klass = Doc do
|
64
|
+
key :user, Hash
|
65
|
+
|
66
|
+
def user=(user)
|
67
|
+
super(:id => user.id, :name => user.name)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
user_class = Struct.new(:id, :name)
|
72
|
+
@klass.create(:user => user_class.new(1, 'John Nunemaker'))
|
73
|
+
end
|
74
|
+
|
75
|
+
should "use []= for keys instead of public writer" do
|
76
|
+
assert_nothing_raised do
|
77
|
+
doc = @klass.first
|
78
|
+
doc.user['id'].should == 1
|
79
|
+
doc.user['name'].should == 'John Nunemaker'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context ".load" do
|
85
|
+
should "return nil if argument is nil" do
|
86
|
+
Doc().load(nil).should be_nil
|
87
|
+
end
|
88
|
+
end
|
89
|
+
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,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
|