couchrest_model 2.2.0.beta1 → 2.2.0.beta2

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.
@@ -185,72 +185,6 @@ describe CouchRest::Model::Designs do
185
185
 
186
186
  end
187
187
 
188
- describe "a model class with database provided manually" do
189
-
190
- class Unattached < CouchRest::Model::Base
191
- property :title
192
- property :questions
193
- property :professor
194
- design do
195
- view :by_title
196
- end
197
-
198
- # Force the database to always be nil
199
- def self.database
200
- nil
201
- end
202
- end
203
-
204
- before(:all) do
205
- reset_test_db!
206
- @db = DB
207
- %w{aaa bbb ddd eee}.each do |title|
208
- u = Unattached.new(:title => title)
209
- u.database = @db
210
- u.save
211
- @first_id ||= u.id
212
- end
213
- end
214
- it "should barf on all if no database given" do
215
- expect do
216
- Unattached.all.first
217
- end.to raise_error(CouchRest::Model::DatabaseNotDefined)
218
- end
219
- it "should query all" do
220
- rs = Unattached.all.database(@db).all
221
- expect(rs.length).to eql(4)
222
- end
223
- it "should barf on query if no database given" do
224
- expect() { Unattached.by_title.all }.to raise_error(CouchRest::Model::DatabaseNotDefined)
225
- end
226
- it "should make the design doc upon first query" do
227
- Unattached.by_title.database(@db)
228
- doc = Unattached.design_doc
229
- expect(doc['views']['all']['map']).to include('Unattached')
230
- end
231
- it "should merge query params" do
232
- rs = Unattached.by_title.database(@db).startkey("bbb").endkey("eee")
233
- expect(rs.length).to eq(3)
234
- end
235
- it "should get from specific database" do
236
- u = Unattached.get(@first_id, @db)
237
- expect(u.title).to eq("aaa")
238
- end
239
- it "should barf on first if no database given" do
240
- expect{ Unattached.first }.to raise_error(CouchRest::Model::DatabaseNotDefined)
241
- end
242
- it "should get first with database set" do
243
- u = Unattached.all.database(@db).first
244
- expect(u.title).to match(/\A...\z/)
245
- expect(u.database).to eql(@db)
246
- end
247
- it "should get last" do
248
- u = Unattached.all.database(@db).last
249
- expect(u.title).to eq("aaa")
250
- end
251
-
252
- end
253
-
254
188
  describe "a model with a compound key view" do
255
189
  before(:all) do
256
190
  reset_test_db!
@@ -485,6 +485,13 @@ describe CouchRest::Model::Persistence do
485
485
  i = Article.create!(:title => "Reload return self")
486
486
  expect(i.reload).to be(i)
487
487
  end
488
+
489
+ it "should raise DocumentNotFound if doc has been deleted" do
490
+ i = Article.create!(:title => "Reload deleted")
491
+ dup = Article.find(i.id)
492
+ dup.destroy
493
+ expect { i.reload }.to raise_error(CouchRest::Model::DocumentNotFound)
494
+ end
488
495
  end
489
496
 
490
497
  describe ".model_type_value" do
@@ -61,6 +61,23 @@ describe CouchRest::Model::Proxyable do
61
61
  allow(@class).to receive(:proxy_database_suffixes).and_return({ assoc: 'suffix' })
62
62
  expect(@obj.proxy_database(:assoc).name).to eql('couchrest-proxy-suffix')
63
63
  end
64
+
65
+ context "when use_database is set" do
66
+ before do
67
+ @class = Class.new(CouchRest::Model::Base)
68
+ @class.class_eval do
69
+ use_database "another_database"
70
+ def slug; 'proxy'; end
71
+ end
72
+ @obj = @class.new
73
+ end
74
+
75
+ it "should provide proxy database from method not considering use database" do
76
+ expect(@class).to receive(:proxy_database_method).at_least(:twice).and_return(:slug)
77
+ expect(@obj.proxy_database(:assoc)).to be_a(CouchRest::Database)
78
+ expect(@obj.proxy_database(:assoc).name).to eql('couchrest_proxy')
79
+ end
80
+ end
64
81
  end
65
82
 
66
83
  describe "class methods" do
@@ -312,17 +329,35 @@ describe CouchRest::Model::Proxyable do
312
329
  end
313
330
 
314
331
  it "should proxy #get" do
315
- expect(@model).to receive(:get).with(32, 'database')
332
+ expect(@model).to receive(:fetch_and_build_from_database).with(32, 'database')
316
333
  expect(@obj).to receive(:proxy_update)
317
334
  @obj.get(32)
318
335
  end
319
336
 
