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