mongo_mapper 0.12.0 → 0.13.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +35 -13
- data/bin/mmconsole +1 -1
- data/lib/mongo_mapper.rb +4 -0
- data/lib/mongo_mapper/connection.rb +17 -6
- data/lib/mongo_mapper/document.rb +1 -0
- data/lib/mongo_mapper/exceptions.rb +4 -1
- data/lib/mongo_mapper/extensions/binary.rb +1 -1
- data/lib/mongo_mapper/extensions/boolean.rb +20 -23
- data/lib/mongo_mapper/extensions/date.rb +3 -3
- data/lib/mongo_mapper/extensions/integer.rb +5 -1
- data/lib/mongo_mapper/extensions/kernel.rb +2 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +23 -0
- data/lib/mongo_mapper/extensions/string.rb +2 -2
- data/lib/mongo_mapper/extensions/time.rb +7 -5
- data/lib/mongo_mapper/middleware/identity_map.rb +3 -4
- data/lib/mongo_mapper/plugins.rb +1 -1
- data/lib/mongo_mapper/plugins/associations.rb +11 -5
- data/lib/mongo_mapper/plugins/associations/base.rb +5 -3
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +0 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +8 -8
- data/lib/mongo_mapper/plugins/associations/collection.rb +2 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +32 -7
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +2 -2
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +12 -12
- data/lib/mongo_mapper/plugins/associations/proxy.rb +5 -1
- data/lib/mongo_mapper/plugins/associations/single_association.rb +6 -6
- data/lib/mongo_mapper/plugins/clone.rb +4 -2
- data/lib/mongo_mapper/plugins/dirty.rb +22 -21
- data/lib/mongo_mapper/plugins/document.rb +4 -4
- data/lib/mongo_mapper/plugins/dumpable.rb +22 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +58 -9
- data/lib/mongo_mapper/plugins/identity_map.rb +42 -32
- data/lib/mongo_mapper/plugins/keys.rb +133 -54
- data/lib/mongo_mapper/plugins/keys/key.rb +68 -22
- data/lib/mongo_mapper/plugins/modifiers.rb +26 -19
- data/lib/mongo_mapper/plugins/persistence.rb +15 -5
- data/lib/mongo_mapper/plugins/querying.rb +15 -40
- data/lib/mongo_mapper/plugins/querying/{decorator.rb → decorated_plucky_query.rb} +24 -4
- data/lib/mongo_mapper/plugins/rails.rb +22 -2
- data/lib/mongo_mapper/plugins/safe.rb +8 -5
- data/lib/mongo_mapper/plugins/sci.rb +26 -4
- data/lib/mongo_mapper/plugins/scopes.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +11 -4
- data/lib/mongo_mapper/plugins/validations.rb +1 -1
- data/lib/mongo_mapper/utils.rb +12 -0
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +20 -7
- data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +6 -0
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +18 -1
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +9 -5
- data/{test/functional/test_accessible.rb → spec/functional/accessible_spec.rb} +29 -29
- data/{test/functional/associations/test_belongs_to_polymorphic_proxy.rb → spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_belongs_to_proxy.rb → spec/functional/associations/belongs_to_proxy_spec.rb} +82 -64
- data/{test/functional/associations/test_in_array_proxy.rb → spec/functional/associations/in_array_proxy_spec.rb} +68 -68
- data/{test/functional/associations/test_many_documents_as_proxy.rb → spec/functional/associations/many_documents_as_proxy_spec.rb} +37 -38
- data/{test/functional/associations/test_many_documents_proxy.rb → spec/functional/associations/many_documents_proxy_spec.rb} +233 -146
- data/{test/functional/associations/test_many_embedded_polymorphic_proxy.rb → spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb} +19 -20
- data/{test/functional/associations/test_many_embedded_proxy.rb → spec/functional/associations/many_embedded_proxy_spec.rb} +23 -24
- data/{test/functional/associations/test_many_polymorphic_proxy.rb → spec/functional/associations/many_polymorphic_proxy_spec.rb} +45 -46
- data/{test/functional/associations/test_one_as_proxy.rb → spec/functional/associations/one_as_proxy_spec.rb} +75 -77
- data/{test/functional/associations/test_one_embedded_polymorphic_proxy.rb → spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb} +31 -32
- data/{test/functional/associations/test_one_embedded_proxy.rb → spec/functional/associations/one_embedded_proxy_spec.rb} +10 -10
- data/{test/functional/associations/test_one_proxy.rb → spec/functional/associations/one_proxy_spec.rb} +125 -102
- data/spec/functional/associations_spec.rb +48 -0
- data/{test/functional/test_binary.rb → spec/functional/binary_spec.rb} +6 -6
- data/spec/functional/caching_spec.rb +75 -0
- data/{test/functional/test_callbacks.rb → spec/functional/callbacks_spec.rb} +84 -26
- data/{test/functional/test_dirty.rb → spec/functional/dirty_spec.rb} +57 -42
- data/{test/functional/test_document.rb → spec/functional/document_spec.rb} +52 -52
- data/spec/functional/dumpable_spec.rb +24 -0
- data/{test/functional/test_dynamic_querying.rb → spec/functional/dynamic_querying_spec.rb} +14 -14
- data/{test/functional/test_embedded_document.rb → spec/functional/embedded_document_spec.rb} +51 -42
- data/{test/functional/test_equality.rb → spec/functional/equality_spec.rb} +4 -4
- data/spec/functional/extensions_spec.rb +16 -0
- data/{test/functional/test_identity_map.rb → spec/functional/identity_map_spec.rb} +73 -61
- data/spec/functional/indexes_spec.rb +48 -0
- data/spec/functional/keys_spec.rb +224 -0
- data/{test/functional/test_logger.rb → spec/functional/logger_spec.rb} +6 -6
- data/spec/functional/modifiers_spec.rb +550 -0
- data/spec/functional/pagination_spec.rb +89 -0
- data/spec/functional/protected_spec.rb +199 -0
- data/spec/functional/querying_spec.rb +1003 -0
- data/spec/functional/rails_spec.rb +55 -0
- data/spec/functional/safe_spec.rb +163 -0
- data/{test/functional/test_sci.rb → spec/functional/sci_spec.rb} +123 -34
- data/{test/functional/test_scopes.rb → spec/functional/scopes_spec.rb} +59 -26
- data/spec/functional/timestamps_spec.rb +97 -0
- data/{test/functional/test_touch.rb → spec/functional/touch_spec.rb} +13 -13
- data/spec/functional/userstamps_spec.rb +46 -0
- data/{test/functional/test_validations.rb → spec/functional/validations_spec.rb} +64 -64
- data/spec/spec_helper.rb +81 -0
- data/spec/support/matchers.rb +24 -0
- data/{test → spec/support}/models.rb +1 -6
- data/spec/unit/associations/base_spec.rb +146 -0
- data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
- data/spec/unit/associations/many_association_spec.rb +64 -0
- data/spec/unit/associations/one_association_spec.rb +48 -0
- data/{test/unit/associations/test_proxy.rb → spec/unit/associations/proxy_spec.rb} +21 -21
- data/{test/unit/test_clone.rb → spec/unit/clone_spec.rb} +21 -11
- data/spec/unit/config_generator_spec.rb +24 -0
- data/{test/unit/test_document.rb → spec/unit/document_spec.rb} +42 -42
- data/{test/unit/test_dynamic_finder.rb → spec/unit/dynamic_finder_spec.rb} +28 -28
- data/{test/unit/test_embedded_document.rb → spec/unit/embedded_document_spec.rb} +102 -108
- data/{test/unit/test_equality.rb → spec/unit/equality_spec.rb} +7 -7
- data/{test/unit/test_exceptions.rb → spec/unit/exceptions_spec.rb} +3 -3
- data/{test/unit/test_extensions.rb → spec/unit/extensions_spec.rb} +85 -71
- data/spec/unit/identity_map_middleware_spec.rb +134 -0
- data/{test/unit/test_inspect.rb → spec/unit/inspect_spec.rb} +8 -8
- data/{test/unit/test_key.rb → spec/unit/key_spec.rb} +82 -52
- data/spec/unit/keys_spec.rb +155 -0
- data/spec/unit/model_generator_spec.rb +47 -0
- data/spec/unit/mongo_mapper_spec.rb +184 -0
- data/spec/unit/pagination_spec.rb +11 -0
- data/{test/unit/test_plugins.rb → spec/unit/plugins_spec.rb} +14 -14
- data/spec/unit/rails_compatibility_spec.rb +40 -0
- data/{test/unit/test_rails_reflect_on_association.rb → spec/unit/rails_reflect_on_association_spec.rb} +9 -9
- data/{test/unit/test_rails.rb → spec/unit/rails_spec.rb} +31 -31
- data/spec/unit/serialization_spec.rb +169 -0
- data/spec/unit/serializers/json_serializer_spec.rb +218 -0
- data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
- data/{test/unit/test_time_zones.rb → spec/unit/time_zones_spec.rb} +8 -8
- data/{test/unit/test_translation.rb → spec/unit/translation_spec.rb} +6 -6
- data/{test/unit/test_validations.rb → spec/unit/validations_spec.rb} +72 -59
- metadata +199 -179
- data/test/_NOTE_ON_TESTING +0 -1
- data/test/functional/test_associations.rb +0 -46
- data/test/functional/test_caching.rb +0 -77
- data/test/functional/test_indexes.rb +0 -50
- data/test/functional/test_modifiers.rb +0 -537
- data/test/functional/test_pagination.rb +0 -91
- data/test/functional/test_protected.rb +0 -201
- data/test/functional/test_querying.rb +0 -935
- data/test/functional/test_safe.rb +0 -76
- data/test/functional/test_timestamps.rb +0 -62
- data/test/functional/test_userstamps.rb +0 -44
- data/test/support/railtie.rb +0 -4
- data/test/support/railtie/autoloaded.rb +0 -2
- data/test/support/railtie/not_autoloaded.rb +0 -3
- data/test/support/railtie/parent.rb +0 -3
- data/test/test_active_model_lint.rb +0 -18
- data/test/test_helper.rb +0 -93
- data/test/unit/associations/test_base.rb +0 -146
- data/test/unit/associations/test_belongs_to_association.rb +0 -29
- data/test/unit/associations/test_many_association.rb +0 -63
- data/test/unit/associations/test_one_association.rb +0 -47
- data/test/unit/serializers/test_json_serializer.rb +0 -216
- data/test/unit/serializers/test_xml_serializer.rb +0 -196
- data/test/unit/test_identity_map_middleware.rb +0 -132
- data/test/unit/test_keys.rb +0 -65
- data/test/unit/test_mongo_mapper.rb +0 -157
- data/test/unit/test_pagination.rb +0 -11
- data/test/unit/test_rails_compatibility.rb +0 -38
- data/test/unit/test_serialization.rb +0 -166
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
$:.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'fileutils'
|
6
|
+
require 'timecop'
|
7
|
+
require "generator_spec/test_case"
|
8
|
+
|
9
|
+
if RUBY_PLATFORM != "java"
|
10
|
+
if ENV['TRAVIS']
|
11
|
+
require 'coveralls'
|
12
|
+
Coveralls.wear!
|
13
|
+
elsif ENV['COVERAGE'] && RUBY_VERSION > "1.8"
|
14
|
+
require 'simplecov'
|
15
|
+
SimpleCov.start do
|
16
|
+
add_filter 'spec'
|
17
|
+
add_group 'Core', 'lib/mongo_mapper'
|
18
|
+
add_group 'Rails', 'lib/rails'
|
19
|
+
add_group 'Extensions', 'lib/mongo_mapper/extensions'
|
20
|
+
add_group 'Plugins', 'lib/mongo_mapper/plugins'
|
21
|
+
add_group 'Associations', 'lib/mongo_mapper/plugins/associations'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'mongo_mapper'
|
27
|
+
|
28
|
+
def Doc(name='Class', &block)
|
29
|
+
klass = Class.new
|
30
|
+
klass.class_eval do
|
31
|
+
include MongoMapper::Document
|
32
|
+
|
33
|
+
if name
|
34
|
+
class_eval "def self.name; '#{name}' end"
|
35
|
+
class_eval "def self.to_s; '#{name}' end"
|
36
|
+
else
|
37
|
+
set_collection_name :test
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
klass.class_eval(&block) if block_given?
|
42
|
+
klass.collection.remove
|
43
|
+
klass
|
44
|
+
end
|
45
|
+
|
46
|
+
def EDoc(name='Class', &block)
|
47
|
+
klass = Class.new do
|
48
|
+
include MongoMapper::EmbeddedDocument
|
49
|
+
|
50
|
+
if name
|
51
|
+
class_eval "def self.name; '#{name}' end"
|
52
|
+
class_eval "def self.to_s; '#{name}' end"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
klass.class_eval(&block) if block_given?
|
57
|
+
klass
|
58
|
+
end
|
59
|
+
|
60
|
+
def drop_indexes(klass)
|
61
|
+
klass.collection.drop_indexes if klass.database.collection_names.include?(klass.collection.name)
|
62
|
+
end
|
63
|
+
|
64
|
+
log_dir = File.expand_path('../../log', __FILE__)
|
65
|
+
FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
|
66
|
+
logger = Logger.new(log_dir + '/test.log')
|
67
|
+
|
68
|
+
MongoMapper.connection = Mongo::MongoClient.new('127.0.0.1', 27017, :logger => logger)
|
69
|
+
MongoMapper.database = "test"
|
70
|
+
MongoMapper.database.collections.each { |c| c.drop_indexes }
|
71
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
|
72
|
+
|
73
|
+
RSpec.configure do |c|
|
74
|
+
c.treat_symbols_as_metadata_keys_with_true_values = true
|
75
|
+
c.around(:each, :without_connection) do |example|
|
76
|
+
old, MongoMapper.connection = MongoMapper.connection, nil
|
77
|
+
example.run
|
78
|
+
MongoMapper.connection = old
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
RSpec::Matchers.define :have_error_on do |*args|
|
2
|
+
@message = nil
|
3
|
+
@attributes = [args]
|
4
|
+
|
5
|
+
chain :or do |*args|
|
6
|
+
@attributes << args
|
7
|
+
end
|
8
|
+
|
9
|
+
match do |model|
|
10
|
+
model.valid?
|
11
|
+
@has_errors = @attributes.detect {|attribute| model.errors[attribute[0]].present? }
|
12
|
+
if @message
|
13
|
+
!!@has_errors && model.errors[@has_errors[0]].include?(@has_errors[1])
|
14
|
+
else
|
15
|
+
!!@has_errors
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec::Matchers.define :have_index do |index_name|
|
21
|
+
match do |model|
|
22
|
+
model.collection.index_information.detect { |index| index[0] == index_name }.present?
|
23
|
+
end
|
24
|
+
end
|
@@ -125,12 +125,7 @@ class Project
|
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
128
|
-
many :addresses
|
129
|
-
def find_all_by_state(state)
|
130
|
-
# can't use select here for some reason
|
131
|
-
find_all { |a| a.state == state }
|
132
|
-
end
|
133
|
-
end
|
128
|
+
many :addresses
|
134
129
|
end
|
135
130
|
|
136
131
|
class Collaborator
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FooMonster; end
|
4
|
+
module AssociationSpec
|
5
|
+
include MongoMapper::Plugins::Associations
|
6
|
+
|
7
|
+
describe MongoMapper::Plugins::Associations::Base do
|
8
|
+
it "should initialize with type and name" do
|
9
|
+
base = ManyAssociation.new(:foos)
|
10
|
+
base.name.should == :foos
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should also allow options when initializing" do
|
14
|
+
base = ManyAssociation.new(:foos, :polymorphic => true)
|
15
|
+
base.options[:polymorphic].should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
context "klass" do
|
19
|
+
it "should default to class_name constantized" do
|
20
|
+
BelongsToAssociation.new(:foo_monster).klass.should == FooMonster
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be the specified class" do
|
24
|
+
anonnymous_class = Class.new
|
25
|
+
BelongsToAssociation.new(:foo_monster, :class => anonnymous_class).klass.should == anonnymous_class
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "polymorphic?" do
|
30
|
+
it "should be true if polymorphic" do
|
31
|
+
ManyAssociation.new(:foos, :polymorphic => true).polymorphic?.should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be false if not polymorphic" do
|
35
|
+
ManyAssociation.new(:bars).polymorphic?.should be_false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "as?" do
|
40
|
+
it "should be true if one" do
|
41
|
+
OneAssociation.new(:foo, :as => :commentable).as?.should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should be false if not one" do
|
45
|
+
ManyAssociation.new(:foo).as?.should be_false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "in_array?" do
|
50
|
+
it "should be true if one" do
|
51
|
+
OneAssociation.new(:foo, :in => :list_ids).in_array?.should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should be false if not one" do
|
55
|
+
ManyAssociation.new(:foo).in_array?.should be_false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "query_options" do
|
60
|
+
it "should default to empty hash" do
|
61
|
+
base = ManyAssociation.new(:foos)
|
62
|
+
base.query_options.should == {}
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should work with order" do
|
66
|
+
base = ManyAssociation.new(:foos, :order => 'position')
|
67
|
+
base.query_options.should == {:order => 'position'}
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should correctly parse from options" do
|
71
|
+
base = ManyAssociation.new(:foos, :order => 'position', :somekey => 'somevalue')
|
72
|
+
base.query_options.should == {:order => 'position', :somekey => 'somevalue'}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "type_key_name" do
|
77
|
+
it "should be association name _ type for belongs_to" do
|
78
|
+
BelongsToAssociation.new(:foo).type_key_name.should == 'foo_type'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "foreign_key" do
|
83
|
+
it "should default to assocation name _id for belongs to" do
|
84
|
+
base = BelongsToAssociation.new(:foo)
|
85
|
+
base.foreign_key.should == 'foo_id'
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should be overridable with :foreign_key option" do
|
89
|
+
base = BelongsToAssociation.new(:foo, :foreign_key => 'foobar_id')
|
90
|
+
base.foreign_key.should == 'foobar_id'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should have ivar that is association name" do
|
95
|
+
BelongsToAssociation.new(:foo).ivar.should == '@_foo'
|
96
|
+
end
|
97
|
+
|
98
|
+
context "embeddable?" do
|
99
|
+
it "should be true if class is embeddable" do
|
100
|
+
base = ManyAssociation.new(:medias)
|
101
|
+
base.embeddable?.should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should be false if class is not embeddable" do
|
105
|
+
base = ManyAssociation.new(:statuses)
|
106
|
+
base.embeddable?.should be_false
|
107
|
+
|
108
|
+
base = BelongsToAssociation.new(:project)
|
109
|
+
base.embeddable?.should be_false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "proxy_class" do
|
114
|
+
it "should be BelongsToProxy for belongs_to" do
|
115
|
+
base = BelongsToAssociation.new(:project)
|
116
|
+
base.proxy_class.should == BelongsToProxy
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should be BelongsToPolymorphicProxy for polymorphic belongs_to" do
|
120
|
+
base = BelongsToAssociation.new(:target, :polymorphic => true)
|
121
|
+
base.proxy_class.should == BelongsToPolymorphicProxy
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should be OneProxy for one" do
|
125
|
+
base = OneAssociation.new(:status, :polymorphic => true)
|
126
|
+
base.proxy_class.should == OneProxy
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should be OneEmbeddedProxy for one embedded" do
|
130
|
+
base = OneAssociation.new(:media)
|
131
|
+
base.proxy_class.should == OneEmbeddedProxy
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context "touch?" do
|
136
|
+
it "should be true if touch" do
|
137
|
+
BelongsToAssociation.new(:car, :touch => true).touch?.should be_true
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should be false if not touch" do
|
141
|
+
BelongsToAssociation.new(:car).touch?.should be_false
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module BelongsToAssociation
|
4
|
+
include MongoMapper::Plugins::Associations
|
5
|
+
describe "BelongsToAssociation" do
|
6
|
+
|
7
|
+
context "class_name" do
|
8
|
+
it "should camelize the name" do
|
9
|
+
BelongsToAssociation.new(:user).class_name.should == 'User'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should be changeable using class_name option" do
|
13
|
+
association = BelongsToAssociation.new(:user, :class_name => 'Person')
|
14
|
+
association.class_name.should == 'Person'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "embeddable?" do
|
19
|
+
it "should be false even if class is embeddable" do
|
20
|
+
base = BelongsToAssociation.new(:address)
|
21
|
+
base.embeddable?.should be_false
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be false if class is not embeddable" do
|
25
|
+
base = BelongsToAssociation.new(:project)
|
26
|
+
base.embeddable?.should be_false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
module ManyAssociationSpec
|
5
|
+
include MongoMapper::Plugins::Associations
|
6
|
+
describe "ManyAssociation" do
|
7
|
+
|
8
|
+
context "class_name" do
|
9
|
+
it "should camelize the name" do
|
10
|
+
ManyAssociation.new(:smart_people).class_name.should == 'SmartPerson'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be changeable using class_name option" do
|
14
|
+
base = ManyAssociation.new(:smart_people, :class_name => 'IntelligentPerson')
|
15
|
+
base.class_name.should == 'IntelligentPerson'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "type_key_name" do
|
20
|
+
it "should be _type" do
|
21
|
+
ManyAssociation.new(:foos).type_key_name.should == '_type'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "embeddable?" do
|
26
|
+
it "should be true if class is embeddable" do
|
27
|
+
base = ManyAssociation.new(:medias)
|
28
|
+
base.embeddable?.should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be false if class is not embeddable" do
|
32
|
+
base = ManyAssociation.new(:statuses)
|
33
|
+
base.embeddable?.should be_false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "proxy_class" do
|
38
|
+
it "should be ManyDocumentsProxy for many" do
|
39
|
+
base = ManyAssociation.new(:statuses)
|
40
|
+
base.proxy_class.should == ManyDocumentsProxy
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be ManyPolymorphicProxy for polymorphic many" do
|
44
|
+
base = ManyAssociation.new(:messages, :polymorphic => true)
|
45
|
+
base.proxy_class.should == ManyPolymorphicProxy
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should be ManyEmbeddedProxy for many embedded" do
|
49
|
+
base = ManyAssociation.new(:medias)
|
50
|
+
base.proxy_class.should == ManyEmbeddedProxy
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be ManyEmbeddedPolymorphicProxy for polymorphic many embedded" do
|
54
|
+
base = ManyAssociation.new(:medias, :polymorphic => true)
|
55
|
+
base.proxy_class.should == ManyEmbeddedPolymorphicProxy
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should be InArrayProxy for many with :in option" do
|
59
|
+
base = ManyAssociation.new(:messages, :in => :message_ids)
|
60
|
+
base.proxy_class.should == InArrayProxy
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
module OneAssociationSpec
|
5
|
+
include MongoMapper::Plugins::Associations
|
6
|
+
describe "OneAssociation" do
|
7
|
+
|
8
|
+
context "type_key_name" do
|
9
|
+
it "should be _type" do
|
10
|
+
OneAssociation.new(:foo).type_key_name.should == '_type'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "embeddable?" do
|
15
|
+
it "should be true if class is embeddable" do
|
16
|
+
base = OneAssociation.new(:media)
|
17
|
+
base.embeddable?.should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be false if class is not embeddable" do
|
21
|
+
base = OneAssociation.new(:project)
|
22
|
+
base.embeddable?.should be_false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "proxy_class" do
|
27
|
+
it "should be OneProxy for one" do
|
28
|
+
base = OneAssociation.new(:status)
|
29
|
+
base.proxy_class.should == OneProxy
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be OneAsProxy for one with :as option" do
|
33
|
+
base = OneAssociation.new(:message, :as => :messagable)
|
34
|
+
base.proxy_class.should == OneAsProxy
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be OneEmbeddedProxy for one embedded" do
|
38
|
+
base = OneAssociation.new(:media)
|
39
|
+
base.proxy_class.should == OneEmbeddedProxy
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be OneEmbeddedPolymorphicProxy for polymorphic one embedded" do
|
43
|
+
base = OneAssociation.new(:media, :polymorphic => true)
|
44
|
+
base.proxy_class.should == OneEmbeddedPolymorphicProxy
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
class FakeNilProxy < MongoMapper::Plugins::Associations::Proxy
|
4
4
|
def find_target; nil end
|
@@ -16,84 +16,84 @@ class FakeProxy < MongoMapper::Plugins::Associations::Proxy
|
|
16
16
|
def find_target; [1, 2] end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
@owner =
|
22
|
-
@owner.
|
23
|
-
@association =
|
24
|
-
@association.
|
19
|
+
describe "Proxy" do
|
20
|
+
before do
|
21
|
+
@owner = double('owner')
|
22
|
+
@owner.stub(:new?).and_return(false)
|
23
|
+
@association = double('association')
|
24
|
+
@association.stub(:options).and_return({:extend => []})
|
25
25
|
|
26
26
|
@proxy = FakeProxy.new(@owner, @association)
|
27
27
|
@nil_proxy = FakeNilProxy.new(@owner, @association)
|
28
28
|
@blank_proxy = FakeBlankProxy.new(@owner, @association)
|
29
29
|
end
|
30
30
|
|
31
|
-
should
|
31
|
+
it "should set target to nil when reset is called" do
|
32
32
|
@proxy.reset
|
33
33
|
@proxy.target.should be_nil
|
34
34
|
end
|
35
35
|
|
36
|
-
should
|
36
|
+
it "should be able to inspect the proxy" do
|
37
37
|
@proxy.inspect.should == '[1, 2]'
|
38
38
|
end
|
39
39
|
|
40
40
|
context "nil?" do
|
41
|
-
should
|
41
|
+
it "should be true if nil" do
|
42
42
|
@nil_proxy.nil?.should be_true
|
43
43
|
end
|
44
44
|
|
45
|
-
should
|
45
|
+
it "should be false if not nil" do
|
46
46
|
@proxy.nil?.should be_false
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
context "blank?" do
|
51
|
-
should
|
51
|
+
it "should be true if blank" do
|
52
52
|
@blank_proxy.blank?.should be_true
|
53
53
|
@nil_proxy.blank?.should be_true
|
54
54
|
end
|
55
55
|
|
56
|
-
should
|
56
|
+
it "should be false if not blank" do
|
57
57
|
@proxy.blank?.should be_false
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
context "present?" do
|
62
|
-
should
|
62
|
+
it "should be true if present" do
|
63
63
|
@proxy.present?.should be_true
|
64
64
|
end
|
65
65
|
|
66
|
-
should
|
66
|
+
it "should be false if not present" do
|
67
67
|
@blank_proxy.present?.should be_false
|
68
68
|
@nil_proxy.present?.should be_false
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
should
|
72
|
+
it "should delegate respond_to? to target" do
|
73
73
|
@proxy.respond_to?(:each).should be_true
|
74
74
|
@proxy.respond_to?(:size).should be_true
|
75
75
|
@proxy.respond_to?(:gsub).should be_false
|
76
76
|
end
|
77
77
|
|
78
|
-
should
|
78
|
+
it "should alias proxy owner to owner" do
|
79
79
|
@proxy.proxy_owner.should == @owner
|
80
80
|
end
|
81
81
|
|
82
|
-
should
|
82
|
+
it "should alias proxy target to target" do
|
83
83
|
@proxy.proxy_target.should == @target
|
84
84
|
end
|
85
85
|
|
86
86
|
context "send" do
|
87
|
-
should
|
87
|
+
it "should work if proxy responds to method" do
|
88
88
|
@proxy.send(:reset)
|
89
89
|
@proxy.target.should be_nil
|
90
90
|
end
|
91
91
|
|
92
|
-
should
|
92
|
+
it "should work if the target responds to the method" do
|
93
93
|
@proxy.send(:size).should == 2
|
94
94
|
end
|
95
95
|
|
96
|
-
should
|
96
|
+
it "should not work if neither the proxy or target respond to method" do
|
97
97
|
lambda { @proxy.send(:gsub) }.should raise_error
|
98
98
|
end
|
99
99
|
end
|