couch_potato 1.6.4 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +38 -0
- data/.gitignore +3 -0
- data/CHANGES.md +193 -122
- data/Gemfile +4 -0
- data/README.md +61 -85
- data/Rakefile +11 -10
- data/couch_potato-rspec.gemspec +3 -2
- data/couch_potato.gemspec +9 -7
- data/gemfiles/active_support_5_0 +7 -0
- data/gemfiles/active_support_5_1 +7 -0
- data/gemfiles/active_support_5_2 +7 -0
- data/gemfiles/active_support_6_0 +7 -0
- data/gemfiles/active_support_6_1 +7 -0
- data/lib/couch_potato/database.rb +168 -71
- data/lib/couch_potato/persistence/dirty_attributes.rb +3 -21
- data/lib/couch_potato/persistence/magic_timestamps.rb +3 -3
- data/lib/couch_potato/persistence/properties.rb +15 -10
- data/lib/couch_potato/persistence/revisions.rb +14 -0
- data/lib/couch_potato/persistence/simple_property.rb +0 -4
- data/lib/couch_potato/persistence/type_caster.rb +11 -6
- data/lib/couch_potato/persistence.rb +5 -3
- data/lib/couch_potato/railtie.rb +7 -12
- data/lib/couch_potato/validation.rb +8 -0
- data/lib/couch_potato/version.rb +2 -2
- data/lib/couch_potato/view/base_view_spec.rb +8 -32
- data/lib/couch_potato/view/custom_views.rb +4 -3
- data/lib/couch_potato/view/flex_view_spec.rb +121 -0
- data/lib/couch_potato/view/view_parameters.rb +34 -0
- data/lib/couch_potato.rb +37 -16
- data/spec/callbacks_spec.rb +45 -19
- data/spec/conflict_handling_spec.rb +1 -2
- data/spec/property_spec.rb +12 -3
- data/spec/railtie_spec.rb +17 -1
- data/spec/revisions_spec.rb +25 -0
- data/spec/spec_helper.rb +4 -3
- data/spec/unit/active_model_compliance_spec.rb +7 -3
- data/spec/unit/attributes_spec.rb +54 -1
- data/spec/unit/caching_spec.rb +105 -0
- data/spec/unit/couch_potato_spec.rb +70 -5
- data/spec/unit/create_spec.rb +5 -4
- data/spec/unit/database_spec.rb +235 -135
- data/spec/unit/dirty_attributes_spec.rb +5 -26
- data/spec/unit/flex_view_spec_spec.rb +17 -0
- data/spec/unit/model_view_spec_spec.rb +1 -1
- data/spec/unit/rspec_stub_db_spec.rb +31 -0
- data/spec/unit/validation_spec.rb +42 -2
- data/spec/views_spec.rb +214 -103
- data/vendor/pouchdb-collate/LICENSE +202 -0
- data/vendor/pouchdb-collate/pouchdb-collate.js +430 -0
- metadata +46 -33
- data/.ruby-version +0 -1
- data/.travis.yml +0 -20
- data/gemfiles/active_support_4_0 +0 -11
- data/gemfiles/active_support_4_1 +0 -11
- data/gemfiles/active_support_4_2 +0 -11
- data/lib/couch_potato/persistence/deep_dirty_attributes.rb +0 -180
- data/spec/unit/deep_dirty_attributes_spec.rb +0 -434
@@ -10,7 +10,8 @@ class Plant
|
|
10
10
|
include CouchPotato::Persistence
|
11
11
|
property :leaf_count
|
12
12
|
property :typed_leaf_count, type: Fixnum
|
13
|
-
property :
|
13
|
+
property :integer_something, type: Integer
|
14
|
+
property :typed_leaf_size, type: Float
|
14
15
|
property :branch, type: Branch
|
15
16
|
end
|
16
17
|
|
@@ -40,6 +41,7 @@ describe 'attributes' do
|
|
40
41
|
plant = Plant.new(leaf_count: 1)
|
41
42
|
|
42
43
|
expect(plant.attributes).to eq('leaf_count' => 1, 'created_at' => nil,
|
44
|
+
'integer_something' => nil,
|
43
45
|
'updated_at' => nil, 'typed_leaf_count' => nil,
|
44
46
|
'typed_leaf_size' => nil, 'branch' => nil)
|
45
47
|
end
|
@@ -109,6 +111,57 @@ describe 'attributes' do
|
|
109
111
|
end
|
110
112
|
end
|
111
113
|
|
114
|
+
describe 'integer' do
|
115
|
+
it 'rounds a float to a fixnum' do
|
116
|
+
@plant.integer_something = 4.5
|
117
|
+
|
118
|
+
expect(@plant.integer_something).to eq(5)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'converts a string into a fixnum' do
|
122
|
+
@plant.integer_something = '4'
|
123
|
+
|
124
|
+
expect(@plant.integer_something).to eq(4)
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'converts a string into a negative fixnum' do
|
128
|
+
@plant.integer_something = '-4'
|
129
|
+
|
130
|
+
expect(@plant.integer_something).to eq(-4)
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'leaves a fixnum as is' do
|
134
|
+
@plant.integer_something = 4
|
135
|
+
|
136
|
+
expect(@plant.integer_something).to eq(4)
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'leaves nil as is' do
|
140
|
+
@plant.integer_something = nil
|
141
|
+
|
142
|
+
expect(@plant.integer_something).to be_nil
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'sets the attributes to zero if a string given' do
|
146
|
+
@plant.integer_something = 'x'
|
147
|
+
|
148
|
+
expect(@plant.integer_something).to eq(0)
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'parses numbers out of a string' do
|
152
|
+
@plant.integer_something = 'x123'
|
153
|
+
|
154
|
+
expect(@plant.integer_something).to eq(123)
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'sets the attributes to nil if given a blank string' do
|
158
|
+
@plant.integer_something = ''
|
159
|
+
|
160
|
+
expect(@plant.integer_something).to be_nil
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
|
112
165
|
describe 'fixnum' do
|
113
166
|
it 'rounds a float to a fixnum' do
|
114
167
|
@plant.typed_leaf_count = 4.5
|
@@ -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, '
|
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
|
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
|
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(
|
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
|
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
|
|
data/spec/unit/create_spec.rb
CHANGED
@@ -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))
|
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
|
62
|
+
describe "with !" do
|
62
63
|
it "should raise an exception" do
|
63
64
|
expect {
|
64
|
-
@
|
65
|
-
}.to raise_error
|
65
|
+
@db.save! @comment
|
66
|
+
}.to raise_error(CouchPotato::Database::ValidationsFailedError)
|
66
67
|
end
|
67
68
|
end
|
68
69
|
end
|