Skip to content

Commit 6e4a6ad

Browse files
author
remi Taylor
committed
Refactor BigQuery sample specs (always create test dataset and table) - plus CSV gen helper
1 parent 524ddb7 commit 6e4a6ad

1 file changed

Lines changed: 128 additions & 135 deletions

File tree

bigquery_sample/spec/bigquery_sample_spec.rb

Lines changed: 128 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@
1717
require "google/cloud"
1818
require "csv"
1919

20-
# TODO: refactor CSV creation to simplify & slightly DRY specs
20+
# TODO: move some helpers to a shared directory and update other specs
21+
# require_relative "../../shared/spec_helpers"
22+
# require "spec_helper"
23+
#
24+
# ...
25+
#
26+
# require_relative "../../shared/spec_helpers"
27+
#
28+
# require "spec_helper/tempfile_helper"
29+
# require "spec_helper/csv_file_helper"
30+
# require "spec_helper/cloud_storage_helper"
31+
# require "spec_helper/capture_output"
32+
# require "spec_helper/bigquery_helper"
2133

2234
RSpec.describe "Google Cloud BigQuery samples" do
2335

@@ -27,16 +39,47 @@
2739
@bigquery = @gcloud.bigquery
2840
@storage = @gcloud.storage
2941
@bucket = @storage.bucket ENV["STORAGE_BUCKET"]
42+
@tempfiles = []
3043

31-
# Examples assume that test_dataset does not exist
32-
test_dataset = @bigquery.dataset "test_dataset"
44+
# Examples assume that newly created test_dataset and test_table exist
45+
delete_test_dataset!
3346

34-
if test_dataset
35-
test_dataset.tables.each &:delete
36-
test_dataset.delete
47+
@dataset = @bigquery.create_dataset "test_dataset"
48+
@table = @dataset.create_table "test_table" do |schema|
49+
schema.string "name"
50+
schema.integer "value"
3751
end
3852
end
3953

54+
after do
55+
# Cleanup any tempfiles that were used by the example spec
56+
@tempfiles.each &:flush
57+
@tempfiles.each &:close
58+
end
59+
60+
def delete_test_dataset!
61+
dataset = @bigquery.dataset "test_dataset"
62+
dataset.tables.each &:delete if dataset
63+
dataset.delete if dataset
64+
end
65+
66+
# Helper to create and return CSV file.
67+
# The block will be passed a CSV object.
68+
#
69+
# @example
70+
# file = create_csv do |csv|
71+
# csv << [ "Alice", 123 ]
72+
# csv << [ "Bob", 456 ]
73+
# end
74+
#
75+
# puts file.path
76+
def create_csv &block
77+
file = Tempfile.new %w[ bigquery-test csv ]
78+
CSV.open file.path, "w", &block
79+
@tempfiles << file
80+
file
81+
end
82+
4083
# Capture and return STDOUT output by block
4184
def capture &block
4285
real_stdout = $stdout
@@ -54,6 +97,7 @@ def capture &block
5497

5598
describe "Managing Datasets" do
5699
example "create dataset" do
100+
delete_test_dataset!
57101
expect(@bigquery.dataset "test_dataset").to be nil
58102

