activerecord-tablefree 3.1.8 → 3.1.9

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.
@@ -1,359 +0,0 @@
1
-
2
- require 'spec_helper'
3
-
4
- def make_tablefree_model(database = nil, nested = nil)
5
- eval <<EOCLASS
6
- class Chair < ActiveRecord::Base
7
- #{database ? "has_no_table :database => :#{database}" : 'has_no_table'}
8
- column :id, :integer
9
- column :name, :string
10
- column :not_nullable, :integer, 1, false
11
- #{if nested
12
- '
13
- has_many :arm_rests
14
- accepts_nested_attributes_for :arm_rests
15
- '
16
- end}
17
- end
18
- EOCLASS
19
- if nested
20
- eval <<EOCLASS
21
- class ArmRest < ActiveRecord::Base
22
- #{database ? "has_no_table :database => :#{database}" : 'has_no_table'}
23
- belongs_to :chair
24
- column :id, :integer
25
- column :chair_id, :integer
26
- column :name, :string
27
- end
28
- EOCLASS
29
- end
30
- end
31
-
32
- def remove_models
33
- begin
34
- Object.send(:remove_const, :Chair)
35
- rescue
36
- nil
37
- end
38
- begin
39
- Object.send(:remove_const, :ArmRest)
40
- rescue
41
- nil
42
- end
43
- end
44
-
45
- ActiveRecord::Base.logger = Logger.new(STDERR)
46
- ActiveRecord::Base.logger.level = Logger::Severity::UNKNOWN
47
-
48
- shared_examples_for 'an active record instance' do
49
- it { should respond_to :id }
50
- it { should respond_to :id= }
51
- it { should respond_to :name }
52
- it { should respond_to :name= }
53
- it { should respond_to :update_attributes }
54
- describe '#attributes=' do
55
- before(:example) { subject.attributes = ({ name: 'Jarl Friis' }) }
56
- it 'assign attributes' do
57
- expect(subject.name).to eq 'Jarl Friis'
58
- end
59
- end
60
- end
61
-
62
- shared_examples_for 'a nested active record' do
63
- describe 'conllection#build' do
64
- specify do
65
- expect(subject.arm_rests.build(name: 'nice arm_rest')).to be_an_instance_of(ArmRest)
66
- end
67
- end
68
- describe 'conllection#<<' do
69
- specify do
70
- expect(subject.arm_rests << ArmRest.new(name: 'nice arm_rest')).to have(1).items
71
- end
72
- describe 'appending two children' do
73
- before(:example) do
74
- subject.arm_rests << [ArmRest.new(name: 'left'),
75
- ArmRest.new(name: 'right')]
76
- end
77
- it 'assigns nested attributes' do
78
- expect(subject.arm_rests[0].name).to eq 'left'
79
- expect(subject.arm_rests[1].name).to eq 'right'
80
- end
81
- end
82
- end
83
- describe '#attributes=' do
84
- before(:example) do
85
- subject.attributes = ({ name: 'Jarl Friis',
86
- arm_rests_attributes: [
87
- { name: 'left' },
88
- { name: 'right' }
89
- ] })
90
- end
91
- it 'assigns attributes' do
92
- expect(subject.name).to eq 'Jarl Friis'
93
- end
94
- it 'assigns nested attributes' do
95
- expect(subject.arm_rests[0].name).to eq 'left'
96
- expect(subject.arm_rests[1].name).to eq 'right'
97
- end
98
- end
99
- end
100
-
101
- shared_examples_for 'a tablefree model with fail_fast' do
102
- case ActiveRecord::VERSION::MAJOR
103
- when 3
104
- describe '#all' do
105
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
106
- expect { subject.all }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
107
- end
108
- end
109
- when 4
110
- describe '#all' do
111
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
112
- expect { subject.all }.to_not raise_exception
113
- end
114
- end
115
- describe '#all[]' do
116
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
117
- expect { subject.all[0] }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
118
- end
119
- end
120
- end
121
- describe '#create' do
122
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
123
- expect { subject.create(name: 'Jarl') }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
124
- end
125
- end
126
- describe '#destroy' do
127
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
128
- expect { subject.destroy(1) }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
129
- end
130
- end
131
- describe '#destroy_all' do
132
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
133
- expect { subject.destroy_all }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
134
- end
135
- end
136
- end
137
-
138
- shared_examples_for 'a tablefree model instance with fail_fast' do
139
- it_behaves_like 'an active record instance'
140
- describe '#save' do
141
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
142
- expect { subject.save }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
143
- end
144
- end
145
- describe '#save!' do
146
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
147
- expect { subject.save! }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
148
- end
149
- end
150
- describe '#reload' do
151
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
152
- expect { subject.reload }.to raise_exception(ActiveRecord::Tablefree::NoDatabase)
153
- end
154
- end
155
- describe '#update_attributes' do
156
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
157
- expect { subject.update_attributes(name: 'Jarl') }.to raise_exception(StandardError)
158
- end
159
- end
160
- end
161
-
162
- describe 'Tablefree model with fail_fast' do
163
- before(:context) { make_tablefree_model(nil, nil) }
164
- after(:context) { remove_models }
165
- subject { Chair }
166
- it 'has the expected null value for columns' do
167
- name = subject.columns.detect { |column| column.name == 'name' }.null
168
- not_nullable = subject.columns.detect { |column| column.name == 'not_nullable' }.null
169
- expect(name).to be_truthy
170
- expect(not_nullable).to be_falsy
171
- end
172
- it_behaves_like 'a tablefree model with fail_fast'
173
- describe 'instance' do
174
- subject { Chair.new(name: 'Jarl') }
175
- it_behaves_like 'a tablefree model instance with fail_fast'
176
- end
177
- end
178
-
179
- describe 'Tablefree nested with fail_fast' do
180
- before(:context) { make_tablefree_model(nil, true) }
181
- after(:context) { remove_models }
182
- subject { Chair }
183
- it_behaves_like 'a tablefree model with fail_fast'
184
- describe '#new' do
185
- it 'accepts attributes' do
186
- expect(subject.new(name: 'Jarl')).to be_an_instance_of(subject)
187
- end
188
- it 'assign attributes' do
189
- expect(subject.new(name: 'Jarl').name).to eq 'Jarl'
190
- end
191
- end
192
- describe 'instance' do
193
- subject { Chair.new(name: 'Jarl') }
194
- it_behaves_like 'a tablefree model instance with fail_fast'
195
- it_behaves_like 'a nested active record'
196
- describe '#update_attributes' do
197
- it 'raises ActiveRecord::Tablefree::NoDatabase' do
198
- expect do
199
- subject.update_attributes(arm_rests: { name: 'nice arm_rest' })
200
- end.to raise_exception(StandardError)
201
- end
202
- end
203
- end
204
- describe 'instance with nested models' do
205
- subject do
206
- Chair.new(name: 'Jarl',
207
- arm_rests: [
208
- ArmRest.new(name: 'left'),
209
- ArmRest.new(name: 'right')
210
- ])
211
- end
212
- it { should be_an_instance_of(Chair) }
213
- it { should have(2).arm_rests }
214
- end
215
- describe 'instance with nested attributes' do
216
- subject do
217
- Chair.new(name: 'Jarl',
218
- arm_rests_attributes: [
219
- { name: 'left' },
220
- { name: 'right' }
221
- ])
222
- end
223
- it { should be_an_instance_of(Chair) }
224
- it { should have(2).arm_rests }
225
- end
226
- end
227
-
228
- ##
229
- ## Succeeding database
230
- ##
231
- shared_examples_for 'a model with succeeding database' do
232
- describe '#all' do
233
- specify { expect(subject.all).to eq [] }
234
- end
235
- describe '#create' do
236
- specify { expect(subject.create(name: 'Jarl')).to be_an_instance_of(subject) }
237
- end
238
- describe '#destroy' do
239
- specify { expect(subject.destroy(1)).to be_an_instance_of(subject) }
240
- end
241
- describe '#destroy_all' do
242
- specify { expect(subject.destroy_all).to eq [] }
243
- end
244
- end
245
-
246
- shared_examples_for 'an instance with succeeding database' do
247
- it_behaves_like 'an active record instance'
248
-
249
- describe '#save' do
250
- specify { expect(subject.save).to eq true }
251
- end
252
- describe '#save!' do
253
- specify { expect(subject.save!).to eq true }
254
- end
255
- describe '#reload' do
256
- before { subject.save! }
257
- specify { expect(subject.reload).to eq subject }
258
- end
259
- describe '#update_attributes' do
260
- specify { expect(subject.update_attributes(name: 'Jarl Friis')).to eq true }
261
- end
262
- end
263
-
264
- describe 'ActiveRecord with real database' do
265
- before(:context) do
266
- FileUtils.mkdir_p 'tmp'
267
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'tmp/test.db')
268
- ActiveRecord::Base.connection.execute('drop table if exists chairs')
269
- ActiveRecord::Base.connection.execute('create table chairs (id INTEGER PRIMARY KEY, name TEXT )')
270
-
271
- class Chair < ActiveRecord::Base
272
- end
273
- end
274
- after(:context) do
275
- remove_models
276
- ActiveRecord::Base.clear_all_connections!
277
- end
278
-
279
- subject { Chair }
280
- it_behaves_like 'a model with succeeding database'
281
- describe 'instance' do
282
- subject { Chair.new(name: 'Jarl') }
283
- it_behaves_like 'an instance with succeeding database'
284
- end
285
-
286
- describe 'association' do
287
- before(:context) do
288
- # reopen the chair to add an association
289
- class Chair < ActiveRecord::Base
290
- has_one :arm_rest, primary_key: :name, foreign_key: :name, dependent: nil
291
- end
292
- class ArmRest < ActiveRecord::Base
293
- has_no_table
294
- column :name, :string
295
- belongs_to :chair, primary_key: :name, foreign_key: :name
296
- end
297
- end
298
- it 'can be traversed by setting association' do
299
- chair = Chair.create(name: 'A')
300
- arm_rest = ArmRest.new(chair: chair)
301
- expect(arm_rest.chair).to eq chair
302
- end
303
- it 'sets foreign key from association' do
304
- chair = Chair.create(name: 'A')
305
- arm_rest = ArmRest.new(chair: chair)
306
- expect(arm_rest.name).to eq 'A'
307
- end
308
- # This works on Rails 5.0, but not newer, because of the statement cache.
309
- # it 'can be traversed by setting foreign key' do
310
- # chair = Chair.create(name: 'B')
311
- # arm_rest = ArmRest.new(name: 'B')
312
- # expect { arm_rest.chair }.to_not raise_error
313
- # expect(arm_rest.chair).to eq(chair)
314
- # end
315
- end
316
- end
317
-
318
- describe 'Tablefree model with succeeding database' do
319
- before(:context) { make_tablefree_model(:pretend_success, nil) }
320
- after(:context) { remove_models }
321
- subject { Chair }
322
- it_behaves_like 'a model with succeeding database'
323
- describe 'instance' do
324
- subject { Chair.new(name: 'Jarl') }
325
- it_behaves_like 'an instance with succeeding database'
326
- end
327
- end
328
-
329
- describe 'Tablefree model can access connection' do
330
- before(:context) { make_tablefree_model(:pretend_success, nil) }
331
- after(:context) { remove_models }
332
- describe '.connection' do
333
- subject { Chair.connection }
334
- it { is_expected.to be_a(ActiveRecord::Tablefree::Connection) }
335
- end
336
- end
337
-
338
- describe 'Tablefree model can access connection transaction' do
339
- before(:context) { make_tablefree_model(:pretend_success, nil) }
340
- after(:context) { remove_models }
341
- describe '.connection' do
342
- subject { Chair.connection.current_transaction }
343
- it { is_expected.to be_a(ActiveRecord::Tablefree::Transaction) }
344
- end
345
- end
346
-
347
- describe 'Bad column type' do
348
- describe 'association' do
349
- subject { -> {
350
- class ArmRest < ActiveRecord::Base
351
- has_no_table
352
- column :chair_id
353
- end
354
- }}
355
- it 'raises an InvalidColumnType error' do
356
- expect { subject.call }.to raise_error ActiveRecord::Tablefree::InvalidColumnType, 'sql_type is (NilClass), which is not supported'
357
- end
358
- end
359
- end
data/spec/spec_helper.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'rspec/collection_matchers'
2
- require 'byebug'
3
- require 'sqlite3'
4
- require 'active_record'
5
- require 'logger'
6
-
7
- require 'coveralls'
8
- Coveralls.wear!
9
-
10
- require 'activerecord/tablefree'