-
Notifications
You must be signed in to change notification settings - Fork 144
impl(bigquery): add BigQuery::from_stub to support client mocking #6620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
alvarowolfx
wants to merge
3
commits into
googleapis:main
Choose a base branch
from
alvarowolfx:impl-bq-from-stub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+258
−15
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use google_cloud_bigquery::client::BigQuery; | ||
| use google_cloud_bigquery::stub::JobService; | ||
| use google_cloud_bigquery_v2::model::{ | ||
| GetJobRequest, GetQueryResultsRequest, GetQueryResultsResponse, InsertJobRequest, Job, | ||
| JobConfiguration, JobConfigurationQuery, JobReference, PostQueryRequest, QueryResponse, | ||
| TableFieldSchema, TableSchema, | ||
| }; | ||
| use google_cloud_gax::Result as GaxResult; | ||
| use google_cloud_gax::error::{ | ||
| Error, | ||
| rpc::{Code, Status}, | ||
| }; | ||
| use google_cloud_gax::options::RequestOptions; | ||
| use google_cloud_gax::response::Response; | ||
| use mockall::mock; | ||
| use serde_json::{Map, json}; | ||
| use std::sync::Arc; | ||
|
|
||
| fn create_test_row(val: &str) -> wkt::Struct { | ||
| Map::from_iter([("f".to_string(), json!([{ "v": val }]))]) | ||
| } | ||
|
|
||
| mock! { | ||
| #[derive(Debug)] | ||
| JobService {} | ||
| impl JobService for JobService { | ||
| async fn query( | ||
| &self, | ||
| req: PostQueryRequest, | ||
| options: RequestOptions, | ||
| ) -> GaxResult<Response<QueryResponse>>; | ||
|
|
||
| async fn get_query_results( | ||
| &self, | ||
| req: GetQueryResultsRequest, | ||
| options: RequestOptions, | ||
| ) -> GaxResult<Response<GetQueryResultsResponse>>; | ||
|
|
||
| async fn get_job( | ||
| &self, | ||
| req: GetJobRequest, | ||
| options: RequestOptions, | ||
| ) -> GaxResult<Response<Job>>; | ||
|
|
||
| async fn insert_job( | ||
| &self, | ||
| req: InsertJobRequest, | ||
| options: RequestOptions, | ||
| ) -> GaxResult<Response<Job>>; | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn mock_query_success() -> anyhow::Result<()> { | ||
| let mut mock = MockJobService::new(); | ||
| mock.expect_query().returning(|req, _| { | ||
| assert_eq!(req.project_id, "test-project"); | ||
| assert_eq!( | ||
| req.query_request.as_ref().unwrap().query, | ||
| "SELECT 'hello world' AS greeting" | ||
| ); | ||
| let schema = TableSchema::new().set_fields([TableFieldSchema::new() | ||
| .set_name("greeting") | ||
| .set_type("STRING")]); | ||
| let rows = vec![create_test_row("hello world")]; | ||
| let response = QueryResponse::new() | ||
| .set_job_complete(true) | ||
| .set_schema(schema) | ||
| .set_rows(rows) | ||
| .set_total_rows(1u64); | ||
| Ok(Response::from(response)) | ||
| }); | ||
|
|
||
| let client = BigQuery::from_stub(mock); | ||
| let mut rows = client | ||
| .query("SELECT 'hello world' AS greeting") | ||
| .with_project_id("test-project") | ||
| .until_done() | ||
| .await? | ||
| .read(); | ||
|
|
||
| let row = rows | ||
| .next() | ||
| .await | ||
| .transpose()? | ||
| .expect("expected at least one row"); | ||
| let greeting: String = row.get("greeting")?; | ||
| assert_eq!(greeting, "hello world"); | ||
| assert!(rows.next().await.transpose()?.is_none()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn mock_query_failure() { | ||
| let mut mock = MockJobService::new(); | ||
| mock.expect_query().returning(|_, _| { | ||
| Err(Error::service( | ||
| Status::default().set_code(Code::InvalidArgument), | ||
| )) | ||
| }); | ||
|
|
||
| let client = BigQuery::from_stub(mock); | ||
| let result = client | ||
| .query("INVALID QUERY") | ||
| .with_project_id("test-project") | ||
| .until_done() | ||
| .await; | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn mock_attach_job_success() -> anyhow::Result<()> { | ||
| let mut mock = MockJobService::new(); | ||
| mock.expect_get_job().returning(|req, _| { | ||
| assert_eq!(req.project_id, "test-project"); | ||
| assert_eq!(req.job_id, "job_123"); | ||
| let job = Job::new() | ||
| .set_job_reference( | ||
| JobReference::new() | ||
| .set_project_id("test-project") | ||
| .set_job_id("job_123"), | ||
| ) | ||
| .set_configuration( | ||
| JobConfiguration::new() | ||
| .set_query(JobConfigurationQuery::new().set_query("SELECT 1")), | ||
| ); | ||
| Ok(Response::from(job)) | ||
| }); | ||
|
|
||
| let client = BigQuery::from_stub(mock); | ||
| let job_ref = JobReference::new() | ||
| .set_project_id("test-project") | ||
| .set_job_id("job_123"); | ||
| let query = client.attach_job(job_ref).await?; | ||
| let metadata = query.metadata(); | ||
| let attached_ref = metadata.job_reference.as_ref().unwrap(); | ||
| assert_eq!(attached_ref.project_id, "test-project"); | ||
| assert_eq!(attached_ref.job_id, "job_123"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn mock_with_shared_arc() -> anyhow::Result<()> { | ||
| let mut mock = MockJobService::new(); | ||
| mock.expect_query().returning(|_, _| { | ||
| let schema = TableSchema::new() | ||
| .set_fields([TableFieldSchema::new().set_name("num").set_type("INTEGER")]); | ||
| let rows = vec![create_test_row("42")]; | ||
| let response = QueryResponse::new() | ||
| .set_job_complete(true) | ||
| .set_schema(schema) | ||
| .set_rows(rows) | ||
| .set_total_rows(1u64); | ||
| Ok(Response::from(response)) | ||
| }); | ||
|
|
||
| let mock_arc = Arc::new(mock); | ||
| let client1 = BigQuery::from_stub::<MockJobService>(mock_arc.clone()); | ||
| let client2 = BigQuery::from_stub::<MockJobService>(mock_arc); | ||
|
|
||
| let mut rows1 = client1 | ||
| .query("SELECT 42") | ||
| .with_project_id("proj1") | ||
| .until_done() | ||
| .await? | ||
| .read(); | ||
| let row1 = rows1.next().await.transpose()?.unwrap(); | ||
| let num1: i64 = row1.get("num")?; | ||
| assert_eq!(num1, 42); | ||
|
|
||
| let mut rows2 = client2 | ||
| .query("SELECT 42") | ||
| .with_project_id("proj2") | ||
| .until_done() | ||
| .await? | ||
| .read(); | ||
| let row2 = rows2.next().await.transpose()?.unwrap(); | ||
| let num2: i64 = row2.get("num")?; | ||
| assert_eq!(num2, 42); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two issues with the current implementation of
from_stub:stubparameter is of typeimpl Into<std::sync::Arc<T>>, butJobService::from_stubexpectsstd::sync::Arc<T>. Since Rust does not perform implicit type conversions for function arguments, you must explicitly callstub.into()to convert it.crate::stub::JobServiceas a public re-export, we should use it here instead of the fully-qualified external pathgoogle_cloud_bigquery_v2::stub::JobServiceto maintain consistency and clean API boundaries.Here is the corrected implementation:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no compilation errors, but on the second path AI is right. Similar to reasons why we removed crate::model, should we also skip adding crate::stub ? Thoughts @dbolduc ?
Also I'm trying to decide how from_stub is going to work in a world where we also support Storage API acceleration.
from_storage_stubthat accepts both ?from_stubto accept a trait has all methods on both JobService and Read client ?