mongoid_versioned_atomic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/lib/mongoid_versioned_atomic.rb +4 -0
  5. data/lib/mongoid_versioned_atomic/v_atomic.rb +336 -0
  6. data/lib/mongoid_versioned_atomic/version.rb +3 -0
  7. data/lib/tasks/mongoid_versioned_atomic_tasks.rake +4 -0
  8. data/test/dummy/README.rdoc +28 -0
  9. data/test/dummy/Rakefile +6 -0
  10. data/test/dummy/app/assets/images/facebook.png +0 -0
  11. data/test/dummy/app/assets/images/keratoscope.jpg +0 -0
  12. data/test/dummy/app/assets/javascripts/application.js +13 -0
  13. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  14. data/test/dummy/app/controllers/application_controller.rb +5 -0
  15. data/test/dummy/app/helpers/application_helper.rb +2 -0
  16. data/test/dummy/app/models/entry.rb +11 -0
  17. data/test/dummy/app/models/thing.rb +5 -0
  18. data/test/dummy/app/models/user.rb +51 -0
  19. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  20. data/test/dummy/bin/bundle +3 -0
  21. data/test/dummy/bin/rails +4 -0
  22. data/test/dummy/bin/rake +4 -0
  23. data/test/dummy/bin/setup +29 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +29 -0
  26. data/test/dummy/config/boot.rb +5 -0
  27. data/test/dummy/config/environment.rb +5 -0
  28. data/test/dummy/config/environments/development.rb +38 -0
  29. data/test/dummy/config/environments/production.rb +76 -0
  30. data/test/dummy/config/environments/test.rb +42 -0
  31. data/test/dummy/config/initializers/assets.rb +11 -0
  32. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  34. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  35. data/test/dummy/config/initializers/inflections.rb +16 -0
  36. data/test/dummy/config/initializers/mime_types.rb +4 -0
  37. data/test/dummy/config/initializers/session_store.rb +3 -0
  38. data/test/dummy/config/initializers/wrap_parameters.rb +9 -0
  39. data/test/dummy/config/locales/en.yml +23 -0
  40. data/test/dummy/config/mongoid.yml +137 -0
  41. data/test/dummy/config/routes.rb +56 -0
  42. data/test/dummy/config/secrets.yml +22 -0
  43. data/test/dummy/log/development.log +21 -0
  44. data/test/dummy/log/test.log +21976 -0
  45. data/test/dummy/public/404.html +67 -0
  46. data/test/dummy/public/422.html +67 -0
  47. data/test/dummy/public/500.html +66 -0
  48. data/test/dummy/public/favicon.ico +0 -0
  49. data/test/dummy/test/fixtures/users.yml +9 -0
  50. data/test/dummy/test/models/user_test.rb +7 -0
  51. data/test/mongoid_versioned_atomic_test.rb +7 -0
  52. data/test/test_helper.rb +19 -0
  53. data/test/v_atomic_test.rb +569 -0
  54. metadata +212 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,9 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+ one:
4
+ name: MyString
5
+ email: MyString
6
+
7
+ two:
8
+ name: MyString
9
+ email: MyString
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class MongoidVersionedAtomicTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, MongoidVersionedAtomic
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ # Filter out Minitest backtrace while allowing backtrace from other libraries
8
+ # to be shown.
9
+ Minitest.backtrace_filter = Minitest::BacktraceFilter.new
10
+
11
+ # Load support files
12
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
13
+
14
+ # Load fixtures from the engine
15
+ if ActiveSupport::TestCase.respond_to?(:fixture_path=)
16
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
17
+ ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
18
+ ActiveSupport::TestCase.fixtures :all
19
+ end
@@ -0,0 +1,569 @@
1
+ require 'test_helper'
2
+
3
+ class CoreExtTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ User.delete_all
7
+ end
8
+
9
+
10
+ def test_cross_model_callbacks
11
+ t1 = Thing.new
12
+ t1.versioned_create
13
+ e = Entry.new
14
+ e.parent_thing_id = t1.id
15
+ e.save
16
+ t1.reload
17
+ assert_equal(1, t1.entries.size, "the entry id was saved to thing entries")
18
+ end
19
+
20
+ def test_versioned_upsert_one_with_set_on_insert
21
+ a1 = User.new
22
+ a1.name = "bhargav"
23
+ a1.email = "bhargav.r.raut@gmail.com"
24
+ User.versioned_upsert_one({"_id" => a1.id},{"$setOnInsert" => {"name" => "cat"}},User)
25
+ a1_from_db = User.find(a1.id)
26
+ assert_equal 1, a1_from_db.version, "set on insert should work with version operator."
27
+
28
+ end
29
+
30
+
31
+ def test_versioned_create_when_document_already_exists
32
+
33
+ a1 = User.new
34
+ a1.name = "bhargav"
35
+ a1.email = "bhargav.r.raut@gmail.com"
36
+ a1.versioned_create
37
+ assert_equal true, a1.op_success, "this op should succeed."
38
+
39
+ a2 = User.new
40
+ a2.name = "bb"
41
+ a2.email = "bhargav.r.raut@gmail.com"
42
+ a2.versioned_create({"email" => a2.email})
43
+ assert_equal true, a2.op_success, "this op should also succeed"
44
+ assert_nil a2.upserted_id,"no new doc should be upserted"
45
+ assert_equal 1, a2.matched_count, "it should match an existing doc"
46
+ end
47
+
48
+
49
+ def test_versioned_upsert_one_returns_a_mongoid_document
50
+
51
+ a1 = User.new
52
+ a1.name = "bhargav"
53
+ a1.email = "bhargav.r.raut@gmail.com"
54
+ a1.versioned_create
55
+
56
+ ret = User.versioned_upsert_one({"_id" => a1.id},{"$set" => {"name" => "roxanne"}},User)
57
+
58
+
59
+
60
+ assert_equal true, (ret.methods.include? :attributes), "it should return a mongoid document"
61
+
62
+ end
63
+
64
+ def test_versioned_upsert_one_increments_version_if_doc_found_but_doesnt_affect_other_docs
65
+
66
+ a1 = User.new
67
+ a1.name = "bhargav"
68
+ a1.email = "bhargav.r.raut@gmail.com"
69
+ a1.versioned_create
70
+
71
+ a2 = User.new
72
+ a2.name = "aditya"
73
+ a2.email = "aditya@gmail.com"
74
+ a2.versioned_create
75
+
76
+ User.versioned_upsert_one({"_id" => a1.id},{"$set" => {"name" => "roxanne"}},User)
77
+
78
+ a1_from_db = User.find(a1.id)
79
+ a2_from_db = User.find(a2.id)
80
+
81
+ assert_equal 2, a1_from_db.version , "the document version should be 2"
82
+ assert_equal "roxanne", a1_from_db.name, "the name should have been updated"
83
+ assert_equal 1, a2_from_db.version, "the other documents should not have been affected"
84
+ assert_equal "aditya",a2_from_db.name, "the name of the other document should be the same as before."
85
+
86
+ end
87
+
88
+ def test_versioned_upsert_one_increments_version_if_doc_created
89
+
90
+ a1 = User.new
91
+ a1.name = "bhargav"
92
+ a1.email = "bh@gmail.com"
93
+ set_hash = {}
94
+ a1.as_document.keys.each do |k|
95
+ if k!= "version"
96
+ set_hash[k] = a1.as_document[k]
97
+ end
98
+ end
99
+ puts "set hash is:"
100
+ puts set_hash.to_s
101
+ User.versioned_upsert_one({"_id" => a1.id},{"$set" => set_hash},User)
102
+
103
+ persisted_doc = User.find(a1.id)
104
+ assert_equal 1, persisted_doc.version, "the persisted document version should be one."
105
+
106
+
107
+ end
108
+
109
+
110
+ def test_bypass_versioning_gives_op_success_in_versioned_update
111
+
112
+ a1 = User.new
113
+ a1.name = "bhargav"
114
+ a1.email = "bh@gmail.com"
115
+ a1.versioned_create
116
+
117
+ a1 = User.find(a1.id)
118
+ a1.name = "updated name"
119
+ a1.versioned_update({},true,{},false)
120
+
121
+ updated_doc = User.find(a1.id)
122
+ assert_equal 1, updated_doc.version , "the persisted document version should be one, since we bypassed versioning."
123
+ assert_equal "updated name", updated_doc.name, "the persisted document name should be the updated name"
124
+ assert_equal true, a1.op_success, "the operation should still be successfull"
125
+
126
+ end
127
+
128
+ def test_versioned_upsert_one_does_not_affect_all_docs_if_query_is_empty
129
+
130
+ a1 = User.new
131
+ a1.name = "bhargav"
132
+ a1.email = "bhargav.r.raut@gmail.com"
133
+ a1.versioned_create
134
+
135
+ persisted_document = User.find(a1.id)
136
+
137
+ a = User.versioned_upsert_one({},{"$set" => {"name" => "dog"}},User)
138
+
139
+ assert_equal 1, User.count, "number of documents should not change"
140
+ assert_equal 1, persisted_document.version, "the version of the only existing document should be 1"
141
+ assert_equal "bhargav", persisted_document.name, "the name of the persisted document should be the same"
142
+
143
+ end
144
+
145
+
146
+ ##if the document that is searched for is found, then the setOnInsert part will not be executed, but any "inc" keys in the update hash will be.
147
+ ##we test that this does not happen.
148
+ def test_does_not_increment_version_of_all_existing_document_on_create
149
+
150
+ a = User.new
151
+ a.name = "bhargav"
152
+ a.email = "u@gmail.com"
153
+ a.versioned_create
154
+
155
+ a2 = User.new
156
+ a2.name = "bb"
157
+ a2.email = "bb@gmail.com"
158
+ a2.versioned_create
159
+
160
+ ##here there will be a record with this id already found, so nothing new will be inserted.
161
+ ##but we assert that the versions of all the other records in the database are still maitained at one.
162
+ u1 = User.new
163
+ u1.name = "b2"
164
+ u1.email = "b2@gmail.com"
165
+ u1.versioned_create({"_id" => a.id})
166
+
167
+ assert_equal 2, User.count, "there should be only two users"
168
+ User.all.each do |user|
169
+ assert_equal 1, user.version, "the version of all docs in the database should be one."
170
+ end
171
+
172
+
173
+ end
174
+
175
+ def test_query_in_create
176
+
177
+ u = User.new
178
+ u.name = "bhargav"
179
+ u.email = "u@gmail.com"
180
+ u.versioned_create
181
+
182
+ u1 = User.new
183
+ u1.name = "aditya"
184
+ u1.email = "s@gmail.com"
185
+ u1.versioned_create({"$or" =>
186
+ [
187
+ {"_id" => u1.id},
188
+ {"name" => "bhargav"}
189
+ ]
190
+ })
191
+
192
+ assert_equal 1, User.count, "the user count should be one"
193
+
194
+ end
195
+
196
+
197
+ def test_create_two_users
198
+
199
+ u = User.new
200
+ u.name = "bhargav"
201
+ u.email = "t@gmail.com"
202
+ u.versioned_create
203
+
204
+ u1 = User.new
205
+ u1.name = "dog"
206
+ u1.email = "d@gmail.com"
207
+ u1.versioned_create
208
+
209
+ assert_equal 1, u.version, "the version of the first user should be one"
210
+ assert_equal 1, u1.version, "the version of the second user should be one"
211
+
212
+ end
213
+
214
+ def test_versioned_create
215
+ u = User.new
216
+ u.name = "bhargav"
217
+ u.email = "raut@gmail.com"
218
+ u.versioned_create
219
+
220
+ ##assert that attributes are set on the instance itself.
221
+ assert_equal 1, User.count, "the user count should be one"
222
+ assert_equal 1, u.version, "the version should have incremented"
223
+ assert_equal "bhargav", u.name, "the name should have been persisted"
224
+ assert_equal "raut@gmail.com",u.email, "the email should have been persisted"
225
+ assert_equal true, u.op_success, "the operation should be successfull"
226
+
227
+ ##assert that attributes are set on the document in the database
228
+ u = User.find(u.id)
229
+ assert_equal 1, u.version, "(db)the version should have incremented"
230
+ assert_equal "bhargav", u.name, "(db)the name should have been persisted"
231
+ assert_equal "raut@gmail.com",u.email, "(db)the email should have been persisted"
232
+
233
+ end
234
+
235
+ def test_versioned_create_should_return_doc_counts
236
+ a1 = User.new
237
+ a1.name = "bhargav"
238
+ a1.email = "rrphotosoft@gmail.com"
239
+ a1.versioned_create
240
+ assert_equal 0,a1.matched_count,"should not have any matched documents"
241
+ assert_not_nil(a1.upserted_id,"there should be an upserted id")
242
+ end
243
+
244
+ def test_versioned_create_should_return_matched_count_as_one_if_doc_exists
245
+ a1 = User.new
246
+ a1.name = "bhargav"
247
+ a1.email = "rrphotosoft@gmail.com"
248
+ a1.versioned_create
249
+
250
+ a2 = User.new
251
+ a2.name = "bhargav"
252
+ a2.email = "rrphotosoft@gmail.com"
253
+ a2.versioned_create({"email" => "rrphotosoft@gmail.com"})
254
+ assert_equal 1,a2.matched_count,"the matched count should be one."
255
+ assert_equal 0,a2.modified_count, "the modified count should be zero"
256
+ assert_nil a2.upserted_id,"the upserted id should be nil"
257
+ end
258
+
259
+
260
+ def test_versioned_update_without_providing_dirty_fields
261
+ u = User.new
262
+ u.name = "bhargav"
263
+ u.email = "bhargav.r.raut@gmail.com"
264
+ u.versioned_create
265
+
266
+ ##now update one field.
267
+ u.email = "c@gmail.com"
268
+
269
+
270
+ ##assert that attributes are set on the provided instance.
271
+ u.versioned_update
272
+ assert_equal 2, u.version, "the document version is 2"
273
+ assert_equal "c@gmail.com", u.email, "the document email should have updated"
274
+ assert_equal true, u.op_success, "the operation should be successfull"
275
+
276
+
277
+ ##assert that the attributes are set on the document in the database
278
+ u = User.find(u.id)
279
+ assert_equal 2,u.version,"(db)the version should be 2"
280
+ assert_equal "c@gmail.com",u.email, "(db)the document email should have updated"
281
+
282
+ end
283
+
284
+
285
+
286
+ def test_versioned_update_providing_dirty_fields
287
+ u = User.new
288
+ u.name = "bhargav"
289
+ u.email = "bhargav.r.raut@gmail.com"
290
+ u.versioned_create
291
+
292
+ ##update two fields
293
+ u.email = "c@gmail.com"
294
+ u.name = "doggy"
295
+
296
+ ##provide only one field as dirty
297
+ u.versioned_update({"email" => 1})
298
+
299
+ ##assert that the attributes are set on the present instance.
300
+ assert_equal 2, u.version, "the document version is 2"
301
+ assert_equal "c@gmail.com", u.email, "the document email should have updated"
302
+ assert_equal "bhargav", u.name, "the document name should not have updated, since it was not provided as a dirty field, even if it has changed"
303
+ assert_equal true, u.op_success, "the operation should be successfull"
304
+
305
+
306
+ ##assert that the document in the database is updated.
307
+ u = User.find(u.id)
308
+ assert_equal 2,u.version,"(db)the version should be 2"
309
+ assert_equal "c@gmail.com",u.email, "(db)the document email should have updated"
310
+ assert_equal "bhargav", u.name, "(db)the document name should not have updated, since it was not provided as a dirty field, even if it has changed"
311
+
312
+ end
313
+
314
+
315
+ def test_invalid_documents_are_not_created
316
+
317
+ u = User.new
318
+ u.email = "horse"
319
+ u.name = "caca"
320
+ u.versioned_create
321
+
322
+
323
+ ##assert that the instance does not contain the invalid fields
324
+ assert_equal false, u.op_success, "the op should fail"
325
+
326
+
327
+ ##assert that there is no such document in the database
328
+ assert_equal 0, User.count, "(db)there should be no such record in the datbase"
329
+
330
+
331
+ ##assert that the error is present on the instance.
332
+ assert_not_empty u.errors.full_messages, "there are errors"
333
+
334
+
335
+ end
336
+
337
+
338
+
339
+ def test_versioned_upsert_one
340
+ u = User.new
341
+ u.name = "bhargav"
342
+ u.email = "bhargav.r.raut@gmail.com"
343
+ u.versioned_create
344
+
345
+ ##update
346
+ updated_doc = User.versioned_upsert_one({"_id" => u.id,"version" => u.version},{"$set" => {"email" => "b.raut@gmail.com"}},User)
347
+ u = User.find(u.id)
348
+ assert_equal updated_doc["email"], "b.raut@gmail.com","it should return the updated document"
349
+ assert_equal u.email, "b.raut@gmail.com","the mongoid document should be updated"
350
+
351
+ end
352
+
353
+
354
+
355
+ def test_before_create_callbacks
356
+
357
+ u = User.new
358
+ u.name = "bhargav"
359
+ u.email = "b@gmail.com"
360
+ u.versioned_create
361
+ u = User.find(u.id)
362
+ assert_equal 1,u.before_create_field, "the before create callback should fire"
363
+
364
+ end
365
+
366
+
367
+ def test_after_create_callbacks
368
+
369
+ u = User.new
370
+ u.name = "bhargav"
371
+ u.email = "b@gmail.com"
372
+ u.versioned_create
373
+ assert_equal 1, u.after_create_field, "the after create callbacks should fire"
374
+
375
+ u = User.find(u.id)
376
+ assert_equal 0, u.after_create_field, "the after create callback should not have persisted anything during the save"
377
+
378
+ end
379
+
380
+
381
+ def test_before_update_self_callbacks
382
+ u = User.new
383
+ u.name = "bhargav"
384
+ u.email = "bhargav.r.raut@gmail.com"
385
+ u.versioned_create
386
+ u.email = "updated_email@gmail.com"
387
+ u.versioned_update
388
+ assert_equal 2, u.version, "the document version is 2"
389
+ assert_equal "updated_email@gmail.com", u.email, "the document email should be cacophony"
390
+ assert_equal 1, u.before_update_field, "the before update callback should have fired"
391
+
392
+ end
393
+
394
+
395
+ def test_version_conflict
396
+
397
+ a = User.new
398
+ a.name = "bhargav"
399
+ a.email = "b.r.raut@gmail.com"
400
+ a.versioned_create
401
+
402
+ ##now we send an update, but before that we already update it using upsert.
403
+ ##so that we get a version conflict.
404
+ User.versioned_upsert_one({"_id" => a.id, "version" => a.version},{"$set" => {"email" => "kkk@gmail.com"}},User,false)
405
+
406
+ a.name = "changed_name"
407
+ a.versioned_update
408
+
409
+ ##if the update fails, if there is a validation error, or the document is not persisted, then
410
+
411
+ assert_equal false, a.op_success, "the op should have failed"
412
+
413
+ end
414
+
415
+ =begin
416
+ THESE THREE TESTS HAVE BEEN COMMENTED OUT BECAUSE WE HAVE BLOCKED OUT THE BEFORE_ACTION THAT USED TO PREVIOUSLY FILTER OUT THE VERSION AND OP_SUCCESS FIELDS IF THEY HAD BEEN SET, BUT WE DONT DO THAT ANYMORE, BECAUSE IT LED TO UNPREDICTABLE BEHAVIOUR WHERE FOR EG:
417
+ - FIRST A MODEL IS SAVED USED VERSIONED_CREATE
418
+ - THAT GIVES IT A VERSION
419
+ - THEN YOU MAKE SOME CHANGES ON THAT MODEL AND CALL CONVENTIONAL SAVE ON IT
420
+ - THIS WILL WIPE OUT THE VERSION
421
+ - THEN SUPPOSE YOU AGAIN WANT TO CALL A VERSIONED_CREATE/UPDATE/UPSERT ON THE SAME RECORD
422
+ - IT DOES NOT WORK BECAUSE NOW VERSION TAKES ITS DEFAULT VALUE OF ZERO.
423
+ - TO AVOID THIS PROBLEM, THIS FILTER IS NO LONGER USED, AND HENCE THESE TESTS ARE REDUNDANT.
424
+ - The filter has been commented out in the module.
425
+ def test_version_and_op_success_not_persisted_on_calling_save
426
+
427
+ a = User.new
428
+ a.email = "bhargav.r.raut@gmail.com"
429
+ a.name = "bhargav"
430
+ a.version = 10
431
+ a.op_success = true
432
+ a.save
433
+
434
+
435
+ u = User.find(a.id)
436
+
437
+ assert_equal true, a.save, "the document should get saved"
438
+ assert_nil a.version , "the version should be nil"
439
+ assert_nil a.op_success, "the op success should be nil"
440
+ assert_equal a.email, "bhargav.r.raut@gmail.com","the email should have been persisted"
441
+
442
+ end
443
+
444
+ def test_version_and_op_success_not_persisted_on_calling_create
445
+
446
+ q = User.create(:email => "bhargav.r.raut@gmail.com", :name => "ten", :version => 10, :op_success => false)
447
+ assert_nil q.version, "the version should be nil"
448
+ assert_nil q.op_success, "the op success shoudl be nil"
449
+ assert_equal 1, User.count, "there should be one user document"
450
+
451
+ end
452
+
453
+
454
+ def test_version_and_op_success_not_persisted_on_calling_update
455
+
456
+ a = User.new
457
+ a.email = "bhargav.r.raut@gmail.com"
458
+ a.name = "bhargav"
459
+ a.save
460
+
461
+ a.version = 10
462
+ a.op_success = true
463
+ a.email = "bharg@gmail.com"
464
+ a.update_attributes
465
+
466
+ r = User.find(a.id)
467
+
468
+ assert_nil a.version, "the version on update should be nil"
469
+ assert_nil a.op_success, "the op success on update should be nil"
470
+ assert_equal a.email, "bharg@gmail.com"
471
+ assert_equal r.email, "bharg@gmail.com"
472
+
473
+ end
474
+ =end
475
+
476
+ def test_image_versioned_create
477
+
478
+ a = User.new
479
+ a.image = File.new("/home/bhargav/Github/mongoid_versioned_atomic/test/dummy/app/assets/images/facebook.png")
480
+ a.name = "bhargav"
481
+ a.email = "test@gmail.com"
482
+
483
+ a.versioned_create
484
+ assert_not_nil a.image
485
+
486
+ end
487
+
488
+ def test_image_versioned_update
489
+
490
+ a = User.new
491
+ a.image = File.new("/home/bhargav/Github/mongoid_versioned_atomic/test/dummy/app/assets/images/facebook.png")
492
+ a.name = "bhargav"
493
+ a.email = "vitesse@gmail.com"
494
+ a.versioned_create
495
+
496
+ a.image = File.new("/home/bhargav/Github/mongoid_versioned_atomic/test/dummy/app/assets/images/keratoscope.jpg")
497
+
498
+ a.versioned_update(Hash[User.image_attributes.zip([])])
499
+ assert_not_nil a.image
500
+
501
+ end
502
+
503
+
504
+ def test_bypass_versioning_on_update
505
+
506
+ a = User.new
507
+ a.email = "bhargav.r.raut@gmail.com"
508
+ a.name = "bhargav"
509
+ a.versioned_create
510
+
511
+ a.name = "changed"
512
+ query,update,options = a.versioned_update({},true)
513
+
514
+ assert_equal 1 , a.version, "the version should be one."
515
+ assert_equal "changed", a.name, "the name should have been persisted"
516
+ assert_equal true, query["version"] == nil, "there should be no version parameter in the query."
517
+
518
+ a = User.find(a.id)
519
+
520
+ assert_equal 1, a.version, "(db) the version in the db should be one"
521
+ assert_equal "changed", a.name, "(db) the name in the db should be one."
522
+
523
+ end
524
+
525
+
526
+ def test_passing_optional_update_hash
527
+
528
+ a = User.new
529
+ a.email = "bhargav.r.raut@gmail.com"
530
+ a.name = "bhargav"
531
+ a.versioned_create
532
+
533
+ query,options,update = a.versioned_update({},false,{"$inc" => {"likes" => 1}, "$set" => {"name" => "changed"}})
534
+
535
+ assert_equal 1, a.likes, "the likes should be one."
536
+ assert_equal "changed",a.name, "the name should be changed"
537
+ assert_equal 2, a.version, "the document version is 1"
538
+
539
+
540
+ a = User.find(a.id)
541
+ assert_equal 1, a.likes, "(db)the likes should be one."
542
+ assert_equal "changed",a.name, "(db)the name should be changed"
543
+ assert_equal 2, a.version, "(db)the document version is 1"
544
+
545
+ end
546
+
547
+ def test_passing_optional_update_hash_and_bypass_versioning
548
+
549
+ a = User.new
550
+ a.email = "bhargav.r.raut@gmail.com"
551
+ a.name = "bhargav"
552
+ a.versioned_create
553
+
554
+ query,options,update = a.versioned_update({},true,{"$inc" => {"likes" => 1}, "$set" => {"name" => "changed"}})
555
+
556
+ assert_equal 1, a.likes, "the likes should be one."
557
+ assert_equal "changed",a.name, "the name should be changed"
558
+ assert_equal 1, a.version, "the document version is 1"
559
+ assert_equal true, query["version"] == nil, "there should be no version parameter in the query."
560
+
561
+ a = User.find(a.id)
562
+ assert_equal 1, a.likes, "(db)the likes should be one."
563
+ assert_equal "changed",a.name, "(db)the name should be changed"
564
+ assert_equal 1, a.version, "(db)the document version is 1"
565
+
566
+
567
+ end
568
+
569
+ end