couchbase-orm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,237 @@
1
+ # frozen_string_literal: true, encoding: ASCII-8BIT
2
+
3
+ require File.expand_path("../support", __FILE__)
4
+
5
+
6
+ class BasicModel < CouchbaseOrm::Base
7
+ attribute :name, :address, :age
8
+ end
9
+
10
+ class ModelWithDefaults < CouchbaseOrm::Base
11
+ attribute :name, default: proc { 'bob' }
12
+ attribute :address
13
+ attribute :age, default: 23
14
+ end
15
+
16
+ class ModelWithCallbacks < CouchbaseOrm::Base
17
+ attribute :name, :address, :age
18
+
19
+ before_create :update_name
20
+ before_save :set_address
21
+ before_update :set_age
22
+ after_initialize do
23
+ self.age = 10
24
+ end
25
+ before_destroy do
26
+ self.name = 'joe'
27
+ end
28
+
29
+ def update_name; self.name = 'bob'; end
30
+ def set_address; self.address = '23'; end
31
+ def set_age; self.age = 30; end
32
+ end
33
+
34
+ class ModelWithValidations < CouchbaseOrm::Base
35
+ attribute :name, :address, type: String
36
+ attribute :age, type: :Integer
37
+
38
+ validates :name, presence: true
39
+ validates :age, numericality: { only_integer: true }
40
+ end
41
+
42
+
43
+ describe CouchbaseOrm::Persistence do
44
+ it "should save a model" do
45
+ model = BasicModel.new
46
+
47
+ expect(model.new_record?).to be(true)
48
+ expect(model.destroyed?).to be(false)
49
+ expect(model.persisted?).to be(false)
50
+
51
+ model.name = 'bob'
52
+ expect(model.name).to eq('bob')
53
+
54
+ model.address = 'somewhere'
55
+ model.age = 34
56
+
57
+ expect(model.new_record?).to be(true)
58
+ expect(model.destroyed?).to be(false)
59
+ expect(model.persisted?).to be(false)
60
+
61
+ result = model.save
62
+ expect(result).to be(true)
63
+
64
+ expect(model.new_record?).to be(false)
65
+ expect(model.destroyed?).to be(false)
66
+ expect(model.persisted?).to be(true)
67
+
68
+ model.destroy
69
+ expect(model.new_record?).to be(false)
70
+ expect(model.destroyed?).to be(true)
71
+ expect(model.persisted?).to be(false)
72
+ end
73
+
74
+ it "should save a model with defaults" do
75
+ model = ModelWithDefaults.new
76
+
77
+ expect(model.name).to eq('bob')
78
+ expect(model.age).to be(23)
79
+ expect(model.address).to be(nil)
80
+
81
+ expect(model.new_record?).to be(true)
82
+ expect(model.destroyed?).to be(false)
83
+ expect(model.persisted?).to be(false)
84
+
85
+ result = model.save
86
+ expect(result).to be(true)
87
+
88
+ expect(model.new_record?).to be(false)
89
+ expect(model.destroyed?).to be(false)
90
+ expect(model.persisted?).to be(true)
91
+
92
+ model.destroy
93
+ expect(model.new_record?).to be(false)
94
+ expect(model.destroyed?).to be(true)
95
+ expect(model.persisted?).to be(false)
96
+ end
97
+
98
+ it "should execute callbacks" do
99
+ model = ModelWithCallbacks.new
100
+
101
+ # Test initialize
102
+ expect(model.name).to be(nil)
103
+ expect(model.age).to be(10)
104
+ expect(model.address).to be(nil)
105
+
106
+ expect(model.new_record?).to be(true)
107
+ expect(model.destroyed?).to be(false)
108
+ expect(model.persisted?).to be(false)
109
+
110
+ # Test create
111
+ result = model.save
112
+ expect(result).to be(true)
113
+
114
+ expect(model.name).to eq('bob')
115
+ expect(model.age).to be(10)
116
+ expect(model.address).to eq('23')
117
+
118
+ # Test Update
119
+ model.address = 'other'
120
+ expect(model.address).to eq('other')
121
+ model.save
122
+
123
+ expect(model.name).to eq('bob')
124
+ expect(model.age).to be(30)
125
+ expect(model.address).to eq('23')
126
+
127
+ # Test destroy
128
+ model.destroy
129
+ expect(model.new_record?).to be(false)
130
+ expect(model.destroyed?).to be(true)
131
+ expect(model.persisted?).to be(false)
132
+
133
+ expect(model.name).to eq('joe')
134
+ end
135
+
136
+ it "should perform validations" do
137
+ model = ModelWithValidations.new
138
+
139
+ expect(model.valid?).to be(false)
140
+
141
+ # Test create
142
+ result = model.save
143
+ expect(result).to be(false)
144
+ expect(model.errors.count).to be(2)
145
+
146
+ begin
147
+ model.save!
148
+ rescue ::CouchbaseOrm::Error::RecordInvalid => e
149
+ expect(e.record).to be(model)
150
+ end
151
+
152
+ model.name = 'bob'
153
+ model.age = 23
154
+ expect(model.valid?).to be(true)
155
+ expect(model.save).to be(true)
156
+
157
+ # Test update
158
+ model.name = nil
159
+ expect(model.valid?).to be(false)
160
+ expect(model.save).to be(false)
161
+ begin
162
+ model.save!
163
+ rescue ::CouchbaseOrm::Error::RecordInvalid => e
164
+ expect(e.record).to be(model)
165
+ end
166
+
167
+ model.age = '23' # This value will be coerced
168
+ model.name = 'joe'
169
+ expect(model.valid?).to be(true)
170
+ expect(model.save!).to be(model)
171
+
172
+ # coercion will fail here
173
+ begin
174
+ model.age = 'a23'
175
+ expect(false).to be(true)
176
+ rescue ArgumentError => e
177
+ end
178
+
179
+ model.destroy
180
+ end
181
+
182
+ it "should reload a model" do
183
+ model = BasicModel.new
184
+
185
+ model.name = 'bob'
186
+ model.address = 'somewhere'
187
+ model.age = 34
188
+
189
+ expect(model.save).to be(true)
190
+ id = model.id
191
+ model.name = nil
192
+ expect(model.changed?).to be(true)
193
+
194
+ model.reload
195
+ expect(model.changed?).to be(false)
196
+ expect(model.id).to be(id)
197
+
198
+ model.destroy
199
+ expect(model.destroyed?).to be(true)
200
+ end
201
+
202
+ it "should update attributes" do
203
+ model = BasicModel.new
204
+
205
+ model.update_attributes({
206
+ name: 'bob',
207
+ age: 34
208
+ })
209
+
210
+ expect(model.new_record?).to be(false)
211
+ expect(model.destroyed?).to be(false)
212
+ expect(model.persisted?).to be(true)
213
+
214
+ expect(model.name).to eq('bob')
215
+ expect(model.age).to be(34)
216
+ expect(model.address).to be(nil)
217
+
218
+ model.destroy
219
+ expect(model.destroyed?).to be(true)
220
+ end
221
+
222
+ describe BasicModel do
223
+ it_behaves_like "ActiveModel"
224
+ end
225
+
226
+ describe ModelWithDefaults do
227
+ it_behaves_like "ActiveModel"
228
+ end
229
+
230
+ describe ModelWithCallbacks do
231
+ it_behaves_like "ActiveModel"
232
+ end
233
+
234
+ describe ModelWithValidations do
235
+ it_behaves_like "ActiveModel"
236
+ end
237
+ end
data/spec/support.rb ADDED
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true, encoding: ASCII-8BIT
2
+
3
+
4
+ require 'couchbase-orm'
5
+ require 'minitest/assertions'
6
+ require 'active_model/lint'
7
+
8
+
9
+ shared_examples_for "ActiveModel" do
10
+ include Minitest::Assertions
11
+ include ActiveModel::Lint::Tests
12
+
13
+ def assertions
14
+ @__assertions__ ||= 0
15
+ end
16
+
17
+ def assertions=(val)
18
+ @__assertions__ = val
19
+ end
20
+
21
+ ActiveModel::Lint::Tests.public_instance_methods.map { |method| method.to_s }.grep(/^test/).each do |method|
22
+ example(method.gsub('_', ' ')) { send method }
23
+ end
24
+
25
+ before do
26
+ @model = subject
27
+ end
28
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true, encoding: ASCII-8BIT
2
+
3
+ require File.expand_path("../support", __FILE__)
4
+
5
+
6
+ class ViewTest < CouchbaseOrm::Base
7
+ attribute :name, type: String
8
+ enum rating: [:awesome, :good, :okay, :bad], default: :okay
9
+
10
+ view :all
11
+ view :by_rating, emit_key: :rating
12
+
13
+ # This generates both:
14
+ # view :by_rating, emit_key: :rating # same as above
15
+ # def self.find_by_rating(rating); end # also provide this helper function
16
+ index_view :rating
17
+ end
18
+
19
+
20
+ describe CouchbaseOrm::Views do
21
+ it "should save a new design document" do
22
+ begin
23
+ ViewTest.bucket.delete_design_doc(ViewTest.design_document)
24
+ rescue Libcouchbase::Error::HttpResponseError
25
+ end
26
+ expect(ViewTest.ensure_design_document!).to be(true)
27
+ end
28
+
29
+ it "should not re-save a design doc if nothing has changed" do
30
+ expect(ViewTest.ensure_design_document!).to be(false)
31
+ end
32
+
33
+ it "should perform a map-reduce and return the view" do
34
+ ViewTest.ensure_design_document!
35
+ mod = ViewTest.create! name: :bob, rating: :good
36
+
37
+ docs = ViewTest.all.collect { |ob|
38
+ ob.destroy
39
+ ob.name
40
+ }
41
+ expect(docs).to eq(['bob'])
42
+ end
43
+
44
+ it "should work with other keys" do
45
+ ViewTest.ensure_design_document!
46
+ ViewTest.create! name: :bob, rating: :good
47
+ ViewTest.create! name: :jane, rating: :awesome
48
+ ViewTest.create! name: :greg, rating: :bad
49
+
50
+ docs = ViewTest.by_rating(descending: :true).collect { |ob|
51
+ ob.destroy
52
+ ob.name
53
+ }
54
+ expect(docs).to eq(['greg', 'bob', 'jane'])
55
+ end
56
+
57
+ it "should return matching results" do
58
+ ViewTest.ensure_design_document!
59
+ ViewTest.create! name: :bob, rating: :awesome
60
+ ViewTest.create! name: :jane, rating: :awesome
61
+ ViewTest.create! name: :greg, rating: :bad
62
+ ViewTest.create! name: :mel, rating: :good
63
+
64
+ docs = ViewTest.find_by_rating(1).collect { |ob|
65
+ ob.name
66
+ }
67
+ ViewTest.all.stream { |ob|
68
+ ob.destroy
69
+ }
70
+
71
+ expect(docs).to eq(['bob', 'jane'])
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: couchbase-orm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen von Takach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: libcouchbase
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: radix
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '11.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '11.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.9'
111
+ description: A Couchbase ORM for Rails
112
+ email:
113
+ - steve@cotag.me
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - couchbase-orm.gemspec
126
+ - lib/couchbase-orm.rb
127
+ - lib/couchbase-orm/associations.rb
128
+ - lib/couchbase-orm/base.rb
129
+ - lib/couchbase-orm/connection.rb
130
+ - lib/couchbase-orm/error.rb
131
+ - lib/couchbase-orm/id_generator.rb
132
+ - lib/couchbase-orm/persistence.rb
133
+ - lib/couchbase-orm/railtie.rb
134
+ - lib/couchbase-orm/utilities/ensure_unique.rb
135
+ - lib/couchbase-orm/utilities/enum.rb
136
+ - lib/couchbase-orm/utilities/has_many.rb
137
+ - lib/couchbase-orm/utilities/index.rb
138
+ - lib/couchbase-orm/utilities/join.rb
139
+ - lib/couchbase-orm/version.rb
140
+ - lib/couchbase-orm/views.rb
141
+ - lib/rails/generators/couchbase_orm/config/config_generator.rb
142
+ - lib/rails/generators/couchbase_orm/config/templates/couchbase.yml
143
+ - lib/rails/generators/couchbase_orm_generator.rb
144
+ - spec/associations_spec.rb
145
+ - spec/base_spec.rb
146
+ - spec/has_many_spec.rb
147
+ - spec/id_generator_spec.rb
148
+ - spec/index_spec.rb
149
+ - spec/persistence_spec.rb
150
+ - spec/support.rb
151
+ - spec/views_spec.rb
152
+ homepage: https://github.com/cotag/couchbase-orm
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: 2.1.0
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.5.1
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Couchbase ORM for Rails
176
+ test_files:
177
+ - spec/associations_spec.rb
178
+ - spec/base_spec.rb
179
+ - spec/has_many_spec.rb
180
+ - spec/id_generator_spec.rb
181
+ - spec/index_spec.rb
182
+ - spec/persistence_spec.rb
183
+ - spec/support.rb
184
+ - spec/views_spec.rb