Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
usefulalgorithm committed Nov 7, 2024
1 parent 4dc2ec3 commit 5dc2b54
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/bigquery/data/tables/external.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"fields": [
{
"fieldName": "col1",
"fieldPath": "col1",
"nativeType": "INT",
"nullable": true
},
{
"fieldName": "col2",
"fieldPath": "col2",
"nativeType": "ARRAY<STRUCT>",
"nullable": false,
"subfields": [
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "ARRAY<TEXT>",
"nullable": false
},
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "BOOL",
"nullable": true
},
{
"fieldName": "col2_3",
"fieldPath": "col2.col2_3",
"nativeType": "RECORD",
"nullable": true
}
]
}
],
"schemaType": "SQL",
"sqlSchema": {
"materialization": "EXTERNAL"
}
}
]
43 changes: 43 additions & 0 deletions tests/bigquery/data/tables/materialized_view.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"fields": [
{
"fieldName": "col1",
"fieldPath": "col1",
"nativeType": "INT",
"nullable": true
},
{
"fieldName": "col2",
"fieldPath": "col2",
"nativeType": "ARRAY<STRUCT>",
"nullable": false,
"subfields": [
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "ARRAY<TEXT>",
"nullable": false
},
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "BOOL",
"nullable": true
},
{
"fieldName": "col2_3",
"fieldPath": "col2.col2_3",
"nativeType": "RECORD",
"nullable": true
}
]
}
],
"schemaType": "SQL",
"sqlSchema": {
"materialization": "MATERIALIZED_VIEW",
"tableSchema": "CREATE VIEW AS SELECT * FROM my-project.dataset.upstream"
}
}
]
43 changes: 43 additions & 0 deletions tests/bigquery/data/tables/snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"fields": [
{
"fieldName": "col1",
"fieldPath": "col1",
"nativeType": "INT",
"nullable": true
},
{
"fieldName": "col2",
"fieldPath": "col2",
"nativeType": "ARRAY<STRUCT>",
"nullable": false,
"subfields": [
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "ARRAY<TEXT>",
"nullable": false
},
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "BOOL",
"nullable": true
},
{
"fieldName": "col2_3",
"fieldPath": "col2.col2_3",
"nativeType": "RECORD",
"nullable": true
}
]
}
],
"schemaType": "SQL",
"sqlSchema": {
"materialization": "SNAPSHOT",
"snapshotTime": "2024-11-05T00:00:00+00:00"
}
}
]
42 changes: 42 additions & 0 deletions tests/bigquery/data/tables/table.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"fields": [
{
"fieldName": "col1",
"fieldPath": "col1",
"nativeType": "INT",
"nullable": true
},
{
"fieldName": "col2",
"fieldPath": "col2",
"nativeType": "ARRAY<STRUCT>",
"nullable": false,
"subfields": [
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "ARRAY<TEXT>",
"nullable": false
},
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "BOOL",
"nullable": true
},
{
"fieldName": "col2_3",
"fieldPath": "col2.col2_3",
"nativeType": "RECORD",
"nullable": true
}
]
}
],
"schemaType": "SQL",
"sqlSchema": {
"materialization": "TABLE"
}
}
]
43 changes: 43 additions & 0 deletions tests/bigquery/data/tables/view.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"fields": [
{
"fieldName": "col1",
"fieldPath": "col1",
"nativeType": "INT",
"nullable": true
},
{
"fieldName": "col2",
"fieldPath": "col2",
"nativeType": "ARRAY<STRUCT>",
"nullable": false,
"subfields": [
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "ARRAY<TEXT>",
"nullable": false
},
{
"fieldName": "col2_1",
"fieldPath": "col2.col2_1",
"nativeType": "BOOL",
"nullable": true
},
{
"fieldName": "col2_3",
"fieldPath": "col2.col2_3",
"nativeType": "RECORD",
"nullable": true
}
]
}
],
"schemaType": "SQL",
"sqlSchema": {
"materialization": "VIEW",
"tableSchema": "CREATE VIEW AS SELECT * FROM my-project.dataset.upstream"
}
}
]
58 changes: 58 additions & 0 deletions tests/bigquery/test_table_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from pathlib import Path

import pytest
from google.cloud import bigquery

from metaphor.bigquery.table import TableExtractor
from tests.test_utils import load_json


@pytest.mark.parametrize(
"table_type",
[
"TABLE",
"VIEW",
"MATERIALIZED_VIEW",
"EXTERNAL",
"SNAPSHOT",
],
)
def test_extract_table(test_root_dir: str, table_type: str):
table_ref = bigquery.TableReference(
dataset_ref=bigquery.DatasetReference(
project="project",
dataset_id="dataset1",
),
table_id="table1",
)
table = bigquery.Table(
table_ref=table_ref,
schema=[
bigquery.SchemaField("col1", "INT"),
bigquery.SchemaField(
"col2",
"STRUCT",
mode="REPEATED",
fields=[
bigquery.SchemaField("col2_1", "TEXT", mode="REPEATED"),
bigquery.SchemaField("col2_1", "BOOL", mode="NULLABLE"),
bigquery.SchemaField("col2_3", "RECORD", mode="NULLABLE"),
],
),
],
)
table._properties["type"] = table_type # type: ignore
table.mview_query = "CREATE VIEW AS SELECT * FROM my-project.dataset.upstream"
table.view_query = "CREATE VIEW AS SELECT * FROM my-project.dataset.upstream"
table._properties["snapshotDefinition"] = {
"snapshotTime": "2024-11-05T00:00:00.000Z"
} # :(
schema = TableExtractor.extract_schema(table).to_dict()
expected = (
Path(test_root_dir)
/ "bigquery"
/ "data"
/ "tables"
/ f"{table_type.lower()}.json"
)
assert [schema] == load_json(expected)

0 comments on commit 5dc2b54

Please sign in to comment.