Skip to content

Commit

Permalink
Merge pull request #9196 from khoaguin/bigquery-mock-client
Browse files Browse the repository at this point in the history
Create mock bigquery client so bigquery API endpoints can be run without a real API
  • Loading branch information
madhavajay authored Aug 28, 2024
2 parents 1f087e0 + cb0a9ba commit df0c75d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 33 deletions.
14 changes: 6 additions & 8 deletions notebooks/scenarios/bigquery/01-setup-datasite.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,14 @@
"source": [
"# !docker image ls | grep bigquery"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
Expand All @@ -276,7 +274,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
14 changes: 6 additions & 8 deletions notebooks/scenarios/bigquery/02-configure-api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -755,16 +755,14 @@
"source": [
"server.land()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
Expand All @@ -775,7 +773,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
14 changes: 6 additions & 8 deletions notebooks/scenarios/bigquery/03-ds-submit-request.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,14 @@
"source": [
"server.land()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
Expand All @@ -243,7 +241,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
14 changes: 6 additions & 8 deletions notebooks/scenarios/bigquery/04-do-review-requests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,14 @@
"source": [
"server.land()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
Expand All @@ -263,7 +261,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
7 changes: 6 additions & 1 deletion notebooks/scenarios/bigquery/05-ds-get-results.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
Expand All @@ -138,7 +143,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
45 changes: 45 additions & 0 deletions packages/syft/src/syft/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import types
from types import ModuleType
from typing import Any
from unittest.mock import Mock

# third party
from IPython.display import display
Expand Down Expand Up @@ -1143,3 +1144,47 @@ def repr_truncation(obj: Any, max_elements: int = 10) -> str:
r.maxother = 100 # For other objects

return r.repr(obj)


class MockBigQueryError(Exception):
def __init__(self, errors: list) -> None:
self._errors = errors
super().__init__(self._errors[0]["message"])


class MockBigQueryClient:
def __init__(self, credentials: dict, location: str | None = None) -> None:
self.credentials = credentials
self.location = location

def query_and_wait(self, sql_query: str, project: str | None = None) -> Mock | None:
if self.credentials["mock_result"] == "timeout":
raise TimeoutError("Simulated query timeout.")

if self.credentials["mock_result"] == "success":
# Simulate a successful response
rows = Mock()
rows.total_rows = 1 # or any number within acceptable limits
rows.to_dataframe = Mock(return_value="Simulated DataFrame")
return rows

if self.credentials["mock_result"] == "bigquery_error":
errors = [
{
"reason": "Simulated BigQuery error.",
"message": "Simulated BigQuery error.",
}
]
raise MockBigQueryError(errors)

raise Exception("Simulated non-BigQuery exception.")


class MockBigQuery:
@staticmethod
def mock_credentials(mock_result: str = "success") -> dict:
return {"mocked": "credentials", "mock_result": mock_result}

@staticmethod
def Client(credentials: dict, location: str | None = None) -> MockBigQueryClient:
return MockBigQueryClient(credentials, location)

0 comments on commit df0c75d

Please sign in to comment.