337
+ it "should proxy #get and catch missing doc error" do
338
+ expect(@model).to receive(:fetch_and_build_from_database).with(32, 'database').and_raise(CouchRest::Model::DocumentNotFound)
339
+ expect(@obj.get(32)).to be_nil
340
+ end
341
+
342
+
320
343
  it "should proxy #find" do
321
- expect(@model).to receive(:get).with(32, 'database')
344
+ expect(@model).to receive(:fetch_and_build_from_database).with(32, 'database')
322
345
  expect(@obj).to receive(:proxy_update)
323
346
  @obj.find(32)
324
347
  end
325
348
 
349
+ it "should proxy #get!" do
350
+ expect(@model).to receive(:fetch_and_build_from_database).with(32, 'database')
351
+ expect(@obj).to receive(:proxy_update)
352
+ @obj.get!(32)
353
+ end
354
+
355
+ it "should proxy #find!" do
356
+ expect(@model).to receive(:fetch_and_build_from_database).with(32, 'database')
357
+ expect(@obj).to receive(:proxy_update)
358
+ @obj.find!(32)
359
+ end
360
+
326
361
  it "should proxy factory methods" do
327
362
  expect(@model).to receive(:factory_method).with(@obj, "arg_1", "arg_2")
328
363
  @obj.factory_method("arg_1", "arg_2")
@@ -0,0 +1,31 @@
1
+
2
+ require "spec_helper"
3
+
4
+ describe CouchRest::Model::ServerPool do
5
+
6
+ subject { CouchRest::Model::ServerPool }
7
+
8
+ describe ".instance" do
9
+
10
+ it "should provide a singleton" do
11
+ expect(subject.instance).to be_a(CouchRest::Model::ServerPool)
12
+ end
13
+
14
+ end
15
+
16
+ describe "#[url]" do
17
+
18
+ it "should provide a server object" do
19
+ srv = subject.instance[COUCHHOST]
20
+ expect(srv).to be_a(CouchRest::Server)
21
+ end
22
+
23
+ it "should always provide same object" do
24
+ srv = subject.instance[COUCHHOST]
25
+ srv2 = subject.instance[COUCHHOST]
26
+ expect(srv.object_id).to eql(srv2.object_id)
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe CouchRest::Model::Support::Database do
4
+
5
+ describe "#delete!" do
6
+
7
+ it "should empty design cache after a database is destroyed" do
8
+ Thread.current[:couchrest_design_cache] = { :foo => :bar }
9
+ DB.delete!
10
+ expect(Thread.current[:couchrest_design_cache]).to be_empty
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -1,6 +1,36 @@
1
-
2
1
  require 'spec_helper'
3
2
 
3
+ class MigrateModel < CouchRest::Model::Base
4
+ use_database :migrations
5
+ proxy_database_method :id
6
+ proxy_for :migrate_proxy_models
7
+ property :name
8
+ property :value
9
+ design { view :by_name }
10
+ end
11
+
12
+ class MigrateProxyModel < CouchRest::Model::Base
13
+ proxied_by :migrate_model
14
+ proxy_database_method :id
15
+ proxy_for :migrate_proxy_nested_models
16
+ property :name
17
+ property :value
18
+ design { view :by_name }
19
+ end
20
+
21
+ class MigrateProxyNestedModel < CouchRest::Model::Base
22
+ proxied_by :migrate_proxy_model
23
+ property :name
24
+ property :value
25
+ design { view :by_name }
26
+ end
27
+
28
+ RSpec::Matchers.define :database_matching do |database|
29
+ match do |actual|
30
+ actual.server == database.server && actual.name == database.name
31
+ end
32
+ end
33
+
4
34
  describe CouchRest::Model::Utils::Migrate do
5
35
 
6
36
  before :each do
@@ -11,6 +41,7 @@ describe CouchRest::Model::Utils::Migrate do
11
41
  it "should not do anything if Rails is not available" do
12
42
  @module.load_all_models
13
43
  end
44
+
14
45
  it "should detect if Rails is available and require models" do
15
46
  Rails = double()
16
47
  allow(Rails).to receive(:root).and_return("")
@@ -22,4 +53,108 @@ describe CouchRest::Model::Utils::Migrate do
22
53
  end
23
54
  end
24
55
 
