masamune 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +54 -0
  4. data/Rakefile +15 -0
  5. data/bin/masamune-elastic-mapreduce +4 -0
  6. data/bin/masamune-hive +4 -0
  7. data/bin/masamune-psql +4 -0
  8. data/bin/masamune-shell +4 -0
  9. data/lib/masamune.rb +56 -0
  10. data/lib/masamune/accumulate.rb +60 -0
  11. data/lib/masamune/actions.rb +38 -0
  12. data/lib/masamune/actions/data_flow.rb +131 -0
  13. data/lib/masamune/actions/date_parse.rb +75 -0
  14. data/lib/masamune/actions/elastic_mapreduce.rb +68 -0
  15. data/lib/masamune/actions/execute.rb +52 -0
  16. data/lib/masamune/actions/filesystem.rb +37 -0
  17. data/lib/masamune/actions/hadoop_filesystem.rb +40 -0
  18. data/lib/masamune/actions/hadoop_streaming.rb +41 -0
  19. data/lib/masamune/actions/hive.rb +74 -0
  20. data/lib/masamune/actions/postgres.rb +76 -0
  21. data/lib/masamune/actions/postgres_admin.rb +34 -0
  22. data/lib/masamune/actions/s3cmd.rb +44 -0
  23. data/lib/masamune/actions/transform.rb +89 -0
  24. data/lib/masamune/after_initialize_callbacks.rb +55 -0
  25. data/lib/masamune/cached_filesystem.rb +110 -0
  26. data/lib/masamune/commands.rb +37 -0
  27. data/lib/masamune/commands/elastic_mapreduce.rb +119 -0
  28. data/lib/masamune/commands/hadoop_filesystem.rb +57 -0
  29. data/lib/masamune/commands/hadoop_streaming.rb +116 -0
  30. data/lib/masamune/commands/hive.rb +178 -0
  31. data/lib/masamune/commands/interactive.rb +37 -0
  32. data/lib/masamune/commands/postgres.rb +128 -0
  33. data/lib/masamune/commands/postgres_admin.rb +72 -0
  34. data/lib/masamune/commands/postgres_common.rb +33 -0
  35. data/lib/masamune/commands/retry_with_backoff.rb +60 -0
  36. data/lib/masamune/commands/s3cmd.rb +70 -0
  37. data/lib/masamune/commands/shell.rb +202 -0
  38. data/lib/masamune/configuration.rb +195 -0
  39. data/lib/masamune/data_plan.rb +31 -0
  40. data/lib/masamune/data_plan/builder.rb +66 -0
  41. data/lib/masamune/data_plan/elem.rb +190 -0
  42. data/lib/masamune/data_plan/engine.rb +162 -0
  43. data/lib/masamune/data_plan/rule.rb +292 -0
  44. data/lib/masamune/data_plan/set.rb +176 -0
  45. data/lib/masamune/environment.rb +164 -0
  46. data/lib/masamune/filesystem.rb +567 -0
  47. data/lib/masamune/has_environment.rb +40 -0
  48. data/lib/masamune/helpers.rb +27 -0
  49. data/lib/masamune/helpers/postgres.rb +84 -0
  50. data/lib/masamune/io.rb +33 -0
  51. data/lib/masamune/last_element.rb +53 -0
  52. data/lib/masamune/method_logger.rb +41 -0
  53. data/lib/masamune/multi_io.rb +39 -0
  54. data/lib/masamune/schema.rb +36 -0
  55. data/lib/masamune/schema/catalog.rb +233 -0
  56. data/lib/masamune/schema/column.rb +527 -0
  57. data/lib/masamune/schema/dimension.rb +133 -0
  58. data/lib/masamune/schema/event.rb +121 -0
  59. data/lib/masamune/schema/fact.rb +133 -0
  60. data/lib/masamune/schema/map.rb +265 -0
  61. data/lib/masamune/schema/row.rb +133 -0
  62. data/lib/masamune/schema/store.rb +115 -0
  63. data/lib/masamune/schema/table.rb +308 -0
  64. data/lib/masamune/schema/table_reference.rb +76 -0
  65. data/lib/masamune/spec_helper.rb +23 -0
  66. data/lib/masamune/string_format.rb +34 -0
  67. data/lib/masamune/tasks/elastic_mapreduce_thor.rb +60 -0
  68. data/lib/masamune/tasks/hive_thor.rb +55 -0
  69. data/lib/masamune/tasks/postgres_thor.rb +47 -0
  70. data/lib/masamune/tasks/shell_thor.rb +63 -0
  71. data/lib/masamune/template.rb +77 -0
  72. data/lib/masamune/thor.rb +186 -0
  73. data/lib/masamune/thor_loader.rb +38 -0
  74. data/lib/masamune/topological_hash.rb +34 -0
  75. data/lib/masamune/transform.rb +47 -0
  76. data/lib/masamune/transform/bulk_upsert.psql.erb +64 -0
  77. data/lib/masamune/transform/bulk_upsert.rb +52 -0
  78. data/lib/masamune/transform/consolidate_dimension.rb +54 -0
  79. data/lib/masamune/transform/deduplicate_dimension.psql.erb +52 -0
  80. data/lib/masamune/transform/deduplicate_dimension.rb +53 -0
  81. data/lib/masamune/transform/define_event_view.hql.erb +51 -0
  82. data/lib/masamune/transform/define_event_view.rb +60 -0
  83. data/lib/masamune/transform/define_index.psql.erb +34 -0
  84. data/lib/masamune/transform/define_schema.hql.erb +23 -0
  85. data/lib/masamune/transform/define_schema.psql.erb +79 -0
  86. data/lib/masamune/transform/define_schema.rb +56 -0
  87. data/lib/masamune/transform/define_table.hql.erb +34 -0
  88. data/lib/masamune/transform/define_table.psql.erb +95 -0
  89. data/lib/masamune/transform/define_table.rb +40 -0
  90. data/lib/masamune/transform/define_unique.psql.erb +30 -0
  91. data/lib/masamune/transform/insert_reference_values.psql.erb +43 -0
  92. data/lib/masamune/transform/insert_reference_values.rb +64 -0
  93. data/lib/masamune/transform/load_dimension.rb +47 -0
  94. data/lib/masamune/transform/load_fact.rb +45 -0
  95. data/lib/masamune/transform/operator.rb +96 -0
  96. data/lib/masamune/transform/relabel_dimension.psql.erb +76 -0
  97. data/lib/masamune/transform/relabel_dimension.rb +39 -0
  98. data/lib/masamune/transform/rollup_fact.psql.erb +79 -0
  99. data/lib/masamune/transform/rollup_fact.rb +149 -0
  100. data/lib/masamune/transform/snapshot_dimension.psql.erb +75 -0
  101. data/lib/masamune/transform/snapshot_dimension.rb +74 -0
  102. data/lib/masamune/transform/stage_dimension.psql.erb +39 -0
  103. data/lib/masamune/transform/stage_dimension.rb +83 -0
  104. data/lib/masamune/transform/stage_fact.psql.erb +80 -0
  105. data/lib/masamune/transform/stage_fact.rb +111 -0
  106. data/lib/masamune/version.rb +25 -0
  107. data/spec/fixtures/aggregate.sql.erb +25 -0
  108. data/spec/fixtures/comment.sql.erb +27 -0
  109. data/spec/fixtures/invalid.sql.erb +23 -0
  110. data/spec/fixtures/relative.sql.erb +23 -0
  111. data/spec/fixtures/simple.sql.erb +28 -0
  112. data/spec/fixtures/whitespace.sql.erb +30 -0
  113. data/spec/masamune/actions/elastic_mapreduce_spec.rb +108 -0
  114. data/spec/masamune/actions/execute_spec.rb +50 -0
  115. data/spec/masamune/actions/hadoop_filesystem_spec.rb +44 -0
  116. data/spec/masamune/actions/hadoop_streaming_spec.rb +74 -0
  117. data/spec/masamune/actions/hive_spec.rb +117 -0
  118. data/spec/masamune/actions/postgres_admin_spec.rb +58 -0
  119. data/spec/masamune/actions/postgres_spec.rb +134 -0
  120. data/spec/masamune/actions/s3cmd_spec.rb +44 -0
  121. data/spec/masamune/actions/transform_spec.rb +144 -0
  122. data/spec/masamune/after_initialization_callbacks_spec.rb +61 -0
  123. data/spec/masamune/cached_filesystem_spec.rb +167 -0
  124. data/spec/masamune/commands/hadoop_filesystem_spec.rb +50 -0
  125. data/spec/masamune/commands/hadoop_streaming_spec.rb +106 -0
  126. data/spec/masamune/commands/hive_spec.rb +117 -0
  127. data/spec/masamune/commands/postgres_admin_spec.rb +69 -0
  128. data/spec/masamune/commands/postgres_spec.rb +100 -0
  129. data/spec/masamune/commands/retry_with_backoff_spec.rb +116 -0
  130. data/spec/masamune/commands/s3cmd_spec.rb +50 -0
  131. data/spec/masamune/commands/shell_spec.rb +101 -0
  132. data/spec/masamune/configuration_spec.rb +102 -0
  133. data/spec/masamune/data_plan/builder_spec.rb +91 -0
  134. data/spec/masamune/data_plan/elem_spec.rb +102 -0
  135. data/spec/masamune/data_plan/engine_spec.rb +356 -0
  136. data/spec/masamune/data_plan/rule_spec.rb +407 -0
  137. data/spec/masamune/data_plan/set_spec.rb +517 -0
  138. data/spec/masamune/environment_spec.rb +65 -0
  139. data/spec/masamune/filesystem_spec.rb +1421 -0
  140. data/spec/masamune/helpers/postgres_spec.rb +95 -0
  141. data/spec/masamune/schema/catalog_spec.rb +613 -0
  142. data/spec/masamune/schema/column_spec.rb +696 -0
  143. data/spec/masamune/schema/dimension_spec.rb +137 -0
  144. data/spec/masamune/schema/event_spec.rb +75 -0
  145. data/spec/masamune/schema/fact_spec.rb +117 -0
  146. data/spec/masamune/schema/map_spec.rb +593 -0
  147. data/spec/masamune/schema/row_spec.rb +28 -0
  148. data/spec/masamune/schema/store_spec.rb +49 -0
  149. data/spec/masamune/schema/table_spec.rb +395 -0
  150. data/spec/masamune/string_format_spec.rb +60 -0
  151. data/spec/masamune/tasks/elastic_mapreduce_thor_spec.rb +57 -0
  152. data/spec/masamune/tasks/hive_thor_spec.rb +75 -0
  153. data/spec/masamune/tasks/postgres_thor_spec.rb +42 -0
  154. data/spec/masamune/tasks/shell_thor_spec.rb +51 -0
  155. data/spec/masamune/template_spec.rb +77 -0
  156. data/spec/masamune/thor_spec.rb +238 -0
  157. data/spec/masamune/transform/bulk_upsert.dimension_spec.rb +200 -0
  158. data/spec/masamune/transform/consolidate_dimension_spec.rb +62 -0
  159. data/spec/masamune/transform/deduplicate_dimension_spec.rb +84 -0
  160. data/spec/masamune/transform/define_event_view_spec.rb +84 -0
  161. data/spec/masamune/transform/define_schema_spec.rb +83 -0
  162. data/spec/masamune/transform/define_table.dimension_spec.rb +306 -0
  163. data/spec/masamune/transform/define_table.fact_spec.rb +291 -0
  164. data/spec/masamune/transform/define_table.table_spec.rb +525 -0
  165. data/spec/masamune/transform/insert_reference_values.dimension_spec.rb +111 -0
  166. data/spec/masamune/transform/insert_reference_values.fact_spec.rb +149 -0
  167. data/spec/masamune/transform/load_dimension_spec.rb +76 -0
  168. data/spec/masamune/transform/load_fact_spec.rb +89 -0
  169. data/spec/masamune/transform/relabel_dimension_spec.rb +102 -0
  170. data/spec/masamune/transform/rollup_fact_spec.rb +333 -0
  171. data/spec/masamune/transform/snapshot_dimension_spec.rb +103 -0
  172. data/spec/masamune/transform/stage_dimension_spec.rb +115 -0
  173. data/spec/masamune/transform/stage_fact_spec.rb +204 -0
  174. data/spec/masamune_spec.rb +32 -0
  175. data/spec/spec_helper.rb +41 -0
  176. data/spec/support/masamune/example_group.rb +36 -0
  177. data/spec/support/masamune/mock_command.rb +99 -0
  178. data/spec/support/masamune/mock_delegate.rb +51 -0
  179. data/spec/support/masamune/mock_filesystem.rb +96 -0
  180. data/spec/support/masamune/thor_mute.rb +35 -0
  181. data/spec/support/rspec/example/action_example_group.rb +34 -0
  182. data/spec/support/rspec/example/task_example_group.rb +80 -0
  183. data/spec/support/rspec/example/transform_example_group.rb +36 -0
  184. data/spec/support/shared_examples/postgres_common_examples.rb +53 -0
  185. metadata +462 -0
