-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtest_transaction.rb
222 lines (197 loc) · 10.7 KB
/
test_transaction.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require_relative './helper'
require 'embulk/output/bigquery'
Bigquery = Embulk::Output::Bigquery unless defined?(Bigquery)
module Embulk
class Output::Bigquery
class TestTransaction < Test::Unit::TestCase
def least_config
DataSource.new({
'project' => 'your_project_name',
'dataset' => 'your_dataset_name',
'table' => 'your_table_name',
'temp_table' => 'temp_table', # randomly created is not good for our test
'path_prefix' => 'tmp/', # randomly created is not good for our test
})
end
def schema
Schema.new([
Column.new({index: 0, name: 'boolean', type: :boolean}),
Column.new({index: 1, name: 'long', type: :long}),
Column.new({index: 2, name: 'double', type: :double}),
Column.new({index: 3, name: 'string', type: :string}),
Column.new({index: 4, name: 'timestamp', type: :timestamp}),
Column.new({index: 5, name: 'json', type: :json}),
])
end
def processor_count
1
end
def control
Proc.new {|task| task_reports = [] }
end
def setup
stub(Bigquery).transaction_report { {'num_input_rows' => 1, 'num_output_rows' => 1, 'num_rejected_rows' => 0} }
end
sub_test_case "append_direct" do
def test_append_direc_without_auto_create
config = least_config.merge('mode' => 'append_direct', 'auto_create_dataset' => false, 'auto_create_table' => false)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).get_table(config['table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_append_direct_with_auto_create
config = least_config.merge('mode' => 'append_direct', 'auto_create_dataset' => true, 'auto_create_table' => true)
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).create_dataset(config['dataset'])
mock(obj).create_table_if_not_exists(config['table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_append_direct_with_partition_without_auto_create
config = least_config.merge('mode' => 'append_direct', 'table' => 'table$20160929', 'auto_create_dataset' => false, 'auto_create_table' => false)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).get_table(config['table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_append_direct_with_partition_with_auto_create
config = least_config.merge('mode' => 'append_direct', 'table' => 'table$20160929', 'auto_create_dataset' => true, 'auto_create_table' => true)
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).create_dataset(config['dataset'])
mock(obj).create_table_if_not_exists(config['table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
end
sub_test_case "delete_in_advance" do
def test_delete_in_advance
config = least_config.merge('mode' => 'delete_in_advance')
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).delete_table_or_partition(config['table'])
mock(obj).create_table_if_not_exists(config['table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_delete_in_advance_with_partitioning
config = least_config.merge('mode' => 'delete_in_advance', 'table' => 'table$20160929', 'auto_create_table' => true)
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).delete_table_or_partition(config['table'])
mock(obj).create_table_if_not_exists(config['table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
end
sub_test_case "replace" do
def test_replace
config = least_config.merge('mode' => 'replace')
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).create_table_if_not_exists(config['temp_table'], options: {"expiration_time"=>nil})
mock(obj).create_table_if_not_exists(config['table'])
mock(obj).copy(config['temp_table'], config['table'], write_disposition: 'WRITE_TRUNCATE')
mock(obj).delete_table(config['temp_table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_replace_with_partitioning
config = least_config.merge('mode' => 'replace', 'table' => 'table$20160929')
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).create_table_if_not_exists(config['temp_table'], options: {"expiration_time"=>nil})
mock(obj).create_table_if_not_exists(config['table'])
mock(obj).copy(config['temp_table'], config['table'], write_disposition: 'WRITE_TRUNCATE')
mock(obj).delete_table(config['temp_table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
end
sub_test_case "replace_backup" do
def test_replace_backup
config = least_config.merge('mode' => 'replace_backup', 'dataset_old' => 'dataset_old', 'table_old' => 'table_old', 'temp_table' => 'temp_table')
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).get_dataset(config['dataset_old'])
mock(obj).create_table_if_not_exists(config['temp_table'], options: {"expiration_time"=>nil})
mock(obj).create_table_if_not_exists(config['table'])
mock(obj).create_table_if_not_exists(config['table_old'], dataset: config['dataset_old'])
mock(obj).get_table_or_partition(config['table'])
mock(obj).copy(config['table'], config['table_old'], config['dataset_old'])
mock(obj).copy(config['temp_table'], config['table'], write_disposition: 'WRITE_TRUNCATE')
mock(obj).delete_table(config['temp_table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_replace_backup_auto_create_dataset
config = least_config.merge('mode' => 'replace_backup', 'dataset_old' => 'dataset_old', 'table_old' => 'table_old', 'temp_table' => 'temp_table', 'auto_create_dataset' => true)
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).create_dataset(config['dataset'])
mock(obj).create_dataset(config['dataset_old'], reference: config['dataset'])
mock(obj).create_table_if_not_exists(config['table'])
mock(obj).create_table_if_not_exists(config['temp_table'], options: {"expiration_time"=>nil})
mock(obj).create_table_if_not_exists(config['table_old'], dataset: config['dataset_old'])
mock(obj).get_table_or_partition(config['table'])
mock(obj).copy(config['table'], config['table_old'], config['dataset_old'])
mock(obj).copy(config['temp_table'], config['table'], write_disposition: 'WRITE_TRUNCATE')
mock(obj).delete_table(config['temp_table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_replace_backup_with_partitioning
config = least_config.merge('mode' => 'replace_backup', 'table' => 'table$20160929', 'dataset_old' => 'dataset_old', 'table_old' => 'table_old$20160929', 'temp_table' => 'temp_table', 'auto_create_table' => true)
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).get_dataset(config['dataset_old'])
mock(obj).create_table_if_not_exists(config['temp_table'], options: {"expiration_time"=>nil})
mock(obj).create_table_if_not_exists(config['table'])
mock(obj).create_table_if_not_exists(config['table_old'], dataset: config['dataset_old'])
mock(obj).get_table_or_partition(config['table'])
mock(obj).copy(config['table'], config['table_old'], config['dataset_old'])
mock(obj).copy(config['temp_table'], config['table'], write_disposition: 'WRITE_TRUNCATE')
mock(obj).delete_table(config['temp_table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
end
sub_test_case "append" do
def test_append
config = least_config.merge('mode' => 'append')
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).create_table_if_not_exists(config['temp_table'], options: {"expiration_time"=>nil})
mock(obj).create_table_if_not_exists(config['table'])
mock(obj).copy(config['temp_table'], config['table'], write_disposition: 'WRITE_APPEND')
mock(obj).delete_table(config['temp_table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
def test_append_with_partitioning
config = least_config.merge('mode' => 'append', 'table' => 'table$20160929', 'auto_create_table' => true)
task = Bigquery.configure(config, schema, processor_count)
any_instance_of(BigqueryClient) do |obj|
mock(obj).get_dataset(config['dataset'])
mock(obj).create_table_if_not_exists(config['temp_table'], options: {"expiration_time"=>nil})
mock(obj).create_table_if_not_exists(config['table'])
mock(obj).copy(config['temp_table'], config['table'], write_disposition: 'WRITE_APPEND')
mock(obj).delete_table(config['temp_table'])
end
Bigquery.transaction(config, schema, processor_count, &control)
end
end
end
end
end