embulk-input-google_spreadsheets 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +12 -0
  4. data/CHANGELOG.md +67 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +94 -0
  8. data/Rakefile +13 -0
  9. data/embulk-input-google_spreadsheets.gemspec +24 -0
  10. data/example/config_authorized_user.yml +19 -0
  11. data/example/config_authorized_user.yml.liquid +25 -0
  12. data/example/config_authorized_user_emoji_worksheet.yml +19 -0
  13. data/example/config_authorized_user_empty_rows_appears_at_the_same_as_max_fetch_rows.yml +20 -0
  14. data/example/config_authorized_user_large_data.yml +19 -0
  15. data/example/config_authorized_user_no_data.yml +18 -0
  16. data/example/config_service_account.yml +19 -0
  17. data/example/config_service_account_emoji_worksheet.yml +19 -0
  18. data/example/config_service_account_empty_rows_appears_at_the_same_as_max_fetch_rows.yml +20 -0
  19. data/example/config_service_account_large_data.yml +19 -0
  20. data/example/config_service_account_no_data.yml +18 -0
  21. data/example/setup_authorized_user_credentials.rb +34 -0
  22. data/lib/embulk/input/google_spreadsheets.rb +182 -0
  23. data/lib/embulk/input/google_spreadsheets/auth.rb +63 -0
  24. data/lib/embulk/input/google_spreadsheets/error.rb +36 -0
  25. data/lib/embulk/input/google_spreadsheets/pager.rb +107 -0
  26. data/lib/embulk/input/google_spreadsheets/pager_util.rb +28 -0
  27. data/lib/embulk/input/google_spreadsheets/record_typecaster.rb +73 -0
  28. data/lib/embulk/input/google_spreadsheets/spreadsheets_client.rb +75 -0
  29. data/lib/embulk/input/google_spreadsheets/spreadsheets_url_util.rb +23 -0
  30. data/lib/embulk/input/google_spreadsheets/typecast/base.rb +62 -0
  31. data/lib/embulk/input/google_spreadsheets/typecast/loose_typecast.rb +84 -0
  32. data/lib/embulk/input/google_spreadsheets/typecast/minimal_typecast.rb +109 -0
  33. data/lib/embulk/input/google_spreadsheets/typecast/strict_typecast.rb +236 -0
  34. data/lib/embulk/input/google_spreadsheets/typecast/timestamp_format_util.rb +29 -0
  35. data/lib/embulk/input/google_spreadsheets/typecast_factory.rb +34 -0
  36. data/test/assert_embulk_nothing_raised.rb +11 -0
  37. data/test/assert_embulk_raise.rb +11 -0
  38. data/test/dummy.key +27 -0
  39. data/test/helper.rb +21 -0
  40. data/test/test_auth.rb +82 -0
  41. data/test/test_configure.rb +155 -0
  42. data/test/test_loose_typecast.rb +194 -0
  43. data/test/test_minimal_typecast.rb +616 -0
  44. data/test/test_pager_util.rb +24 -0
  45. data/test/test_run_examples.rb +125 -0
  46. data/test/test_spreadsheets_client.rb +87 -0
  47. data/test/test_spreadsheets_url_util.rb +23 -0
  48. data/test/test_strict_typecast.rb +666 -0
  49. data/test/test_typecast_factory.rb +36 -0
  50. metadata +220 -0
