minidoc 1.0.0.rc1 → 1.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,379 @@
1
+ require "spec_helper"
2
+
3
+ describe Minidoc do
4
+ class ValidationsUser < User
5
+ validates :name, presence: true
6
+ end
7
+
8
+ describe "#model_name" do
9
+ it "returns the name of the class" do
10
+ expect(User.model_name.to_s).to eq "User"
11
+ end
12
+ end
13
+
14
+ describe "#to_model" do
15
+ it "returns itself" do
16
+ user = User.new
17
+ expect(user.to_model).to eq user
18
+ end
19
+ end
20
+
21
+ describe "#to_key" do
22
+ it "returns an array of key attributes" do
23
+ user = User.new
24
+ user.id = BSON::ObjectId("52955618f9f6a52444000001")
25
+ expect(user.to_key).to eq ["52955618f9f6a52444000001"]
26
+ end
27
+ end
28
+
29
+ describe "#to_param" do
30
+ it "returns a string suitable for use in URLs" do
31
+ user = User.new
32
+ user.id = BSON::ObjectId("52955618f9f6a52444000001")
33
+ expect(user.to_param).to eq "52955618f9f6a52444000001"
34
+ end
35
+ end
36
+
37
+ describe "#to_partial_path" do
38
+ it "returns a string which ActionPack could use to look up a partial template" do
39
+ expect(User.new.to_partial_path).to eq "users/user"
40
+ end
41
+ end
42
+
43
+ describe ".rescue_duplicate_key_errors" do
44
+ it "returns the value of the block when there are no errors" do
45
+ user = User.create!
46
+ result = Minidoc.rescue_duplicate_key_errors { User.create!(name: "two") }
47
+ expect(result.name).to eq "two"
48
+ end
49
+
50
+ it "returns false when a duplicate key error occurs" do
51
+ user = User.create!
52
+ result = Minidoc.rescue_duplicate_key_errors do
53
+ User.create!(:_id => user.id, name: "two")
54
+ end
55
+ expect(result).to eq false
56
+ end
57
+ end
58
+
59
+ describe ".new" do
60
+ it "sets ids before the document is persisted" do
61
+ user = User.new
62
+ expect(user.id).to be_a BSON::ObjectId
63
+ end
64
+
65
+ it "allows you to provide an id" do
66
+ user = User.new(:_id => BSON::ObjectId("52955cc5f9f6a538a9000001"))
67
+ expect(user.id).to eq BSON::ObjectId("52955cc5f9f6a538a9000001")
68
+ end
69
+ end
70
+
71
+ describe "#new_record?" do
72
+ it "tells you whether the document has been persisted to the database or not" do
73
+ user = User.new
74
+ expect(user.new_record?).to eq true
75
+ user.save
76
+ expect(user.new_record?).to eq false
77
+ end
78
+ end
79
+
80
+ describe "#save" do
81
+ it "persists the document" do
82
+ expect {
83
+ user = User.new
84
+ user.save
85
+ }.to change { User.count }.from(0).to(1)
86
+ end
87
+
88
+ it "doesn't change id when persisted" do
89
+ user = User.new
90
+ expect {
91
+ user.save
92
+ }.to_not change { user.id }
93
+ end
94
+
95
+ it "raises Minidoc::DuplicateKey where appropriate" do
96
+ collection = double
97
+ expect(User).to receive(:collection).and_return(collection)
98
+ expect(collection).to receive(:<<).and_raise(Mongo::OperationFailure.new("Duplicate key exception", Minidoc::DuplicateKey::DUPLICATE_KEY_ERROR_CODE))
99
+ user = User.new
100
+
101
+ expect { user.save }.to raise_error(Minidoc::DuplicateKey)
102
+ end
103
+
104
+ it "doesn't suppress errors it shouldn't" do
105
+ collection = double
106
+ expect(User).to receive(:collection).and_return(collection)
107
+ expect(collection).to receive(:<<).and_raise(ArgumentError)
108
+ user = User.new
109
+
110
+ expect { user.save }.to raise_error(ArgumentError)
111
+ end
112
+
113
+ it "persists changes to the database" do
114
+ user = User.create(name: "Bryan")
115
+ expect(user.persisted?).to be true
116
+
117
+ user.name = "Noah"
118
+ expect(user.name).to eq "Noah"
119
+ user.save
120
+
121
+ expect(user.reload.name).to eq "Noah"
122
+ end
123
+
124
+ it "doesn't persist changes when the validations aren't satisfied" do
125
+ user = ValidationsUser.new
126
+ expect(user.save).to be false
127
+ expect(user.new_record?).to be true
128
+ expect(user.errors[:name]).to eq ["can't be blank"]
129
+ end
130
+
131
+ it "isn't thrown off when two classes are backed by the same collection" do
132
+ user = User.create(name: "Bryan", age: 20)
133
+ user.name = "Noah"
134
+ expect(user.name).to eq "Noah"
135
+ user.save
136
+ user.reload
137
+ expect(user.name).to eq "Noah"
138
+ expect(user.age).to eq 20
139
+
140
+ second_user = SecondUser.find_one(age: 20)
141
+ expect(user.id).to eq second_user.id
142
+ second_user.age = 21
143
+ expect(second_user.age).to eq 21
144
+ second_user.save
145
+ expect(second_user.reload.age).to eq 21
146
+
147
+ user.reload
148
+ expect(user.name).to eq "Noah"
149
+ expect(user.age).to eq 21
150
+ end
151
+ end
152
+
153
+ describe "#save!" do
154
+ it "persists the change to the database" do
155
+ user = User.create!(name: "Bryan")
156
+ user.name = "Noah"
157
+ user.save!
158
+
159
+ expect(user.reload.name).to eq "Noah"
160
+ end
161
+
162
+ it "does not persist the change to the database when a validation is not satisfied" do
163
+ expect {
164
+ ValidationsUser.new.save!
165
+ }.to raise_error(Minidoc::RecordInvalid)
166
+ end
167
+ end
168
+
169
+ describe "#valid?" do
170
+ it "checks that that validations are satisfied" do
171
+ user = ValidationsUser.new
172
+ expect(user).to_not be_valid
173
+
174
+ user.name = "Noah"
175
+ expect(user).to be_valid
176
+ end
177
+ end
178
+
179
+ describe "#persisted?" do
180
+ let(:user) { User.new }
181
+
182
+ it "knows new records are not persisted" do
183
+ expect(user.persisted?).to eq false
184
+ end
185
+
186
+ it "knows saved records are persisted" do
187
+ user.save
188
+ expect(user.persisted?).to eq true
189
+ end
190
+
191
+ it "knows deleted records are not persisted" do
192
+ user.save!
193
+ user.destroy
194
+ expect(user.persisted?).to eq false
195
+ end
196
+ end
197
+
198
+ describe ".create!" do
199
+ it "inserts a document into the database" do
200
+ user = User.create!(name: "Bryan")
201
+ expect(user.name).to eq "Bryan"
202
+ expect(user.persisted?).to be true
203
+ expect(User.count).to eq 1
204
+ end
205
+
206
+ it "raises an error when a validation is not satisfied" do
207
+ expect { ValidationsUser.create! }.to raise_error(Minidoc::RecordInvalid)
208
+ expect(User.count).to eq 0
209
+ end
210
+ end
211
+
212
+ describe ".create" do
213
+ it "inserts a document into the database" do
214
+ user = User.create(name: "Bryan")
215
+ expect(user.name).to eq "Bryan"
216
+ expect(User.count).to eq 1
217
+ end
218
+
219
+ it "does not insert a document when a validation is not satisfied" do
220
+ user = ValidationsUser.create
221
+ expect(user.persisted?).to be false
222
+ expect(ValidationsUser.count).to eq 0
223
+ end
224
+ end
225
+
226
+ describe "#destroy, #destroyed?" do
227
+ it "removes the document from the collection, and the document knows it" do
228
+ user = User.create!(name: "Bryan")
229
+ expect(user.destroyed?).to eq false
230
+
231
+ user.destroy
232
+
233
+ expect(User.count).to eq 0
234
+ expect(user.destroyed?).to eq true
235
+ end
236
+ end
237
+
238
+ describe ".delete" do
239
+ it "removes a document by id" do
240
+ user = User.create!(name: "Bryan")
241
+
242
+ User.delete(user.id)
243
+
244
+ expect(User.count).to eq 0
245
+
246
+ expect { user.reload }.to raise_error(Minidoc::DocumentNotFoundError)
247
+ end
248
+ end
249
+
250
+ describe "#delete" do
251
+ it "removes itself from the collection" do
252
+ user = User.create!(name: "Bryan")
253
+ user.delete
254
+ expect(User.count).to eq 0
255
+ end
256
+ end
257
+
258
+ describe "#reload" do
259
+ it "refreshes the object with any changed data from the underlying database" do
260
+ user = User.create!(name: "Bryan")
261
+ expect(user.reload.name).to eq "Bryan"
262
+
263
+ User.collection.update({ :_id => user.id }, name: "Noah")
264
+ expect(user.name).to eq "Bryan"
265
+
266
+ user.reload
267
+ expect(user.name).to eq "Noah"
268
+ end
269
+ end
270
+
271
+ describe ".set" do
272
+ it "sets a field on a document with a provided id" do
273
+ user = User.create!(name: "Bryan")
274
+ User.set(user.id, name: "Noah")
275
+ expect(user.name).to eq "Bryan"
276
+ expect(user.reload.name).to eq "Noah"
277
+ end
278
+
279
+ it "allows passing an id as a string rather than a BSON::ObjectId" do
280
+ user = User.create!(name: "Noah")
281
+ User.set(user.id.to_s, name: "Mike")
282
+ expect(user.reload.name).to eq "Mike"
283
+ expect(User.first.name).to eq "Mike"
284
+ end
285
+ end
286
+
287
+ describe "#set" do
288
+ it "changes the field (without needing to call save)" do
289
+ user = User.create!(name: "Bryan")
290
+ user.set(name: "Noah")
291
+ expect(user.name).to eq "Noah"
292
+ expect(user.reload.name).to eq "Noah"
293
+ expect(User.first.name).to eq "Noah"
294
+ end
295
+
296
+ it "allows changing the field, referenced as a string" do
297
+ user = User.create(name: "Bryan")
298
+ user.set("name" => "Noah")
299
+ expect(user.name).to eq "Noah"
300
+ expect(user.reload.name).to eq "Noah"
301
+ expect(User.first.name).to eq "Noah"
302
+ end
303
+
304
+ it "bypasses validations, so be careful" do
305
+ user = ValidationsUser.create!(name: "Bryan")
306
+ expect(user).to be_valid
307
+
308
+ user.set(name: nil)
309
+ user.reload
310
+
311
+ expect(user).to_not be_valid
312
+ end
313
+ end
314
+
315
+ describe ".unset" do
316
+ it "allows removing a field for a document with the provided id" do
317
+ user = User.create!(name: "Bryan")
318
+ User.unset(user.id, :name)
319
+ expect(user.name).to eq "Bryan"
320
+ expect(user.reload.name).to eq nil
321
+ expect(User.first.name).to eq nil
322
+ end
323
+
324
+ it "doesn't mind if the provided id is a string rather than a BSON::ObjectId" do
325
+ user = User.create!(name: "Bryan")
326
+ User.unset(user.id.to_s, :name)
327
+ expect(user.reload.name).to eq nil
328
+ expect(User.first.name).to eq nil
329
+ end
330
+ end
331
+
332
+ describe "#unset" do
333
+ it "removes a field for a document" do
334
+ user = User.create!(name: "Bryan")
335
+ user.unset(:name)
336
+ expect(user.name).to eq nil
337
+ expect(user.reload.name).to eq nil
338
+ expect(User.first.name).to eq nil
339
+ end
340
+
341
+ it "doesn't mind if you reference the field as a string" do
342
+ user = User.create!(name: "Bryan")
343
+ user.unset("name")
344
+ expect(user.name).to eq nil
345
+ expect(user.reload.name).to eq nil
346
+ end
347
+ end
348
+
349
+ describe "#atomic_set" do
350
+ it "updates the document (and not others) as long as the provided query is satisfied" do
351
+ user = User.create!(name: "from", age: 18)
352
+ other_user = User.create!(name: "from", age: 21)
353
+
354
+ expect(user.atomic_set({ name: "from" }, name: "to")).to be_truthy
355
+
356
+ expect(user.name).to eq "to"
357
+ expect(user.age).to eq 18
358
+ user.reload
359
+ expect(user.name).to eq "to"
360
+ expect(user.age).to eq 18
361
+
362
+ other_user.reload
363
+ expect(other_user.name).to eq "from"
364
+ expect(other_user.age).to eq 21
365
+ end
366
+
367
+ it "does not update the document when the provided query is not satisfied" do
368
+ user = User.create!(name: "from", age: 18)
369
+
370
+ expect(user.atomic_set({ name: "not-from" }, name: "to")).to be_falsey
371
+
372
+ expect(user.name).to eq "from"
373
+ expect(user.age).to eq 18
374
+ user.reload
375
+ expect(user.name).to eq "from"
376
+ expect(user.age).to eq 18
377
+ end
378
+ end
379
+ end
@@ -5,8 +5,6 @@ end
5
5
 