56
+ describe "migrations" do
57
+ let!(:stdout) { $stdout }
58
+ before :each do
59
+ allow(CouchRest::Model::Base).to receive(:subclasses).and_return([MigrateModel, MigrateProxyModel, MigrateProxyNestedModel])
60
+ $stdout = StringIO.new
61
+ end
62
+
63
+ after :each do
64
+ $stdout = stdout
65
+ end
66
+
67
+ describe "#all_models" do
68
+ it "should migrate root subclasses of CouchRest::Model::Base" do
69
+ expect(MigrateModel.design_docs.first).to receive(:migrate)
70
+ @module.all_models
71
+ end
72
+
73
+ it "shouldn't migrate proxied subclasses with of CouchRest::Model::Base" do
74
+ expect(MigrateProxyModel.design_docs.first).not_to receive(:migrate)
75
+ expect(MigrateProxyNestedModel.design_docs.first).not_to receive(:migrate)
76
+ @module.all_models
77
+ end
78
+
79
+ context "migration design docs" do
80
+ before :each do
81
+ @module.all_models
82
+ @design_doc = MigrateModel.design_doc
83
+ end
84
+
85
+ it "shouldn't modify the original design doc if activate is false" do
86
+ @design_doc.create_view(:by_name_and_id)
87
+ @module.all_models(activate: false)
88
+
89
+ fetched_ddoc = MigrateModel.get(@design_doc.id)
90
+ expect(fetched_ddoc['views']).not_to have_key('by_name_and_id')
91
+ end
92
+
93
+ it "should remove a leftover migration doc" do
94
+ @design_doc.create_view(:by_name_and_value)
95
+ @module.all_models(activate: false)
96
+
97
+ expect(MigrateModel.get("#{@design_doc.id}_migration")).not_to be_nil
98
+ @module.all_models
99
+ expect(MigrateModel.get("#{@design_doc.id}_migration")).to be_nil
100
+ end
101
+ end
102
+ end
103
+
104
+ describe "#all_models_and_proxies" do
105
+ before :each do
106
+ # clear data from previous test runs
107
+ MigrateModel.all.each do |mm|
108
+ next if mm.nil?
109
+ mm.migrate_proxy_models.all.each do |mpm|
110
+ mpm.migrate_proxy_nested_models.database.delete! rescue nil
111
+ end rescue nil
112
+ mm.migrate_proxy_models.database.delete!
113
+ mm.destroy rescue nil
114
+ end
115
+ MigrateModel.database.recreate!
116
+ end
117
+
118
+ it "should migrate first level proxied subclasses of CouchRest::Model::Base" do
119
+ mm = MigrateModel.new(name: "Migration").save
120
+ expect(MigrateProxyModel.design_docs.first).to receive(:migrate).with(database_matching(mm.migrate_proxy_models.database)).and_call_original
121
+ @module.all_models_and_proxies
122
+ end
123
+
124
+ it "should migrate the second level proxied subclasses of CouchRest::Model::Base" do
125
+ mm = MigrateModel.new(name: "Migration").save
126
+ mpm = mm.migrate_proxy_models.new(name: "Migration Proxy").save
127
+ expect(MigrateProxyNestedModel.design_docs.first).to receive(:migrate).with(database_matching(mpm.migrate_proxy_nested_models.database))
128
+ @module.all_models_and_proxies
129
+ end
130
+
131
+ context "migration design docs" do
132
+ before :each do
133
+ @mm_instance = MigrateModel.new(name: "Migration").save
134
+ mpm = @mm_instance.migrate_proxy_models.new(name: "Migration Proxy")
135
+
136
+ @module.all_models_and_proxies
137
+
138
+ @design_doc = MigrateProxyModel.design_doc
139
+ end
140
+
141
+ it "shouldn't modify the original design doc if activate is false" do
142
+ @design_doc.create_view(:by_name_and_id)
143
+ @module.all_models_and_proxies(activate: false)
144
+
145
+ fetched_ddoc = @mm_instance.migrate_proxy_models.get(@design_doc.id)
146
+ expect(fetched_ddoc['views']).not_to have_key('by_name_and_id')
147
+ end
148
+
149
+ it "should remove a leftover migration doc" do
150
+ @design_doc.create_view(:by_name_and_value)
151
+ @module.all_models_and_proxies(activate: false)
152
+
153
+ expect(@mm_instance.migrate_proxy_models.get("#{@design_doc.id}_migration")).not_to be_nil
154
+ @module.all_models_and_proxies
155
+ expect(@mm_instance.migrate_proxy_models.get("#{@design_doc.id}_migration")).to be_nil
156
+ end
157
+ end
158
+ end
159
+ end
25
160
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchrest_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0.beta1
4
+ version: 2.2.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2016-08-20 00:00:00.000000000 Z
15
+ date: 2017-12-03 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: couchrest
@@ -20,14 +20,14 @@ dependencies:
20
20
  requirements:
21
21
  - - '='
22
22
  - !ruby/object:Gem::Version
23
- version: 2.0.0
23
+ version: 2.0.1
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
28
  - - '='
29
29
  - !ruby/object:Gem::Version
30
- version: 2.0.0
30
+ version: 2.0.1
31
31
  - !ruby/object:Gem::Dependency
32
32
  name: activemodel
33
33
  requirement: !ruby/object:Gem::Requirement
@@ -105,6 +105,9 @@ dependencies:
105
105
  - - ">="
106
106
  - !ruby/object:Gem::Version
