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