59103
expect {
@@ -66,8 +110,6 @@ def capture &block
66110
end
67111

68112
example "list datasets" do
69-
@bigquery.create_dataset "test_dataset"
70-
71113
expect {
72114
list_datasets project_id: @project_id
73115
}.to output(
@@ -76,7 +118,7 @@ def capture &block
76118
end
77119

78120
example "delete dataset" do
79-
@bigquery.create_dataset "test_dataset"
121+
@dataset.tables.each &:delete
80122
expect(@bigquery.dataset "test_dataset").not_to be nil
81123

82124
expect {
@@ -92,8 +134,8 @@ def capture &block
92134
describe "Managing Tables" do
93135

94136
example "create table" do
95-
dataset = @bigquery.create_dataset "test_dataset"
96-
expect(dataset.table "test_table").to be nil
137+
@table.delete
138+
expect(@dataset.table "test_table").to be nil
97139

98140
expect {
99141
create_table project_id: @project_id,
@@ -103,13 +145,10 @@ def capture &block
103145
"Created table: test_table\n"
104146
).to_stdout
105147

106-
expect(dataset.table "test_table").not_to be nil
148+
expect(@dataset.table "test_table").not_to be nil
107149
end
108150

109151
example "list tables" do
110-
dataset = @bigquery.create_dataset "test_dataset"
111-
dataset.create_table "test_table"
112-
113152
expect {
114153
list_tables project_id: @project_id, dataset_id: "test_dataset"
115154
}.to output(
@@ -118,9 +157,7 @@ def capture &block
118157
end
119158

120159
example "delete table" do
121-
dataset = @bigquery.create_dataset "test_dataset"
122-
dataset.create_table "test_table"
123-
expect(dataset.table "test_table").not_to be nil
160+
expect(@dataset.table "test_table").not_to be nil
124161

125162
expect {
126163
delete_table project_id: @project_id,
@@ -130,40 +167,26 @@ def capture &block
130167
"Deleted table: test_table\n"
131168
).to_stdout
132169

133-
expect(dataset.table "test_table").to be nil
170+
expect(@dataset.table "test_table").to be nil
134171
end
135172

136173
example "list table data" do
137-
begin
138-
dataset = @bigquery.create_dataset "test_dataset"
139-
140-
table = dataset.create_table "test_table" do |schema|
141-
schema.string "name"
142-
schema.integer "value"
143-
end
144-
145-
csv_file = Tempfile.new %w[ bigquery-test csv ]
146-
147-
CSV.open csv_file.path, "w" do |csv|
148-
csv << [ "Alice", 5 ]
149-
csv << [ "Bob", 10 ]
150-
end
151-
152-
load_job = table.load csv_file.path
153-
154-
load_job.wait_until_done!
155-
156-
expect {
157-
list_table_data project_id: @project_id,
158-
dataset_id: "test_dataset",
159-
table_id: "test_table"
160-
}.to output(
161-
"name = Alice\nvalue = 5\nname = Bob\nvalue = 10\n"
162-
).to_stdout
163-
ensure
164-
csv_file.flush
165-
csv_file.close
174+
csv_file = create_csv do |csv|
175+
csv << [ "Alice", 5 ]
176+
csv << [ "Bob", 10 ]
166177
end
178+
179+
load_job = @table.load csv_file.path
180+
181+
load_job.wait_until_done!
182+
183+
expect {
184+
list_table_data project_id: @project_id,
185+
dataset_id: "test_dataset",
186+
table_id: "test_table"
187+
}.to output(
188+
"name = Alice\nvalue = 5\nname = Bob\nvalue = 10\n"
189+
).to_stdout
167190
end
168191

169192
example "list table data with pagination"
@@ -172,110 +195,80 @@ def capture &block
172195
describe "Importing data" do
173196

174197
example "import data from file" do
175-
begin
176-
dataset = @bigquery.create_dataset "test_dataset"
177-
178-
table = dataset.create_table "test_table" do |schema|
179-
schema.string "name"
180-
schema.integer "value"
181-
end
182-
183-
csv_file = Tempfile.new %w[ bigquery-test csv ]
184-
185-
CSV.open csv_file.path, "w" do |csv|
186-
csv << [ "Alice", 5 ]
187-
csv << [ "Bob", 10 ]
188-
end
189-
190-
expect(table.data).to be_empty
198+
csv_file = create_csv do |csv|
199+
csv << [ "Alice", 5 ]
200+
csv << [ "Bob", 10 ]
201+
end
191202

192-
capture do
193-
import_table_data_from_file project_id: @project_id,
194-
dataset_id: "test_dataset",
195-
table_id: "test_table",
196-
local_file_path: csv_file.path
197-
end
203+
expect(@table.data).to be_empty
198204

199-
expect(captured_output).to include(
200-
"Importing data from file: #{csv_file.path}\n"
201-
)
202-
expect(captured_output).to match(
203-
/Waiting for load job to complete: job_\w+/
204-
)
205-
expect(captured_output).to include "Data imported"
206-
207-
loaded_data = table.data
208-
209-
expect(loaded_data).not_to be_empty
210-
expect(loaded_data.count).to eq 2
211-
expect(loaded_data.first["name"]).to eq "Alice"
212-
expect(loaded_data.first["value"]).to eq 5
213-
expect(loaded_data.last["name"]).to eq "Bob"
214-
expect(loaded_data.last["value"]).to eq 10
215-
ensure
216-
csv_file.flush
217-
csv_file.close
205+
capture do
206+
import_table_data_from_file project_id: @project_id,
207+
dataset_id: "test_dataset",
208+
table_id: "test_table",
209+
local_file_path: csv_file.path
218210
end
211+
212+
expect(captured_output).to include(
213+
"Importing data from file: #{csv_file.path}\n"
214+
)
215+
expect(captured_output).to match(
216+
/Waiting for load job to complete: job_\w+/
217+
)
218+
expect(captured_output).to include "Data imported"
219+
220+
loaded_data = @table.data
221+
222+
expect(loaded_data).not_to be_empty
223+
expect(loaded_data.count).to eq 2
224+
expect(loaded_data.first["name"]).to eq "Alice"
225+
expect(loaded_data.first["value"]).to eq 5
226+
expect(loaded_data.last["name"]).to eq "Bob"
227+
expect(loaded_data.last["value"]).to eq 10
219228
end
220229

221230
example "import data from Cloud Storage" do
222-
begin
223-
dataset = @bigquery.create_dataset "test_dataset"
224-
225-
table = dataset.create_table "test_table" do |schema|
226-
schema.string "name"
227-
schema.integer "value"
228-
end
229-
230-
csv_file = Tempfile.new %w[ bigquery-test csv ]
231-
232-
CSV.open csv_file.path, "w" do |csv|
233-
csv << [ "Alice", 5 ]
234-
csv << [ "Bob", 10 ]
235-
end
236-
237-
file = @bucket.create_file csv_file.path, "bigquery-test.csv"
231+
csv_file = create_csv do |csv|
232+
csv << [ "Alice", 5 ]
233+
csv << [ "Bob", 10 ]
234+
end
238235

239-
expect(table.data).to be_empty
236+
file = @bucket.create_file csv_file.path, "bigquery-test.csv"
240237

241-
capture do
242-
import_table_data_from_cloud_storage(
243-
project_id: @project_id,
244-
dataset_id: "test_dataset",
245-
table_id: "test_table",
246-
storage_path: "gs://#{@bucket.name}/bigquery-test.csv"
247-
)
248-
end
238+
expect(@table.data).to be_empty
249239

250-
expect(captured_output).to include(
251-
"Importing data from Cloud Storage file: " +
252-
"gs://#{@bucket.name}/bigquery-test.csv"
253-
)
254-
expect(captured_output).to match(
255-
/Waiting for load job to complete: job_\w+/
240+
capture do
241+
import_table_data_from_cloud_storage(
242+
project_id: @project_id,
243+
dataset_id: @dataset.dataset_id,
244+
table_id: @table.table_id,
245+
storage_path: "gs://#{@bucket.name}/bigquery-test.csv"
256246
)
257-
expect(captured_output).to include "Data imported"
258-
259-
loaded_data = table.data
260-
261-
expect(loaded_data).not_to be_empty
262-
expect(loaded_data.count).to eq 2
263-
expect(loaded_data.first["name"]).to eq "Alice"
264-
expect(loaded_data.first["value"]).to eq 5
265-
expect(loaded_data.last["name"]).to eq "Bob"
266-
expect(loaded_data.last["value"]).to eq 10
267-
ensure
268-
csv_file.flush
269-
csv_file.close
270247
end
248+
249+
expect(captured_output).to include(
250+
"Importing data from Cloud Storage file: " +
251+
"gs://#{@bucket.name}/bigquery-test.csv"
252+
)
253+
expect(captured_output).to match(
254+
/Waiting for load job to complete: job_\w+/
255+
)
256+
expect(captured_output).to include "Data imported"
257+
258+
loaded_data = @table.data
259+
260+
expect(loaded_data).not_to be_empty
261+
expect(loaded_data.count).to eq 2
262+
expect(loaded_data.first["name"]).to eq "Alice"
263+
expect(loaded_data.first["value"]).to eq 5
264+
expect(loaded_data.last["name"]).to eq "Bob"
265+
expect(loaded_data.last["value"]).to eq 10
271266
end
272267

273268
example "stream data import"
274269
end
275270

276271
describe "Exporting data" do
277-
# Needs a CSV file to import into the table before running
278-
# the export command, so refactor CSV code before writing this
279272
example "export data to Cloud Storage"
280273
end
281274

0 commit comments

Comments
 (0)