facemock 0.0.4 → 0.0.5
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.
- data/.coveralls.yml +1 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +154 -0
- data/Rakefile +6 -0
- data/db/.gitkeep +0 -0
- data/facemock.gemspec +31 -0
- data/lib/facemock/config.rb +88 -0
- data/lib/facemock/database/application.rb +20 -0
- data/lib/facemock/database/permission.rb +21 -0
- data/lib/facemock/database/table.rb +340 -0
- data/lib/facemock/database/user.rb +26 -0
- data/lib/facemock/database.rb +121 -0
- data/lib/facemock/errors.rb +7 -0
- data/lib/facemock/fb_graph/application/test_users.rb +36 -0
- data/lib/facemock/fb_graph/application/user/permission.rb +10 -0
- data/lib/facemock/fb_graph/application/user.rb +69 -0
- data/lib/facemock/fb_graph/application.rb +48 -0
- data/lib/facemock/fb_graph/user.rb +13 -0
- data/lib/facemock/fb_graph.rb +30 -0
- data/lib/facemock/version.rb +3 -0
- data/lib/facemock.rb +19 -0
- data/spec/facemock/config_spec.rb +185 -0
- data/spec/facemock/database/application_spec.rb +73 -0
- data/spec/facemock/database/permission_spec.rb +52 -0
- data/spec/facemock/database/tables_spec.rb +728 -0
- data/spec/facemock/database/user_spec.rb +169 -0
- data/spec/facemock/database_spec.rb +270 -0
- data/spec/facemock/errors_spec.rb +9 -0
- data/spec/facemock/fb_graph/application/test_users_spec.rb +155 -0
- data/spec/facemock/fb_graph/application/user_spec.rb +208 -0
- data/spec/facemock/fb_graph/application_spec.rb +132 -0
- data/spec/facemock/fb_graph/user_spec.rb +36 -0
- data/spec/facemock/fb_graph_spec.rb +47 -0
- data/spec/facemock_spec.rb +74 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/tables_helper.rb +46 -0
- metadata +64 -3
@@ -0,0 +1,728 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Facemock::Database::Table do
|
4
|
+
include TableHelper
|
5
|
+
|
6
|
+
let(:db_name) { ".test" }
|
7
|
+
let(:table_name) { :tables }
|
8
|
+
let(:column_names) { [:id, :text, :active, :number, :created_at] }
|
9
|
+
|
10
|
+
before do
|
11
|
+
stub_const("Facemock::Database::DEFAULT_DB_NAME", db_name)
|
12
|
+
create_tables_table_for_test
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
Facemock::Database.new.drop
|
17
|
+
remove_dynamically_defined_class_method(Facemock::Database::Table)
|
18
|
+
remove_dynamically_defined_instance_method(Facemock::Database::Table)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '::TABLE_NAME' do
|
22
|
+
subject { Facemock::Database::Table::TABLE_NAME }
|
23
|
+
it { is_expected.to eq table_name }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '::COLUMN_NAMES' do
|
27
|
+
subject { Facemock::Database::Table::COLUMN_NAMES }
|
28
|
+
it { is_expected.to eq column_names }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#initialize' do
|
32
|
+
context 'without option' do
|
33
|
+
it 'should have accessor of column' do
|
34
|
+
@table = Facemock::Database::Table.new
|
35
|
+
column_names.each do |column_name|
|
36
|
+
if column_name == :active
|
37
|
+
expect(@table.send(column_name)).to be false
|
38
|
+
else
|
39
|
+
expect(@table.send(column_name)).to be_nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
@table.id = id = 1
|
44
|
+
@table.text = text = "test"
|
45
|
+
@table.active = active = true
|
46
|
+
@table.number = number = 0
|
47
|
+
@table.created_at = created_at = Time.now
|
48
|
+
expect(@table.id).to eq id
|
49
|
+
expect(@table.text).to eq text
|
50
|
+
expect(@table.active).to eq active
|
51
|
+
expect(@table.number).to eq number
|
52
|
+
expect(@table.created_at).to eq created_at
|
53
|
+
|
54
|
+
expect(lambda{ @table.name }).to raise_error NoMethodError
|
55
|
+
expect(lambda{ @table.name = nil }).to raise_error NoMethodError
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with option' do
|
60
|
+
it 'should have accessor of column' do
|
61
|
+
options = { id: 1, text: "test", active: true, number: 0, created_at: Time.now }
|
62
|
+
@table = Facemock::Database::Table.new(options)
|
63
|
+
options.each_key do |key|
|
64
|
+
expect(@table.send(key)).to eq options[key]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#save!' do
|
71
|
+
before { @table = Facemock::Database::Table.new }
|
72
|
+
|
73
|
+
context 'when text does not set value' do
|
74
|
+
subject { lambda { @table.save! } }
|
75
|
+
it { is_expected.to raise_error }
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when text and number set value' do
|
79
|
+
before do
|
80
|
+
@table.text = @text = "test"
|
81
|
+
@table.number = @number = 0
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'without option' do
|
85
|
+
context 'when id and created_at are nil' do
|
86
|
+
it 'should set value to id and created_at' do
|
87
|
+
expect(@table.id).to be_nil
|
88
|
+
expect(@table.text).to eq @text
|
89
|
+
expect(@table.active).to eq false
|
90
|
+
expect(@table.number).to eq @number
|
91
|
+
expect(@table.created_at).to be_nil
|
92
|
+
@table.save!
|
93
|
+
expect(@table.id).to be > 0
|
94
|
+
expect(@table.text).to eq @text
|
95
|
+
expect(@table.active).to eq false
|
96
|
+
expect(@table.number).to eq @number
|
97
|
+
expect(@table.created_at).to be <= Time.now
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'when id is specified but record does not exist' do
|
102
|
+
before { @table.id = @id = 1 }
|
103
|
+
|
104
|
+
it 'should set value to created_at and id does not change' do
|
105
|
+
expect(@table.id).to eq @id
|
106
|
+
expect(@table.text).to eq @text
|
107
|
+
expect(@table.active).to eq false
|
108
|
+
expect(@table.number).to eq @number
|
109
|
+
expect(@table.created_at).to be_nil
|
110
|
+
@table.save!
|
111
|
+
expect(@table.id).to eq @id
|
112
|
+
expect(@table.text).to eq @text
|
113
|
+
expect(@table.active).to eq false
|
114
|
+
expect(@table.number).to eq @number
|
115
|
+
expect(@table.created_at).to be <= Time.now
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when id is specified and record exists' do
|
120
|
+
before { @table.save! }
|
121
|
+
|
122
|
+
it 'should not change id and created_at' do
|
123
|
+
id = @table.id
|
124
|
+
created_at = @table.created_at
|
125
|
+
@table.save!
|
126
|
+
expect(@table.id).to eq id
|
127
|
+
expect(@table.created_at).to eq created_at
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'with option' do
|
133
|
+
context 'that are id and text, active, number, created_at' do
|
134
|
+
context 'instance attributes does not set values without text' do
|
135
|
+
before do
|
136
|
+
@id = 100
|
137
|
+
@text = "hogehoge"
|
138
|
+
@active = true
|
139
|
+
@number = 0
|
140
|
+
@created_at = Time.now + 1000
|
141
|
+
@options = { id: @id, text: @text, active: @active, number: @number, created_at: @created_at }
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'should set specified value by option withou created_at' do
|
145
|
+
@table.save!(@options)
|
146
|
+
expect(@table.id).to eq @id
|
147
|
+
expect(@table.text).to eq @text
|
148
|
+
expect(@table.active).to eq @active
|
149
|
+
expect(@table.number).to eq @number
|
150
|
+
expect(@table.created_at).not_to eq @created_at
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
context 'instance all attributes set values' do
|
155
|
+
before do
|
156
|
+
@table.id = @setting_id = 10
|
157
|
+
@setting_text = @text
|
158
|
+
@setting_active = @active
|
159
|
+
@setting_number = @number
|
160
|
+
@table.created_at = @setting_created_at = Time.now - 1000
|
161
|
+
@options_id = 100
|
162
|
+
@options_text = "hogehoge"
|
163
|
+
@options_active = false
|
164
|
+
@options_number = 1
|
165
|
+
@options_created_at = Time.now + 1000
|
166
|
+
@options = { id: @options_id,
|
167
|
+
text: @options_text,
|
168
|
+
active: @options_active,
|
169
|
+
number: @options_number,
|
170
|
+
created_at: @options_created_at }
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'should set specified value by option withou created_at' do
|
174
|
+
@table.save!(@options)
|
175
|
+
expect(@table.id).to eq @options_id
|
176
|
+
expect(@table.text).to eq @options_text
|
177
|
+
expect(@table.active).to eq @options_active
|
178
|
+
expect(@table.number).to eq @options_number
|
179
|
+
expect(@table.created_at).not_to eq @options_created_at
|
180
|
+
expect(@table.created_at).not_to eq @setting_created_at
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe '#update_attributes!' do
|
189
|
+
before { @table = Facemock::Database::Table.new({text: "test", number: 1}) }
|
190
|
+
|
191
|
+
context 'without options' do
|
192
|
+
subject { lambda { @table.update_attributes! } }
|
193
|
+
it { is_expected.to raise_error ArgumentError }
|
194
|
+
end
|
195
|
+
|
196
|
+
context 'with options' do
|
197
|
+
context 'before save!' do
|
198
|
+
subject { @table.update_attributes!({ created_at: Time.now }) }
|
199
|
+
it { is_expected.to eq true }
|
200
|
+
|
201
|
+
context 'that does not include column name' do
|
202
|
+
before { @options = { hoge: "hoge" } }
|
203
|
+
subject { lambda { @table.update_attributes!(@options) } }
|
204
|
+
it { is_expected.to raise_error NoMethodError }
|
205
|
+
end
|
206
|
+
|
207
|
+
context 'that is id' do
|
208
|
+
it 'should set specified value by option' do
|
209
|
+
id = 100
|
210
|
+
@table.update_attributes!({ id: id })
|
211
|
+
expect(id).to eq id
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'should set specified value by option when instance id is set' do
|
215
|
+
@table.id = 10
|
216
|
+
id = 100
|
217
|
+
@table.update_attributes!({ id: id })
|
218
|
+
expect(id).to eq id
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'that is text' do
|
223
|
+
context 'but instance does not set value to text' do
|
224
|
+
it 'should change created_at value' do
|
225
|
+
table = Facemock::Database::Table.new
|
226
|
+
expect(lambda { table.update_attributes!({ text: "test" }) }).to raise_error
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
context 'but instance is set value to text' do
|
231
|
+
it 'should not change created_at value' do
|
232
|
+
text = "text"
|
233
|
+
table = Facemock::Database::Table.new({ text: text, number: 1 })
|
234
|
+
table.update_attributes!({ text: text })
|
235
|
+
expect(table.text).to eq text
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
context 'that is created_at' do
|
241
|
+
it 'should ignore the value' do
|
242
|
+
created_at = Time.now + 60
|
243
|
+
@table.update_attributes!({ created_at: created_at })
|
244
|
+
expect(@table.created_at).not_to eq created_at
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context 'after save!' do
|
250
|
+
before { @table.save! }
|
251
|
+
|
252
|
+
subject { @table.update_attributes!({ created_at: @table.created_at + 60 }) }
|
253
|
+
it { is_expected.to eq true }
|
254
|
+
|
255
|
+
context 'with option that does not include column name' do
|
256
|
+
before { @options = { hoge: "hoge" } }
|
257
|
+
subject { lambda { @table.update_attributes!(@options) } }
|
258
|
+
it { is_expected.to raise_error NoMethodError }
|
259
|
+
end
|
260
|
+
|
261
|
+
context 'when any column values does not change' do
|
262
|
+
it 'should not change created_at value' do
|
263
|
+
created_at = @table.created_at
|
264
|
+
@table.update_attributes!({ created_at: created_at })
|
265
|
+
expect(@table.created_at).to eq created_at
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
context 'when created_at column changes' do
|
270
|
+
it 'should change created_at value' do
|
271
|
+
created_at = @table.created_at + 60
|
272
|
+
@table.update_attributes!({ created_at: created_at })
|
273
|
+
expect(@table.created_at).to eq created_at
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
describe '#persisted?' do
|
281
|
+
before do
|
282
|
+
@table = Facemock::Database::Table.new({ text: "test", number: 1 })
|
283
|
+
end
|
284
|
+
|
285
|
+
context 'before save' do
|
286
|
+
subject { @table.persisted? }
|
287
|
+
it { is_expected.to be false }
|
288
|
+
end
|
289
|
+
|
290
|
+
context 'after save' do
|
291
|
+
before { @table.save! }
|
292
|
+
subject { @table.persisted? }
|
293
|
+
it { is_expected.to be true }
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
describe '#destroy!' do
|
298
|
+
before { @table = Facemock::Database::Table.new({ text: "test", number: 1 }) }
|
299
|
+
|
300
|
+
context 'before record is saved' do
|
301
|
+
subject { lambda { @table.destroy } }
|
302
|
+
it { is_expected.to raise_error RuntimeError }
|
303
|
+
end
|
304
|
+
|
305
|
+
context 'after records is saved' do
|
306
|
+
before { @table.save! }
|
307
|
+
subject { lambda { @table.destroy } }
|
308
|
+
it { is_expected.not_to raise_error }
|
309
|
+
end
|
310
|
+
|
311
|
+
context 'when tables table has two record' do
|
312
|
+
before do
|
313
|
+
@table.save!
|
314
|
+
Facemock::Database::Table.new({text: "test", number: 1}).save!
|
315
|
+
end
|
316
|
+
|
317
|
+
it 'should delete one record' do
|
318
|
+
expect(Facemock::Database::Table.all.count).to eq 2
|
319
|
+
@table.destroy
|
320
|
+
expect(Facemock::Database::Table.all.count).to eq 1
|
321
|
+
expect(Facemock::Database::Table.find_by_id(@table.id)).to eq nil
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
describe '.all' do
|
327
|
+
context 'when tables record does not exist' do
|
328
|
+
subject { Facemock::Database::Table.all }
|
329
|
+
|
330
|
+
it { is_expected.to be_kind_of Array }
|
331
|
+
it { is_expected.to be_empty }
|
332
|
+
end
|
333
|
+
|
334
|
+
context 'when tables record exists' do
|
335
|
+
before do
|
336
|
+
@ids = 3.times.inject([]) do |ary, i|
|
337
|
+
table = Facemock::Database::Table.new({text: "test", number: 1})
|
338
|
+
table.save!
|
339
|
+
ary << table.id
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
it 'should be array and should have three Table instances' do
|
344
|
+
tables = Facemock::Database::Table.all
|
345
|
+
expect(tables).to be_kind_of Array
|
346
|
+
expect(tables.count).to eq 3
|
347
|
+
tables.each do |table|
|
348
|
+
expect(table).to be_kind_of Facemock::Database::Table
|
349
|
+
expect(@ids).to be_include table.id
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
describe '.first' do
|
356
|
+
context 'when tables record does not exist' do
|
357
|
+
subject { Facemock::Database::Table.first }
|
358
|
+
it { is_expected.to be_nil }
|
359
|
+
end
|
360
|
+
|
361
|
+
context 'when tables record exists' do
|
362
|
+
before do
|
363
|
+
@ids = 3.times.inject([]) do |ary, i|
|
364
|
+
table = Facemock::Database::Table.new({text: "test", number: 1})
|
365
|
+
table.save!
|
366
|
+
ary << table.id
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
it 'should be Table instances and id is the smallest' do
|
371
|
+
finded = Facemock::Database::Table.first
|
372
|
+
expect(finded).to be_kind_of Facemock::Database::Table
|
373
|
+
expect(finded.id).to eq @ids.sort.first
|
374
|
+
end
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
describe '.last' do
|
379
|
+
context 'when tables record does not exist' do
|
380
|
+
subject { Facemock::Database::Table.last }
|
381
|
+
it { is_expected.to be_nil }
|
382
|
+
end
|
383
|
+
|
384
|
+
context 'when tables record exists' do
|
385
|
+
before do
|
386
|
+
@ids = 3.times.inject([]) do |ary, i|
|
387
|
+
table = Facemock::Database::Table.new({text: "test", number: 1})
|
388
|
+
table.save!
|
389
|
+
ary << table.id
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
it 'should be Table instances and id is biggest' do
|
394
|
+
finded = Facemock::Database::Table.last
|
395
|
+
expect(finded).to be_kind_of Facemock::Database::Table
|
396
|
+
expect(finded.id).to eq @ids.sort.last
|
397
|
+
end
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
describe '.where' do
|
402
|
+
context 'when tables record does not exist' do
|
403
|
+
subject { Facemock::Database::Table.where(id: 1) }
|
404
|
+
it { is_expected.to be_kind_of Array }
|
405
|
+
it { is_expected.to be_empty }
|
406
|
+
end
|
407
|
+
|
408
|
+
context 'when tables record exists' do
|
409
|
+
before do
|
410
|
+
@ids = 3.times.inject([]) do |ary, i|
|
411
|
+
table = Facemock::Database::Table.new({text: "test", number: 1})
|
412
|
+
table.save!
|
413
|
+
ary << table.id
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
it 'should be Array and should have only a Table instances' do
|
418
|
+
@ids.each do |id|
|
419
|
+
finded = Facemock::Database::Table.where(id: id)
|
420
|
+
expect(finded).to be_kind_of Array
|
421
|
+
expect(finded.count).to eq 1
|
422
|
+
expect(finded.first).to be_kind_of Facemock::Database::Table
|
423
|
+
expect(finded.first.id).to eq id
|
424
|
+
end
|
425
|
+
end
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
describe '.method_missing' do
|
430
|
+
context 'method name does not include find_by and find_all_by' do
|
431
|
+
subject { lambda { Facemock::Database::Table.find_hoge } }
|
432
|
+
it { is_expected.to raise_error NoMethodError }
|
433
|
+
end
|
434
|
+
|
435
|
+
context 'method name does not inlcude column name' do
|
436
|
+
context 'without argument' do
|
437
|
+
subject { lambda { Facemock::Database::Table.find_by_hoge } }
|
438
|
+
it { is_expected.to raise_error NoMethodError }
|
439
|
+
end
|
440
|
+
|
441
|
+
context 'with argument' do
|
442
|
+
subject { lambda { Facemock::Database::Table.find_by_hoge("hoge") } }
|
443
|
+
it { is_expected.to raise_error NoMethodError }
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
context 'method name inlcudes by_column_name' do
|
448
|
+
context 'without argument' do
|
449
|
+
subject { lambda { Facemock::Database::Table.find_by_id } }
|
450
|
+
it { is_expected.to raise_error ArgumentError }
|
451
|
+
end
|
452
|
+
|
453
|
+
describe '.find_by_id' do
|
454
|
+
context 'with not id' do
|
455
|
+
subject { Facemock::Database::Table.find_by_id("hoge") }
|
456
|
+
it { is_expected.to be_nil }
|
457
|
+
end
|
458
|
+
|
459
|
+
context 'with id' do
|
460
|
+
context 'when record does not exist' do
|
461
|
+
subject { Facemock::Database::Table.find_by_id(1) }
|
462
|
+
it { is_expected.to be_nil }
|
463
|
+
end
|
464
|
+
|
465
|
+
context 'when record exists' do
|
466
|
+
it 'should be Table instance' do
|
467
|
+
created = Facemock::Database::Table.new({text: "test", number: 1})
|
468
|
+
created.save!
|
469
|
+
finded = Facemock::Database::Table.find_by_id(created.id)
|
470
|
+
expect(finded).to be_kind_of Facemock::Database::Table
|
471
|
+
expect(finded.id).to eq created.id
|
472
|
+
finded.instance_variables.each do |key|
|
473
|
+
expect(finded.instance_variable_get(key)).to eq created.instance_variable_get(key)
|
474
|
+
end
|
475
|
+
end
|
476
|
+
end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
describe '.find_by_created_at' do
|
481
|
+
context 'with not created_at' do
|
482
|
+
subject { Facemock::Database::Table.find_by_created_at("hoge") }
|
483
|
+
it { is_expected.to be_nil }
|
484
|
+
end
|
485
|
+
|
486
|
+
context 'with created_at' do
|
487
|
+
context 'when record does not exist' do
|
488
|
+
subject { Facemock::Database::Table.find_by_created_at(Time.now) }
|
489
|
+
it { is_expected.to be_nil }
|
490
|
+
end
|
491
|
+
|
492
|
+
context 'when record exists' do
|
493
|
+
it 'should be Table instance' do
|
494
|
+
created = Facemock::Database::Table.new({text: "test", number: 1})
|
495
|
+
created.save!
|
496
|
+
finded = Facemock::Database::Table.find_by_created_at(created.created_at)
|
497
|
+
expect(finded).to be_kind_of Facemock::Database::Table
|
498
|
+
expect(finded.id).to eq created.id
|
499
|
+
finded.instance_variables.each do |key|
|
500
|
+
expect(finded.instance_variable_get(key)).to eq created.instance_variable_get(key)
|
501
|
+
end
|
502
|
+
end
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end
|
506
|
+
end
|
507
|
+
|
508
|
+
context 'method name includes find_all_by_column_name' do
|
509
|
+
context 'without argument' do
|
510
|
+
subject { lambda { Facemock::Database::Table.find_all_by_id } }
|
511
|
+
it { is_expected.to raise_error ArgumentError }
|
512
|
+
end
|
513
|
+
|
514
|
+
describe '.find_all_by_id' do
|
515
|
+
context 'with not id' do
|
516
|
+
subject { Facemock::Database::Table.find_all_by_id("hoge") }
|
517
|
+
it { is_expected.to be_empty }
|
518
|
+
end
|
519
|
+
|
520
|
+
context 'with id' do
|
521
|
+
context 'when record does not exist' do
|
522
|
+
subject { Facemock::Database::Table.find_all_by_id(1) }
|
523
|
+
it { is_expected.to be_empty }
|
524
|
+
end
|
525
|
+
|
526
|
+
context 'when record exists' do
|
527
|
+
it 'should be array and should have only one Table instances' do
|
528
|
+
created = Facemock::Database::Table.new({text: "test", number: 1})
|
529
|
+
created.save!
|
530
|
+
tables = Facemock::Database::Table.find_all_by_id(created.id)
|
531
|
+
expect(tables).to be_kind_of Array
|
532
|
+
expect(tables.count).to eq 1
|
533
|
+
tables.each do |finded|
|
534
|
+
finded.instance_variables.each do |key|
|
535
|
+
expect(finded.instance_variable_get(key)).to eq created.instance_variable_get(key)
|
536
|
+
end
|
537
|
+
end
|
538
|
+
end
|
539
|
+
end
|
540
|
+
end
|
541
|
+
end
|
542
|
+
|
543
|
+
describe '.find_all_by_created_at' do
|
544
|
+
context 'with not created_at' do
|
545
|
+
subject { Facemock::Database::Table.find_all_by_created_at("hoge") }
|
546
|
+
it { is_expected.to be_empty }
|
547
|
+
end
|
548
|
+
|
549
|
+
context 'with created_at' do
|
550
|
+
context 'when record does not exist' do
|
551
|
+
subject { Facemock::Database::Table.find_all_by_created_at(Time.now) }
|
552
|
+
it { is_expected.to be_empty }
|
553
|
+
end
|
554
|
+
|
555
|
+
context 'when record exists' do
|
556
|
+
it 'should be Table instance' do
|
557
|
+
created = Facemock::Database::Table.new({text: "test", number: 1})
|
558
|
+
created.save!
|
559
|
+
created_at = created.created_at
|
560
|
+
updated = Facemock::Database::Table.new({text: "test", number: 1})
|
561
|
+
updated.save!
|
562
|
+
updated.created_at = created_at
|
563
|
+
updated.save!
|
564
|
+
|
565
|
+
tables = Facemock::Database::Table.find_all_by_created_at(created_at)
|
566
|
+
expect(tables).to be_kind_of Array
|
567
|
+
expect(tables.count).to eq 2
|
568
|
+
tables.each do |finded|
|
569
|
+
expect(finded).to be_kind_of Facemock::Database::Table
|
570
|
+
expect(finded.created_at).to eq created_at
|
571
|
+
end
|
572
|
+
end
|
573
|
+
end
|
574
|
+
end
|
575
|
+
end
|
576
|
+
end
|
577
|
+
end
|
578
|
+
|
579
|
+
describe '#method_missing' do
|
580
|
+
before do
|
581
|
+
@table = Facemock::Database::Table.new({text: "test", number: 1})
|
582
|
+
@table.save!
|
583
|
+
end
|
584
|
+
|
585
|
+
context 'when method is getter' do
|
586
|
+
describe '#id' do
|
587
|
+
subject { @table.id }
|
588
|
+
it { is_expected.to be_kind_of Integer }
|
589
|
+
end
|
590
|
+
|
591
|
+
describe '#identifier' do
|
592
|
+
subject { @table.identifier }
|
593
|
+
it { is_expected.to eq @table.id }
|
594
|
+
end
|
595
|
+
end
|
596
|
+
|
597
|
+
context 'when method is setter' do
|
598
|
+
describe '#id=' do
|
599
|
+
before { @id = 1 }
|
600
|
+
|
601
|
+
it 'should set attribute to id' do
|
602
|
+
expect(@table.id).to be_kind_of Integer
|
603
|
+
@table.id = @id
|
604
|
+
expect(@table.id).to eq @id
|
605
|
+
end
|
606
|
+
end
|
607
|
+
|
608
|
+
describe '#identifier=' do
|
609
|
+
before { @id = 1 }
|
610
|
+
|
611
|
+
it 'should set attribute to id' do
|
612
|
+
expect(@table.identifier).to be_kind_of Integer
|
613
|
+
@table.identifier = @id
|
614
|
+
expect(@table.id).to eq @id
|
615
|
+
expect(@table.identifier).to eq @table.id
|
616
|
+
end
|
617
|
+
end
|
618
|
+
end
|
619
|
+
end
|
620
|
+
|
621
|
+
describe '.table_info' do
|
622
|
+
subject { Facemock::Database::Table.table_info }
|
623
|
+
it { is_expected.to be_kind_of Hashie::Mash }
|
624
|
+
|
625
|
+
it 'has keys that is id and created_at' do
|
626
|
+
table_info = Facemock::Database::Table.table_info
|
627
|
+
table_info.each_keys do |key|
|
628
|
+
expect(key.to_sym).to include column_names
|
629
|
+
end
|
630
|
+
end
|
631
|
+
|
632
|
+
context 'then keys' do
|
633
|
+
before { @table_info = Facemock::Database::Table.table_info }
|
634
|
+
|
635
|
+
describe '#id' do
|
636
|
+
subject { @table_info.id }
|
637
|
+
it { is_expected.to be_kind_of Hashie::Mash }
|
638
|
+
|
639
|
+
it 'should have column_info' do
|
640
|
+
expect(@table_info.id.cid).to eq 0
|
641
|
+
expect(@table_info.id.name).to eq :id
|
642
|
+
expect(@table_info.id.type).to eq "INTEGER"
|
643
|
+
expect(@table_info.id.notnull).to eq false
|
644
|
+
expect(@table_info.id.dflt_value).to be_nil
|
645
|
+
expect(@table_info.id.pk).to eq true
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
describe '#text' do
|
650
|
+
subject { @table_info.created_at }
|
651
|
+
it { is_expected.to be_kind_of Hashie::Mash }
|
652
|
+
|
653
|
+
it 'should have column_info' do
|
654
|
+
expect(@table_info.text.cid).to eq 1
|
655
|
+
expect(@table_info.text.name).to eq :text
|
656
|
+
expect(@table_info.text.type).to eq "TEXT"
|
657
|
+
expect(@table_info.text.notnull).to eq true
|
658
|
+
expect(@table_info.text.dflt_value).to be_nil
|
659
|
+
expect(@table_info.text.pk).to eq false
|
660
|
+
end
|
661
|
+
end
|
662
|
+
|
663
|
+
describe '#active' do
|
664
|
+
subject { @table_info.active }
|
665
|
+
it { is_expected.to be_kind_of Hashie::Mash }
|
666
|
+
|
667
|
+
it 'should have column_info' do
|
668
|
+
expect(@table_info.active.cid).to eq 2
|
669
|
+
expect(@table_info.active.name).to eq :active
|
670
|
+
expect(@table_info.active.type).to eq "BOOLEAN"
|
671
|
+
expect(@table_info.active.notnull).to eq false
|
672
|
+
expect(@table_info.active.dflt_value).to be_nil
|
673
|
+
expect(@table_info.active.pk).to eq false
|
674
|
+
end
|
675
|
+
end
|
676
|
+
|
677
|
+
describe '#number' do
|
678
|
+
subject { @table_info.number }
|
679
|
+
it { is_expected.to be_kind_of Hashie::Mash }
|
680
|
+
|
681
|
+
it 'should have column_info' do
|
682
|
+
expect(@table_info.number.cid).to eq 3
|
683
|
+
expect(@table_info.number.name).to eq :number
|
684
|
+
expect(@table_info.number.type).to eq "INTEGER"
|
685
|
+
expect(@table_info.number.notnull).to eq true
|
686
|
+
expect(@table_info.number.dflt_value).to be_nil
|
687
|
+
expect(@table_info.number.pk).to eq false
|
688
|
+
end
|
689
|
+
end
|
690
|
+
|
691
|
+
describe '#created_at' do
|
692
|
+
subject { @table_info.created_at }
|
693
|
+
it { is_expected.to be_kind_of Hashie::Mash }
|
694
|
+
|
695
|
+
it 'should have column_info' do
|
696
|
+
expect(@table_info.created_at.cid).to eq 4
|
697
|
+
expect(@table_info.created_at.name).to eq :created_at
|
698
|
+
expect(@table_info.created_at.type).to eq "DATETIME"
|
699
|
+
expect(@table_info.created_at.notnull).to eq true
|
700
|
+
expect(@table_info.created_at.dflt_value).to be_nil
|
701
|
+
expect(@table_info.created_at.pk).to eq false
|
702
|
+
end
|
703
|
+
end
|
704
|
+
end
|
705
|
+
end
|
706
|
+
|
707
|
+
describe '.column_type' do
|
708
|
+
context 'without argument' do
|
709
|
+
subject { lambda { Facemock::Database::Table.column_type } }
|
710
|
+
it { is_expected.to raise_error ArgumentError }
|
711
|
+
end
|
712
|
+
|
713
|
+
context 'with not column name' do
|
714
|
+
subject { Facemock::Database::Table.column_type(:hoge) }
|
715
|
+
it { is_expected.to be_nil }
|
716
|
+
end
|
717
|
+
|
718
|
+
context 'with id' do
|
719
|
+
subject { Facemock::Database::Table.column_type(:id) }
|
720
|
+
it { is_expected.to eq "INTEGER" }
|
721
|
+
end
|
722
|
+
|
723
|
+
context 'with created_at' do
|
724
|
+
subject { Facemock::Database::Table.column_type(:created_at) }
|
725
|
+
it { is_expected.to eq "DATETIME" }
|
726
|
+
end
|
727
|
+
end
|
728
|
+
end
|