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,155 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets'
3
+ require_relative 'assert_embulk_raise'
4
+
5
+ module Embulk
6
+ class Input::GoogleSpreadsheets < InputPlugin
7
+
8
+ class TestConfigure < Test::Unit::TestCase
9
+
10
+ setup do
11
+ # TODO: to keep class instance variables' default value always
12
+ @_default_format = CustomColumns.default_format
13
+ @_default_timezone = CustomColumns.default_timezone
14
+ @_default_typecast = CustomColumns.default_typecast
15
+ end
16
+
17
+ teardown do
18
+ CustomColumns.default_format = @_default_format
19
+ CustomColumns.default_timezone = @_default_timezone
20
+ CustomColumns.default_typecast = @_default_typecast
21
+ end
22
+
23
+ include AssertEmbulkRaise
24
+
25
+ GoogleSpreadsheets = Embulk::Input::GoogleSpreadsheets unless defined?(GoogleSpreadsheets)
26
+
27
+ def least_config
28
+ DataSource.new({
29
+ 'spreadsheets_url' => TEST_SPREADSHEETS_URL,
30
+ 'worksheet_title' => TEST_WORKSHEET_TITLE,
31
+ 'columns' => columns,
32
+ })
33
+ end
34
+
35
+ def columns
36
+ [
37
+ {'name' => '_c1', 'type' => 'boolean'},
38
+ {'name' => '_c2', 'type' => 'string'},
39
+ {'name' => '_c3', 'type' => 'long'},
40
+ {'name' => '_c4', 'type' => 'double'},
41
+ {'name' => '_c5', 'type' => 'timestamp'},
42
+ ]
43
+ end
44
+
45
+ def config(config = {})
46
+ least_config.merge(config)
47
+ end
48
+
49
+ sub_test_case 'configure by buildin API' do
50
+ test 'default' do
51
+ task = GoogleSpreadsheets.configure(config)
52
+ assert_equal(1, task['start_column'])
53
+ assert_equal(1, task['start_row'])
54
+ assert_equal(-1, task['end_row'])
55
+ assert_equal(10000, task['max_fetch_rows'])
56
+ assert_equal('', task['null_string'])
57
+ assert_equal(true, task['stop_on_invalid_record'])
58
+ assert_equal('%Y-%m-%d %H:%M:%S.%N %z', task['default_timestamp_format'])
59
+ assert_equal('UTC', task['default_timezone'])
60
+ assert_equal('strict', task['default_typecast'])
61
+ end
62
+ end
63
+
64
+ sub_test_case 'end_column' do
65
+ test 'end_column' do
66
+ task = GoogleSpreadsheets.configure(config('start_column' => 5, 'columns' => [{},{},{}]))
67
+ assert_equal(7, task['end_column'])
68
+ end
69
+ end
70
+
71
+ sub_test_case 'LocalFile' do
72
+ test 'not set json_keyfile' do
73
+ task = GoogleSpreadsheets.configure(config)
74
+ assert_equal(nil, task['json_keyfile'])
75
+ end
76
+
77
+ test 'set file path' do
78
+ dummy = File.join(APP_ROOT, 'Gemfile')
79
+ task = GoogleSpreadsheets.configure(config('json_keyfile' => dummy))
80
+ assert_equal(File.read(dummy), task['json_keyfile'])
81
+ end
82
+
83
+ test 'set content' do
84
+ dummy = 'hoge'
85
+ task = GoogleSpreadsheets.configure(config('json_keyfile' => {'content' => dummy}))
86
+ assert_equal(dummy, task['json_keyfile'])
87
+ end
88
+ end
89
+
90
+ sub_test_case 'CustomColumns' do
91
+ test 'not array' do
92
+ assert_embulk_raise Embulk::ConfigError do
93
+ GoogleSpreadsheets.configure(config('columns' => 'hoge'))
94
+ end
95
+ end
96
+
97
+ test 'not array of hash' do
98
+ assert_embulk_raise Embulk::ConfigError do
99
+ GoogleSpreadsheets.configure(config('columns' => ['hoge']))
100
+ end
101
+ end
102
+
103
+ test 'complete default' do
104
+ task = GoogleSpreadsheets.configure(config('columns' => [{'name' => '_c5', 'type' => 'timestamp'}]))
105
+ assert_equal('%Y-%m-%d %H:%M:%S.%N %z', task['columns'].first['format'])
106
+ assert_equal('UTC', task['columns'].first['timezone'])
107
+ assert_equal('strict', task['columns'].first['typecast'])
108
+ end
109
+
110
+ test 'complete default by user defined default' do
111
+ task = GoogleSpreadsheets.configure(config(
112
+ 'default_timestamp_format' => '%N',
113
+ 'default_timezone' => 'Asia/Tokyo',
114
+ 'default_typecast' => 'loose',
115
+ 'columns' => [{'name' => '_c5', 'type' => 'timestamp'}]
116
+ ))
117
+ assert_equal('%N', task['columns'].first['format'])
118
+ assert_equal('Asia/Tokyo', task['columns'].first['timezone'])
119
+ assert_equal('loose', task['columns'].first['typecast'])
120
+ end
121
+
122
+ test 'not complete except timestamp' do
123
+ task = GoogleSpreadsheets.configure(config(
124
+ 'columns' => [
125
+ {'name' => '_c1', 'type' => 'boolean'},
126
+ {'name' => '_c2', 'type' => 'string'},
127
+ {'name' => '_c3', 'type' => 'long'},
128
+ {'name' => '_c4', 'type' => 'double'},
129
+ {'name' => '_c5', 'type' => 'json'},
130
+ ]))
131
+ 5.times do |i|
132
+ assert_equal(nil, task['columns'][i]['format'], i)
133
+ assert_equal(nil, task['columns'][i]['timezone'], i)
134
+ end
135
+ end
136
+
137
+ test 'complete typecast if any type' do
138
+ task = GoogleSpreadsheets.configure(config(
139
+ 'columns' => [
140
+ {'name' => '_c1', 'type' => 'boolean'},
141
+ {'name' => '_c2', 'type' => 'string'},
142
+ {'name' => '_c3', 'type' => 'long'},
143
+ {'name' => '_c4', 'type' => 'double'},
144
+ {'name' => '_c5', 'type' => 'json'},
145
+ ]))
146
+ 5.times do |i|
147
+ assert_equal('strict', task['columns'][i]['typecast'], i)
148
+ end
149
+ end
150
+ end
151
+
152
+ end
153
+
154
+ end
155
+ end
@@ -0,0 +1,194 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets'
3
+ require 'embulk/input/google_spreadsheets/typecast/loose_typecast'
4
+
5
+ module Embulk
6
+ class Input::GoogleSpreadsheets < InputPlugin
7
+ class TestStrictTypecast < Test::Unit::TestCase
8
+ def least_task
9
+ {
10
+ 'null_string' => ''
11
+ }
12
+ end
13
+
14
+ sub_test_case 'as_string' do
15
+ test 'value is unknown class' do
16
+ expect = nil
17
+ result = Typecast::LooseTypecast.new(least_task).as_string(self)
18
+ assert_equal(expect, result)
19
+ end
20
+ end
21
+
22
+ sub_test_case 'as_long' do
23
+ test 'value is TrueClass' do
24
+ expect = nil
25
+ result = Typecast::LooseTypecast.new(least_task).as_long(true)
26
+ assert_equal(expect, result)
27
+ end
28
+
29
+ test 'value is FalseClass' do
30
+ expect = nil
31
+ result = Typecast::LooseTypecast.new(least_task).as_long(false)
32
+ assert_equal(expect, result)
33
+ end
34
+
35
+ test 'value is Float: the first decimal place < 5' do
36
+ expect = nil
37
+ result = Typecast::LooseTypecast.new(least_task).as_long(1.1)
38
+ assert_equal(expect, result)
39
+ end
40
+
41
+ test 'value is Float: the first decimal place >= 5' do
42
+ expect = nil
43
+ result = Typecast::LooseTypecast.new(least_task).as_long(1.9)
44
+ assert_equal(expect, result)
45
+ end
46
+
47
+ test 'value is unable to typecast to String' do
48
+ expect = nil
49
+ result = Typecast::LooseTypecast.new(least_task).as_long('111:222:333')
50
+ assert_equal(expect, result)
51
+ end
52
+
53
+ test 'value is Hash' do
54
+ expect = nil
55
+ result = Typecast::LooseTypecast.new(least_task).as_long({'test' => 1})
56
+ assert_equal(expect, result)
57
+ end
58
+ test 'value is Array' do
59
+ expect = nil
60
+ result = Typecast::LooseTypecast.new(least_task).as_long([1, 2, 3])
61
+ assert_equal(expect, result)
62
+ end
63
+ end
64
+
65
+ sub_test_case 'as_double' do
66
+ test 'value is TrueClass' do
67
+ expect = nil
68
+ result = Typecast::LooseTypecast.new(least_task).as_double(true)
69
+ assert_equal(expect, result)
70
+ end
71
+
72
+ test 'value is FalseClass' do
73
+ expect = nil
74
+ result = Typecast::LooseTypecast.new(least_task).as_double(false)
75
+ assert_equal(expect, result)
76
+ end
77
+
78
+ test 'value is unable to typecast to String' do
79
+ expect = nil
80
+ result = Typecast::LooseTypecast.new(least_task).as_double('44:55:3.6')
81
+ assert_equal(expect, result)
82
+ end
83
+
84
+ test 'value is Hash' do
85
+ expect = nil
86
+ result = Typecast::LooseTypecast.new(least_task).as_double({'test' => 1})
87
+ assert_equal(expect, result)
88
+ end
89
+
90
+ test 'value is Array' do
91
+ expect = nil
92
+ result = Typecast::LooseTypecast.new(least_task).as_double([1, 2, 3])
93
+ assert_equal(expect, result)
94
+ end
95
+ end
96
+
97
+ sub_test_case 'as_boolean' do
98
+ test 'value is unable to typecast to String' do
99
+ expect = nil
100
+ result = Typecast::LooseTypecast.new(least_task).as_boolean('test')
101
+ assert_equal(expect, result)
102
+ end
103
+
104
+ test 'value is Hash' do
105
+ expect = nil
106
+ result = Typecast::LooseTypecast.new(least_task).as_boolean({'test' => 1})
107
+ assert_equal(expect, result)
108
+ end
109
+
110
+ test 'value is Array' do
111
+ expect = nil
112
+ result = Typecast::LooseTypecast.new(least_task).as_boolean([1, 2, 3])
113
+ assert_equal(expect, result)
114
+ end
115
+ end
116
+
117
+ sub_test_case 'as_timestamp' do
118
+ test 'value is TrueClass' do
119
+ expect = nil
120
+ result = Typecast::LooseTypecast.new(least_task).as_timestamp(true)
121
+ assert_equal(expect, result)
122
+ end
123
+
124
+ test 'value is FalseClass' do
125
+ expect = nil
126
+ result = Typecast::LooseTypecast.new(least_task).as_timestamp(false)
127
+ assert_equal(expect, result)
128
+ end
129
+
130
+ test 'value is unable to typecast to String' do
131
+ expect = nil
132
+ result = Typecast::LooseTypecast.new(least_task).as_timestamp('test')
133
+ assert_equal(expect, result)
134
+ end
135
+
136
+ test 'value is Hash' do
137
+ expect = nil
138
+ result = Typecast::LooseTypecast.new(least_task).as_timestamp({'test' => 1})
139
+ assert_equal(expect, result)
140
+ end
141
+
142
+ test 'value is Array' do
143
+ expect = nil
144
+ result = Typecast::LooseTypecast.new(least_task).as_timestamp([1, 2, 3])
145
+ assert_equal(expect, result)
146
+ end
147
+ end
148
+
149
+ sub_test_case 'as_json' do
150
+ test 'value is true' do
151
+ expect = nil
152
+ result = Typecast::LooseTypecast.new(least_task).as_json(true)
153
+ assert_equal(expect, result)
154
+ end
155
+
156
+ test 'value is false' do
157
+ expect = nil
158
+ result = Typecast::LooseTypecast.new(least_task).as_json(false)
159
+ assert_equal(expect, result)
160
+ end
161
+
162
+ test 'value is Fixnum' do
163
+ expect = nil
164
+ result = Typecast::LooseTypecast.new(least_task).as_json(10)
165
+ assert_equal(expect, result)
166
+ end
167
+
168
+ test 'value is Bignum' do
169
+ expect = nil
170
+ result = Typecast::LooseTypecast.new(least_task).as_json(10000000000000000000)
171
+ assert_equal(expect, result)
172
+ end
173
+
174
+ test 'value is Float' do
175
+ expect = nil
176
+ result = Typecast::LooseTypecast.new(least_task).as_json(5.5)
177
+ assert_equal(expect, result)
178
+ end
179
+
180
+ test 'value is Time class' do
181
+ expect = nil
182
+ result = Typecast::LooseTypecast.new(least_task).as_json(Time.at(1475572099))
183
+ assert_equal(expect, result)
184
+ end
185
+
186
+ test 'value is unknown class' do
187
+ expect = nil
188
+ result = Typecast::LooseTypecast.new(least_task).as_json(self)
189
+ assert_equal(expect, result)
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,616 @@
1
+ require_relative 'helper'
2
+ require 'embulk/input/google_spreadsheets'
3
+ require 'embulk/input/google_spreadsheets/typecast/minimal_typecast'
4
+ require_relative 'assert_embulk_raise'
5
+
6
+ module Embulk
7
+ class Input::GoogleSpreadsheets < InputPlugin
8
+ class TestMinimalTypecast < 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::MinimalTypecast.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::MinimalTypecast.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::MinimalTypecast.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::MinimalTypecast.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::MinimalTypecast.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::MinimalTypecast.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}.to_s
58
+ result = Typecast::MinimalTypecast.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].to_s
64
+ result = Typecast::MinimalTypecast.new(least_task).as_string([1, 2, 3])
65
+ assert_equal(expect, result)
66
+ end
67
+
68
+ test 'value is an unknown instance' do
69
+ expect = self.to_s
70
+ result = Typecast::MinimalTypecast.new(least_task).as_string(self)
71
+ assert_equal(expect, result)
72
+ end
73
+ end
74
+
75
+ sub_test_case 'return nil' do
76
+ test 'value is nil' do
77
+ expect = nil
78
+ result = Typecast::MinimalTypecast.new(least_task).as_string(nil)
79
+ assert_equal(expect, result)
80
+ end
81
+
82
+ test 'value is empty String' do
83
+ expect = nil
84
+ result = Typecast::MinimalTypecast.new(least_task).as_string('')
85
+ assert_equal(expect, result)
86
+ end
87
+
88
+ test 'value and null_string is `\N`' do
89
+ expect = nil
90
+ result = Typecast::MinimalTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
91
+ assert_equal(expect, result)
92
+ end
93
+ end
94
+ end
95
+
96
+ sub_test_case 'as_long' do
97
+ sub_test_case 'return Fixnum or Bignum' do
98
+ test 'value is Fixnum' do
99
+ expect = 7
100
+ result = Typecast::MinimalTypecast.new(least_task).as_long(7)
101
+ assert_equal(expect, result)
102
+ end
103
+
104
+ test 'value is Bignum' do
105
+ expect = 10000000000000000000
106
+ result = Typecast::MinimalTypecast.new(least_task).as_long(10000000000000000000)
107
+ assert_equal(expect, result)
108
+ end
109
+
110
+ test 'value is String' do
111
+ expect = 7
112
+ result = Typecast::MinimalTypecast.new(least_task).as_long('7')
113
+ assert_equal(expect, result)
114
+ end
115
+
116
+ test 'value is String which has numbers' do
117
+ expect = 111
118
+ result = Typecast::MinimalTypecast.new(least_task).as_long('111:222:333')
119
+ assert_equal(expect, result)
120
+ end
121
+
122
+ test 'value is Float: the first decimal place < 5' do
123
+ expect = 1
124
+ result = Typecast::MinimalTypecast.new(least_task).as_long(1.1)
125
+ assert_equal(expect, result)
126
+ end
127
+
128
+ test 'value is Float: the first decimal place >= 5' do
129
+ expect = 1
130
+ result = Typecast::MinimalTypecast.new(least_task).as_long(1.9)
131
+ assert_equal(expect, result)
132
+ end
133
+ end
134
+
135
+ sub_test_case 'return nil' do
136
+ test 'value is nil' do
137
+ expect = nil
138
+ result = Typecast::MinimalTypecast.new(least_task).as_long(nil)
139
+ assert_equal(expect, result)
140
+ end
141
+
142
+ test 'value is empty String' do
143
+ expect = nil
144
+ result = Typecast::MinimalTypecast.new(least_task).as_string('')
145
+ assert_equal(expect, result)
146
+ end
147
+
148
+ test 'value and null_string is `\N`' do
149
+ expect = nil
150
+ result = Typecast::MinimalTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
151
+ assert_equal(expect, result)
152
+ end
153
+ end
154
+
155
+ sub_test_case 'call TypecastError' do
156
+ test 'value is TrueClass' do
157
+ assert_embulk_raise TypecastError do
158
+ Typecast::MinimalTypecast.new(least_task).as_long(true)
159
+ end
160
+ end
161
+
162
+ test 'value is FalseClass' do
163
+ assert_embulk_raise TypecastError do
164
+ Typecast::MinimalTypecast.new(least_task).as_long(false)
165
+ end
166
+ end
167
+
168
+ test 'value is Hash' do
169
+ assert_embulk_raise TypecastError do
170
+ Typecast::MinimalTypecast.new(least_task).as_long({'test' => 1})
171
+ end
172
+ end
173
+
174
+ test 'value is Array' do
175
+ assert_embulk_raise TypecastError do
176
+ Typecast::MinimalTypecast.new(least_task).as_long([1, 2, 3])
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ sub_test_case 'as_double' do
183
+ sub_test_case 'return Float' do
184
+ test 'value is Float' do
185
+ expect = 7.7
186
+ result = Typecast::MinimalTypecast.new(least_task).as_double(7.7)
187
+ assert_equal(expect, result)
188
+ end
189
+
190
+ test 'value is String' do
191
+ expect = 7.7
192
+ result = Typecast::MinimalTypecast.new(least_task).as_double('7.7')
193
+ assert_equal(expect, result)
194
+ end
195
+
196
+ test 'value is Fixnum' do
197
+ expect = 7.0
198
+ result = Typecast::MinimalTypecast.new(least_task).as_double(7)
199
+ assert_equal(expect, result)
200
+ end
201
+
202
+ test 'value is Bignum' do
203
+ expect = 10000000000000000000.0
204
+ result = Typecast::MinimalTypecast.new(least_task).as_double(10000000000000000000)
205
+ assert_equal(expect, result)
206
+ end
207
+
208
+ test 'value is String which has numbers' do
209
+ expect = 44.0
210
+ result = Typecast::MinimalTypecast.new(least_task).as_double('44:55:3.6')
211
+ assert_equal(expect, result)
212
+ end
213
+ end
214
+
215
+ sub_test_case 'return nil' do
216
+ test 'value is nil' do
217
+ expect = nil
218
+ result = Typecast::MinimalTypecast.new(least_task).as_double(nil)
219
+ assert_equal(expect, result)
220
+ end
221
+
222
+ test 'value is empty String' do
223
+ expect = nil
224
+ result = Typecast::MinimalTypecast.new(least_task).as_string('')
225
+ assert_equal(expect, result)
226
+ end
227
+
228
+ test 'value and null_string is `\N`' do
229
+ expect = nil
230
+ result = Typecast::MinimalTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
231
+ assert_equal(expect, result)
232
+ end
233
+ end
234
+
235
+ sub_test_case 'call TypecastError' do
236
+ test 'value is TrueClass' do
237
+ assert_embulk_raise TypecastError do
238
+ Typecast::MinimalTypecast.new(least_task).as_double(true)
239
+ end
240
+ end
241
+
242
+ test 'value is FalseClass' do
243
+ assert_embulk_raise TypecastError do
244
+ Typecast::MinimalTypecast.new(least_task).as_double(false)
245
+ end
246
+ end
247
+
248
+ test 'value is Hash' do
249
+ assert_embulk_raise TypecastError do
250
+ Typecast::MinimalTypecast.new(least_task).as_double({'test' => 1})
251
+ end
252
+ end
253
+
254
+ test 'value is Array' do
255
+ assert_embulk_raise TypecastError do
256
+ Typecast::MinimalTypecast.new(least_task).as_double([1, 2, 3])
257
+ end
258
+ end
259
+ end
260
+ end
261
+
262
+ sub_test_case 'as_boolean' do
263
+ sub_test_case 'return false' do
264
+ test 'value is FalseClass' do
265
+ expect = false
266
+ result = Typecast::MinimalTypecast.new(least_task).as_boolean(false)
267
+ assert_equal(expect, result)
268
+ end
269
+
270
+ test "value is String 'false'" do
271
+ expect = false
272
+ result = Typecast::MinimalTypecast.new(least_task).as_boolean('false')
273
+ assert_equal(expect, result)
274
+ end
275
+
276
+ test "value is String 'FaLsE'" do
277
+ expect = false
278
+ result = Typecast::MinimalTypecast.new(least_task).as_boolean('FaLsE')
279
+ assert_equal(expect, result)
280
+ end
281
+ end
282
+
283
+ sub_test_case 'return true' do
284
+ test 'value is TrueClass' do
285
+ expect = true
286
+ result = Typecast::MinimalTypecast.new(least_task).as_boolean(true)
287
+ assert_equal(expect, result)
288
+ end
289
+
290
+ test "value is String 'true'" do
291
+ expect = true
292
+ result = Typecast::MinimalTypecast.new(least_task).as_boolean('true')
293
+ assert_equal(expect, result)
294
+ end
295
+
296
+ test "value is String 'TrUe'" do
297
+ expect = true
298
+ result = Typecast::MinimalTypecast.new(least_task).as_boolean('TrUe')
299
+ assert_equal(expect, result)
300
+ end
301
+ end
302
+
303
+ sub_test_case 'return nil' do
304
+ test 'value is nil' do
305
+ expect = nil
306
+ result = Typecast::MinimalTypecast.new(least_task).as_boolean(nil)
307
+ assert_equal(expect, result)
308
+ end
309
+
310
+ test 'value is empty String' do
311
+ expect = nil
312
+ result = Typecast::MinimalTypecast.new(least_task).as_string('')
313
+ assert_equal(expect, result)
314
+ end
315
+
316
+ test 'value and null_string is `\N`' do
317
+ expect = nil
318
+ result = Typecast::MinimalTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
319
+ assert_equal(expect, result)
320
+ end
321
+ end
322
+
323
+ sub_test_case 'call TypecastError' do
324
+ test 'value is unable to typecast to String' do
325
+ assert_embulk_raise TypecastError do
326
+ Typecast::MinimalTypecast.new(least_task).as_boolean('test')
327
+ end
328
+ end
329
+
330
+ test "value is String 'y'" do
331
+ assert_embulk_raise TypecastError do
332
+ Typecast::MinimalTypecast.new(least_task).as_boolean('y')
333
+ end
334
+ end
335
+
336
+ test "value is String 'n'" do
337
+ assert_embulk_raise TypecastError do
338
+ Typecast::MinimalTypecast.new(least_task).as_boolean('y')
339
+ end
340
+ end
341
+
342
+ test "value is String 't'" do
343
+ assert_embulk_raise TypecastError do
344
+ Typecast::MinimalTypecast.new(least_task).as_boolean('t')
345
+ end
346
+ end
347
+
348
+ test "value is String 'f'" do
349
+ assert_embulk_raise TypecastError do
350
+ Typecast::MinimalTypecast.new(least_task).as_boolean('f')
351
+ end
352
+ end
353
+
354
+ test 'value is Hash' do
355
+ assert_embulk_raise TypecastError do
356
+ Typecast::MinimalTypecast.new(least_task).as_boolean({'test' => 1})
357
+ end
358
+ end
359
+
360
+ test 'value is Array' do
361
+ assert_embulk_raise TypecastError do
362
+ Typecast::MinimalTypecast.new(least_task).as_boolean([1, 2, 3])
363
+ end
364
+ end
365
+
366
+ test "value is String 'no'" do
367
+ assert_embulk_raise TypecastError do
368
+ Typecast::MinimalTypecast.new(least_task).as_boolean('no')
369
+ end
370
+ end
371
+
372
+ test "value is String '0'" do
373
+ assert_embulk_raise TypecastError do
374
+ Typecast::MinimalTypecast.new(least_task).as_boolean('0')
375
+ end
376
+ end
377
+
378
+ test 'value is 0' do
379
+ assert_embulk_raise TypecastError do
380
+ Typecast::MinimalTypecast.new(least_task).as_boolean(0)
381
+ end
382
+ end
383
+
384
+ test "value is String 'yes'" do
385
+ assert_embulk_raise TypecastError do
386
+ Typecast::MinimalTypecast.new(least_task).as_boolean('yes')
387
+ end
388
+ end
389
+
390
+ test "value is String '1'" do
391
+ assert_embulk_raise TypecastError do
392
+ Typecast::MinimalTypecast.new(least_task).as_boolean('1')
393
+ end
394
+ end
395
+
396
+ test 'value is 1' do
397
+ assert_embulk_raise TypecastError do
398
+ Typecast::MinimalTypecast.new(least_task).as_boolean(1)
399
+ end
400
+ end
401
+ end
402
+ end
403
+
404
+ sub_test_case 'as_timestamp' do
405
+ sub_test_case 'return Time' do
406
+ sub_test_case 'no timestamp_format, no timezone' do
407
+ test 'value is String with timezone' do
408
+ expect = Time.parse('2015-12-15 09:31:09 UTC')
409
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09 UTC')
410
+ assert_equal(expect, result)
411
+ end
412
+
413
+ test 'value is String without timezone' do
414
+ expect = Time.parse('2015-12-15 09:31:09 JST')
415
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09')
416
+ assert_equal(expect, result)
417
+ end
418
+ end
419
+
420
+ sub_test_case 'has timestamp_format, no timezone' do
421
+ test 'value is JST' do
422
+ expect = Time.parse('2001-02-03 06:05:04 +0900')
423
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2001-02-03T04:05:06+09:00', '%Y-%m-%dT%S:%M:%H%z')
424
+ assert_equal(expect, result)
425
+ end
426
+
427
+ test 'value is UTC' do
428
+ expect = Time.parse('2001-02-03 06:05:04 UTC')
429
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2001-02-03T04:05:06UTC', '%Y-%m-%dT%S:%M:%H%Z')
430
+ assert_equal(expect, result)
431
+ end
432
+ end
433
+
434
+ sub_test_case 'no timestamp_format, has timezone' do
435
+ test 'value is String without timezone' do
436
+ expect = Time.parse('2015-12-15 09:31:09 JST')
437
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09', nil, 'Asia/Tokyo')
438
+ assert_equal(expect, result)
439
+ end
440
+
441
+ test 'value is String with timezone' do
442
+ expect = Time.parse('2015-12-15 09:31:09 JST')
443
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09 UTC', nil, 'Asia/Tokyo')
444
+ assert_equal(expect, result)
445
+ end
446
+ end
447
+
448
+ sub_test_case 'has timestamp_format, has timezone' do
449
+ test 'value is without timezone' do
450
+ expect = Time.parse('2001-02-03 06:05:04 JST')
451
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2001-02-03T04:05:06', '%Y-%m-%dT%S:%M:%H', 'Asia/Tokyo')
452
+ assert_equal(expect, result)
453
+ end
454
+
455
+ test 'value is with timezone' do
456
+ expect = Time.parse('2001-02-03 06:05:04 UTC')
457
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp('2001/02/03T06:05:04+00:00', '%Y/%m/%dT%H:%M:%S%z', 'Asia/Tokyo')
458
+ assert_equal(expect, result)
459
+ end
460
+ end
461
+ end
462
+
463
+ sub_test_case 'return nil' do
464
+ test 'value is nil' do
465
+ expect = nil
466
+ result = Typecast::MinimalTypecast.new(least_task).as_timestamp(nil)
467
+ assert_equal(expect, result)
468
+ end
469
+
470
+ test 'value is empty String' do
471
+ expect = nil
472
+ result = Typecast::MinimalTypecast.new(least_task).as_string('')
473
+ assert_equal(expect, result)
474
+ end
475
+
476
+ test 'value and null_string is `\N`' do
477
+ expect = nil
478
+ result = Typecast::MinimalTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
479
+ assert_equal(expect, result)
480
+ end
481
+ end
482
+
483
+ sub_test_case 'call TypecastError' do
484
+ test 'invalid timezone format' do
485
+ assert_embulk_raise TypecastError do
486
+ Typecast::MinimalTypecast.new(least_task).as_timestamp('2015-12-15 09:31:09 UTC', nil, 'JST')
487
+ end
488
+ end
489
+
490
+ test 'value is TrueClass' do
491
+ assert_embulk_raise TypecastError do
492
+ Typecast::MinimalTypecast.new(least_task).as_timestamp(true)
493
+ end
494
+ end
495
+
496
+ test 'value is FalseClass' do
497
+ assert_embulk_raise TypecastError do
498
+ Typecast::MinimalTypecast.new(least_task).as_timestamp(false)
499
+ end
500
+ end
501
+
502
+ test 'value is unable to typecast to String' do
503
+ assert_embulk_raise TypecastError do
504
+ Typecast::MinimalTypecast.new(least_task).as_timestamp('test')
505
+ end
506
+ end
507
+
508
+ test 'value is Hash' do
509
+ assert_embulk_raise TypecastError do
510
+ Typecast::MinimalTypecast.new(least_task).as_timestamp({'test' => 1})
511
+ end
512
+ end
513
+
514
+ test 'value is Array' do
515
+ assert_embulk_raise TypecastError do
516
+ Typecast::MinimalTypecast.new(least_task).as_timestamp([1, 2, 3])
517
+ end
518
+ end
519
+ end
520
+ end
521
+
522
+ sub_test_case 'as_json' do
523
+ sub_test_case 'return String' do
524
+ test 'value is String' do
525
+ expect = {'hoge' => 'fuga'}
526
+ result = Typecast::MinimalTypecast.new(least_task).as_json('{"hoge":"fuga"}')
527
+ assert_equal(expect, result)
528
+ end
529
+
530
+ test 'value is true' do
531
+ expect = true
532
+ result = Typecast::MinimalTypecast.new(least_task).as_json(true)
533
+ assert_equal(expect, result)
534
+ end
535
+
536
+ test 'value is false' do
537
+ expect = false
538
+ result = Typecast::MinimalTypecast.new(least_task).as_json(false)
539
+ assert_equal(expect, result)
540
+ end
541
+
542
+ test 'value is Fixnum' do
543
+ expect = 10
544
+ result = Typecast::MinimalTypecast.new(least_task).as_json(10)
545
+ assert_equal(expect, result)
546
+ end
547
+
548
+ test 'value is Bignum' do
549
+ expect = 10000000000000000000
550
+ result = Typecast::MinimalTypecast.new(least_task).as_json(10000000000000000000)
551
+ assert_equal(expect, result)
552
+ end
553
+
554
+ test 'value is Float' do
555
+ expect = 5.5
556
+ result = Typecast::MinimalTypecast.new(least_task).as_json(5.5)
557
+ assert_equal(expect, result)
558
+ end
559
+
560
+ test 'value is Hash' do
561
+ expect = {'test' => 1}
562
+ result = Typecast::MinimalTypecast.new(least_task).as_json({'test' => 1})
563
+ assert_equal(expect, result)
564
+ end
565
+
566
+ test 'value is Array' do
567
+ expect = [1,2,3]
568
+ result = Typecast::MinimalTypecast.new(least_task).as_json([1, 2, 3])
569
+ assert_equal(expect, result)
570
+ end
571
+ end
572
+
573
+ sub_test_case 'return nil' do
574
+ test 'value is nil' do
575
+ expect = nil
576
+ result = Typecast::MinimalTypecast.new(least_task).as_json(nil)
577
+ assert_equal(expect, result)
578
+ end
579
+
580
+ test 'value is empty String' do
581
+ expect = nil
582
+ result = Typecast::MinimalTypecast.new(least_task).as_string('')
583
+ assert_equal(expect, result)
584
+ end
585
+
586
+ test 'value and null_string is `\N`' do
587
+ expect = nil
588
+ result = Typecast::MinimalTypecast.new(least_task.tap{|t|t['null_string'] = '\N'}).as_string('\N')
589
+ assert_equal(expect, result)
590
+ end
591
+ end
592
+
593
+ sub_test_case 'call TypecastError' do
594
+ test 'value is not JSON String' do
595
+ assert_embulk_raise TypecastError do
596
+ Typecast::MinimalTypecast.new(least_task).as_json('hoge')
597
+ end
598
+ end
599
+
600
+ test 'value is Time class' do
601
+ assert_embulk_raise TypecastError do
602
+ Typecast::MinimalTypecast.new(least_task).as_json(Time.at(1475572099))
603
+ end
604
+ end
605
+
606
+ test 'value is unknown class' do
607
+ assert_embulk_raise TypecastError do
608
+ Typecast::MinimalTypecast.new(least_task).as_json(self)
609
+ end
610
+ end
611
+ end
612
+ end
613
+ end
614
+
615
+ end
616
+ end