flydata 0.0.4.4 → 0.0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,250 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+
4
+ if defined? ActiveModel
5
+ # TestUserModel model
6
+ class TestUserModel
7
+ include FlyDataModel
8
+ attr_accessor :name, :birthday, :timestamp
9
+
10
+ def initialize(attributes = {})
11
+ attributes.each do |name, value|
12
+ send("#{name}=", value)
13
+ end
14
+ end
15
+
16
+ def attributes
17
+ {
18
+ 'name' => name,
19
+ 'birthday' => birthday,
20
+ 'timestamp' => timestamp,
21
+ }
22
+ end
23
+ end
24
+
25
+ # TestOrderModel model
26
+ class TestOrderModel
27
+ include FlyDataModel
28
+ attr_accessor :user_id, :item, :quantity, :purchased_on, :timestamp
29
+
30
+ def initialize(attributes = {})
31
+ attributes.each do |name, value|
32
+ send("#{name}=", value)
33
+ end
34
+ end
35
+
36
+ def attributes
37
+ {
38
+ 'user_id' => user_id,
39
+ 'item' => item,
40
+ 'quantity' => quantity,
41
+ 'purchased_on' => purchased_on,
42
+ 'timestamp' => timestamp,
43
+ }
44
+ end
45
+ end
46
+
47
+ # TestOrder model with configured table name and attributes
48
+ class ConfiguredTestOrderModel < TestOrderModel
49
+ flydata_table :other_table
50
+ flydata_attr :item, :quantity
51
+ end
52
+ end
53
+
54
+ describe Flydata do
55
+
56
+ describe '.send_to' do
57
+ before do
58
+ # Use ActiveSupport::TimeWithZone
59
+ Time.zone = 'Japan'
60
+ end
61
+
62
+ describe 'ActiveModel support' do
63
+ context '1st argument is includes ActiveModel::Serialization' do
64
+ let(:first_argument) do
65
+ TestOrderModel.new(user_id: 2,
66
+ item: 'orange',
67
+ quantity: 6,
68
+ purchased_on: Date.new(2013, 6, 1),
69
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
70
+ end
71
+
72
+ it 'puts a JSON string represents 1st argument' do
73
+ actual = capture(:stdout) { described_class.send_to(first_argument) }.chomp
74
+ json = <<-EOS
75
+ {
76
+ "test_order_models": {
77
+ "item": "orange",
78
+ "purchased_on": "2013-06-01",
79
+ "quantity": 6,
80
+ "timestamp": "2013-06-01 11:22:33",
81
+ "user_id": 2
82
+ }
83
+ }
84
+ EOS
85
+ expectation = JSON.parse(json).to_json
86
+ expect(actual).to eq(expectation)
87
+ end
88
+ end
89
+
90
+ context '1st argument is an Array of models which includes ActiveModel::Serialization' do
91
+ let(:first_argument) do
92
+ [
93
+ TestOrderModel.new(user_id: 2,
94
+ item: 'orange',
95
+ quantity: 6,
96
+ purchased_on: Date.new(2013, 6, 1),
97
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33)),
98
+ TestOrderModel.new(user_id: 2,
99
+ item: 'banana',
100
+ quantity: 12,
101
+ purchased_on: Date.new(2013, 7, 1),
102
+ timestamp: Time.local(2013, 7, 1, 11, 22, 33)),
103
+ ]
104
+ end
105
+
106
+ it 'puts a JSON string represents 1st argument' do
107
+ actual = capture(:stdout) { described_class.send_to(first_argument) }.chomp
108
+ json = <<-EOS
109
+ {
110
+ "test_order_models": [{
111
+ "item": "orange",
112
+ "purchased_on": "2013-06-01",
113
+ "quantity": 6,
114
+ "timestamp": "2013-06-01 11:22:33",
115
+ "user_id": 2
116
+ }, {
117
+ "item": "banana",
118
+ "purchased_on": "2013-07-01",
119
+ "quantity": 12,
120
+ "timestamp": "2013-07-01 11:22:33",
121
+ "user_id": 2
122
+ }
123
+ ]
124
+ }
125
+ EOS
126
+ expectation = JSON.parse(json).to_json
127
+ expect(actual).to eq(expectation)
128
+ end
129
+ end
130
+
131
+ context 'Arguments are including ActiveModel::Base' do
132
+ let(:first_argument) {
133
+ TestOrderModel.new(user_id: 2,
134
+ item: 'orange',
135
+ quantity: 6,
136
+ purchased_on: Date.new(2013, 6, 1),
137
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
138
+ }
139
+ let(:second_argument) {
140
+ TestOrderModel.new(user_id: 2,
141
+ item: 'banana',
142
+ quantity: 12,
143
+ purchased_on: Date.new(2013, 7, 1),
144
+ timestamp: Time.local(2013, 7, 1, 11, 22, 33))
145
+ }
146
+ let(:third_argument) { TestUserModel.new(name: 'John') }
147
+
148
+ it 'puts a JSON string represents all arguments' do
149
+ actual = capture(:stdout) do
150
+ described_class.send_to(first_argument,
151
+ second_argument,
152
+ third_argument)
153
+ end.chomp
154
+ json = <<-EOS
155
+ {
156
+ "test_order_models": [{
157
+ "item": "orange",
158
+ "purchased_on": "2013-06-01",
159
+ "quantity": 6,
160
+ "timestamp": "2013-06-01 11:22:33",
161
+ "user_id": 2
162
+ },
163
+ {
164
+ "item": "banana",
165
+ "purchased_on": "2013-07-01",
166
+ "quantity": 12,
167
+ "timestamp": "2013-07-01 11:22:33",
168
+ "user_id": 2
169
+ }
170
+ ],
171
+ "test_user_models": {
172
+ "birthday": null,
173
+ "name": "John",
174
+ "timestamp": null
175
+ }
176
+ }
177
+ EOS
178
+ expectation = JSON.parse(json).to_json
179
+ expect(actual).to eq(expectation)
180
+ end
181
+ end
182
+ end
183
+ end
184
+
185
+ if defined? ActiveModel
186
+ describe 'ActiveModel::Serialization#send_to_flydata' do
187
+ let(:order) do
188
+ TestOrderModel.new(user_id: 2,
189
+ item: 'orange',
190
+ quantity: 6,
191
+ purchased_on: Date.new(2013, 6, 1),
192
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
193
+ end
194
+
195
+ it 'puts a JSON string represents a model' do
196
+ actual = capture(:stdout) { order.send_to_flydata }.chomp
197
+ json = <<-EOS
198
+ {
199
+ "test_orders": {
200
+ "item": "orange",
201
+ "purchased_on": "2013-06-01",
202
+ "quantity": 6,
203
+ "timestamp": "2013-06-01 11:22:33",
204
+ "user_id": 2
205
+ }
206
+ }
207
+ EOS
208
+ expectation = JSON.parse(json).to_json
209
+ end
210
+ end
211
+
212
+ describe 'ActiveModel::Serialization.flydata_table' do
213
+ let(:order) {
214
+ ConfiguredTestOrderModel.new(user_id: 2, item: 'orange', quantity: 6)
215
+ }
216
+
217
+ it 'puts a JSON string represents a model to configured table name' do
218
+ output = capture(:stdout) { order.send_to_flydata }
219
+ actual = JSON.parse(output)
220
+ expect(actual.keys).to include('other_table')
221
+ expect(actual.keys).not_to include('test_orders')
222
+ end
223
+ end
224
+
225
+ describe 'ActiveModel::Serialization.flydata_attr' do
226
+ let(:order) { ConfiguredTestOrderModel.new(user_id: 2, item: 'orange', quantity: 6) }
227
+
228
+ it 'puts a JSON string represents a model to configured attributes' do
229
+ output = capture(:stdout) { order.send_to_flydata }
230
+ actual = JSON.parse(output)
231
+ expect(actual['other_table'].keys).to include('item')
232
+ expect(actual['other_table'].keys).to include('quantity')
233
+ expect(actual['other_table'].keys).not_to include('purchased_on')
234
+ expect(actual['other_table'].keys).not_to include('timestamp')
235
+ expect(actual['other_table'].keys).not_to include('user_id')
236
+ end
237
+
238
+ context 'without attributes' do
239
+ it 'raise exception' do
240
+ expect {
241
+ class InvalidConfiguredTestOrderModel < TestOrderModel
242
+ flydata_attr
243
+ end
244
+ }.to raise_error(RuntimeError, 'flydata_attr attributes cannot be blank')
245
+ end
246
+ end
247
+ end
248
+ end
249
+
250
+ end
@@ -0,0 +1,668 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+
4
+ if defined? ActiveRecord
5
+ # Prepare for tests ActiveRecord
6
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
7
+
8
+ # Database migrations
9
+ ActiveRecord::Migration.create_table :test_users do |t|
10
+ t.string :name
11
+ t.date :birthday
12
+ t.timestamp :timestamp
13
+ end
14
+ ActiveRecord::Migration.create_table :test_orders do |t|
15
+ t.references :user
16
+ t.string :item
17
+ t.integer :quantity
18
+ t.date :purchased_on
19
+ t.timestamp :timestamp
20
+ end
21
+
22
+ # TestUser model
23
+ class TestUser < ActiveRecord::Base
24
+ attr_accessible :id, :name, :birthday, :timestamp
25
+ has_many :orders
26
+ end
27
+
28
+ # TestOrder model
29
+ class TestOrder < ActiveRecord::Base
30
+ attr_accessible :id, :user_id, :item, :quantity, :purchased_on, :timestamp
31
+ belongs_to :user
32
+ end
33
+
34
+ # TestOrder model with configured table name and attributes
35
+ class ConfiguredTestOrder < TestOrder
36
+ flydata_table :other_table
37
+ flydata_attr :id, :item, :purchased_on, :timestamp
38
+ end
39
+ end
40
+
41
+ if defined? ActiveModel
42
+ # TestUserModel model
43
+ class TestUserModel
44
+ include ActiveModel::Serialization
45
+ attr_accessor :name, :birthday, :timestamp
46
+
47
+ def initialize(attributes = {})
48
+ attributes.each do |name, value|
49
+ send("#{name}=", value)
50
+ end
51
+ end
52
+
53
+ def attributes
54
+ {
55
+ 'name' => name,
56
+ 'birthday' => birthday,
57
+ 'timestamp' => timestamp,
58
+ }
59
+ end
60
+ end
61
+
62
+ # TestOrderModel model
63
+ class TestOrderModel
64
+ include ActiveModel::Serialization
65
+ attr_accessor :user_id, :item, :quantity, :purchased_on, :timestamp
66
+
67
+ def initialize(attributes = {})
68
+ attributes.each do |name, value|
69
+ send("#{name}=", value)
70
+ end
71
+ end
72
+
73
+ def attributes
74
+ {
75
+ 'user_id' => user_id,
76
+ 'item' => item,
77
+ 'quantity' => quantity,
78
+ 'purchased_on' => purchased_on,
79
+ 'timestamp' => timestamp,
80
+ }
81
+ end
82
+ end
83
+
84
+ # TestOrder model with configured table name and attributes
85
+ class ConfiguredTestOrderModel < TestOrderModel
86
+ flydata_table :other_table
87
+ flydata_attr :item, :quantity
88
+ end
89
+ end
90
+
91
+ # Captures and returns output to specified stream.
92
+ def capture(stream)
93
+ begin
94
+ stream = stream.to_s
95
+ eval "$#{stream} = StringIO.new"
96
+ yield
97
+ result = eval("$#{stream}").string
98
+ ensure
99
+ eval "$#{stream} = #{stream.upcase}"
100
+ end
101
+ result
102
+ end
103
+
104
+ describe Flydata do
105
+
106
+ describe '.send_to' do
107
+ before do
108
+ # Use ActiveSupport::TimeWithZone
109
+ Time.zone = 'Japan'
110
+ end
111
+
112
+ let(:item_hash) do
113
+ {
114
+ id: 1,
115
+ name: 'orange',
116
+ date: Date.new(2013, 6, 1),
117
+ time: Time.local(2013, 6, 1, 11, 22, 33),
118
+ date_time: Time.local(2013, 6, 1, 11, 22, 33).to_datetime,
119
+ time_with_zone: Time.zone.local(2013, 6, 1, 11, 22, 33),
120
+ }
121
+ end
122
+ let(:item_hash_json) do
123
+ json = <<-EOS
124
+ {
125
+ "items": {
126
+ "id": 1,
127
+ "name": "orange",
128
+ "date": "2013-06-01",
129
+ "time": "2013-06-01 11:22:33",
130
+ "date_time": "2013-06-01 11:22:33",
131
+ "time_with_zone": "2013-06-01 11:22:33"
132
+ }
133
+ }
134
+ EOS
135
+ JSON.parse(json).to_json
136
+ end
137
+
138
+ let(:item_array) do
139
+ [
140
+ {
141
+ id: 1,
142
+ name: 'orange',
143
+ date: Date.new(2013, 6, 1),
144
+ time: Time.local(2013, 6, 1, 11, 22, 33),
145
+ date_time: Time.local(2013, 6, 1, 11, 22, 33).to_datetime,
146
+ time_with_zone: Time.zone.local(2013, 6, 1, 11, 22, 33),
147
+ },
148
+ {
149
+ id: 2,
150
+ name: 'banana',
151
+ date: Date.new(2013, 6, 2),
152
+ time: Time.local(2013, 6, 2, 11, 22, 33),
153
+ date_time: Time.local(2013, 6, 2, 11, 22, 33).to_datetime,
154
+ time_with_zone: Time.zone.local(2013, 6, 2, 11, 22, 33),
155
+ },
156
+ ]
157
+ end
158
+ let(:item_array_json) do
159
+ json = <<-EOS
160
+ {
161
+ "items": [{
162
+ "id": 1,
163
+ "name": "orange",
164
+ "date": "2013-06-01",
165
+ "time": "2013-06-01 11:22:33",
166
+ "date_time": "2013-06-01 11:22:33",
167
+ "time_with_zone": "2013-06-01 11:22:33"
168
+ }, {
169
+ "id": 2,
170
+ "name": "banana",
171
+ "date": "2013-06-02",
172
+ "time": "2013-06-02 11:22:33",
173
+ "date_time": "2013-06-02 11:22:33",
174
+ "time_with_zone": "2013-06-02 11:22:33"
175
+ }
176
+ ]
177
+ }
178
+ EOS
179
+ JSON.parse(json).to_json
180
+ end
181
+
182
+ context '1st argument is a nil' do
183
+ it 'raises an ArgumentError' do
184
+ expect {
185
+ capture(:stdout) { described_class.send_to }
186
+ }.to raise_error(ArgumentError, 'The argument of send_to must be non-nil')
187
+ end
188
+ end
189
+
190
+ context '1st argument is a String' do
191
+ context '1st argument is an empty String' do
192
+ it 'raises an ArgumentError' do
193
+ expect {
194
+ capture(:stdout) { described_class.send_to('') }
195
+ }.to raise_error(ArgumentError, 'The 1st argument of send_to must not be empty')
196
+ end
197
+ end
198
+
199
+ context '2nd argument is a Hash' do
200
+ it 'puts a JSON string' do
201
+ actual = capture(:stdout) do
202
+ described_class.send_to('items', item_hash)
203
+ end.chomp
204
+ expect(actual).to eq(item_hash_json)
205
+ end
206
+ end
207
+
208
+ context '2nd argument is an Array' do
209
+ it 'puts a JSON string' do
210
+ actual = capture(:stdout) do
211
+ described_class.send_to('items', item_array)
212
+ end.chomp
213
+ expect(actual).to eq(item_array_json)
214
+ end
215
+ end
216
+
217
+ context '2nd argument is nil' do
218
+ it 'raises an ArgumentError' do
219
+ expect {
220
+ capture(:stdout) { described_class.send_to('items') }
221
+ }.to raise_error(ArgumentError, 'The 2nd argument of send_to must be an Array or Hash')
222
+ end
223
+ end
224
+
225
+ context '2nd argument is a String' do
226
+ it 'raises an ArgumentError' do
227
+ expect {
228
+ capture(:stdout) { described_class.send_to('items', 'foo') }
229
+ }.to raise_error(ArgumentError)
230
+ end
231
+ end
232
+ end
233
+
234
+ context '1st argument is kind of a Symbol' do
235
+ context '1st argument is an empty Symbol' do
236
+ it 'raises an ArgumentError' do
237
+ expect {
238
+ capture(:stdout) { described_class.send_to(:'') }
239
+ }.to raise_error(ArgumentError, 'The 1st argument of send_to must not be empty')
240
+ end
241
+ end
242
+
243
+ context '2nd argument is a Hash' do
244
+ it 'puts a JSON string' do
245
+ actual = capture(:stdout) do
246
+ described_class.send_to(:items, item_hash)
247
+ end.chomp
248
+ expect(actual).to eq(item_hash_json)
249
+ end
250
+ end
251
+
252
+ context '2nd argument is an Array' do
253
+ it 'puts a JSON string' do
254
+ actual = capture(:stdout) do
255
+ described_class.send_to(:items, item_array)
256
+ end.chomp
257
+ expect(actual).to eq(item_array_json)
258
+ end
259
+ end
260
+ end
261
+
262
+ context '1st argument is an empty Array' do
263
+ it 'raises an ArgumentError' do
264
+ expect {
265
+ capture(:stdout) { described_class.send_to([]) }
266
+ }.to raise_error(ArgumentError, 'The 1st argument of send_to must not be empty')
267
+ end
268
+ end
269
+
270
+ context '1st argument is an empty Hash' do
271
+ it 'raises an ArgumentError' do
272
+ expect {
273
+ capture(:stdout) { described_class.send_to({}) }
274
+ }.to raise_error(ArgumentError, 'The 1st argument of send_to must not be empty')
275
+ end
276
+ end
277
+
278
+ describe 'ActiveRecord support' do
279
+ let!(:user) do
280
+ TestUser.create!(name: 'John Doe',
281
+ birthday: Date.new(2010, 1, 2),
282
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
283
+ end
284
+ let!(:order1) do
285
+ TestOrder.create!(user_id: user.id,
286
+ item: 'orange',
287
+ quantity: 6,
288
+ purchased_on: Date.new(2013, 6, 1),
289
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
290
+ end
291
+ let!(:order2) do
292
+ TestOrder.create!(user_id: user.id,
293
+ item: 'banana',
294
+ quantity: 12,
295
+ purchased_on: Date.new(2013, 6, 2),
296
+ timestamp: Time.local(2013, 6, 2, 11, 22, 33))
297
+ end
298
+
299
+ context '1st argument is an ActiveRecord::Base' do
300
+ let(:first_argument) { order1 }
301
+
302
+ it 'puts a JSON string' do
303
+ actual = capture(:stdout) { described_class.send_to(first_argument) }.chomp
304
+ json = <<-EOS
305
+ {
306
+ "test_orders": {
307
+ "id": #{order1.id},
308
+ "item": "orange",
309
+ "purchased_on": "2013-06-01",
310
+ "quantity": 6,
311
+ "timestamp": "2013-06-01 11:22:33",
312
+ "user_id": #{user.id}
313
+ }
314
+ }
315
+ EOS
316
+ expectation = JSON.parse(json).to_json
317
+ expect(actual).to eq(expectation)
318
+ end
319
+ end
320
+
321
+ context '1st argument is an Array of ActiveRecord::Base' do
322
+ let(:first_argument) { [order1, order2] }
323
+
324
+ it 'puts a JSON string' do
325
+ actual = capture(:stdout) { described_class.send_to(first_argument) }.chomp
326
+ json = <<-EOS
327
+ {
328
+ "test_orders": [{
329
+ "id": #{order1.id},
330
+ "item": "orange",
331
+ "purchased_on": "2013-06-01",
332
+ "quantity": 6,
333
+ "timestamp": "2013-06-01 11:22:33",
334
+ "user_id": #{user.id}
335
+ }, {
336
+ "id": #{order2.id},
337
+ "item": "banana",
338
+ "purchased_on": "2013-06-02",
339
+ "quantity": 12,
340
+ "timestamp": "2013-06-02 11:22:33",
341
+ "user_id": #{user.id}
342
+ }
343
+ ]
344
+ }
345
+ EOS
346
+ expectation = JSON.parse(json).to_json
347
+ expect(actual).to eq(expectation)
348
+ end
349
+ end
350
+
351
+ context 'Arguments are ActiveRecord::Base' do
352
+ let(:first_argument) { user }
353
+ let(:second_argument) { order1 }
354
+ let(:third_argument) { order2 }
355
+
356
+ it 'puts a JSON string represents all arguments' do
357
+ actual = capture(:stdout) do
358
+ described_class.send_to(first_argument,
359
+ second_argument,
360
+ third_argument)
361
+ end.chomp
362
+
363
+ json = <<-EOS
364
+ {
365
+ "test_orders": [{
366
+ "id": #{order1.id},
367
+ "item": "orange",
368
+ "purchased_on": "2013-06-01",
369
+ "quantity": 6,
370
+ "timestamp": "2013-06-01 11:22:33",
371
+ "user_id": #{user.id}
372
+ }, {
373
+ "id": #{order2.id},
374
+ "item": "banana",
375
+ "purchased_on": "2013-06-02",
376
+ "quantity": 12,
377
+ "timestamp": "2013-06-02 11:22:33",
378
+ "user_id": #{user.id}
379
+ }
380
+ ],
381
+ "test_users": {
382
+ "birthday": "2010-01-02",
383
+ "id": #{user.id},
384
+ "name": "John Doe",
385
+ "timestamp": "2013-06-02 11:22:33"
386
+ }
387
+ }
388
+ EOS
389
+ expectation = JSON.parse(json).to_json
390
+ end
391
+ end
392
+ end
393
+
394
+ describe 'ActiveModel support' do
395
+ context '1st argument is includes ActiveModel::Serialization' do
396
+ let(:first_argument) do
397
+ TestOrderModel.new(user_id: 2,
398
+ item: 'orange',
399
+ quantity: 6,
400
+ purchased_on: Date.new(2013, 6, 1),
401
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
402
+ end
403
+
404
+ it 'puts a JSON string represents 1st argument' do
405
+ actual = capture(:stdout) { described_class.send_to(first_argument) }.chomp
406
+ json = <<-EOS
407
+ {
408
+ "test_order_models": {
409
+ "item": "orange",
410
+ "purchased_on": "2013-06-01",
411
+ "quantity": 6,
412
+ "timestamp": "2013-06-01 11:22:33",
413
+ "user_id": 2
414
+ }
415
+ }
416
+ EOS
417
+ expectation = JSON.parse(json).to_json
418
+ expect(actual).to eq(expectation)
419
+ end
420
+ end
421
+
422
+ context '1st argument is an Array of models which includes ActiveModel::Serialization' do
423
+ let(:first_argument) do
424
+ [
425
+ TestOrderModel.new(user_id: 2,
426
+ item: 'orange',
427
+ quantity: 6,
428
+ purchased_on: Date.new(2013, 6, 1),
429
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33)),
430
+ TestOrderModel.new(user_id: 2,
431
+ item: 'banana',
432
+ quantity: 12,
433
+ purchased_on: Date.new(2013, 7, 1),
434
+ timestamp: Time.local(2013, 7, 1, 11, 22, 33)),
435
+ ]
436
+ end
437
+
438
+ it 'puts a JSON string represents 1st argument' do
439
+ actual = capture(:stdout) { described_class.send_to(first_argument) }.chomp
440
+ json = <<-EOS
441
+ {
442
+ "test_order_models": [{
443
+ "item": "orange",
444
+ "purchased_on": "2013-06-01",
445
+ "quantity": 6,
446
+ "timestamp": "2013-06-01 11:22:33",
447
+ "user_id": 2
448
+ }, {
449
+ "item": "banana",
450
+ "purchased_on": "2013-07-01",
451
+ "quantity": 12,
452
+ "timestamp": "2013-07-01 11:22:33",
453
+ "user_id": 2
454
+ }
455
+ ]
456
+ }
457
+ EOS
458
+ expectation = JSON.parse(json).to_json
459
+ expect(actual).to eq(expectation)
460
+ end
461
+ end
462
+
463
+ context 'Arguments are including ActiveModel::Base' do
464
+ let(:first_argument) {
465
+ TestOrderModel.new(user_id: 2,
466
+ item: 'orange',
467
+ quantity: 6,
468
+ purchased_on: Date.new(2013, 6, 1),
469
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
470
+ }
471
+ let(:second_argument) {
472
+ TestOrderModel.new(user_id: 2,
473
+ item: 'banana',
474
+ quantity: 12,
475
+ purchased_on: Date.new(2013, 7, 1),
476
+ timestamp: Time.local(2013, 7, 1, 11, 22, 33))
477
+ }
478
+ let(:third_argument) { TestUserModel.new(name: 'John') }
479
+
480
+ it 'puts a JSON string represents all arguments' do
481
+ actual = capture(:stdout) do
482
+ described_class.send_to(first_argument,
483
+ second_argument,
484
+ third_argument)
485
+ end.chomp
486
+ json = <<-EOS
487
+ {
488
+ "test_order_models": [{
489
+ "item": "orange",
490
+ "purchased_on": "2013-06-01",
491
+ "quantity": 6,
492
+ "timestamp": "2013-06-01 11:22:33",
493
+ "user_id": 2
494
+ },
495
+ {
496
+ "item": "banana",
497
+ "purchased_on": "2013-07-01",
498
+ "quantity": 12,
499
+ "timestamp": "2013-07-01 11:22:33",
500
+ "user_id": 2
501
+ }
502
+ ],
503
+ "test_user_models": {
504
+ "birthday": null,
505
+ "name": "John",
506
+ "timestamp": null
507
+ }
508
+ }
509
+ EOS
510
+ expectation = JSON.parse(json).to_json
511
+ expect(actual).to eq(expectation)
512
+ end
513
+ end
514
+ end
515
+ end
516
+
517
+ if defined? ActiveRecord
518
+ describe 'ActiveRecord::Base#send_to_flydata' do
519
+ let(:order) do
520
+ TestOrder.create!(id: 1,
521
+ user_id: 2,
522
+ item: 'orange',
523
+ quantity: 6,
524
+ purchased_on: Date.new(2013, 6, 1),
525
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
526
+ end
527
+
528
+ it 'puts a JSON string represents a model' do
529
+ actual = capture(:stdout) { order.send_to_flydata }.chomp
530
+ json = <<-EOS
531
+ {
532
+ "test_orders": {
533
+ "id": 1,
534
+ "item": "orange",
535
+ "purchased_on": "2013-06-01",
536
+ "quantity": 6,
537
+ "timestamp": "2013-06-01 11:22:33",
538
+ "user_id": 2
539
+ }
540
+ }
541
+ EOS
542
+ expectation = JSON.parse(json).to_json
543
+ expect(actual).to eq(expectation)
544
+ end
545
+ end
546
+
547
+ describe 'ActiveRecord::Base.flydata_table' do
548
+ let(:order) do
549
+ ConfiguredTestOrder.create!(id: 1,
550
+ user_id: 2,
551
+ item: 'orange',
552
+ quantity: 6,
553
+ purchased_on: Date.new(2013, 6, 1),
554
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
555
+ end
556
+
557
+ it 'puts a JSON string represents a model to configured table name' do
558
+ output = capture(:stdout) { order.send_to_flydata }
559
+ actual = JSON.parse(output)
560
+ expect(actual.keys).to include('other_table')
561
+ expect(actual.keys).not_to include('test_orders')
562
+ end
563
+ end
564
+
565
+ describe 'ActiveRecord::Base.flydata_attr' do
566
+ let(:order) do
567
+ ConfiguredTestOrder.create!(id: 1,
568
+ user_id: 2,
569
+ item: 'orange',
570
+ quantity: 6,
571
+ purchased_on: Date.new(2013, 6, 1),
572
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
573
+ end
574
+
575
+ it 'puts a JSON string represents a model to configured attributes' do
576
+ actual = capture(:stdout) { order.send_to_flydata }.chomp
577
+ json = <<-EOS
578
+ {
579
+ "other_table": {
580
+ "id": 1,
581
+ "item": "orange",
582
+ "purchased_on": "2013-06-01",
583
+ "timestamp": "2013-06-01 11:22:33"
584
+ }
585
+ }
586
+ EOS
587
+ expectation = JSON.parse(json).to_json
588
+ expect(actual).to eq(expectation)
589
+ end
590
+
591
+ context 'without attributes' do
592
+ it 'raise exception' do
593
+ expect {
594
+ class InvalidConfiguredTestOrder < TestOrder
595
+ flydata_attr
596
+ end
597
+ }.to raise_error(RuntimeError, 'flydata_attr attributes cannot be blank')
598
+ end
599
+ end
600
+ end
601
+ end
602
+
603
+ if defined? ActiveModel
604
+ describe 'ActiveModel::Serialization#send_to_flydata' do
605
+ let(:order) do
606
+ TestOrderModel.new(user_id: 2,
607
+ item: 'orange',
608
+ quantity: 6,
609
+ purchased_on: Date.new(2013, 6, 1),
610
+ timestamp: Time.local(2013, 6, 1, 11, 22, 33))
611
+ end
612
+
613
+ it 'puts a JSON string represents a model' do
614
+ actual = capture(:stdout) { order.send_to_flydata }.chomp
615
+ json = <<-EOS
616
+ {
617
+ "test_orders": {
618
+ "item": "orange",
619
+ "purchased_on": "2013-06-01",
620
+ "quantity": 6,
621
+ "timestamp": "2013-06-01 11:22:33",
622
+ "user_id": 2
623
+ }
624
+ }
625
+ EOS
626
+ expectation = JSON.parse(json).to_json
627
+ end
628
+ end
629
+
630
+ describe 'ActiveModel::Serialization.flydata_table' do
631
+ let(:order) {
632
+ ConfiguredTestOrderModel.new(user_id: 2, item: 'orange', quantity: 6)
633
+ }
634
+
635
+ it 'puts a JSON string represents a model to configured table name' do
636
+ output = capture(:stdout) { order.send_to_flydata }
637
+ actual = JSON.parse(output)
638
+ expect(actual.keys).to include('other_table')
639
+ expect(actual.keys).not_to include('test_orders')
640
+ end
641
+ end
642
+
643
+ describe 'ActiveModel::Serialization.flydata_attr' do
644
+ let(:order) { ConfiguredTestOrderModel.new(user_id: 2, item: 'orange', quantity: 6) }
645
+
646
+ it 'puts a JSON string represents a model to configured attributes' do
647
+ output = capture(:stdout) { order.send_to_flydata }
648
+ actual = JSON.parse(output)
649
+ expect(actual['other_table'].keys).to include('item')
650
+ expect(actual['other_table'].keys).to include('quantity')
651
+ expect(actual['other_table'].keys).not_to include('purchased_on')
652
+ expect(actual['other_table'].keys).not_to include('timestamp')
653
+ expect(actual['other_table'].keys).not_to include('user_id')
654
+ end
655
+
656
+ context 'without attributes' do
657
+ it 'raise exception' do
658
+ expect {
659
+ class InvalidConfiguredTestOrderModel < TestOrderModel
660
+ flydata_attr
661
+ end
662
+ }.to raise_error(RuntimeError, 'flydata_attr attributes cannot be blank')
663
+ end
664
+ end
665
+ end
666
+ end
667
+
668
+ end