tpitale-mongo_mapper 0.6.9
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.
- data/.gitignore +10 -0
- data/LICENSE +20 -0
- data/README.rdoc +53 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/bin/mmconsole +60 -0
- data/lib/mongo_mapper/associations/base.rb +110 -0
- data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +26 -0
- data/lib/mongo_mapper/associations/belongs_to_proxy.rb +21 -0
- data/lib/mongo_mapper/associations/collection.rb +19 -0
- data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +26 -0
- data/lib/mongo_mapper/associations/many_documents_proxy.rb +115 -0
- data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +31 -0
- data/lib/mongo_mapper/associations/many_embedded_proxy.rb +54 -0
- data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
- data/lib/mongo_mapper/associations/proxy.rb +113 -0
- data/lib/mongo_mapper/associations.rb +70 -0
- data/lib/mongo_mapper/callbacks.rb +109 -0
- data/lib/mongo_mapper/dirty.rb +136 -0
- data/lib/mongo_mapper/document.rb +472 -0
- data/lib/mongo_mapper/dynamic_finder.rb +74 -0
- data/lib/mongo_mapper/embedded_document.rb +384 -0
- data/lib/mongo_mapper/finder_options.rb +133 -0
- data/lib/mongo_mapper/key.rb +36 -0
- data/lib/mongo_mapper/observing.rb +50 -0
- data/lib/mongo_mapper/pagination.rb +55 -0
- data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
- data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +27 -0
- data/lib/mongo_mapper/serialization.rb +54 -0
- data/lib/mongo_mapper/serializers/json_serializer.rb +92 -0
- data/lib/mongo_mapper/support.rb +206 -0
- data/lib/mongo_mapper/validations.rb +41 -0
- data/lib/mongo_mapper.rb +120 -0
- data/mongo_mapper.gemspec +173 -0
- data/specs.watchr +32 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
- data/test/functional/associations/test_many_documents_proxy.rb +387 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +192 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +18 -0
- data/test/functional/test_callbacks.rb +85 -0
- data/test/functional/test_dirty.rb +159 -0
- data/test/functional/test_document.rb +1235 -0
- data/test/functional/test_embedded_document.rb +135 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_pagination.rb +95 -0
- data/test/functional/test_rails_compatibility.rb +25 -0
- data/test/functional/test_string_id_compatibility.rb +72 -0
- data/test/functional/test_validations.rb +378 -0
- data/test/models.rb +271 -0
- data/test/support/custom_matchers.rb +55 -0
- data/test/support/timing.rb +16 -0
- data/test/test_helper.rb +27 -0
- data/test/unit/associations/test_base.rb +166 -0
- data/test/unit/associations/test_proxy.rb +91 -0
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_document.rb +204 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +718 -0
- data/test/unit/test_finder_options.rb +296 -0
- data/test/unit/test_key.rb +172 -0
- data/test/unit/test_mongo_mapper.rb +65 -0
- data/test/unit/test_observing.rb +101 -0
- data/test/unit/test_pagination.rb +113 -0
- data/test/unit/test_rails_compatibility.rb +49 -0
- data/test/unit/test_serializations.rb +52 -0
- data/test/unit/test_support.rb +342 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +503 -0
- metadata +235 -0
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class DirtyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@document = Class.new do
|
7
|
+
include MongoMapper::Document
|
8
|
+
set_collection_name 'test'
|
9
|
+
key :phrase, String
|
10
|
+
end
|
11
|
+
@document.collection.remove
|
12
|
+
|
13
|
+
Status.collection.remove
|
14
|
+
Project.collection.remove
|
15
|
+
end
|
16
|
+
|
17
|
+
context "marking changes" do
|
18
|
+
should "not happen if there are none" do
|
19
|
+
doc = @document.new
|
20
|
+
doc.phrase_changed?.should be_false
|
21
|
+
doc.phrase_change.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
should "happen when change happens" do
|
25
|
+
doc = @document.new
|
26
|
+
doc.phrase = 'Golly Gee Willikers Batman'
|
27
|
+
doc.phrase_changed?.should be_true
|
28
|
+
doc.phrase_was.should be_nil
|
29
|
+
doc.phrase_change.should == [nil, 'Golly Gee Willikers Batman']
|
30
|
+
end
|
31
|
+
|
32
|
+
should "happen when initializing" do
|
33
|
+
doc = @document.new(:phrase => 'Foo')
|
34
|
+
doc.changed?.should be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
should "clear changes on save" do
|
38
|
+
doc = @document.new
|
39
|
+
doc.phrase = 'Golly Gee Willikers Batman'
|
40
|
+
doc.phrase_changed?.should be_true
|
41
|
+
doc.save
|
42
|
+
doc.phrase_changed?.should_not be_true
|
43
|
+
doc.phrase_change.should be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
should "clear changes on save!" do
|
47
|
+
doc = @document.new
|
48
|
+
doc.phrase = 'Golly Gee Willikers Batman'
|
49
|
+
doc.phrase_changed?.should be_true
|
50
|
+
doc.save!
|
51
|
+
doc.phrase_changed?.should_not be_true
|
52
|
+
doc.phrase_change.should be_nil
|
53
|
+
end
|
54
|
+
|
55
|
+
should "not happen when loading from database" do
|
56
|
+
doc = @document.create(:phrase => 'Foo')
|
57
|
+
doc.phrase = 'Fart'
|
58
|
+
doc.changed?.should be_true
|
59
|
+
doc.reload
|
60
|
+
doc.changed?.should be_false
|
61
|
+
end
|
62
|
+
|
63
|
+
should "happen if changed after loading from database" do
|
64
|
+
doc = @document.create(:phrase => 'Foo')
|
65
|
+
doc.reload
|
66
|
+
doc.changed?.should be_false
|
67
|
+
doc.phrase = 'Bar'
|
68
|
+
doc.changed?.should be_true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "blank new value and type integer" do
|
73
|
+
should "not mark changes" do
|
74
|
+
@document.key :age, Integer
|
75
|
+
|
76
|
+
[nil, ''].each do |value|
|
77
|
+
doc = @document.new
|
78
|
+
doc.age = value
|
79
|
+
doc.age_changed?.should be_false
|
80
|
+
doc.age_change.should be_nil
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "blank new value and type float" do
|
86
|
+
should "not mark changes" do
|
87
|
+
@document.key :amount, Float
|
88
|
+
|
89
|
+
[nil, ''].each do |value|
|
90
|
+
doc = @document.new
|
91
|
+
doc.amount = value
|
92
|
+
doc.amount_changed?.should be_false
|
93
|
+
doc.amount_change.should be_nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "changed?" do
|
99
|
+
should "be true if key changed" do
|
100
|
+
doc = @document.new
|
101
|
+
doc.phrase = 'A penny saved is a penny earned.'
|
102
|
+
doc.changed?.should be_true
|
103
|
+
end
|
104
|
+
|
105
|
+
should "be false if no keys changed" do
|
106
|
+
@document.new.changed?.should be_false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "changes" do
|
111
|
+
should "be empty hash if no changes" do
|
112
|
+
@document.new.changes.should == {}
|
113
|
+
end
|
114
|
+
|
115
|
+
should "be hash of keys with values of changes if there are changes" do
|
116
|
+
doc = @document.new
|
117
|
+
doc.phrase = 'A penny saved is a penny earned.'
|
118
|
+
doc.changes.should == {'phrase' => [nil, 'A penny saved is a penny earned.']}
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "changed" do
|
123
|
+
should "be empty array if no changes" do
|
124
|
+
@document.new.changed.should == []
|
125
|
+
end
|
126
|
+
|
127
|
+
should "be array of keys that have changed if there are changes" do
|
128
|
+
doc = @document.new
|
129
|
+
doc.phrase = 'A penny saved is a penny earned.'
|
130
|
+
doc.changed.should == ['phrase']
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "will_change!" do
|
135
|
+
should "mark changes" do
|
136
|
+
doc = @document.create(:phrase => 'Foo')
|
137
|
+
|
138
|
+
doc.phrase << 'bar'
|
139
|
+
doc.phrase_changed?.should be_false
|
140
|
+
|
141
|
+
doc.phrase_will_change!
|
142
|
+
doc.phrase_changed?.should be_true
|
143
|
+
doc.phrase_change.should == ['Foobar', 'Foobar']
|
144
|
+
|
145
|
+
doc.phrase << '!'
|
146
|
+
doc.phrase_changed?.should be_true
|
147
|
+
doc.phrase_change.should == ['Foobar', 'Foobar!']
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context "changing a foreign key through association" do
|
152
|
+
should "mark changes" do
|
153
|
+
status = Status.create(:name => 'Foo')
|
154
|
+
status.project = Project.create(:name => 'Bar')
|
155
|
+
status.changed?.should be_true
|
156
|
+
status.changed.should == %w(project_id)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|