Conversation
Added comprehensive unit tests to TestDataConnectApiClient covering client constructor, inputs validation, variables and impersonation serialization, and service URL construction.
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive suite of unit tests for the _DataConnectApiClient class, covering its constructor, input validation, payload preparation, and service URL generation. The review feedback identifies critical issues in the tests, including a missing import for the dataclass decorator and references to undefined classes and attributes in the dataconnect module, both of which will cause runtime errors during test execution.
Implemented _validate_inputs to validate queries, options, variables, and impersonation, and _prepare_graphql_payload to construct GraphQL JSON payloads. Implemented _get_firebase_dataconnect_service_url to build production and emulator service endpoint URLs. Added full unit test coverage for input validation, payload construction, and URL formatting.
…uilder Implemented three helper functions in _DataConnectApiClient along with their corresponding implementations: - _validate_inputs: Validates the query structure, options, variables types, and custom impersonation claims. - _prepare_graphql_payload: Prepares the JSON payload for GraphQL calls, serializing variables (including nested dataclasses) and configuring optional extensions like impersonation. - _get_firebase_dataconnect_service_url: Formats the endpoint URL for execution, supporting both production host formats and local emulator host formats. Added comprehensive unit tests covering standard cases, invalid options, missing configurations, and emulator environments for each of the helper functions. This completes the first half of milestone 3.
Implemented `_get_headers` inside `_DataConnectApiClient` to build standard telemetry headers for outgoing HTTP requests. Specifically, it populates: - X-Firebase-Client: The current Python SDK version header. - x-goog-api-client: The Google telemetry metrics header. Added the `TestDataConnectApiClientGetHeaders` unit test suite to verify the return type and values.
Fixed formatting and style issues highlighted by pylint: - Removed trailing whitespace in TestDataConnectApiClientGetHeaders test class. - Resolved missing final newline warning at the end of the test file.
stephenarosaj
left a comment
There was a problem hiding this comment.
Partial review - just dropping comments early
| return {"unauthenticated": True} | ||
|
|
||
| @staticmethod | ||
| def authenticated(auth_claims: Dict[str, Any]) -> Dict[str, Any]: |
There was a problem hiding this comment.
is there a way to more strongly type this? In node we use this type:
/**
* Type representing the partial claims of a Firebase Auth token used to evaluate the
* Data Connect auth policy.
*/
export type AuthClaims = Partial<DecodedIdToken>;
There was a problem hiding this comment.
That's a good point! After taking a look at AuthClaims and DecodedIdToken, I used typing.TypedDict with total=False to mirror Partial<DecodedIdToken>, along with a nested FirebaseClaim type structure. Additionally, I annotated Impersonation.authenticated with Union[AuthClaims, Dict[str, Any]] so that type checkers provide full auto-completion for standard claims while still supporting arbitrary custom claim dictionaries:
class FirebaseClaim(TypedDict, total=False):
"""Provider-specific identity details and sign-in info within a decoded ID token."""
identities: Dict[str, Any]
sign_in_provider: str
sign_in_second_factor: str
second_factor_identifier: str
tenant: str
class AuthClaims(TypedDict, total=False):
"""Partial claims of a Firebase Auth token used for Data Connect policy evaluation."""
aud: str
auth_time: int
email: str
email_verified: bool
exp: int
firebase: FirebaseClaim
iat: int
iss: str
phone_number: str
picture: str
sub: str
uid: str
class Impersonation:
"""Represents impersonation configuration for DataConnect requests."""
@staticmethod
def unauthenticated() -> Dict[str, bool]:
"""Returns impersonation configuration for unauthenticated requests."""
return {"unauthenticated": True}
@staticmethod
def authenticated(auth_claims: Union[AuthClaims, Dict[str, Any]]) -> Dict[str, Any]:
"""Returns impersonation configuration for authenticated requests."""
return {"authClaims": auth_claims}`
| # Validate Variables against expected variable_type | ||
| variables = graphql_options.variables | ||
| if variables is not None and variable_type is not None: | ||
| if not isinstance(variables, variable_type): |
There was a problem hiding this comment.
isinstsance() can only be used to check that an object is an instance of a class (documentation)
Is it possible that a user passes in a type that's not a class or that will otherwise cause a false / throw an error in isinstance() even it really is the desired type?? after doing some research, it seems so:
- PEP 484 talks about runtime type erasure for generics
- there's also reddit and stackoverflow posts about for example it not working with
list[<some type>]orDict[str, any]
There's a bunch of solutions floating around on the internet / with AI, but I propose we ask Lahiru and/or Denver for advice
There was a problem hiding this comment.
Great catch! You're completely right; passing something like list[User] or Dict[str, Any] will crash isinstance() with a TypeError because of type erasure in Python. I've been seeing a bunch of solutions as well, like utilizing typing.get_origin() to extract the base class from a generic type, and then using typing.get_args()to complete a deep-check of the inner types. I think it's a great idea to ask Lahiru and/or Denver for advice on how to go about this matter.
There was a problem hiding this comment.
Note: this is a point of active discussion and there may be some API changes - for now, let's continue with the rest of the PR and project, and we'll make adjustments later as needed
stephenarosaj
left a comment
There was a problem hiding this comment.
partial review of main file - now for tests
| def _prepare_graphql_payload( | ||
| self, | ||
| graphql_query: str, | ||
| graphql_options: Optional[GraphqlOptions[Any]] |
There was a problem hiding this comment.
is it GraphqlOptions[Any]? shouldn't it be GraphqlOptions[_Variables]?
There was a problem hiding this comment.
I don't think we actually need _Variables here. Since _prepare_graphql_payload just serializes the options to a standard Dict[str, Any] and doesn't propagate the type to the return type, the generic type variable doesn't have an effect. I'm totally happy to switch it back if you'd prefer, though!"
…ost utility - Inherited Impersonation from dict to resolve the type-checking mismatch for static methods while maintaining dictionary behavior at runtime. - Updated GraphqlOptions.impersonate type annotation to accept Union[Impersonation, Dict[str, Any]] to support both helper and raw dictionary inputs. - Extracted generic variable and impersonate validation into separate helper methods _validate_variables_type and _validate_impersonation_options. - Shared emulator host extraction and validation logic in _utils.get_emulator_host and reused it across dataconnect and functions. - Removed AuthClaims and FirebaseClaim TypedDicts to keep them as raw dicts for now. - Renamed generic TypeVars to private naming conventions (_Data and _Variables).
Implemented three helper functions in
_DataConnectApiClientalong with their corresponding implementations:_validate_inputs: Validates the query structure, options, variables types, and custom impersonation claims._prepare_graphql_payload: Prepares the JSON payload for GraphQL calls, serializing variables (including nested dataclasses) and configuring optional extensions like impersonation._get_firebase_dataconnect_service_url: Formats the endpoint URL for execution, supporting both production host formats and local emulator host formats.Added comprehensive unit tests covering standard cases, invalid options, missing configurations, and emulator environments for each of the helper functions.