-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
160 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,50 @@ | ||
from google.cloud.firestore_v1.query_profile import ( | ||
ExplainMetrics, | ||
ExplainOptions, | ||
QueryExplainError, | ||
) | ||
|
||
|
||
from typing import List, Optional, TypeVar | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class QueryResultsList(list): | ||
"""A list of received query results from the query call. | ||
This is a subclass of the built-in list. A new property `explain_metrics` | ||
is added to return the query profile results. | ||
Args: | ||
docs (list[T]): | ||
The list of query results. | ||
explain_options | ||
(Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]): | ||
Options to enable query profiling for this query. When set, | ||
explain_metrics will be available on the returned generator. | ||
explain_metrics (Optional[ExplainMetrics]): | ||
Query profile results. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
docs: List[T], | ||
explain_options: Optional[ExplainOptions] = None, | ||
explain_metrics: Optional[ExplainMetrics] = None, | ||
): | ||
super().__init__(docs) | ||
self._explain_options = explain_options | ||
self._explain_metrics = explain_metrics | ||
|
||
@property | ||
def explain_options(self): | ||
return self._explain_options | ||
|
||
@property | ||
def explain_metrics(self): | ||
if self._explain_options is None: | ||
raise QueryExplainError("explain_options not set on query.") | ||
else: | ||
return self._explain_metrics |
This file contains 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 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 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 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,102 @@ | ||
# Copyright 2020 Google LLC All rights reserved. | ||
# | ||
# 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 | ||
# | ||
# http://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. | ||
|
||
|
||
import mock | ||
|
||
|
||
def _make_base_document_reference(*args, **kwargs): | ||
from google.cloud.firestore_v1.base_document import BaseDocumentReference | ||
|
||
return BaseDocumentReference(*args, **kwargs) | ||
|
||
|
||
def _make_document_snapshot(*args, **kwargs): | ||
from google.cloud.firestore_v1.document import DocumentSnapshot | ||
|
||
return DocumentSnapshot(*args, **kwargs) | ||
|
||
|
||
def _make_query_results_list(*args, **kwargs): | ||
from google.cloud.firestore_v1.query_results import QueryResultsList | ||
|
||
return QueryResultsList(*args, **kwargs) | ||
|
||
|
||
def _make_explain_metrics(): | ||
from google.cloud.firestore_v1.query_profile import ExplainMetrics, PlanSummary | ||
|
||
plan_summary = PlanSummary( | ||
indexes_used=[{"properties": "(__name__ ASC)", "query_scope": "Collection"}], | ||
) | ||
return ExplainMetrics(plan_summary=plan_summary) | ||
|
||
|
||
def test_query_results_list_constructor(): | ||
from google.cloud.firestore_v1.query_profile import ExplainOptions | ||
|
||
client = mock.sentinel.client | ||
reference = _make_base_document_reference("hi", "bye", client=client) | ||
data_1 = {"zoop": 83} | ||
data_2 = {"zoop": 30} | ||
snapshot_1 = _make_document_snapshot( | ||
reference, | ||
data_1, | ||
True, | ||
mock.sentinel.read_time, | ||
mock.sentinel.create_time, | ||
mock.sentinel.update_time, | ||
) | ||
snapshot_2 = _make_document_snapshot( | ||
reference, | ||
data_2, | ||
True, | ||
mock.sentinel.read_time, | ||
mock.sentinel.create_time, | ||
mock.sentinel.update_time, | ||
) | ||
explain_metrics = _make_explain_metrics() | ||
explain_options = ExplainOptions(analyze=True) | ||
snapshot_list = _make_query_results_list( | ||
[snapshot_1, snapshot_2], | ||
explain_options=explain_options, | ||
explain_metrics=explain_metrics, | ||
) | ||
assert len(snapshot_list) == 2 | ||
assert snapshot_list[0] == snapshot_1 | ||
assert snapshot_list[1] == snapshot_2 | ||
assert snapshot_list._explain_options == explain_options | ||
assert snapshot_list._explain_metrics == explain_metrics | ||
|
||
|
||
def test_query_results_list_explain_options(): | ||
from google.cloud.firestore_v1.query_profile import ExplainOptions | ||
|
||
explain_options = ExplainOptions(analyze=True) | ||
snapshot_list = _make_query_results_list([], explain_options=explain_options) | ||
|
||
assert snapshot_list.explain_options == explain_options | ||
|
||
|
||
def test_query_results_list_explain_metrics_w_explain_options(): | ||
from google.cloud.firestore_v1.query_profile import ExplainOptions | ||
|
||
explain_metrics = _make_explain_metrics() | ||
snapshot_list = _make_query_results_list( | ||
[], | ||
explain_options=ExplainOptions(analyze=True), | ||
explain_metrics=explain_metrics, | ||
) | ||
|
||
assert snapshot_list.explain_metrics == explain_metrics |