couch_potato 1.7.0 → 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ruby.yml +50 -0
  3. data/.gitignore +3 -0
  4. data/CHANGES.md +180 -130
  5. data/Gemfile +4 -0
  6. data/README.md +61 -85
  7. data/Rakefile +11 -10
  8. data/couch_potato-rspec.gemspec +2 -1
  9. data/couch_potato.gemspec +9 -7
  10. data/gemfiles/active_support_5_0 +6 -0
  11. data/gemfiles/active_support_5_1 +7 -0
  12. data/gemfiles/active_support_5_2 +6 -0
  13. data/gemfiles/active_support_6_0 +6 -0
  14. data/gemfiles/active_support_6_1 +6 -0
  15. data/gemfiles/active_support_7_0 +6 -0
  16. data/lib/couch_potato/database.rb +170 -71
  17. data/lib/couch_potato/persistence/dirty_attributes.rb +3 -21
  18. data/lib/couch_potato/persistence/magic_timestamps.rb +3 -3
  19. data/lib/couch_potato/persistence/properties.rb +15 -10
  20. data/lib/couch_potato/persistence/simple_property.rb +0 -4
  21. data/lib/couch_potato/persistence/type_caster.rb +11 -6
  22. data/lib/couch_potato/persistence.rb +0 -1
  23. data/lib/couch_potato/railtie.rb +6 -11
  24. data/lib/couch_potato/validation.rb +8 -0
  25. data/lib/couch_potato/version.rb +2 -2
  26. data/lib/couch_potato/view/base_view_spec.rb +8 -32
  27. data/lib/couch_potato/view/custom_views.rb +4 -3
  28. data/lib/couch_potato/view/flex_view_spec.rb +121 -0
  29. data/lib/couch_potato/view/view_parameters.rb +34 -0
  30. data/lib/couch_potato.rb +37 -16
  31. data/spec/callbacks_spec.rb +45 -19
  32. data/spec/conflict_handling_spec.rb +1 -2
  33. data/spec/property_spec.rb +12 -3
  34. data/spec/railtie_spec.rb +10 -0
  35. data/spec/spec_helper.rb +4 -3
  36. data/spec/unit/active_model_compliance_spec.rb +7 -3
  37. data/spec/unit/attributes_spec.rb +54 -1
  38. data/spec/unit/caching_spec.rb +105 -0
  39. data/spec/unit/couch_potato_spec.rb +70 -5
  40. data/spec/unit/create_spec.rb +5 -4
  41. data/spec/unit/database_spec.rb +239 -135
  42. data/spec/unit/dirty_attributes_spec.rb +5 -26
  43. data/spec/unit/flex_view_spec_spec.rb +17 -0
  44. data/spec/unit/model_view_spec_spec.rb +1 -1
  45. data/spec/unit/rspec_stub_db_spec.rb +31 -0
  46. data/spec/unit/validation_spec.rb +42 -2
  47. data/spec/unit/view_query_spec.rb +12 -7
  48. data/spec/views_spec.rb +214 -103
  49. data/vendor/pouchdb-collate/LICENSE +202 -0
  50. data/vendor/pouchdb-collate/pouchdb-collate.js +430 -0
  51. metadata +47 -36
  52. data/.ruby-version +0 -1
  53. data/.travis.yml +0 -21
  54. data/gemfiles/active_support_4_0 +0 -11
  55. data/gemfiles/active_support_4_1 +0 -11
  56. data/gemfiles/active_support_4_2 +0 -11
  57. data/lib/couch_potato/persistence/deep_dirty_attributes.rb +0 -180
  58. data/spec/unit/deep_dirty_attributes_spec.rb +0 -434
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe 'database caching' do
6
+ let(:couchrest_db) do
7
+ double(:couchrest_db, info: double(:info),
8
+ root: '', get: double.as_null_object)
9
+ end
10
+
11
+ let(:db) do
12
+ CouchPotato::Database.new(couchrest_db).tap do |db|
13
+ db.cache = cache
14
+ end
15
+ end
16
+
17
+ let(:cache) do
18
+ {}
19
+ end
20
+
21
+ it 'gets an object from the cache the 2nd time via #load_documemt' do
22
+ expect(couchrest_db).to receive(:get).with('1').exactly(1).times
23
+
24
+ db.load_document '1'
25
+ db.load_document '1'
26
+ end
27
+
28
+ it 'gets an object from the cache the 2nd time via #load' do
29
+ expect(couchrest_db).to receive(:get).with('1').exactly(1).times
30
+
31
+ db.load '1'
32
+ db.load '1'
33
+ end
34
+
35
+ it 'gets an object from the cache the 2nd time via #load!' do
36
+ expect(couchrest_db).to receive(:get).with('1').exactly(1).times
37
+
38
+ db.load! '1'
39
+ db.load! '1'
40
+ end
41
+
42
+ it 'returns the correct object' do
43
+ doc = double(:doc, 'database=': nil)
44
+ allow(couchrest_db).to receive_messages(get: doc)
45
+
46
+ db.load_document '1'
47
+ expect(db.load_document('1')).to eql(doc)
48
+ end
49
+
50
+ it 'does not cache bulk loads' do
51
+ allow(couchrest_db).to receive_messages(bulk_load: {'rows' => []})
52
+ expect(couchrest_db).to receive(:bulk_load).with(['1']).exactly(2).times
53
+
54
+ db.load_document ['1']
55
+ db.load_document ['1']
56
+ end
57
+
58
+ it 'clears the cache when destroying a document via #destroy_document' do
59
+ expect(couchrest_db).to receive(:get).with('1').exactly(2).times
60
+
61
+ db.load_document '1'
62
+ db.destroy_document double.as_null_object
63
+ db.load_document '1'
64
+ end
65
+
66
+ it 'clears the cache when destroying a document via #destroy' do
67
+ expect(couchrest_db).to receive(:get).with('1').exactly(2).times
68
+
69
+ db.load_document '1'
70
+ db.destroy double.as_null_object
71
+ db.load_document '1'
72
+ end
73
+
74
+ it 'clears the cache when updating a document via #save_document' do
75
+ expect(couchrest_db).to receive(:get).with('1').exactly(2).times
76
+
77
+ db.load_document '1'
78
+ db.save_document double.as_null_object
79
+ db.load_document '1'
80
+ end
81
+
82
+ it 'clears the cache when updating a document via #save_document!' do
83
+ expect(couchrest_db).to receive(:get).with('1').exactly(2).times
84
+
85
+ db.load_document '1'
86
+ db.save_document! double.as_null_object
87
+ db.load_document '1'
88
+ end
89
+
90
+ it 'clears the cache when updating a document via #save' do
91
+ expect(couchrest_db).to receive(:get).with('1').exactly(2).times
92
+
93
+ db.load_document '1'
94
+ db.save double.as_null_object
95
+ db.load_document '1'
96
+ end
97
+
98
+ it 'clears the cache when updating a document via #save!' do
99
+ expect(couchrest_db).to receive(:get).with('1').exactly(2).times
100
+
101
+ db.load_document '1'
102
+ db.save! double.as_null_object
103
+ db.load_document '1'
104
+ end
105
+ end
@@ -1,6 +1,62 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe CouchPotato, 'full_url_to_database' do
5
+ describe CouchPotato, '.configure' do
6
+ after(:example) do
7
+ # reset defaults
8
+ CouchPotato::Config.database_name = nil
9
+ CouchPotato::Config.split_design_documents_per_view = false
10
+ CouchPotato::Config.digest_view_names = false
11
+ CouchPotato::Config.default_language = :javascript
12
+ CouchPotato::Config.database_host = 'http://127.0.0.1:5984'
13
+ CouchPotato::Config.additional_databases = {}
14
+ end
15
+
16
+ it 'sets the database name when config is a string' do
17
+ CouchPotato.configure('testdb')
18
+
19
+ expect(CouchPotato::Config.database_name).to eq('testdb')
20
+ end
21
+
22
+ it 'does not override database_host if not given' do
23
+ CouchPotato.configure(
24
+ database: 'testdb'
25
+ )
26
+
27
+ expect(CouchPotato::Config.database_host).to eq('http://127.0.0.1:5984')
28
+ end
29
+
30
+ it 'sets the given config options' do
31
+ CouchPotato.configure(
32
+ database: 'testdb',
33
+ database_host: 'http://10.0.0.1:2000',
34
+ additional_databases: {
35
+ test2: 'test2_db'
36
+ },
37
+ split_design_documents_per_view: true,
38
+ digest_view_names: true,
39
+ default_language: 'erlang'
40
+ )
41
+
42
+ expect(CouchPotato::Config.database_name).to eq('testdb')
43
+ expect(CouchPotato::Config.split_design_documents_per_view).to eq(true)
44
+ expect(CouchPotato::Config.digest_view_names).to eq(true)
45
+ expect(CouchPotato::Config.default_language).to eq('erlang')
46
+ expect(CouchPotato::Config.database_host).to eq('http://10.0.0.1:2000')
47
+ expect(CouchPotato::Config.additional_databases).to eq('test2' => 'test2_db')
48
+ end
49
+
50
+ it 'works with string keys' do
51
+ CouchPotato.configure(
52
+ 'database' => 'testdb'
53
+ )
54
+
55
+ expect(CouchPotato::Config.database_name).to eq('testdb')
56
+ end
57
+ end
58
+
59
+ describe CouchPotato, '.full_url_to_database' do
4
60
  before(:each) do