@@ -0,0 +1,24 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets/pager_util'
3
+
4
+ module Embulk
5
+ class Input::GoogleSpreadsheets < InputPlugin
6
+ class TestPagerUtil < Test::Unit::TestCase
7
+
8
+ sub_test_case 'PagerUtil.num2col' do
9
+ test 'positive value' do
10
+ assert_equal('A', PagerUtil.num2col(1))
11
+ assert_equal('Z', PagerUtil.num2col(26))
12
+ assert_equal('AA', PagerUtil.num2col(27))
13
+ end
14
+
15
+ test '0 or negative value' do
16
+ assert_equal('', PagerUtil.num2col(0))
17
+ assert_equal('', PagerUtil.num2col(-1))
18
+ assert_equal('', PagerUtil.num2col(-26))
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,125 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets'
3
+
4
+ module Embulk
5
+ class Input::GoogleSpreadsheets < InputPlugin
6
+ class TestRunExample < Test::Unit::TestCase
7
+ def embulk_path
8
+ if File.exist?("#{ENV['PATH']}/.embulk/bin/embulk")
9
+ "#{ENV['PATH']}/.embulk/bin/embulk"
10
+ elsif File.exist?('/usr/local/bin/embulk')
11
+ '/usr/local/bin/embulk'
12
+ else
13
+ 'embulk'
14
+ end
15
+ end
16
+
17
+ def embulk_run(config_path)
18
+ Bundler.with_clean_env do
19
+ cmd = "#{embulk_path} run -X page_size=1 -b . -l trace #{config_path}"
20
+ puts '=' * 64
21
+ puts cmd
22
+ system(cmd)
23
+ end
24
+ end
25
+
26
+ sub_test_case 'run' do
27
+ unless File.exist?(JSON_KEYFILE_AUTHORIZED_USER)
28
+ puts "'#{JSON_KEYFILE_AUTHORIZED_USER}' is not found. Skip case authorized_user"
29
+ else
30
+ test 'authorized_user' do
31
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_authorized_user.yml'))
32
+ end
33
+ end
34
+
35
+ unless File.exist?(JSON_KEYFILE_SERVICE_ACCOUNT)
36
+ puts "'#{JSON_KEYFILE_SERVICE_ACCOUNT}' is not found. Skip case service_account"
37
+ else
38
+ test 'service_account' do
39
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_service_account.yml'))
40
+ end
41
+ end
42
+ end
43
+
44
+ sub_test_case 'run emoji worksheet title' do
45
+ unless File.exist?(JSON_KEYFILE_AUTHORIZED_USER)
46
+ puts "'#{JSON_KEYFILE_AUTHORIZED_USER}' is not found. Skip case authorized_user"
47
+ else
48
+ test 'authorized_user' do
49
+ omit 'Skip until closing https://github.com/medjed/embulk-input-google_spreadsheets/issues/6'
50
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_authorized_user_emoji_worksheet.yml'))
51
+ end
52
+ end
53
+
54
+ unless File.exist?(JSON_KEYFILE_SERVICE_ACCOUNT)
55
+ puts "'#{JSON_KEYFILE_SERVICE_ACCOUNT}' is not found. Skip case service_account"
56
+ else
57
+ test 'service_account' do
58
+ omit 'Skip until closing https://github.com/medjed/embulk-input-google_spreadsheets/issues/6'
59
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_service_account_emoji_worksheet.yml'))
60
+ end
61
+ end
62
+ end
63
+
64
+ sub_test_case 'run no data case' do
65
+ unless File.exist?(JSON_KEYFILE_AUTHORIZED_USER)
66
+ puts "'#{JSON_KEYFILE_AUTHORIZED_USER}' is not found. Skip case authorized_user"
67
+ else
68
+ test 'authorized_user' do
69
+
70
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_authorized_user_no_data.yml'))
71
+ end
72
+ end
73
+
74
+ unless File.exist?(JSON_KEYFILE_SERVICE_ACCOUNT)
75
+ puts "'#{JSON_KEYFILE_SERVICE_ACCOUNT}' is not found. Skip case service_account"
76
+ else
77
+ test 'service_account' do
78
+
79
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_service_account_no_data.yml'))
80
+ end
81
+ end
82
+ end
83
+
84
+ sub_test_case 'run empty rows appears at the same as max fetch rows case' do
85
+ unless File.exist?(JSON_KEYFILE_AUTHORIZED_USER)
86
+ puts "'#{JSON_KEYFILE_AUTHORIZED_USER}' is not found. Skip case authorized_user"
87
+ else
88
+ test 'authorized_user' do
89
+
90
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_authorized_user_empty_rows_appears_at_the_same_as_max_fetch_rows.yml'))
91
+ end
92
+ end
93
+
94
+ unless File.exist?(JSON_KEYFILE_SERVICE_ACCOUNT)
95
+ puts "'#{JSON_KEYFILE_SERVICE_ACCOUNT}' is not found. Skip case service_account"
96
+ else
97
+ test 'service_account' do
98
+
99
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_service_account_empty_rows_appears_at_the_same_as_max_fetch_rows.yml'))
100
+ end
101
+ end
102
+ end
103
+
104
+ sub_test_case 'run large data case' do
105
+ unless File.exist?(JSON_KEYFILE_AUTHORIZED_USER)
106
+ puts "'#{JSON_KEYFILE_AUTHORIZED_USER}' is not found. Skip case authorized_user"
107
+ else
108
+ test 'authorized_user' do
109
+
110
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_authorized_user_large_data.yml'))
111
+ end
112
+ end
113
+
114
+ unless File.exist?(JSON_KEYFILE_SERVICE_ACCOUNT)
115
+ puts "'#{JSON_KEYFILE_SERVICE_ACCOUNT}' is not found. Skip case service_account"
116
+ else
117
+ test 'service_account' do
118
+
119
+ assert_true embulk_run(File.join(EXAMPLE_ROOT, 'config_service_account_large_data.yml'))
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,87 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets'
3
+ require_relative 'assert_embulk_nothing_raised'
4
+
5
+ if !File.exist?(JSON_KEYFILE_AUTHORIZED_USER) and !File.exist?(JSON_KEYFILE_SERVICE_ACCOUNT)
6
+ puts "Neither '#{JSON_KEYFILE_AUTHORIZED_USER}' nor '#{JSON_KEYFILE_SERVICE_ACCOUNT}' is not found. Skip test/test_spreadsheets_client.rb"
7
+ else
8
+ module Embulk
9
+ class Input::GoogleSpreadsheets < InputPlugin
10
+ class TestSpreadsheetsClient < Test::Unit::TestCase
11
+
12
+ include AssertEmbulkNothingRaised
13
+
14
+ def auth_config
15
+ if File.exist?(JSON_KEYFILE_AUTHORIZED_USER)
16
+ {
17
+ 'auth_method' => 'authorized_user',
18
+ 'json_keyfile' => File.read(JSON_KEYFILE_AUTHORIZED_USER)
19
+ }
20
+ elsif File.exist?(JSON_KEYFILE_SERVICE_ACCOUNT)
21
+ {
22
+ 'auth_method' => 'service_account',
23
+ 'json_keyfile' => File.read(JSON_KEYFILE_SERVICE_ACCOUNT)
24
+ }
25
+ else
26
+ raise "Neither '#{JSON_KEYFILE_AUTHORIZED_USER}' nor '#{JSON_KEYFILE_SERVICE_ACCOUNT}' is not found."
27
+ end
28
+ end
29
+
30
+ def least_task
31
+ {
32
+ 'spreadsheets_url' => TEST_SPREADSHEETS_URL,
33
+ 'worksheet_title' => TEST_WORKSHEET_TITLE,
34
+ 'start_column' => 1,
35
+ 'start_row' => 2,
36
+ 'end_row' => -1,
37
+ 'end_column' => 6,
38
+ 'max_fetch_rows' => 10000,
39
+ 'columns' => columns,
40
+ }.merge(auth_config)
41
+ end
42
+
43
+ def columns
44
+ [
45
+ {'name' => '_c1', 'type' => 'boolean', 'typecast' => 'strict'},
46
+ {'name' => '_c2', 'type' => 'string', 'typecast' => 'strict'},
47
+ {'name' => '_c3', 'type' => 'long', 'typecast' => 'strict'},
48
+ {'name' => '_c4', 'type' => 'double', 'typecast' => 'strict'},
49
+ {'name' => '_c5', 'type' => 'timestamp', 'format' => '%Y-%m-%d %H:%M:%S.%N', 'timezone' => 'Asia/Tokyo', 'typecast' => 'strict'},
50
+ {'name' => '_c6', 'type' => 'timestamp', 'format' => '%Y-%m-%d', 'timezone' => 'Asia/Tokyo', 'typecast' => 'strict'}
51
+ ]
52
+ end
53
+
54
+ def client(task = {})
55
+ task = least_task.merge(task)
56
+ SpreadsheetsClient.new(task, auth: Auth.new(task), pager: Pager.new(task))
57
+ end
58
+
59
+ test 'parse spreadsheets id' do
60
+ expect = TEST_SPREADSHEETS_ID
61
+ result = client.spreadsheets_id
62
+ assert_equal(expect, result)
63
+ end
64
+
65
+ test 'get spreadsheet' do
66
+ assert_embulk_nothing_raised do
67
+ client.spreadsheets
68
+ end
69
+ end
70
+
71
+ test 'worksheet values' do
72
+ expect = [%w(_c1 _c2 _c3)]
73
+ result = client.worksheet_values('A1:C1')
74
+ assert_equal(expect, result)
75
+ end
76
+
77
+ test 'multi byte worksheet title' do
78
+ omit 'Skip until closing this issue https://github.com/medjed/embulk-input-google_spreadsheets/issues/6'
79
+
80
+ assert_embulk_nothing_raised do
81
+ client('worksheet_title' => TEST_WORKSHEET_TITLE_MULTI_BYTE).worksheet_values('A1:C1')
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets/spreadsheets_url_util'
3
+
4
+ module Embulk
5
+ class Input::GoogleSpreadsheets < InputPlugin
6
+ class TestSpreadsheetsUrlUtil < Test::Unit::TestCase
7
+
8
+ sub_test_case 'SpreadsheetsUrlUtil.capture_id' do
9
+ test 'correct url' do
10
+ assert_equal(TEST_SPREADSHEETS_ID, SpreadsheetsUrlUtil.capture_id(TEST_SPREADSHEETS_URL))
11
+ assert_equal('a', SpreadsheetsUrlUtil.capture_id('https://docs.google.com/spreadsheets/d/a/'))
12
+ end
13
+
14
+ test 'incorrect url' do
15
+ assert_equal(nil, SpreadsheetsUrlUtil.capture_id(''))
16
+ assert_equal(nil, SpreadsheetsUrlUtil.capture_id('https://docs.google.com/spreadsheets/d/'))
17
+ assert_equal(nil, SpreadsheetsUrlUtil.capture_id('https://docs.google.com/spreadsheets/d//'))
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,666 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets'
3
+ require 'embulk/input/google_spreadsheets/typecast/strict_typecast'
4
+ require_relative 'assert_embulk_raise'
5
+
6
+ module Embulk
7
+ class Input::GoogleSpreadsheets < InputPlugin
8
+ class TestStrictTypecast < Test::Unit::TestCase
9
+
10
+ include AssertEmbulkRaise
11
+
12
+ def least_task
13
+ {
14
+ 'null_string' => ''
15
+ }
16
+ end
17
+
18
+ sub_test_case 'as_string' do
19
+ sub_test_case 'return String' do
20
+ test 'value is String' do
21
+ expect = 'hoge'
22
+ result = Typecast::StrictTypecast.new(least_task).as_string('hoge')
23
+ assert_equal(expect, result)
24
+ end
25
+
26
+ test 'value is true' do
27
+ expect = 'true'
28
+ result = Typecast::StrictTypecast.new(least_task).as_string(true)
29
+ assert_equal(expect, result)
30
+ end
31
+
32
+ test 'value is false' do
33
+ expect = 'false'
34
+ result = Typecast::StrictTypecast.new(least_task).as_string(false)
35
+ assert_equal(expect, result)
36
+ end
37
+
38
+ test 'value is Fixnum' do
39
+ expect = '10'
40
+ result = Typecast::StrictTypecast.new(least_task).as_string(10)
41
+ assert_equal(expect, result)
42
+ end
43
+
44
+ test 'value is Bignum' do
45
+ expect = '10000000000000000000'
46
+ result = Typecast::StrictTypecast.new(least_task).as_string(10000000000000000000)
47
+ assert_equal(expect, result)
48
+ end
49
+
50
+ test 'value is Float' do
51
+ expect = '5.5'
52
+ result = Typecast::StrictTypecast.new(least_task).as_string(5.5)
53
+ assert_equal(expect, result)
54
+ end
55
+
56
+ test 'value is Hash' do
57
+ expect = "{\"test\":1}"
58
+ result = Typecast::StrictTypecast.new(least_task).as_string({'test' => 1})
59
+ assert_equal(expect, result)
60
+ end
61
+
62
+ test 'value is Array' do
63
+ expect = '[1,2,3]'
64
+ result = Typecast::StrictTypecast.new(least_task).as_string([1, 2, 3])
65
+ assert_equal(expect, result)
66
+ end
67
+ end
68
+
69
+ sub_test_case 'return nil' do
70
+ test 'value is nil' do
71
+ expect = nil
72
+ result = Typecast::StrictTypecast.new(least_task).as_string(nil)
73
+ assert_equal(expect, result)
74
+ end
75
+
76
+ test 'value is empty String' do
77
+ expect = nil
78
+ result = Typecast::StrictTypecast.new(least_task).as_string('')
79
+ assert_equal(expect, result)
80
+ end
81
+
82
+ test 'value and null_string is `\N`' do
83
+ expect = nil
84
+ result = Typecast::StrictTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
85
+ assert_equal(expect, result)
86
+ end
87
+ end
88
+
89
+ sub_test_case 'call TypecastError' do
90
+ test 'value is unknown class' do
91
+ assert_embulk_raise TypecastError do
92
+ Typecast::StrictTypecast.new(least_task).as_string(self)
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ sub_test_case 'as_long' do
99
+ sub_test_case 'return Fixnum or Bignum' do
100
+ test 'value is Fixnum' do
101
+ expect = 7
102
+ result = Typecast::StrictTypecast.new(least_task).as_long(7)
103
+ assert_equal(expect, result)
104
+ end
105
+
106
+ test 'value is Bignum' do
107
+ expect = 10000000000000000000
108
+ result = Typecast::StrictTypecast.new(least_task).as_long(10000000000000000000)
109
+ assert_equal(expect, result)
110
+ end
111
+
112
+ test 'value is String' do
113
+ expect = 7
114
+ result = Typecast::StrictTypecast.new(least_task).as_long('7')
115
+ assert_equal(expect, result)
116
+ end
117
+ end
118
+
119
+ sub_test_case 'return nil' do
120
+ test 'value is nil' do
121
+ expect = nil
122
+ result = Typecast::StrictTypecast.new(least_task).as_long(nil)
123
+ assert_equal(expect, result)
124
+ end
125
+
126
+ test 'value is empty String' do
127
+ expect = nil
128
+ result = Typecast::StrictTypecast.new(least_task).as_string('')
129
+ assert_equal(expect, result)
130
+ end
131
+
132
+ test 'value and null_string is `\N`' do
133
+ expect = nil
134
+ result = Typecast::StrictTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
135
+ assert_equal(expect, result)
136
+ end
137
+ end
138
+
139
+ sub_test_case 'call TypecastError' do
140
+ test 'value is TrueClass' do
141
+ assert_embulk_raise TypecastError do
142
+ Typecast::StrictTypecast.new(least_task).as_long(true)
143
+ end
144
+ end
145
+
146
+ test 'value is FalseClass' do
147
+ assert_embulk_raise TypecastError do
148
+ Typecast::StrictTypecast.new(least_task).as_long(false)
149
+ end
150
+ end
151
+
152
+ test 'value is Float: the first decimal place < 5' do
153
+ assert_embulk_raise TypecastError do
154
+ Typecast::StrictTypecast.new(least_task).as_long(1.1)
155
+ end
156
+ end
157
+
158
+ test 'value is Float: the first decimal place >= 5' do
159
+ assert_embulk_raise TypecastError do
160
+ Typecast::StrictTypecast.new(least_task).as_long(1.9)
161
+ end
162
+ end
163
+
164
+ test 'value is String unable to typecast' do
165
+ assert_embulk_raise TypecastError do
166
+ Typecast::StrictTypecast.new(least_task).as_long('111:222:333')
167
+ end
168
+ end
169
+
170
+ test 'value is Hash' do
171
+ assert_embulk_raise TypecastError do
172
+ Typecast::StrictTypecast.new(least_task).as_long({'test' => 1})
173
+ end
174
+ end
175
+
176
+ test 'value is Array' do
177
+ assert_embulk_raise TypecastError do
178
+ Typecast::StrictTypecast.new(least_task).as_long([1, 2, 3])
179
+ end
180
+ end
181
+ end
182
+ end
183
+
184
+ sub_test_case 'as_double' do
185
+ sub_test_case 'return Float' do
186
+ test 'value is Float' do
187
+ expect = 7.7
188
+ result = Typecast::StrictTypecast.new(least_task).as_double(7.7)
189
+ assert_equal(expect, result)
190
+ end
191
+
192
+ test 'value is String' do
193
+ expect = 7.7
194
+ result = Typecast::StrictTypecast.new(least_task).as_double('7.7')
195
+ assert_equal(expect, result)
196
+ end
197
+
198
+ test 'value is Fixnum' do
199
+ expect = 7.0
200
+ result = Typecast::StrictTypecast.new(least_task).as_double(7)
201
+ assert_equal(expect, result)
202
+ end
203
+
204
+ test 'value is Bignum' do
205
+ expect = 10000000000000000000.0
206
+ result = Typecast::StrictTypecast.new(least_task).as_double(10000000000000000000)
207
+ assert_equal(expect, result)
208
+ end
209
+ end
210
+
211
+ sub_test_case 'return nil' do
212
+ test 'value is nil' do
213
+ expect = nil
214
+ result = Typecast::StrictTypecast.new(least_task).as_double(nil)
215
+ assert_equal(expect, result)
216
+ end
217
+
218
+ test 'value is empty String' do
219
+ expect = nil
220
+ result = Typecast::StrictTypecast.new(least_task).as_string('')
221
+ assert_equal(expect, result)
222
+ end
223
+
224
+ test 'value and null_string is `\N`' do
225
+ expect = nil
226
+ result = Typecast::StrictTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
227
+ assert_equal(expect, result)
228
+ end
229
+ end
230
+
231
+ sub_test_case 'call TypecastError' do
232
+ test 'value is TrueClass' do
233
+ assert_embulk_raise TypecastError do
234
+ Typecast::StrictTypecast.new(least_task).as_double(true)
235
+ end
236
+ end
237
+
238
+ test 'value is FalseClass' do
239
+ assert_embulk_raise TypecastError do
240
+ Typecast::StrictTypecast.new(least_task).as_double(false)
241
+ end
242
+ end
243
+
244
+ test 'value is String unable to typecast' do
245
+ assert_embulk_raise TypecastError do
246
+ Typecast::StrictTypecast.new(least_task).as_double('44:55:3.6')
247
+ end
248
+ end
249
+
250
+ test 'value is Hash' do
251
+ assert_embulk_raise TypecastError do
252
+ Typecast::StrictTypecast.new(least_task).as_double({'test' => 1})
253
+ end
254
+ end
255
+
256
+ test 'value is Array' do
257
+ assert_embulk_raise TypecastError do
258
+ Typecast::StrictTypecast.new(least_task).as_double([1, 2, 3])
259
+ end
260
+ end
261
+ end
262
+ end
263
+
264
+ sub_test_case 'as_boolean' do
265
+ sub_test_case 'return false' do
266
+ test 'value is FalseClass' do
267
+ expect = false
268
+ result = Typecast::StrictTypecast.new(least_task).as_boolean(false)
269
+ assert_equal(expect, result)
270
+ end
271
+
272
+ test "value is String 'false'" do
273
+ expect = false
274
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('false')
275
+ assert_equal(expect, result)
276
+ end
277
+
278
+ test "value is String 'f'" do
279
+ expect = false
280
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('f')
281
+ assert_equal(expect, result)
282
+ end
283
+
284
+ test "value is String 'no'" do
285
+ expect = false
286
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('no')
287
+ assert_equal(expect, result)
288
+ end
289
+
290
+ test "value is String 'n'" do
291
+ expect = false
292
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('n')
293
+ assert_equal(expect, result)
294
+ end
295
+
296
+ test "value is String 'FALSE'" do
297
+ expect = false
298
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('FALSE')
299
+ assert_equal(expect, result)
300
+ end
301
+
302
+ test "value is String 'F'" do
303
+ expect = false
304
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('F')
305
+ assert_equal(expect, result)
306
+ end
307
+
308
+ test "value is String 'NO'" do
309
+ expect = false
310
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('NO')
311
+ assert_equal(expect, result)
312
+ end
313
+
314
+ test "value is String 'N'" do
315
+ expect = false
316
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('N')
317
+ assert_equal(expect, result)
318
+ end
319
+
320
+ test "value is String 'FaLsE'" do
321
+ expect = false
322
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('FaLsE')
323
+ assert_equal(expect, result)
324
+ end
325
+
326
+ test "value is String '0'" do
327
+ expect = false
328
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('0')
329
+ assert_equal(expect, result)
330
+ end
331
+
332
+ test 'value is 0' do
333
+ expect = false
334
+ result = Typecast::StrictTypecast.new(least_task).as_boolean(0)
335
+ assert_equal(expect, result)
336
+ end
337
+ end
338
+
339
+ sub_test_case 'return true' do
340
+ test 'value is TrueClass' do
341
+ expect = true
342
+ result = Typecast::StrictTypecast.new(least_task).as_boolean(true)
343
+ assert_equal(expect, result)
344
+ end
345
+
346
+ test "value is String 'true'" do
347
+ expect = true
348
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('true')
349
+ assert_equal(expect, result)
350
+ end
351
+
352
+ test "value is String 't'" do
353
+ expect = true
354
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('t')
355
+ assert_equal(expect, result)
356
+ end
357
+
358
+ test "value is String 'yes'" do
359
+ expect = true
360
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('yes')
361
+ assert_equal(expect, result)
362
+ end
363
+
364
+ test "value is String 'y'" do
365
+ expect = true
366
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('y')
367
+ assert_equal(expect, result)
368
+ end
369
+
370
+ test "value is String 'TRUE'" do
371
+ expect = true
372
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('TRUE')
373
+ assert_equal(expect, result)
374
+ end
375
+
376
+ test "value is String 'T'" do
377
+ expect = true
378
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('T')
379
+ assert_equal(expect, result)
380
+ end
381
+
382
+ test "value is String 'YES'" do
383
+ expect = true
384
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('YES')
385
+ assert_equal(expect, result)
386
+ end
387
+
388
+ test "value is String 'Y'" do
389
+ expect = true
390
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('Y')
391
+ assert_equal(expect, result)
392
+ end
393
+
394
+ test "value is String 'tRuE'" do
395
+ expect = true
396
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('tRuE')
397
+ assert_equal(expect, result)
398
+ end
399
+
400
+ test "value is String '1'" do
401
+ expect = true
402
+ result = Typecast::StrictTypecast.new(least_task).as_boolean('1')
403
+ assert_equal(expect, result)
404
+ end
405
+
406
+ test 'value is 1' do
407
+ expect = true
408
+ result = Typecast::StrictTypecast.new(least_task).as_boolean(1)
409
+ assert_equal(expect, result)
410
+ end
411
+ end
412
+
413
+ sub_test_case 'return nil' do
414
+ test 'value is nil' do
415
+ expect = nil
416
+ result = Typecast::StrictTypecast.new(least_task).as_boolean(nil)
417
+ assert_equal(expect, result)
418
+ end
419
+
420
+ test 'value is empty String' do
421
+ expect = nil
422
+ result = Typecast::StrictTypecast.new(least_task).as_string('')
423
+ assert_equal(expect, result)
424
+ end
425
+
426
+ test 'value and null_string is `\N`' do
427
+ expect = nil
428
+ result = Typecast::StrictTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
429
+ assert_equal(expect, result)
430
+ end
431
+ end
432
+
433
+ sub_test_case 'call TypecastError' do
434
+ test 'value is String unable to typecast' do
435
+ assert_embulk_raise TypecastError do
436
+ Typecast::StrictTypecast.new(least_task).as_boolean('test')
437
+ end
438
+ end
439
+
440
+ test 'value is Hash' do
441
+ assert_embulk_raise TypecastError do
442
+ Typecast::StrictTypecast.new(least_task).as_boolean({'test' => 1})
443
+ end
444
+ end
445
+
446
+ test 'value is Array' do
447
+ assert_embulk_raise TypecastError do
448
+ Typecast::StrictTypecast.new(least_task).as_boolean([1, 2, 3])
449
+ end
450
+ end
451
+ end
452
+ end
453
+
454
+ sub_test_case 'as_timestamp' do
455
+ sub_test_case 'return Time' do
456
+ sub_test_case 'no timestamp_format, no timezone' do
457
+ test 'value is String with timezone' do
458
+ expect = Time.parse('2015-12-15 09:31:09 UTC')
459
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09 UTC')
460
+ assert_equal(expect, result)
461
+ end
462
+
463
+ test 'value is String without timezone' do
464
+ expect = Time.parse('2015-12-15 09:31:09 JST')
465
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09')
466
+ assert_equal(expect, result)
467
+ end
468
+ end
469
+
470
+ sub_test_case 'has timestamp_format, no timezone' do
471
+ test 'value is JST' do
472
+ expect = Time.parse('2001-02-03 06:05:04 +0900')
473
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2001-02-03T04:05:06+09:00', '%Y-%m-%dT%S:%M:%H%z')
474
+ assert_equal(expect, result)
475
+ end
476
+
477
+ test 'value is UTC' do
478
+ expect = Time.parse('2001-02-03 06:05:04 UTC')
479
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2001-02-03T04:05:06UTC', '%Y-%m-%dT%S:%M:%H%Z')
480
+ assert_equal(expect, result)
481
+ end
482
+ end
483
+
484
+ sub_test_case 'no timestamp_format, has timezone' do
485
+ test 'value is String without timezone' do
486
+ expect = Time.parse('2015-12-15 09:31:09 JST')
487
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09', nil, 'Asia/Tokyo')
488
+ assert_equal(expect, result)
489
+ end
490
+
491
+ test 'value is String with timezone' do
492
+ expect = Time.parse('2015-12-15 09:31:09 JST')
493
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09 UTC', nil, 'Asia/Tokyo')
494
+ assert_equal(expect, result)
495
+ end
496
+ end
497
+
498
+ sub_test_case 'has timestamp_format, has timezone' do
499
+ test 'value is without timezone' do
500
+ expect = Time.parse('2001-02-03 06:05:04 JST')
501
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2001-02-03T04:05:06', '%Y-%m-%dT%S:%M:%H', 'Asia/Tokyo')
502
+ assert_equal(expect, result)
503
+ end
504
+
505
+ test 'value is with timezone' do
506
+ expect = Time.parse('2001-02-03 06:05:04 UTC')
507
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp('2001/02/03T06:05:04+00:00', '%Y/%m/%dT%H:%M:%S%z', 'Asia/Tokyo')
508
+ assert_equal(expect, result)
509
+ end
510
+ end
511
+ end
512
+
513
+ sub_test_case 'return nil' do
514
+ test 'value is nil' do
515
+ expect = nil
516
+ result = Typecast::StrictTypecast.new(least_task).as_timestamp(nil)
517
+ assert_equal(expect, result)
518
+ end
519
+
520
+ test 'value is empty String' do
521
+ expect = nil
522
+ result = Typecast::StrictTypecast.new(least_task).as_string('')
523
+ assert_equal(expect, result)
524
+ end
525
+
526
+ test 'value and null_string is `\N`' do
527
+ expect = nil
528
+ result = Typecast::StrictTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
529
+ assert_equal(expect, result)
530
+ end
531
+ end
532
+
533
+ sub_test_case 'call TypecastError' do
534
+ test 'invalid timezone format' do
535
+ assert_embulk_raise TypecastError do
536
+ Typecast::StrictTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09 UTC', nil, 'JST')
537
+ end
538
+ end
539
+
540
+ test 'value is TrueClass' do
541
+ assert_embulk_raise TypecastError do
542
+ Typecast::StrictTypecast.new(least_task).as_timestamp(true)
543
+ end
544
+ end
545
+
546
+ test 'value is FalseClass' do
547
+ assert_embulk_raise TypecastError do
548
+ Typecast::StrictTypecast.new(least_task).as_timestamp(false)
549
+ end
550
+ end
551
+
552
+ test 'value is String unable to typecast' do
553
+ assert_embulk_raise TypecastError do
554
+ Typecast::StrictTypecast.new(least_task).as_timestamp('test')
555
+ end
556
+ end
557
+
558
+ test 'value is Hash' do
559
+ assert_embulk_raise TypecastError do
560
+ Typecast::StrictTypecast.new(least_task).as_timestamp({'test' => 1})
561
+ end
562
+ end
563
+
564
+ test 'value is Array' do
565
+ assert_embulk_raise TypecastError do
566
+ Typecast::StrictTypecast.new(least_task).as_timestamp([1, 2, 3])
567
+ end
568
+ end
569
+ end
570
+ end
571
+
572
+ sub_test_case 'as_json' do
573
+ sub_test_case 'return String' do
574
+ test 'value is String' do
575
+ expect = {'hoge' => 'fuga'}
576
+ result = Typecast::StrictTypecast.new(least_task).as_json('{"hoge":"fuga"}')
577
+ assert_equal(expect, result)
578
+ end
579
+
580
+ test 'value is Hash' do
581
+ expect = {'test' => 1}
582
+ result = Typecast::StrictTypecast.new(least_task).as_json({'test' => 1})
583
+ assert_equal(expect, result)
584
+ end
585
+
586
+ test 'value is Array' do
587
+ expect = [1,2,3]
588
+ result = Typecast::StrictTypecast.new(least_task).as_json([1, 2, 3])
589
+ assert_equal(expect, result)
590
+ end
591
+ end
592
+
593
+ sub_test_case 'return nil' do
594
+ test 'value is nil' do
595
+ expect = nil
596
+ result = Typecast::StrictTypecast.new(least_task).as_json(nil)
597
+ assert_equal(expect, result)
598
+ end
599
+
600
+ test 'value is empty String' do
601
+ expect = nil
602
+ result = Typecast::StrictTypecast.new(least_task).as_string('')
603
+ assert_equal(expect, result)
604
+ end
605
+
606
+ test 'value and null_string is `\N`' do
607
+ expect = nil
608
+ result = Typecast::StrictTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
609
+ assert_equal(expect, result)
610
+ end
611
+ end
612
+
613
+ sub_test_case 'call TypecastError' do
614
+ test 'value is not JSON String' do
615
+ assert_embulk_raise TypecastError do
616
+ Typecast::StrictTypecast.new(least_task).as_json('hoge')
617
+ end
618
+ end
619
+
620
+ test 'value is true' do
621
+ assert_embulk_raise TypecastError do
622
+ Typecast::StrictTypecast.new(least_task).as_json(true)
623
+ end
624
+ end
625
+
626
+ test 'value is false' do
627
+ assert_embulk_raise TypecastError do
628
+ Typecast::StrictTypecast.new(least_task).as_json(false)
629
+ end
630
+ end
631
+
632
+ test 'value is Fixnum' do
633
+ assert_embulk_raise TypecastError do
634
+ Typecast::StrictTypecast.new(least_task).as_json(10)
635
+ end
636
+ end
637
+
638
+ test 'value is Bignum' do
639
+ assert_embulk_raise TypecastError do
640
+ Typecast::StrictTypecast.new(least_task).as_json(10000000000000000000)
641
+ end
642
+ end
643
+
644
+ test 'value is Float' do
645
+ assert_embulk_raise TypecastError do
646
+ Typecast::StrictTypecast.new(least_task).as_json(5.5)
647
+ end
648
+ end
649
+
650
+ test 'value is Time class' do
651
+ assert_embulk_raise TypecastError do
652
+ Typecast::StrictTypecast.new(least_task).as_json(Time.at(1475572099))
653
+ end
654
+ end
655
+
656
+ test 'value is unknown class' do
657
+ assert_embulk_raise TypecastError do
658
+ Typecast::StrictTypecast.new(least_task).as_json(self)
659
+ end
660
+ end
661
+ end
662
+ end
663
+
664
+ end
665
+ end
666
+ end