Skip to content

Commit

Permalink
Tests for handling hidden tables (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline committed Jul 4, 2024
1 parent 0d36ef2 commit 9edebe0
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 84 deletions.
Binary file modified sandbox/metabase/metabase.db.mv.db
Binary file not shown.
2 changes: 2 additions & 0 deletions sandbox/models/staging/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: 2

models:
- name: stg_customers
meta:
metabase.visibility_type: hidden
columns:
- name: customer_id
data_tests:
Expand Down
42 changes: 31 additions & 11 deletions tests/_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dotenv import dotenv_values

from dbtmetabase.core import DbtMetabase
from dbtmetabase.manifest import Manifest, Model
from dbtmetabase.manifest import Column, Manifest, Model
from dbtmetabase.metabase import Metabase

FIXTURES_PATH = Path("tests") / "fixtures"
Expand Down Expand Up @@ -50,28 +50,30 @@ def _api(
params: Optional[Dict[str, Any]] = None,
**kwargs,
) -> Union[Mapping, Sequence]:
result = {}
path_toks = f"{path.lstrip('/')}.json".split("/")
json_path = Path.joinpath(FIXTURES_PATH, *path_toks)

if self.record:
resp = super()._api(method, path, params, **kwargs)

if method == "get":
json_path.parent.mkdir(parents=True, exist_ok=True)
with open(json_path, "w", encoding="utf-8") as f:
json.dump(resp, f, indent=4)

return resp
is_auth = path == "/api/session"
if method == "get" or is_auth:
result = super()._api(method, path, params, **kwargs)

if not is_auth:
json_path.parent.mkdir(parents=True, exist_ok=True)
with open(json_path, "w", encoding="utf-8") as f:
json.dump(result, f, indent=4)
else:
if method == "get":
if json_path.exists():
with open(json_path, encoding="utf-8") as f:
return json.load(f)
result = json.load(f)
else:
response = requests.Response()
response.status_code = 404
raise requests.exceptions.HTTPError(response=response)
return {}

return result


class MockManifest(Manifest):
Expand All @@ -82,6 +84,24 @@ def read_models(self) -> Sequence[Model]:
self._models = super().read_models()
return self._models

def find_model(self, model_name: str) -> Optional[Model]:
filtered = [m for m in self._models if m.name == model_name]
if filtered:
return filtered[0]
return None

def find_column(
self,
model_name: str,
column_name: str,
) -> Optional[Column]:
model = self.find_model(model_name=model_name)
if model:
filtered = [c for c in model.columns if c.name == column_name]
if filtered:
return filtered[0]
return None


class MockDbtMetabase(DbtMetabase):
def __init__(
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/api/card/27.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cache_invalidated_at": null,
"description": "Orders and customers",
"archived": false,
"view_count": 361,
"view_count": 389,
"collection_position": null,
"table_id": 10,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -561,7 +561,7 @@
"creator": {
"email": "dbtmetabase@example.com",
"first_name": "dbtmetabase",
"last_login": "2024-06-20T06:03:33.519227Z",
"last_login": "2024-07-04T05:55:25.914119Z",
"is_qbnewb": false,
"is_superuser": true,
"id": 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/api/card/28.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cache_invalidated_at": null,
"description": null,
"archived": false,
"view_count": 180,
"view_count": 194,
"collection_position": null,
"table_id": 10,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -549,7 +549,7 @@
"creator": {
"email": "dbtmetabase@example.com",
"first_name": "dbtmetabase",
"last_login": "2024-06-20T06:03:33.519227Z",
"last_login": "2024-07-04T05:55:25.914119Z",
"is_qbnewb": false,
"is_superuser": true,
"id": 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/api/card/29.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cache_invalidated_at": null,
"description": null,
"archived": false,
"view_count": 179,
"view_count": 193,
"collection_position": null,
"table_id": null,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -41,7 +41,7 @@
"creator": {
"email": "dbtmetabase@example.com",
"first_name": "dbtmetabase",
"last_login": "2024-06-20T06:03:33.519227Z",
"last_login": "2024-07-04T05:55:25.914119Z",
"is_qbnewb": false,
"is_superuser": true,
"id": 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/api/card/30.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cache_invalidated_at": null,
"description": "Dummy 1",
"archived": false,
"view_count": 21,
"view_count": 28,
"collection_position": null,
"table_id": null,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -41,7 +41,7 @@
"creator": {
"email": "dbtmetabase@example.com",
"first_name": "dbtmetabase",
"last_login": "2024-06-20T06:03:33.519227Z",
"last_login": "2024-07-04T05:55:25.914119Z",
"is_qbnewb": false,
"is_superuser": true,
"id": 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/api/card/31.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cache_invalidated_at": null,
"description": "Dummy 2",
"archived": false,
"view_count": 21,
"view_count": 28,
"collection_position": null,
"table_id": null,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -41,7 +41,7 @@
"creator": {
"email": "dbtmetabase@example.com",
"first_name": "dbtmetabase",
"last_login": "2024-06-20T06:03:33.519227Z",
"last_login": "2024-07-04T05:55:25.914119Z",
"is_qbnewb": false,
"is_superuser": true,
"id": 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/api/card/32.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cache_invalidated_at": null,
"description": "CTE SQL",
"archived": false,
"view_count": 12,
"view_count": 19,
"collection_position": null,
"table_id": null,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -276,7 +276,7 @@
"creator": {
"email": "dbtmetabase@example.com",
"first_name": "dbtmetabase",
"last_login": "2024-06-20T06:03:33.519227Z",
"last_login": "2024-07-04T05:55:25.914119Z",
"is_qbnewb": false,
"is_superuser": true,
"id": 1,
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/api/dashboard/2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"description": "Dashboard is a dashboard is a dashboard",
"archived": false,
"view_count": 96,
"view_count": 103,
"collection_position": null,
"dashcards": [
{
Expand All @@ -14,7 +14,7 @@
"cache_invalidated_at": null,
"description": "Orders and customers",
"archived": false,
"view_count": 360,
"view_count": 388,
"collection_position": null,
"table_id": 10,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -654,7 +654,7 @@
"cache_invalidated_at": null,
"description": null,
"archived": false,
"view_count": 180,
"view_count": 194,
"collection_position": null,
"table_id": 10,
"can_run_adhoc_query": true,
Expand Down Expand Up @@ -1268,7 +1268,7 @@
"cache_invalidated_at": null,
"description": null,
"archived": false,
"view_count": 179,
"view_count": 193,
"collection_position": null,
"table_id": null,
"can_run_adhoc_query": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/api/database.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"caveats": null,
"creator_id": 1,
"is_full_sync": true,
"updated_at": "2024-06-20T05:33:42.131951Z",
"updated_at": "2024-07-04T05:54:51.198536Z",
"native_permissions": "write",
"cache_ttl": null,
"details": {
Expand Down
20 changes: 10 additions & 10 deletions tests/fixtures/api/database/2/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@
],
"caveats": null,
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:52.259601Z",
"active": true,
"id": 12,
"db_id": 2,
Expand Down Expand Up @@ -937,7 +937,7 @@
],
"caveats": "Some facts are derived from payments",
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:52.259601Z",
"active": true,
"id": 10,
"db_id": 2,
Expand Down Expand Up @@ -1099,7 +1099,7 @@
],
"caveats": null,
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:52.259601Z",
"active": true,
"id": 13,
"db_id": 2,
Expand Down Expand Up @@ -1309,7 +1309,7 @@
],
"caveats": null,
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:52.259601Z",
"active": true,
"id": 14,
"db_id": 2,
Expand Down Expand Up @@ -1523,7 +1523,7 @@
],
"caveats": null,
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:52.259601Z",
"active": true,
"id": 11,
"db_id": 2,
Expand Down Expand Up @@ -1700,11 +1700,11 @@
],
"caveats": null,
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:57.125982Z",
"active": true,
"id": 16,
"db_id": 2,
"visibility_type": null,
"visibility_type": "hidden",
"field_order": "custom",
"is_upload": false,
"initial_sync_status": "complete",
Expand Down Expand Up @@ -1925,7 +1925,7 @@
],
"caveats": null,
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:52.259601Z",
"active": true,
"id": 15,
"db_id": 2,
Expand Down Expand Up @@ -2154,7 +2154,7 @@
],
"caveats": null,
"segments": [],
"updated_at": "2024-06-20T06:03:34.567961Z",
"updated_at": "2024-07-04T05:54:52.259601Z",
"active": true,
"id": 9,
"db_id": 2,
Expand All @@ -2171,7 +2171,7 @@
],
"creator_id": 1,
"is_full_sync": true,
"updated_at": "2024-06-20T05:33:42.131951Z",
"updated_at": "2024-07-04T05:54:51.198536Z",
"cache_ttl": null,
"details": {
"ssl": false,
Expand Down
Loading

0 comments on commit 9edebe0

Please sign in to comment.