5
61
  @original_database_name = CouchPotato::Config.database_name
6
62
  end
@@ -8,12 +64,12 @@ describe CouchPotato, 'full_url_to_database' do
8
64
  CouchPotato::Config.database_name = @original_database_name
9
65
  end
10
66
 
11
- it "should add the default localhost and port if only a name is set" do
67
+ it 'should add the default localhost and port if only a name is set' do
12
68
  CouchPotato::Config.database_name = 'test'
13
69
  expect(CouchPotato.full_url_to_database).to eq('http://127.0.0.1:5984/test')
14
70
  end
15
71
 
16
- it "should return the set url" do
72
+ it 'should return the set url' do
17
73
  CouchPotato::Config.database_name = 'http://db.local/test'
18
74
  expect(CouchPotato.full_url_to_database).to eq('http://db.local/test')
19
75
  end
@@ -21,13 +77,22 @@ end
21
77
 
22
78
  describe CouchPotato, 'use' do
23
79
  it 'should return the db object' do
24
- db = CouchPotato.use("testdb")
80
+ db = CouchPotato.use('testdb')
25
81
  expect(db.couchrest_database.root.to_s).to eq('http://127.0.0.1:5984/testdb')
26
82
  end
83
+
84
+ it 'returns a db from the additional_databases pool' do
85
+ CouchPotato::Config.database_host = 'http://127.0.0.1:5984'
86
+ CouchPotato::Config.additional_databases = { '1' => 'db1', '2' => 'db2' }
87
+
88
+ db = CouchPotato.use('2')
89
+
90
+ expect(db.couchrest_database.root.to_s).to eq('http://127.0.0.1:5984/db2')
91
+ end
27
92
  end
28
93
 
29
94
  describe CouchPotato, '.models' do
30
- it "returns all classes that have implemented CouchPotato::Persistence" do
95
+ it 'returns all classes that have implemented CouchPotato::Persistence' do
31
96
  clazz = Class.new
32
97
  clazz.send(:include, CouchPotato::Persistence)
33
98
 
@@ -39,7 +39,8 @@ describe "create" do
39
39
  describe "fails" do
40
40
  before(:each) do
41
41
  @comment = Comment.new
42
- CouchPotato::Database.new(double('database', :info => nil)).save_document(@comment)
42
+ @db = CouchPotato::Database.new(double('database', :info => nil))
43
+ @db.save_document(@comment)
43
44
  end
44
45
 
45
46
  it "should not assign an id" do
@@ -58,11 +59,11 @@ describe "create" do
58
59
  expect(@comment.updated_at).to be_nil
59
60
  end
60
61
 
61
- describe "with bank" do
62
+ describe "with !" do
62
63
  it "should raise an exception" do
63
64
  expect {
64
- @comment.save!
65
- }.to raise_error
65
+ @db.save! @comment
66
+ }.to raise_error(CouchPotato::Database::ValidationsFailedError)
66
67
  end
67
68
  end
68
69
  end