@@ -0,0 +1,696 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Schema::Column do
26
+ describe '.initialize' do
27
+ subject(:column) { described_class.new(id: 'id') }
28
+ it { expect(column).to_not be_nil }
29
+
30
+ context 'default' do
31
+ context '#unique' do
32
+ subject { column.unique }
33
+ it { is_expected.to be_empty }
34
+ end
35
+
36
+ context '#index' do
37
+ subject { column.index }
38
+ it { is_expected.to be_empty }
39
+ end
40
+ end
41
+
42
+ context 'without id' do
43
+ subject(:column) { described_class.new }
44
+ it { expect { column }.to raise_error ArgumentError }
45
+ end
46
+
47
+ context 'with default: true' do
48
+ subject(:column) { described_class.new(id: 'boolean', type: :boolean, default: true) }
49
+ context '#default' do
50
+ subject { column.default }
51
+ it { is_expected.to eq(true) }
52
+ end
53
+ end
54
+
55
+ context 'with default: false' do
56
+ subject(:column) { described_class.new(id: 'boolean', type: :boolean, default: false) }
57
+ context '#default' do
58
+ subject { column.default }
59
+ it { is_expected.to eq(false) }
60
+ end
61
+ end
62
+
63
+ context 'with type :enum' do
64
+ subject(:column) { described_class.new(id: 'mode', type: :enum, values: %w(public private)) }
65
+
66
+ context '#default' do
67
+ subject { column.default }
68
+ it { is_expected.to eq('public') }
69
+ end
70
+
71
+ context '#sql_type' do
72
+ subject { column.sql_type }
73
+ it { is_expected.to eq('MODE_TYPE') }
74
+ end
75
+
76
+ context 'with :sub_type' do
77
+ subject(:column) { described_class.new(id: 'permission', type: :enum, sub_type: 'mode') }
78
+
79
+ context '#sql_type' do
80
+ subject { column.sql_type }
81
+ it { is_expected.to eq('MODE_TYPE') }
82
+ end
83
+ end
84
+
85
+ context 'with :default' do
86
+ subject(:column) { described_class.new(id: 'mode', type: :enum, values: %w(public private), default: 'private') }
87
+
88
+ context '#default' do
89
+ subject { column.default }
90
+ it { is_expected.to eq('private') }
91
+ end
92
+ end
93
+ end
94
+
95
+ context 'with index: false' do
96
+ subject(:column) { described_class.new(id: 'id', index: false) }
97
+ context '#index' do
98
+ subject { column.index }
99
+ it { is_expected.to be_empty }
100
+ end
101
+ end
102
+
103
+ context 'with index: []' do
104
+ subject(:column) { described_class.new(id: 'id', index: []) }
105
+ context '#index' do
106
+ subject { column.index }
107
+ it { is_expected.to be_empty }
108
+ end
109
+ end
110
+
111
+ context 'with index: true' do
112
+ subject(:column) { described_class.new(id: 'id', index: true) }
113
+ context '#index' do
114
+ subject { column.index }
115
+ it { is_expected.to include(:id) }
116
+ end
117
+ end
118
+
119
+ context 'with index: "shared"' do
120
+ subject(:column) { described_class.new(id: 'id', index: 'shared') }
121
+ context '#index' do
122
+ subject { column.index }
123
+ it { is_expected.to include(:shared) }
124
+ end
125
+ end
126
+
127
+ context 'with index: ["id", "shared"]' do
128
+ subject(:column) { described_class.new(id: 'id', index: ['id', 'shared']) }
129
+ context '#index' do
130
+ subject { column.index }
131
+ it { is_expected.to include(:id) }
132
+ it { is_expected.to include(:shared) }
133
+ end
134
+ end
135
+
136
+ context 'with index: nil' do
137
+ subject(:column) { described_class.new(id: 'id', index: nil) }
138
+ it { expect { column }.to raise_error ArgumentError }
139
+ end
140
+
141
+ context 'with unknown index: type' do
142
+ subject(:column) { described_class.new(id: 'id', index: 1) }
143
+ it { expect { column }.to raise_error ArgumentError }
144
+ end
145
+ end
146
+
147
+ describe '#sql_type' do
148
+ subject(:result) { column.sql_type }
149
+
150
+ context 'with type :integer' do
151
+ let(:column) { described_class.new(id: 'integer', type: :integer) }
152
+ it { is_expected.to eq('INTEGER') }
153
+ end
154
+
155
+ context 'with type :integer and :array' do
156
+ let(:column) { described_class.new(id: 'integer', type: :integer, array: true) }
157
+ it { is_expected.to eq('INTEGER[]') }
158
+ end
159
+
160
+ context 'with type :string' do
161
+ let(:column) { described_class.new(id: 'string', type: :string) }
162
+ it { is_expected.to eq('VARCHAR') }
163
+ end
164
+
165
+ context 'with type :string and :array' do
166
+ let(:column) { described_class.new(id: 'string', type: :string, array: true) }
167
+ it { is_expected.to eq('VARCHAR[]') }
168
+ end
169
+ end
170
+
171
+ describe '#sql_value' do
172
+ subject(:result) { column.sql_value(value) }
173
+
174
+ shared_examples 'with :null value' do
175
+ context 'with :null value' do
176
+ let(:value) { :null }
177
+ it { is_expected.to eq('NULL') }
178
+ end
179
+ end
180
+
181
+ context 'with type :integer' do
182
+ let(:column) { described_class.new(id: 'integer', type: :integer) }
183
+ context 'with integer value' do
184
+ let(:value) { 0 }
185
+ it { is_expected.to eq(0) }
186
+ end
187
+ it_behaves_like 'with :null value'
188
+ end
189
+
190
+ context 'with type :string' do
191
+ let(:column) { described_class.new(id: 'string', type: :string) }
192
+ context 'with string value' do
193
+ let(:value) { 'value' }
194
+ it { is_expected.to eq(%q{'value'}) }
195
+ end
196
+ it_behaves_like 'with :null value'
197
+ end
198
+
199
+ context 'with type :boolean' do
200
+ let(:column) { described_class.new(id: 'boolean', type: :boolean) }
201
+ context 'with boolean value' do
202
+ let(:value) { true }
203
+ it { is_expected.to eq('TRUE') }
204
+ end
205
+
206
+ context 'with string value' do
207
+ let(:value) { 'true' }
208
+ it { is_expected.to eq('TRUE') }
209
+ end
210
+
211
+ it_behaves_like 'with :null value'
212
+ end
213
+
214
+ context 'with type :enum' do
215
+ let(:column) { described_class.new(id: 'enum', type: :enum, values: %w(public private)) }
216
+ context 'with enum value' do
217
+ let(:value) { 'public' }
218
+ it { is_expected.to eq(%q{'public'::ENUM_TYPE}) }
219
+ end
220
+
221
+ it_behaves_like 'with :null value'
222
+ end
223
+ end
224
+
225
+ describe '#hql_type' do
226
+ subject(:result) { column.hql_type }
227
+
228
+ context 'with type :integer' do
229
+ let(:column) { described_class.new(id: 'integer', type: :integer) }
230
+ it { is_expected.to eq('INT') }
231
+ end
232
+
233
+ context 'with type :integer and :array' do
234
+ let(:column) { described_class.new(id: 'integer', type: :integer, array: true) }
235
+ it { is_expected.to eq('ARRAY<INT>') }
236
+ end
237
+
238
+ context 'with type :string' do
239
+ let(:column) { described_class.new(id: 'string', type: :string) }
240
+ it { is_expected.to eq('STRING') }
241
+ end
242
+
243
+ context 'with type :string and :array' do
244
+ let(:column) { described_class.new(id: 'string', type: :string, array: true) }
245
+ it { is_expected.to eq('ARRAY<STRING>') }
246
+ end
247
+ end
248
+
249
+ describe '#ruby_value' do
250
+ subject(:result) { column.ruby_value(value) }
251
+
252
+ context 'with type :boolean' do
253
+ let(:column) { described_class.new(id: 'bool', type: :boolean) }
254
+
255
+ context 'when 1' do
256
+ let(:value) { 1 }
257
+ it { is_expected.to eq(true) }
258
+ end
259
+
260
+ context "when '1'" do
261
+ let(:value) { '1' }
262
+ it { is_expected.to eq(true) }
263
+ end
264
+
265
+ context "when ''1''" do
266
+ let(:value) { %Q{'1'} }
267
+ it { is_expected.to eq(true) }
268
+ end
269
+
270
+ context 'when TRUE' do
271
+ let(:value) { 'TRUE' }
272
+ it { is_expected.to eq(true) }
273
+ end
274
+
275
+ context 'when true' do
276
+ let(:value) { 'true' }
277
+ it { is_expected.to eq(true) }
278
+ end
279
+
280
+ context 'when 0' do
281
+ let(:value) { 0 }
282
+ it { is_expected.to eq(false) }
283
+ end
284
+
285
+ context "when '0'" do
286
+ let(:value) { '0' }
287
+ it { is_expected.to eq(false) }
288
+ end
289
+
290
+ context "when ''0''" do
291
+ let(:value) { %Q{'0'} }
292
+ it { is_expected.to eq(false) }
293
+ end
294
+
295
+ context 'when FALSE' do
296
+ let(:value) { 'FALSE' }
297
+ it { is_expected.to eq(false) }
298
+ end
299
+
300
+ context 'when false' do
301
+ let(:value) { 'false' }
302
+ it { is_expected.to eq(false) }
303
+ end
304
+
305
+ context 'when nil ' do
306
+ let(:value) { nil }
307
+ it { is_expected.to be(nil) }
308
+ end
309
+
310
+ context 'when "junk"' do
311
+ let(:value) { 'junk' }
312
+ it { is_expected.to be(nil) }
313
+ end
314
+ end
315
+
316
+ context 'with type :date' do
317
+ let(:column) { described_class.new(id: 'date', type: :date) }
318
+
319
+ context 'when nil' do
320
+ let(:value) { nil }
321
+ it { is_expected.to be(nil) }
322
+ end
323
+
324
+ context 'when unknown' do
325
+ let(:value) { 'unknown' }
326
+ it { expect { result }.to raise_error ArgumentError, "Could not coerce 'unknown' into :date for column 'date'" }
327
+ end
328
+
329
+ context 'when Date' do
330
+ let(:value) { Date.civil(2015,01,01) }
331
+ it { is_expected.to eq(value) }
332
+ end
333
+
334
+ context 'when YYYY-mm-dd' do
335
+ let(:value) { '2015-01-01' }
336
+ it { is_expected.to eq(Date.civil(2015,01,01)) }
337
+ end
338
+
339
+ context 'when ISO8601' do
340
+ let(:value) { Date.parse('2015-01-01').iso8601 }
341
+ it { is_expected.to eq(Date.civil(2015,01,01)) }
342
+ end
343
+ end
344
+
345
+ context 'with type :timestamp' do
346
+ let(:column) { described_class.new(id: 'timestamp', type: :timestamp) }
347
+
348
+ context 'when nil' do
349
+ let(:value) { nil }
350
+ it { is_expected.to be(nil) }
351
+ end
352
+
353
+ context 'when Date' do
354
+ let(:value) { Date.civil(2015,01,01) }
355
+ it { is_expected.to eq(value.to_time) }
356
+ end
357
+
358
+ context 'when DateTime' do
359
+ let(:value) { DateTime.civil(2015,01,01) }
360
+ it { is_expected.to eq(value.to_time) }
361
+ end
362
+
363
+ context 'when Time' do
364
+ let(:value) { Time.now }
365
+ it { is_expected.to eq(value) }
366
+ end
367
+
368
+ context 'when Integer' do
369
+ let(:value) { Time.now.utc.to_i }
370
+ it { is_expected.to eq(Time.at(value)) }
371
+ end
372
+
373
+ context 'when String encoded Integer' do
374
+ let(:value) { "#{Time.now.utc.to_i}" }
375
+ it { is_expected.to eq(Time.at(value.to_i)) }
376
+ end
377
+
378
+ context 'when YYYY-mm-dd' do
379
+ let(:value) { '2015-01-01' }
380
+ it { is_expected.to eq(Time.parse(value)) }
381
+ end
382
+
383
+ context 'when ISO8601' do
384
+ let(:value) { Date.parse('2015-01-01').to_time.iso8601 }
385
+ it { is_expected.to eq(Date.civil(2015,01,01).to_time) }
386
+ end
387
+ end
388
+
389
+ context 'with type :integer and :array' do
390
+ let(:column) { described_class.new(id: 'int[]', type: :integer, array: true) }
391
+
392
+ context 'when nil' do
393
+ let(:value) { nil }
394
+ it { is_expected.to eq([]) }
395
+ end
396
+
397
+ context "when 'NULL'" do
398
+ let(:value) { 'NULL' }
399
+ it { is_expected.to eq([]) }
400
+ end
401
+
402
+ context 'when scalar' do
403
+ let(:value) { '1' }
404
+ it { is_expected.to eq([1]) }
405
+ end
406
+
407
+ context 'when array' do
408
+ let(:value) { '[1,2]' }
409
+ it { is_expected.to eq([1,2]) }
410
+ end
411
+ end
412
+
413
+ context 'with type :json' do
414
+ let(:column) { described_class.new(id: 'json', type: :json) }
415
+
416
+ context 'when nil' do
417
+ let(:value) { nil }
418
+ it { is_expected.to eq({}) }
419
+ end
420
+
421
+ context "when 'NULL'" do
422
+ let(:value) { 'NULL' }
423
+ it { is_expected.to eq({}) }
424
+ end
425
+
426
+ context 'when scalar' do
427
+ let(:value) { '1' }
428
+ it { is_expected.to eq(1) }
429
+ end
430
+
431
+ context 'when array' do
432
+ let(:value) { '{"k":"v"}' }
433
+ it { is_expected.to eq({"k" => "v"}) }
434
+ end
435
+ end
436
+
437
+ context 'with type :yaml and sub_type :boolean' do
438
+ let(:column) { described_class.new(id: 'yaml', type: :yaml, sub_type: :boolean) }
439
+ let(:value) do
440
+ {
441
+ 'true' => true,
442
+ 'one' => '1',
443
+ 'zero' => '0',
444
+ 'false' => false,
445
+ 'string' => 'string',
446
+ 'one_integer' => 1,
447
+ 'zero_integer' => 0
448
+ }.to_yaml
449
+ end
450
+
451
+ it 'should cast yaml to ruby' do
452
+ expect(result['true']).to eq(true)
453
+ expect(result['false']).to eq(false)
454
+ expect(result['one']).to eq(true)
455
+ expect(result['zero']).to eq(false)
456
+ expect(result['one_integer']).to eq(true)
457
+ expect(result['zero_integer']).to eq(false)
458
+ expect(result.key?('string')).to eq(false)
459
+ end
460
+ end
461
+ end
462
+
463
+ describe '#default_ruby_value' do
464
+ subject(:result) { column.default_ruby_value }
465
+
466
+ [:boolean, :integer, :string].each do |type|
467
+ context "with type :#{type}" do
468
+ let(:column) { described_class.new(id: 'column', type: type) }
469
+ it { is_expected.to be_nil }
470
+ end
471
+ end
472
+
473
+ context 'with type :date' do
474
+ let(:column) { described_class.new(id: 'column', type: :date) }
475
+ it { is_expected.to eq(Date.new(0)) }
476
+ end
477
+
478
+ context 'with type :timestamp' do
479
+ let(:column) { described_class.new(id: 'column', type: :timestamp) }
480
+ it { is_expected.to eq(Time.new(0)) }
481
+ end
482
+
483
+ [:json, :yaml, :key_value].each do |type|
484
+ context "with type :#{type}" do
485
+ let(:column) { described_class.new(id: 'column', type: type) }
486
+ it { is_expected.to eq({}) }
487
+ end
488
+ end
489
+
490
+ context 'with array' do
491
+ let(:column) { described_class.new(id: 'column', type: :integer, array: true) }
492
+ it { is_expected.to eq([]) }
493
+ end
494
+ end
495
+
496
+ describe '#csv_value' do
497
+ subject(:result) { column.csv_value(value) }
498
+
499
+ context 'with type :string' do
500
+ let(:column) { described_class.new(id: 'string', type: :string) }
501
+
502
+ context 'when nil' do
503
+ let(:value) { nil }
504
+ it { is_expected.to eq(nil) }
505
+ end
506
+
507
+ context 'when blank' do
508
+ let(:value) { '' }
509
+ it { is_expected.to eq(nil) }
510
+ end
511
+
512
+ context 'when present' do
513
+ let(:value) { 'value' }
514
+ it { is_expected.to eq('value') }
515
+ end
516
+ end
517
+
518
+ context 'with type :boolean' do
519
+ let(:column) { described_class.new(id: 'bool', type: :boolean) }
520
+
521
+ context 'when true' do
522
+ let(:value) { true }
523
+ it { is_expected.to eq('TRUE') }
524
+ end
525
+
526
+ context 'when false' do
527
+ let(:value) { false }
528
+ it { is_expected.to eq('FALSE') }
529
+ end
530
+ end
531
+
532
+ context 'with type :boolean with store :hive' do
533
+ let(:column) { described_class.new(id: 'bool', type: :boolean) }
534
+
535
+ before do
536
+ allow(column).to receive_message_chain(:parent, :store, :type).and_return(:hive)
537
+ end
538
+
539
+ context 'when true' do
540
+ let(:value) { true }
541
+ it { is_expected.to eq('TRUE') }
542
+ end
543
+
544
+ context 'when false' do
545
+ let(:value) { false }
546
+ it { is_expected.to eq(nil) }
547
+ end
548
+ end
549
+
550
+ context 'with type :integer and :array' do
551
+ let(:column) { described_class.new(id: 'int[]', type: :integer, array: true) }
552
+
553
+ context 'when nil' do
554
+ let(:value) { nil }
555
+ it { is_expected.to eq('[]') }
556
+ end
557
+
558
+ context 'when scalar integer' do
559
+ let(:value) { 1 }
560
+ it { is_expected.to eq('[1]') }
561
+ end
562
+
563
+ context 'when scalar string' do
564
+ let(:value) { '1' }
565
+ it { is_expected.to eq('[1]') }
566
+ end
567
+
568
+ context 'when array of integer' do
569
+ let(:value) { [1,2] }
570
+ it { is_expected.to eq('[1,2]') }
571
+ end
572
+
573
+ context 'when array of string' do
574
+ let(:value) { ['1','2'] }
575
+ it { is_expected.to eq('[1,2]') }
576
+ end
577
+ end
578
+
579
+ context 'with type :string and :array' do
580
+ let(:column) { described_class.new(id: 'string[]', type: :string, array: true) }
581
+
582
+ context 'when nil' do
583
+ let(:value) { nil }
584
+ it { is_expected.to eq('[]') }
585
+ end
586
+
587
+ context 'when scalar string' do
588
+ let(:value) { '1' }
589
+ it { is_expected.to eq('["1"]') }
590
+ end
591
+
592
+ context 'when scalar integer' do
593
+ let(:value) { 1 }
594
+ it { is_expected.to eq('["1"]') }
595
+ end
596
+
597
+ context 'when array of string' do
598
+ let(:value) { ['1','2'] }
599
+ it { is_expected.to eq('["1","2"]') }
600
+ end
601
+
602
+ context 'when array of integer' do
603
+ let(:value) { [1,2] }
604
+ it { is_expected.to eq('["1","2"]') }
605
+ end
606
+ end
607
+
608
+ context 'with type :timestamp' do
609
+ let(:column) { described_class.new(id: 'timestamp', type: :timestamp) }
610
+
611
+ context 'when nil' do
612
+ let(:value) { nil }
613
+ it { is_expected.to be_nil }
614
+ end
615
+
616
+ context 'when Time' do
617
+ let(:value) { Time.now }
618
+ it { is_expected.to eq(value.utc.iso8601(3)) }
619
+ end
620
+
621
+ context 'when unknown' do
622
+ let(:value) { 'unknown' }
623
+ it { expect { result }.to raise_error ArgumentError, "Could not coerce 'unknown' into :timestamp for column 'timestamp'" }
624
+ end
625
+ end
626
+ end
627
+
628
+ describe '#==' do
629
+ subject { column == other }
630
+
631
+ context 'when identical reference' do
632
+ let(:column) { described_class.new id: 'name', type: :string }
633
+ let(:other) { column }
634
+ it { is_expected.to eq(true) }
635
+ end
636
+
637
+ context 'when identical value' do
638
+ let(:column) { described_class.new id: 'name', type: :string }
639
+ let(:other) { column.dup }
640
+ it { is_expected.to eq(true) }
641
+ end
642
+
643
+ context 'when different value' do
644
+ let(:column) { described_class.new id: 'name', type: :string }
645
+ let(:other) { described_class.new id: 'name', type: :integer }
646
+ it { is_expected.to eq(false) }
647
+ end
648
+ end
649
+
650
+ describe '#required_value?' do
651
+ subject { column.required_value? }
652
+
653
+ context 'by default' do
654
+ let(:column) { described_class.new id: 'name', type: :string }
655
+ it { is_expected.to eq(false) }
656
+ end
657
+
658
+ context 'when surrogate_key' do
659
+ let(:column) { described_class.new id: 'name', type: :string, surrogate_key: true }
660
+ it { is_expected.to eq(true) }
661
+
662
+ context 'when reference allow null' do
663
+ before do
664
+ allow(column).to receive(:reference).and_return(double(null: true, default: nil))
665
+ end
666
+ it { is_expected.to eq(false) }
667
+ end
668
+
669
+ context 'when reference has default' do
670
+ before do
671
+ allow(column).to receive(:reference).and_return(double(null: false, default: 'Unknown'))
672
+ end
673
+ it { is_expected.to eq(false) }
674
+ end
675
+ end
676
+
677
+ context 'when natural_key' do
678
+ let(:column) { described_class.new id: 'name', type: :string, natural_key: true }
679
+ it { is_expected.to eq(true) }
680
+
681
+ context 'when reference allow null' do
682
+ before do
683
+ allow(column).to receive(:reference).and_return(double(null: true, default: nil))
684
+ end
685
+ it { is_expected.to eq(false) }
686
+ end
687
+
688
+ context 'when reference has default' do
689
+ before do
690
+ allow(column).to receive(:reference).and_return(double(null: false, default: 'Unknown'))
691
+ end
692
+ it { is_expected.to eq(false) }
693
+ end
694
+ end
695
+ end
696
+ end