mongo_mapper 0.12.0 → 0.13.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class BelongsToAssociationTest < Test::Unit::TestCase
|
5
|
-
include MongoMapper::Plugins::Associations
|
6
|
-
|
7
|
-
context "class_name" do
|
8
|
-
should "camelize the name" do
|
9
|
-
BelongsToAssociation.new(:user).class_name.should == 'User'
|
10
|
-
end
|
11
|
-
|
12
|
-
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
|
-
should "be false even if class is embeddable" do
|
20
|
-
base = BelongsToAssociation.new(:address)
|
21
|
-
base.embeddable?.should be_false
|
22
|
-
end
|
23
|
-
|
24
|
-
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
|
@@ -1,63 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class ManyAssociationTest < Test::Unit::TestCase
|
5
|
-
include MongoMapper::Plugins::Associations
|
6
|
-
|
7
|
-
context "class_name" do
|
8
|
-
should "camelize the name" do
|
9
|
-
ManyAssociation.new(:smart_people).class_name.should == 'SmartPerson'
|
10
|
-
end
|
11
|
-
|
12
|
-
should "be changeable using class_name option" do
|
13
|
-
base = ManyAssociation.new(:smart_people, :class_name => 'IntelligentPerson')
|
14
|
-
base.class_name.should == 'IntelligentPerson'
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context "type_key_name" do
|
19
|
-
should "be _type" do
|
20
|
-
ManyAssociation.new(:foos).type_key_name.should == '_type'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context "embeddable?" do
|
25
|
-
should "be true if class is embeddable" do
|
26
|
-
base = ManyAssociation.new(:medias)
|
27
|
-
base.embeddable?.should be_true
|
28
|
-
end
|
29
|
-
|
30
|
-
should "be false if class is not embeddable" do
|
31
|
-
base = ManyAssociation.new(:statuses)
|
32
|
-
base.embeddable?.should be_false
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context "proxy_class" do
|
37
|
-
should "be ManyDocumentsProxy for many" do
|
38
|
-
base = ManyAssociation.new(:statuses)
|
39
|
-
base.proxy_class.should == ManyDocumentsProxy
|
40
|
-
end
|
41
|
-
|
42
|
-
should "be ManyPolymorphicProxy for polymorphic many" do
|
43
|
-
base = ManyAssociation.new(:messages, :polymorphic => true)
|
44
|
-
base.proxy_class.should == ManyPolymorphicProxy
|
45
|
-
end
|
46
|
-
|
47
|
-
should "be ManyEmbeddedProxy for many embedded" do
|
48
|
-
base = ManyAssociation.new(:medias)
|
49
|
-
base.proxy_class.should == ManyEmbeddedProxy
|
50
|
-
end
|
51
|
-
|
52
|
-
should "be ManyEmbeddedPolymorphicProxy for polymorphic many embedded" do
|
53
|
-
base = ManyAssociation.new(:medias, :polymorphic => true)
|
54
|
-
base.proxy_class.should == ManyEmbeddedPolymorphicProxy
|
55
|
-
end
|
56
|
-
|
57
|
-
should "be InArrayProxy for many with :in option" do
|
58
|
-
base = ManyAssociation.new(:messages, :in => :message_ids)
|
59
|
-
base.proxy_class.should == InArrayProxy
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'models'
|
3
|
-
|
4
|
-
class OneAssociationTest < Test::Unit::TestCase
|
5
|
-
include MongoMapper::Plugins::Associations
|
6
|
-
|
7
|
-
context "type_key_name" do
|
8
|
-
should "be _type" do
|
9
|
-
OneAssociation.new(:foo).type_key_name.should == '_type'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
context "embeddable?" do
|
14
|
-
should "be true if class is embeddable" do
|
15
|
-
base = OneAssociation.new(:media)
|
16
|
-
base.embeddable?.should be_true
|
17
|
-
end
|
18
|
-
|
19
|
-
should "be false if class is not embeddable" do
|
20
|
-
base = OneAssociation.new(:project)
|
21
|
-
base.embeddable?.should be_false
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context "proxy_class" do
|
26
|
-
should "be OneProxy for one" do
|
27
|
-
base = OneAssociation.new(:status)
|
28
|
-
base.proxy_class.should == OneProxy
|
29
|
-
end
|
30
|
-
|
31
|
-
should "be OneAsProxy for one with :as option" do
|
32
|
-
base = OneAssociation.new(:message, :as => :messagable)
|
33
|
-
base.proxy_class.should == OneAsProxy
|
34
|
-
end
|
35
|
-
|
36
|
-
should "be OneEmbeddedProxy for one embedded" do
|
37
|
-
base = OneAssociation.new(:media)
|
38
|
-
base.proxy_class.should == OneEmbeddedProxy
|
39
|
-
end
|
40
|
-
|
41
|
-
should "be OneEmbeddedPolymorphicProxy for polymorphic one embedded" do
|
42
|
-
base = OneAssociation.new(:media, :polymorphic => true)
|
43
|
-
base.proxy_class.should == OneEmbeddedPolymorphicProxy
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
@@ -1,216 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class JsonSerializationTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
# Helper function in case things change in the future
|
6
|
-
# - replacing all those to_json calls was a nightmare
|
7
|
-
def convert_to_json(object, options={})
|
8
|
-
ActiveSupport::JSON.encode(object, options)
|
9
|
-
end
|
10
|
-
|
11
|
-
class Tag
|
12
|
-
include MongoMapper::EmbeddedDocument
|
13
|
-
key :name, String
|
14
|
-
end
|
15
|
-
|
16
|
-
class Contact
|
17
|
-
include MongoMapper::Document
|
18
|
-
key :name, String
|
19
|
-
key :age, Integer
|
20
|
-
key :created_at, Time
|
21
|
-
key :awesome, Boolean
|
22
|
-
key :preferences, Hash
|
23
|
-
|
24
|
-
many :tags, :class_name => 'JsonSerializationTest::Tag'
|
25
|
-
end
|
26
|
-
|
27
|
-
def setup
|
28
|
-
Kernel.const_set('TopLevelContact', Doc('TopLevelContact'))
|
29
|
-
TopLevelContact.key :name, String
|
30
|
-
|
31
|
-
Contact.include_root_in_json = false
|
32
|
-
@contact = Contact.new(
|
33
|
-
:name => 'Konata Izumi',
|
34
|
-
:age => 16,
|
35
|
-
:created_at => Time.utc(2006, 8, 1),
|
36
|
-
:awesome => true,
|
37
|
-
:preferences => { :shows => 'anime' }
|
38
|
-
)
|
39
|
-
@top_level_contact = TopLevelContact.new(
|
40
|
-
:name => 'Konata Izumi'
|
41
|
-
)
|
42
|
-
end
|
43
|
-
|
44
|
-
def teardown
|
45
|
-
Kernel.send(:remove_const, 'TopLevelContact') if Object.const_defined?('TopLevelContact')
|
46
|
-
end
|
47
|
-
|
48
|
-
should "include root for class with no module" do
|
49
|
-
TopLevelContact.include_root_in_json = true
|
50
|
-
assert_match %r{^\{"top_level_contact":\s?\{}, convert_to_json(@top_level_contact)
|
51
|
-
end
|
52
|
-
|
53
|
-
should "include demodulized root" do
|
54
|
-
Contact.include_root_in_json = true
|
55
|
-
assert_match %r{^\{"contact":\s?\{}, convert_to_json(@contact)
|
56
|
-
end
|
57
|
-
|
58
|
-
should "encode all encodable attributes" do
|
59
|
-
json = convert_to_json(@contact)
|
60
|
-
|
61
|
-
assert_no_match %r{"_id"}, json
|
62
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
63
|
-
assert_match %r{"age":16}, json
|
64
|
-
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
|
65
|
-
assert_match %r{"awesome":true}, json
|
66
|
-
assert_match %r{"preferences":\{"shows":"anime"\}}, json
|
67
|
-
end
|
68
|
-
|
69
|
-
should "allow attribute filtering with only" do
|
70
|
-
json = convert_to_json(@contact, :only => [:name, :age])
|
71
|
-
|
72
|
-
assert_no_match %r{"_id"}, json
|
73
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
74
|
-
assert_match %r{"age":16}, json
|
75
|
-
assert_no_match %r{"awesome"}, json
|
76
|
-
assert_no_match %r{"created_at"}, json
|
77
|
-
assert_no_match %r{"preferences"}, json
|
78
|
-
end
|
79
|
-
|
80
|
-
should "allow attribute filtering with except" do
|
81
|
-
json = convert_to_json(@contact, :except => [:name, :age])
|
82
|
-
|
83
|
-
assert_no_match %r{"_id"}, json
|
84
|
-
assert_no_match %r{"name"}, json
|
85
|
-
assert_no_match %r{"age"}, json
|
86
|
-
assert_match %r{"awesome"}, json
|
87
|
-
assert_match %r{"created_at"}, json
|
88
|
-
assert_match %r{"preferences"}, json
|
89
|
-
end
|
90
|
-
|
91
|
-
context "_id key" do
|
92
|
-
should "not be included by default" do
|
93
|
-
json = convert_to_json(@contact)
|
94
|
-
assert_no_match %r{"_id":}, json
|
95
|
-
end
|
96
|
-
|
97
|
-
should "not be included even if :except is used" do
|
98
|
-
json = convert_to_json(@contact, :except => :name)
|
99
|
-
assert_no_match %r{"_id":}, json
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context "id method" do
|
104
|
-
setup do
|
105
|
-
def @contact.label; "Has cheezburger"; end
|
106
|
-
def @contact.favorite_quote; "Constraints are liberating"; end
|
107
|
-
end
|
108
|
-
|
109
|
-
should "be included by default" do
|
110
|
-
json = convert_to_json(@contact)
|
111
|
-
assert_match %r{"id"}, json
|
112
|
-
end
|
113
|
-
|
114
|
-
should "be included when single method included" do
|
115
|
-
json = convert_to_json(@contact, :methods => :label)
|
116
|
-
assert_match %r{"id"}, json
|
117
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
118
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
119
|
-
assert_no_match %r{"favorite_quote":"Constraints are liberating"}, json
|
120
|
-
end
|
121
|
-
|
122
|
-
should "be included when multiple methods included" do
|
123
|
-
json = convert_to_json(@contact, :methods => [:label, :favorite_quote])
|
124
|
-
assert_match %r{"id"}, json
|
125
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
126
|
-
assert_match %r{"favorite_quote":"Constraints are liberating"}, json
|
127
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
128
|
-
end
|
129
|
-
|
130
|
-
should "not be included if :only is present" do
|
131
|
-
json = convert_to_json(@contact, :only => :name)
|
132
|
-
assert_no_match %r{"id":}, json
|
133
|
-
end
|
134
|
-
|
135
|
-
should "be represented by a string" do
|
136
|
-
json = convert_to_json(@contact)
|
137
|
-
assert_match %r{"id":"}, json
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
context "including methods" do
|
142
|
-
setup do
|
143
|
-
def @contact.label; "Has cheezburger"; end
|
144
|
-
def @contact.favorite_quote; "Constraints are liberating"; end
|
145
|
-
end
|
146
|
-
|
147
|
-
should "include single method" do
|
148
|
-
json = convert_to_json(@contact, :methods => :label)
|
149
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
150
|
-
end
|
151
|
-
|
152
|
-
should "include multiple methods" do
|
153
|
-
json = convert_to_json(@contact, :only => :name, :methods => [:label, :favorite_quote])
|
154
|
-
assert_match %r{"label":"Has cheezburger"}, json
|
155
|
-
assert_match %r{"favorite_quote":"Constraints are liberating"}, json
|
156
|
-
assert_match %r{"name":"Konata Izumi"}, json
|
157
|
-
assert_no_match %r{"age":16}, json
|
158
|
-
assert_no_match %r{"awesome"}, json
|
159
|
-
assert_no_match %r{"created_at"}, json
|
160
|
-
assert_no_match %r{"preferences"}, json
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
context "array of records" do
|
165
|
-
setup do
|
166
|
-
@contacts = [
|
167
|
-
Contact.new(:name => 'David', :age => 39),
|
168
|
-
Contact.new(:name => 'Mary', :age => 14)
|
169
|
-
]
|
170
|
-
end
|
171
|
-
|
172
|
-
should "allow attribute filtering with only" do
|
173
|
-
json =convert_to_json(@contacts, :only => :name)
|
174
|
-
assert_match %r{\{"name":"David"\}}, json
|
175
|
-
assert_match %r{\{"name":"Mary"\}}, json
|
176
|
-
end
|
177
|
-
|
178
|
-
should "allow attribute filtering with except" do
|
179
|
-
json = convert_to_json(@contacts, :except => [:name, :preferences, :awesome, :created_at, :updated_at])
|
180
|
-
assert_match %r{"age":39}, json
|
181
|
-
assert_match %r{"age":14}, json
|
182
|
-
assert_no_match %r{"name":}, json
|
183
|
-
assert_no_match %r{"preferences":}, json
|
184
|
-
assert_no_match %r{"awesome":}, json
|
185
|
-
assert_no_match %r{"created_at":}, json
|
186
|
-
assert_no_match %r{"updated_at":}, json
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
should "allow options for hash of records" do
|
191
|
-
contacts = {
|
192
|
-
1 => Contact.new(:name => 'David', :age => 39),
|
193
|
-
2 => Contact.new(:name => 'Mary', :age => 14)
|
194
|
-
}
|
195
|
-
json = convert_to_json(contacts, :only => [1, :name])
|
196
|
-
assert_match %r{"1":}, json
|
197
|
-
assert_match %r{\{"name":"David"\}}, json
|
198
|
-
assert_no_match %r{"2":}, json
|
199
|
-
end
|
200
|
-
|
201
|
-
should "include embedded attributes" do
|
202
|
-
contact = Contact.new(:name => 'John', :age => 27)
|
203
|
-
contact.tags = [Tag.new(:name => 'awesome'), Tag.new(:name => 'ruby')]
|
204
|
-
json = convert_to_json(contact)
|
205
|
-
assert_match %r{"tags":}, json
|
206
|
-
assert_match %r{"name":"awesome"}, json
|
207
|
-
assert_match %r{"name":"ruby"}, json
|
208
|
-
end
|
209
|
-
|
210
|
-
should "include dynamic attributes" do
|
211
|
-
contact = Contact.new(:name => 'John', :age => 27, :foo => 'bar')
|
212
|
-
contact['smell'] = 'stinky'
|
213
|
-
json = convert_to_json(contact)
|
214
|
-
assert_match %r{"smell":"stinky"}, json
|
215
|
-
end
|
216
|
-
end
|
@@ -1,196 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class XmlSerializationTest < Test::Unit::TestCase
|
4
|
-
class Tag
|
5
|
-
include MongoMapper::EmbeddedDocument
|
6
|
-
key :name, String
|
7
|
-
end
|
8
|
-
|
9
|
-
class Contact
|
10
|
-
include MongoMapper::Document
|
11
|
-
key :name, String
|
12
|
-
key :age, Integer
|
13
|
-
key :created_at, Time
|
14
|
-
key :awesome, Boolean
|
15
|
-
key :preferences, Hash
|
16
|
-
|
17
|
-
many :tags, :class_name => 'XmlSerializationTest::Tag'
|
18
|
-
end
|
19
|
-
|
20
|
-
def setup
|
21
|
-
Kernel.const_set('TopLevelContact', Doc('TopLevelContact'))
|
22
|
-
TopLevelContact.key :name, String
|
23
|
-
|
24
|
-
Contact.include_root_in_json = false
|
25
|
-
@contact = Contact.new(
|
26
|
-
:name => 'Konata Izumi',
|
27
|
-
:age => 16,
|
28
|
-
:created_at => Time.utc(2006, 8, 1),
|
29
|
-
:awesome => true,
|
30
|
-
:preferences => { :shows => 'anime' }
|
31
|
-
)
|
32
|
-
@top_level_contact = TopLevelContact.new(
|
33
|
-
:name => 'Konata Izumi'
|
34
|
-
)
|
35
|
-
end
|
36
|
-
|
37
|
-
def teardown
|
38
|
-
Kernel.send(:remove_const, 'TopLevelContact') if Object.const_defined?('TopLevelContact')
|
39
|
-
end
|
40
|
-
|
41
|
-
should "include root for class with no module" do
|
42
|
-
assert_match %r{<top-level-contact>}, @top_level_contact.to_xml
|
43
|
-
end
|
44
|
-
|
45
|
-
should "include demodulized root" do
|
46
|
-
assert_match %r{<contact>}, @contact.to_xml
|
47
|
-
end
|
48
|
-
|
49
|
-
should "encode all encodable attributes" do
|
50
|
-
xml = @contact.to_xml
|
51
|
-
|
52
|
-
assert_no_match %r{_id}, xml
|
53
|
-
assert_match %r{<id>#{@contact.id}</id>}, xml
|
54
|
-
assert_match %r{<name>Konata Izumi</name>}, xml
|
55
|
-
assert_match %r{<age.*>16</age>}, xml
|
56
|
-
assert_match %r(<created-at type="datetime">), xml
|
57
|
-
assert_match %r{<awesome type="boolean">true</awesome>}, xml
|
58
|
-
assert_match %r{<preferences>}, xml
|
59
|
-
assert_match %r{<shows>anime</shows>}, xml
|
60
|
-
end
|
61
|
-
|
62
|
-
should "allow attribute filtering with only" do
|
63
|
-
xml = @contact.to_xml(:only => [:name, :age])
|
64
|
-
|
65
|
-
assert_no_match %r{<id>}, xml
|
66
|
-
assert_match %r{<name>Konata Izumi</name>}, xml
|
67
|
-
assert_match %r{<age type="integer">16</age>}, xml
|
68
|
-
assert_no_match %r{awesome}, xml
|
69
|
-
assert_no_match %r{created-at}, xml
|
70
|
-
assert_no_match %r{preferences}, xml
|
71
|
-
end
|
72
|
-
|
73
|
-
should "allow attribute filtering with except" do
|
74
|
-
xml = @contact.to_xml(:except => [:name, :age])
|
75
|
-
|
76
|
-
assert_no_match %r{<name>Konata Izumi</name>}, xml
|
77
|
-
assert_no_match %r{<age type="integer">16</age>}, xml
|
78
|
-
assert_match %r{<id>}, xml
|
79
|
-
assert_match %r{awesome}, xml
|
80
|
-
assert_match %r{created-at}, xml
|
81
|
-
assert_match %r{preferences}, xml
|
82
|
-
end
|
83
|
-
|
84
|
-
context "_id key" do
|
85
|
-
should "not be included by default" do
|
86
|
-
assert_no_match %r{_id}, @contact.to_xml
|
87
|
-
end
|
88
|
-
|
89
|
-
should "not be included even if :except is used" do
|
90
|
-
assert_no_match %r{_id}, @contact.to_xml(:except => :name)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
context "id method" do
|
95
|
-
setup do
|
96
|
-
def @contact.label; "Has cheezburger"; end
|
97
|
-
def @contact.favorite_quote; "Constraints are liberating"; end
|
98
|
-
end
|
99
|
-
|
100
|
-
should "be included by default" do
|
101
|
-
assert_match %r{<id>#{@contact.id}</id>}, @contact.to_xml
|
102
|
-
end
|
103
|
-
|
104
|
-
should "be included when single method included" do
|
105
|
-
xml = @contact.to_xml(:methods => :label)
|
106
|
-
assert_match %r{<id>}, xml
|
107
|
-
assert_match %r{<label>Has cheezburger</label>}, xml
|
108
|
-
assert_match %r{<name>Konata Izumi</name>}, xml
|
109
|
-
assert_no_match %r{favorite_quote}, xml
|
110
|
-
end
|
111
|
-
|
112
|
-
should "be included when multiple methods included" do
|
113
|
-
xml = @contact.to_xml(:methods => [:label, :favorite_quote])
|
114
|
-
assert_match %r{<id>}, xml
|
115
|
-
assert_match %r{<label>Has cheezburger</label>}, xml
|
116
|
-
assert_match %r{<name>Konata Izumi</name>}, xml
|
117
|
-
assert_match %r{<favorite-quote>Constraints are liberating</favorite-quote>}, xml
|
118
|
-
end
|
119
|
-
|
120
|
-
should "not be included if :only is present" do
|
121
|
-
assert_no_match %r{<id}, @contact.to_xml(:only => :name)
|
122
|
-
end
|
123
|
-
|
124
|
-
should "be represented by a string" do
|
125
|
-
assert_match %r{<id>}, @contact.to_xml
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
context "including methods" do
|
130
|
-
setup do
|
131
|
-
def @contact.label; "Has cheezburger"; end
|
132
|
-
def @contact.favorite_quote; "Constraints are liberating"; end
|
133
|
-
end
|
134
|
-
|
135
|
-
should "include single method" do
|
136
|
-
assert_match %r{<label>Has cheezburger</label>}, @contact.to_xml(:methods => :label)
|
137
|
-
end
|
138
|
-
|
139
|
-
should "include multiple methods" do
|
140
|
-
xml = @contact.to_xml(:only => :name, :methods => [:label, :favorite_quote])
|
141
|
-
assert_match %r{<label>Has cheezburger</label>}, xml
|
142
|
-
assert_match %r{<favorite-quote>Constraints are liberating</favorite-quote>}, xml
|
143
|
-
assert_match %r{<name>Konata Izumi</name>}, xml
|
144
|
-
assert_no_match %r{age}, xml
|
145
|
-
assert_no_match %r{awesome}, xml
|
146
|
-
assert_no_match %r{created-at}, xml
|
147
|
-
assert_no_match %r{preferences}, xml
|
148
|
-
|
149
|
-
# Assert only one tag is created
|
150
|
-
xml.scan(/favorite-quote/).size.should == 2
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
context "array of records" do
|
155
|
-
setup do
|
156
|
-
@contacts = [
|
157
|
-
Contact.new(:name => 'David', :age => 39),
|
158
|
-
Contact.new(:name => 'Mary', :age => 14)
|
159
|
-
]
|
160
|
-
end
|
161
|
-
|
162
|
-
should "allow attribute filtering with only" do
|
163
|
-
xml = @contacts.to_xml(:only => :name)
|
164
|
-
assert_match %r{<name>David</name>}, xml
|
165
|
-
assert_match %r{<name>Mary</name>}, xml
|
166
|
-
end
|
167
|
-
|
168
|
-
should "allow attribute filtering with except" do
|
169
|
-
xml = @contacts.to_xml(:except => [:name, :preferences, :awesome, :created_at, :updated_at])
|
170
|
-
assert_match %r{<age type="integer">39</age>}, xml
|
171
|
-
assert_match %r{<age type="integer">14</age>}, xml
|
172
|
-
assert_no_match %r{name}, xml
|
173
|
-
assert_no_match %r{preferences}, xml
|
174
|
-
assert_no_match %r{awesome}, xml
|
175
|
-
assert_no_match %r{created-at}, xml
|
176
|
-
assert_no_match %r{updated-at}, xml
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
should "include embedded attributes" do
|
181
|
-
contact = Contact.new(:name => 'John', :age => 27)
|
182
|
-
contact.tags = [Tag.new(:name => 'awesome'), Tag.new(:name => 'ruby')]
|
183
|
-
xml = contact.to_xml
|
184
|
-
assert_match %r{<tags type="array">}, xml
|
185
|
-
assert_match %r{<id>#{contact.tags[0].id}</id>}, xml
|
186
|
-
assert_match %r{<id>#{contact.tags[1].id}</id>}, xml
|
187
|
-
assert_match %r{<name>awesome</name>}, xml
|
188
|
-
assert_match %r{<name>ruby</name>}, xml
|
189
|
-
end
|
190
|
-
|
191
|
-
should "include dynamic attributes" do
|
192
|
-
contact = Contact.new(:name => 'John', :age => 27, :foo => 'bar')
|
193
|
-
contact['smell'] = 'stinky'
|
194
|
-
assert_match %r{<smell>stinky</smell>}, contact.to_xml
|
195
|
-
end
|
196
|
-
end
|