107
107
  version: 0.8.0
108
+ - - "<"
109
+ - !ruby/object:Gem::Version
110
+ version: '11.0'
108
111
  type: :development
109
112
  prerelease: false
110
113
  version_requirements: !ruby/object:Gem::Requirement
@@ -112,6 +115,9 @@ dependencies:
112
115
  - - ">="
113
116
  - !ruby/object:Gem::Version
114
117
  version: 0.8.0
118
+ - - "<"
119
+ - !ruby/object:Gem::Version
120
+ version: '11.0'
115
121
  - !ruby/object:Gem::Dependency
116
122
  name: test-unit
117
123
  requirement: !ruby/object:Gem::Requirement
@@ -188,9 +194,11 @@ files:
188
194
  - ".gitignore"
189
195
  - ".rspec"
190
196
  - ".travis.yml"
197
+ - Dockerfile
191
198
  - Gemfile
192
199
  - Gemfile.activesupport-4.x
193
200
  - Gemfile.activesupport-5.x
201
+ - Guardfile
194
202
  - LICENSE
195
203
  - README.md
196
204
  - Rakefile
@@ -200,6 +208,7 @@ files:
200
208
  - benchmarks/connections.rb
201
209
  - benchmarks/dirty.rb
202
210
  - couchrest_model.gemspec
211
+ - docker-compose.yml
203
212
  - history.md
204
213
  - init.rb
205
214
  - lib/couchrest/model.rb
@@ -210,6 +219,7 @@ files:
210
219
  - lib/couchrest/model/casted_by.rb
211
220
  - lib/couchrest/model/configuration.rb
212
221
  - lib/couchrest/model/connection.rb
222
+ - lib/couchrest/model/connection_config.rb
213
223
  - lib/couchrest/model/core_extensions/hash.rb
214
224
  - lib/couchrest/model/core_extensions/time_parsing.rb
215
225
  - lib/couchrest/model/design.rb
@@ -227,6 +237,7 @@ files:
227
237
  - lib/couchrest/model/property.rb
228
238
  - lib/couchrest/model/property_protection.rb
229
239
  - lib/couchrest/model/proxyable.rb
240
+ - lib/couchrest/model/server_pool.rb
230
241
  - lib/couchrest/model/support/couchrest_database.rb
231
242
  - lib/couchrest/model/translation.rb
232
243
  - lib/couchrest/model/typecast.rb
@@ -281,6 +292,7 @@ files:
281
292
  - spec/unit/casted_array_spec.rb
282
293
  - spec/unit/casted_spec.rb
283
294
  - spec/unit/configuration_spec.rb
295
+ - spec/unit/connection_config_spec.rb
284
296
  - spec/unit/connection_spec.rb
285
297
  - spec/unit/core_extensions/time_parsing_spec.rb
286
298
  - spec/unit/design_spec.rb
@@ -296,7 +308,9 @@ files:
296
308
  - spec/unit/property_protection_spec.rb
297
309
  - spec/unit/property_spec.rb
298
310
  - spec/unit/proxyable_spec.rb
311
+ - spec/unit/server_pool_spec.rb
299
312
  - spec/unit/subclass_spec.rb
313
+ - spec/unit/support/couchrest_database_spec.rb
300
314
  - spec/unit/translations_spec.rb
301
315
  - spec/unit/typecast_spec.rb
302
316
  - spec/unit/utils/migrate_spec.rb
@@ -321,7 +335,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
321
335
  version: 1.3.1
322
336
  requirements: []
323
337
  rubyforge_project:
324
- rubygems_version: 2.4.8
338
+ rubygems_version: 2.6.8
325
339
  signing_key:
326
340
  specification_version: 4
327
341
  summary: Extends the CouchRest Document class for advanced modelling.
@@ -363,6 +377,7 @@ test_files:
363
377
  - spec/unit/casted_array_spec.rb
364
378
  - spec/unit/casted_spec.rb
365
379
  - spec/unit/configuration_spec.rb
380
+ - spec/unit/connection_config_spec.rb
366
381
  - spec/unit/connection_spec.rb
367
382
  - spec/unit/core_extensions/time_parsing_spec.rb
368
383
  - spec/unit/design_spec.rb
@@ -378,7 +393,9 @@ test_files:
378
393
  - spec/unit/property_protection_spec.rb
379
394
  - spec/unit/property_spec.rb
380
395
  - spec/unit/proxyable_spec.rb
396
+ - spec/unit/server_pool_spec.rb
381
397
  - spec/unit/subclass_spec.rb
398
+ - spec/unit/support/couchrest_database_spec.rb
382
399
  - spec/unit/translations_spec.rb
383
400
  - spec/unit/typecast_spec.rb
384
401
  - spec/unit/utils/migrate_spec.rb