6
6
  require "minidoc"
7
7
  require "minidoc/test_helpers"
8
- require "minitest/autorun"
9
- require "mocha/mini_test"
10
8
 
11
9
  I18n.enforce_available_locales = false
12
10
  I18n.load_path << File.expand_path("../locale/en.yml", __FILE__)
@@ -28,8 +26,16 @@ $mongo = Mongo::MongoClient.from_uri(ENV["MONGODB_URI"] || "mongodb://localhost"
28
26
  Minidoc.connection = $mongo
29
27
  Minidoc.database_name = "minidoc_test"
30
28
 
31
- class Minidoc::TestCase < Minitest::Test
32
- def setup
29
+ RSpec.configure do |config|
30
+ if config.files_to_run.one?
31
+ config.default_formatter = "doc"
32
+ end
33
+
34
+ config.order = :random
35
+
36
+ Kernel.srand config.seed
37
+
38
+ config.before(:each) do
33
39
  Minidoc::TestHelpers.clear_database
34
40
  end
35
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minidoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc1
4
+ version: 1.0.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Helmkamp
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-09-29 00:00:00.000000000 Z
12
+ date: 2016-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -95,6 +95,7 @@ extra_rdoc_files: []
95
95
  files:
96
96
  - ".codeclimate.yml"
97
97
  - ".gitignore"
98
+ - ".rspec"
98
99
  - ".rubocop.yml"
99
100
  - ".ruby-version"
100
101
  - ".travis.yml"
@@ -120,21 +121,17 @@ files:
120
121
  - lib/minidoc/value.rb
121
122
  - lib/minidoc/version.rb
122
123
  - minidoc.gemspec
123
- - test/activemodel_test.rb
124
- - test/belongs_to_test.rb
125
- - test/connection_test.rb
126
- - test/counters_test.rb
127
- - test/duplicate_key_test.rb
128
- - test/first_run_test.rb
129
- - test/grid_test.rb
130
- - test/helper.rb
131
- - test/locale/en.yml
132
- - test/persistence_test.rb
133
- - test/query_test.rb
134
- - test/read_only_test.rb
135
- - test/timestamps_test.rb
136
- - test/uniqueness_validator_test.rb
137
- - test/validations_test.rb
124
+ - spec/locale/en.yml
125
+ - spec/minidoc/associations_spec.rb
126
+ - spec/minidoc/connection_spec.rb
127
+ - spec/minidoc/counters_spec.rb
128
+ - spec/minidoc/finders_spec.rb
129
+ - spec/minidoc/grid_spec.rb
130
+ - spec/minidoc/read_only_spec.rb
131
+ - spec/minidoc/timestamps_spec.rb
132
+ - spec/minidoc/validations_spec.rb
133
+ - spec/minidoc_spec.rb
134
+ - spec/spec_helper.rb
138
135
  homepage: https://github.com/codeclimate/minidoc
139
136
  licenses:
140
137
  - MIT
@@ -160,18 +157,14 @@ signing_key:
160
157
  specification_version: 4
161
158
  summary: Lightweight wrapper for MongoDB documents
162
159
  test_files:
163
- - test/activemodel_test.rb
164
- - test/belongs_to_test.rb
165
- - test/connection_test.rb
166
- - test/counters_test.rb
167
- - test/duplicate_key_test.rb
168
- - test/first_run_test.rb
169
- - test/grid_test.rb
170
- - test/helper.rb
171
- - test/locale/en.yml
172
- - test/persistence_test.rb
173
- - test/query_test.rb
174
- - test/read_only_test.rb
175
- - test/timestamps_test.rb
176
- - test/uniqueness_validator_test.rb
177
- - test/validations_test.rb
160
+ - spec/locale/en.yml
161
+ - spec/minidoc/associations_spec.rb
162
+ - spec/minidoc/connection_spec.rb
163
+ - spec/minidoc/counters_spec.rb
164
+ - spec/minidoc/finders_spec.rb
165
+ - spec/minidoc/grid_spec.rb
166
+ - spec/minidoc/read_only_spec.rb
167
+ - spec/minidoc/timestamps_spec.rb
168
+ - spec/minidoc/validations_spec.rb
169
+ - spec/minidoc_spec.rb
170
+ - spec/spec_helper.rb
@@ -1,28 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class ActiveModelTest < Minidoc::TestCase
4
- def test_model_name
5
- assert_equal "User", User.model_name.to_s
6
- end
7
-
8
- def test_to_model
9
- user = User.new
10
- assert_equal user, user.to_model
11
- end
12
-
13
- def test_to_key
14
- user = User.new
15
- user.id = BSON::ObjectId("52955618f9f6a52444000001")
16
- assert_equal ["52955618f9f6a52444000001"], user.to_key
17
- end
18
-
19
- def test_to_param
20
- user = User.new
21
- user.id = BSON::ObjectId("52955618f9f6a52444000001")
22
- assert_equal "52955618f9f6a52444000001", user.to_param
23
- end
24
-
25
- def test_to_path
26
- assert_equal "users/user", User.new.to_partial_path
27
- end
28
- end
@@ -1,82 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class BelongsToTest < Minidoc::TestCase
4
- class Cat < Minidoc
5
- include Minidoc::Associations
6
- belongs_to :owner, class_name: "User"
7
- end
8
-
9
- class ::Dog < Minidoc
10
- include Minidoc::Associations
11
- belongs_to :user
12
- end
13
-
14
- class User < ::User
15
- end
16
-
17
- module Animal
18
- class Armadillo < Minidoc
19
- include Minidoc::Associations
20
- belongs_to :predator
21
- end
22
-
23
- class Predator < Minidoc
24
- end
25
- end
26
-
27
- def test_loading
28
- assert_nil Cat.new.owner
29
- user = User.create
30
- cat = Cat.new(owner_id: user.id)
31
- assert_equal user.id, cat.owner.id
32
- cat.save
33
- assert_equal user.id, cat.owner.id
34
- end
35
-
36
- def test_caching
37
- user = User.create(name: "Bryan")
38
- cat = Cat.create(owner_id: user.id)
39
- assert_equal "Bryan", cat.owner.name
40
- user.set(name: "Noah")
41
- assert_equal "Bryan", cat.owner.name # doesn't change
42
- assert_equal "Noah", cat.reload.owner.name # changes
43
- end
44
-
45
- def test_cache_expiry_on_field_update
46
- user = User.create(name: "Bryan")
47
- cat = Cat.create(owner_id: user.id)
48
- assert_equal "Bryan", cat.owner.name
49
- user.set(name: "Noah")
50
- assert_equal "Bryan", cat.owner.name # doesn't change
51
- cat.owner = user
52
- assert_equal "Noah", cat.owner.name # changes
53
- end
54
-
55
- def test_cache_expiry_on_id_update
56
- user = User.create(name: "Bryan")
57
- cat = Cat.create(owner_id: user.id)
58
- assert_equal "Bryan", cat.owner.name
59
- user.set(name: "Noah")
60
- assert_equal "Bryan", cat.owner.name # doesn't change
61
- cat.owner_id = user.id
62
- assert_equal "Noah", cat.owner.name # changes
63
- end
64
-
65
- def test_top_level_inferred_class_name
66
- assert_nil Dog.new.user
67
- user = User.create
68
- sam = Dog.new(user_id: user.id)
69
- assert_equal user.id, sam.user.id
70
- sam.save
71
- assert_equal user.id, sam.user.id
72
- end
73
-
74
- def test_nested_inferred_class_name
75
- assert_nil Animal::Armadillo.new.predator
76
- predator = Animal::Predator.create
77
- arnie = Animal::Armadillo.new(predator_id: predator.id)
78
- assert_equal predator.id, arnie.predator.id
79
- arnie.save
80
- assert_equal predator.id, arnie.predator.id
81
- end
82
- end
@@ -1,20 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class ConnectionTest < Minidoc::TestCase
4
- class Company < Minidoc
5
- self.collection_name = "accounts"
6
- end
7
-
8
- def test_collection_name
9
- assert_equal "users", User.collection_name
10
- assert_equal "accounts", Company.collection_name
11
- end
12
-
13
- def test_collection
14
- assert_equal "users", User.collection.name
15
- end
16
-
17
- def test_database
18
- assert_equal "minidoc_test", User.database.name
19
- end
20
- end
@@ -1,48 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class CountersTest < Minidoc::TestCase
4
- class SimpleCounter < Minidoc
5
- include Minidoc::Counters
6
-
7
- counter :counter
8
- end
9
-
10
- class AdvancedCounter < Minidoc
11
- include Minidoc::Counters
12
-
13
- counter :counter, start: 2, step_size: 3
14
- end
15
-
16
- def test_incrementing
17
- x = SimpleCounter.create!
18
-
19
- assert_equal 0, x.counter
20
- assert_equal 1, x.increment_counter
21
- assert_equal 2, x.increment_counter
22
- assert_equal 3, x.increment_counter
23
- assert_equal 3, x.reload.counter
24
- end
25
-
26
- def test_options
27
- x = AdvancedCounter.create!
28
-
29
- assert_equal 2, x.counter
30
- assert_equal 5, x.increment_counter
31
- assert_equal 8, x.increment_counter
32
- assert_equal 11, x.increment_counter
33
- assert_equal 11, x.reload.counter
34
- end
35
-
36
- def test_threading
37
- x = SimpleCounter.create!
38
- counters = []
39
-
40
- [
41
- Thread.new { 5.times { counters << x.increment_counter } },
42
- Thread.new { 5.times { counters << x.increment_counter } },
43
- Thread.new { 5.times { counters << x.increment_counter } },
44
- ].map(&:join)
45
-
46
- assert_equal 15, counters.uniq.length
47
- end
48
- end
@@ -1,21 +0,0 @@
1
- require File.expand_path("../helper", __FILE__)
2
-
3
- class DuplicateKeyTest < Minidoc::TestCase
4
- def test_rescue_duplicate_key_errors_result
5
- user = User.create!
6
- result = Minidoc.rescue_duplicate_key_errors do
7
- User.create!(name: "two")
8
- end
9
-
10
- assert_equal "two", result.name
11
- end
12
-
13
- def test_rescue_duplicate_key_errors_false
14
- user = User.create!
15
- result = Minidoc.rescue_duplicate_key_errors do
16
- User.create!(_id: user.id, name: "two")
17
- end
18
-
19
- assert_equal false, result
20
- end
21
- end