Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking Bigquery for integration tests

While other interfaces are relatively easy to mock in my Java integration tests, I couldn't find a proper way of mocking Bigquery.

One possibility is to mock the layer I wrote on top of Bigquery itself, but I prefer mocking Bigquery in a more natural way. I'm looking for a limited, lightweight implementation, which allows defining the table contents, and supports queries using the standard API. Is there such a library? If not, what alternative approaches are recommended?

like image 907
Eyal Schneider Avatar asked Sep 15 '16 09:09

Eyal Schneider


People also ask

Should I mock database integration test?

General case : you don't mock in the integration tests In a general way, mocking and integration tests don't suit together. Most of time, you want to use the one or you want to use the second, not both.

How do you mock a method in integration test?

In order to mock things like databases, web services, the file system and so on, you will probably want to refactor a little. For each external service, you should write a wrapper class that has a method for each operation that you wish to perform.

How do you test a big query?

Unit tests in BigQuery (standard) SQL Create BigQuery object (dataset, table, UDF) to meet some business requirement. Create a SQL unit test to check the object. Run SQL unit test to check the object does the job or not. If the test is passed then move on to the next SQL unit test.


1 Answers

In unit testing it is perfectly fine to mock all external dependencies, and as long you are using interfaces to abstract out access to BigQuery client, mocking should not be an issue.

With integration testing I would rather get all my 3rd parties dependencies tested to the extend an application needs it.

For instance one case would be an ETL which streams data from external sources to BigQuery, in this case an integration test needs to verify that all data is in BigQuery as expected, which means that verification stage needs to take into account repeated, and nested messages as required.

Another case would an application that runs some business SQLs, in this case you would have populate BigQuery with some test data before applicaiton run, then the applicaiton needs to publishe the SQL output either as view/new table/or stream out of data out of for verification.

There are already some libraries taking care of integration testing with datastores including BigQuery/NoSQL/SQL

They would provide an easy solution for the cases described above and full support for SQL, dynamic macro/predicate etc ....

  1. Dsunit (go-lang)
  2. JDsunit (java)
  3. Endly(language agnostic)

See more how to use endly for ETL and BiqQuery testing

If datastore integration test library is not an option for you and you are looking for just testing BigQuery client, the good news is that the client uses REST, so using network sniffers you can easy record what is being send back and forth, then you can use it in replayer. In order to redirect BigQuery from public BG endpoints to your replayer you would use http java proxy.

like image 99
Adrian Avatar answered Oct 24 '22 09:10

Adrian