Google-cloud-ruby: Question: is this snippet accurate?

Created on 16 Mar 2017  路  11Comments  路  Source: googleapis/google-cloud-ruby

Found this code below in this doc page. Is it accurate? I feel the second require (when the job doesn't fail) should be for storage, and not for bigquery.

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
source_table = dataset.table "baby_names"
result_table = dataset.create_table "baby_names_results"

sql = "SELECT name, number as count " \
      "FROM baby_names " \
      "WHERE name CONTAINS 'Sam' " \
      "ORDER BY count DESC"
query_job = dataset.query_job sql, table: result_table

query_job.wait_until_done!

if !query_job.failed?

  require "google/cloud/bigquery"

  storage = Google::Cloud::Storage.new
  bucket_id = "bigquery-exports-#{SecureRandom.uuid}"
  bucket = storage.create_bucket bucket_id
  extract_url = "gs://#{bucket.id}/baby-names-sam.csv"

  extract_job = result_table.extract extract_url

  extract_job.wait_until_done!

  # Download to local filesystem
  bucket.files.first.download "baby-names-sam.csv"

end
bigquery acknowledged bug

All 11 comments

The second require should be for storage.

Thanks @omaray!

Cool. Just out of curiosity though: shouldn't we have had tests to verify that? Or did we just miss that one?

The way we need to set up our code example tests means that this would not fail. We have to mock out all the API calls, so we have another storage require in the mock setup.

@omaray wrote:

shouldn't we have had tests to verify that? Or did we just miss that one?

Please see #1337 (and #1338, #1339, #1340, for Datastore, Storage, and Logging, respectively), which I opened yesterday and which refers to this issue.

@blowmage It occurred to me that this particular issue probably would not be caught by any unit test using mocks, since the mocks would have already performed the require.

Agreed. See my earlier comment.

@blowmage I'm wondering if we should have a policy that examples used in the guides MUST also be used (and therefore tested) in API docs. What do you think?

I'm pretty sure this is the case for all recent packages for which I've created the guides, because I create the guide as a final step, using examples from the API docs. But it seems like a good policy in any case.

Also agreed. I assumed that was the case already. We want the guides and class/method code examples to be as cohesive as possible.

I updated the title and description for the 4 issues listed above to reflect this policy: Instead of manually testing the guides, ensure that all guide examples are copied from tested API docs examples.

Was this page helpful?
0 / 5 - 0 ratings