2021-05-13 21:42:53 +03:00
|
|
|
import logging
|
2021-09-16 23:09:45 -07:00
|
|
|
import pathlib
|
2021-05-13 21:42:53 +03:00
|
|
|
import sys
|
2021-09-16 23:09:45 -07:00
|
|
|
from typing import Any
|
|
|
|
from unittest import mock
|
2021-05-13 21:42:53 +03:00
|
|
|
|
|
|
|
import pytest
|
2021-07-29 20:04:40 -07:00
|
|
|
from freezegun import freeze_time
|
2021-09-16 23:09:45 -07:00
|
|
|
from looker_sdk.sdk.api31.models import DBConnection
|
2021-05-13 21:42:53 +03:00
|
|
|
|
2021-10-03 20:04:26 -07:00
|
|
|
from datahub.configuration.common import PipelineExecutionError
|
2021-05-13 21:42:53 +03:00
|
|
|
from datahub.ingestion.run.pipeline import Pipeline
|
2021-10-14 01:16:35 -07:00
|
|
|
from tests.test_helpers import mce_helpers # noqa: F401
|
2021-05-13 21:42:53 +03:00
|
|
|
|
|
|
|
logging.getLogger("lkml").setLevel(logging.INFO)
|
|
|
|
|
2021-09-16 23:09:45 -07:00
|
|
|
|
2021-07-29 20:04:40 -07:00
|
|
|
FROZEN_TIME = "2020-04-14 07:00:00"
|
2021-05-13 21:42:53 +03:00
|
|
|
|
2021-07-29 20:04:40 -07:00
|
|
|
|
|
|
|
@freeze_time(FROZEN_TIME)
|
2021-05-13 21:42:53 +03:00
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="lkml requires Python 3.7+")
|
|
|
|
def test_lookml_ingest(pytestconfig, tmp_path, mock_time):
|
2021-09-16 23:09:45 -07:00
|
|
|
"""Test backwards compatibility with previous form of config with new flags turned off"""
|
2021-05-13 21:42:53 +03:00
|
|
|
test_resources_dir = pytestconfig.rootpath / "tests/integration/lookml"
|
2021-09-16 23:09:45 -07:00
|
|
|
mce_out_file = "expected_output.json"
|
2021-05-13 21:42:53 +03:00
|
|
|
|
2021-09-16 23:09:45 -07:00
|
|
|
# Note this config below is known to create "bad" lineage since the config author has not provided enough information
|
|
|
|
# to resolve relative table names (which are not fully qualified)
|
|
|
|
# We keep this check just to validate that ingestion doesn't croak on this config
|
2021-05-13 21:42:53 +03:00
|
|
|
pipeline = Pipeline.create(
|
|
|
|
{
|
|
|
|
"run_id": "lookml-test",
|
|
|
|
"source": {
|
|
|
|
"type": "lookml",
|
|
|
|
"config": {
|
2021-07-16 11:59:50 -07:00
|
|
|
"base_folder": str(test_resources_dir / "lkml_samples"),
|
2021-05-13 21:42:53 +03:00
|
|
|
"connection_to_platform_map": {"my_connection": "conn"},
|
|
|
|
"parse_table_names_from_sql": True,
|
2021-09-16 23:09:45 -07:00
|
|
|
"tag_measures_and_dimensions": False,
|
|
|
|
"project_name": "lkml_samples",
|
2021-05-13 21:42:53 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"sink": {
|
|
|
|
"type": "file",
|
|
|
|
"config": {
|
2021-09-16 23:09:45 -07:00
|
|
|
"filename": f"{tmp_path}/{mce_out_file}",
|
2021-05-13 21:42:53 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
pipeline.run()
|
2021-07-16 11:59:50 -07:00
|
|
|
pipeline.pretty_print_summary()
|
|
|
|
pipeline.raise_from_status(raise_warnings=True)
|
2021-05-13 21:42:53 +03:00
|
|
|
|
2021-10-14 18:09:32 -07:00
|
|
|
mce_helpers.check_golden_file(
|
|
|
|
pytestconfig,
|
|
|
|
output_path=tmp_path / mce_out_file,
|
|
|
|
golden_path=test_resources_dir / mce_out_file,
|
|
|
|
)
|
2021-09-16 23:09:45 -07:00
|
|
|
|
|
|
|
|
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="lkml requires Python 3.7+")
|
|
|
|
def test_lookml_ingest_offline(pytestconfig, tmp_path, mock_time):
|
|
|
|
"""New form of config with offline specification of connection defaults"""
|
|
|
|
test_resources_dir = pytestconfig.rootpath / "tests/integration/lookml"
|
|
|
|
mce_out = "lookml_mces_offline.json"
|
|
|
|
pipeline = Pipeline.create(
|
|
|
|
{
|
|
|
|
"run_id": "lookml-test",
|
|
|
|
"source": {
|
|
|
|
"type": "lookml",
|
|
|
|
"config": {
|
|
|
|
"base_folder": str(test_resources_dir / "lkml_samples"),
|
|
|
|
"connection_to_platform_map": {
|
|
|
|
"my_connection": {
|
|
|
|
"platform": "snowflake",
|
|
|
|
"default_db": "default_db",
|
|
|
|
"default_schema": "default_schema",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"parse_table_names_from_sql": True,
|
|
|
|
"project_name": "lkml_samples",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"sink": {
|
|
|
|
"type": "file",
|
|
|
|
"config": {
|
|
|
|
"filename": f"{tmp_path}/{mce_out}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-05-13 21:42:53 +03:00
|
|
|
)
|
2021-09-16 23:09:45 -07:00
|
|
|
pipeline.run()
|
|
|
|
pipeline.pretty_print_summary()
|
|
|
|
pipeline.raise_from_status(raise_warnings=True)
|
|
|
|
|
2021-10-14 18:09:32 -07:00
|
|
|
mce_helpers.check_golden_file(
|
2022-01-27 15:31:25 -08:00
|
|
|
pytestconfig,
|
|
|
|
output_path=tmp_path / mce_out,
|
|
|
|
golden_path=test_resources_dir / mce_out,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="lkml requires Python 3.7+")
|
|
|
|
def test_lookml_ingest_offline_platform_instance(pytestconfig, tmp_path, mock_time):
|
|
|
|
"""New form of config with offline specification of connection defaults"""
|
|
|
|
test_resources_dir = pytestconfig.rootpath / "tests/integration/lookml"
|
|
|
|
mce_out = "lookml_mces_offline_platform_instance.json"
|
|
|
|
pipeline = Pipeline.create(
|
|
|
|
{
|
|
|
|
"run_id": "lookml-test",
|
|
|
|
"source": {
|
|
|
|
"type": "lookml",
|
|
|
|
"config": {
|
|
|
|
"base_folder": str(test_resources_dir / "lkml_samples"),
|
|
|
|
"connection_to_platform_map": {
|
|
|
|
"my_connection": {
|
|
|
|
"platform": "snowflake",
|
|
|
|
"platform_instance": "warehouse",
|
|
|
|
"platform_env": "dev",
|
|
|
|
"default_db": "default_db",
|
|
|
|
"default_schema": "default_schema",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"parse_table_names_from_sql": True,
|
|
|
|
"project_name": "lkml_samples",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"sink": {
|
|
|
|
"type": "file",
|
|
|
|
"config": {
|
|
|
|
"filename": f"{tmp_path}/{mce_out}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
pipeline.run()
|
|
|
|
pipeline.pretty_print_summary()
|
|
|
|
pipeline.raise_from_status(raise_warnings=True)
|
|
|
|
|
|
|
|
mce_helpers.check_golden_file(
|
2021-10-14 18:09:32 -07:00
|
|
|
pytestconfig,
|
|
|
|
output_path=tmp_path / mce_out,
|
|
|
|
golden_path=test_resources_dir / mce_out,
|
|
|
|
)
|
2021-09-16 23:09:45 -07:00
|
|
|
|
|
|
|
|
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="lkml requires Python 3.7+")
|
2021-09-19 17:28:44 -07:00
|
|
|
def test_lookml_ingest_api_bigquery(pytestconfig, tmp_path, mock_time):
|
2021-09-16 23:09:45 -07:00
|
|
|
# test with BigQuery connection
|
|
|
|
ingestion_test(
|
|
|
|
pytestconfig,
|
|
|
|
tmp_path,
|
|
|
|
mock_time,
|
|
|
|
DBConnection(
|
|
|
|
dialect_name="bigquery", host="project-foo", database="default-db"
|
|
|
|
),
|
|
|
|
)
|
2021-09-19 17:28:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="lkml requires Python 3.7+")
|
|
|
|
def test_lookml_ingest_api_hive(pytestconfig, tmp_path, mock_time):
|
2021-09-16 23:09:45 -07:00
|
|
|
# test with Hive connection
|
|
|
|
ingestion_test(
|
|
|
|
pytestconfig,
|
|
|
|
tmp_path,
|
|
|
|
mock_time,
|
|
|
|
DBConnection(
|
|
|
|
dialect_name="hive2",
|
|
|
|
database="default-hive-db",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def ingestion_test(
|
|
|
|
pytestconfig: Any,
|
|
|
|
tmp_path: pathlib.Path,
|
|
|
|
mock_time: int,
|
|
|
|
mock_connection: DBConnection,
|
|
|
|
) -> None: # noqa : No need for type annotations here
|
|
|
|
test_resources_dir = pytestconfig.rootpath / "tests/integration/lookml"
|
|
|
|
mce_out_file = f"lookml_mces_api_{mock_connection.dialect_name}.json"
|
|
|
|
mocked_client = mock.MagicMock()
|
|
|
|
mock_model = mock.MagicMock(project_name="lkml_samples")
|
|
|
|
with mock.patch("looker_sdk.init31") as mock_sdk:
|
|
|
|
mock_sdk.return_value = mocked_client
|
|
|
|
# mock_connection = mock.MagicMock()
|
|
|
|
mocked_client.connection.return_value = mock_connection
|
|
|
|
mocked_client.lookml_model.return_value = mock_model
|
|
|
|
|
|
|
|
pipeline = Pipeline.create(
|
|
|
|
{
|
|
|
|
"run_id": "lookml-test",
|
|
|
|
"source": {
|
|
|
|
"type": "lookml",
|
|
|
|
"config": {
|
|
|
|
"base_folder": str(test_resources_dir / "lkml_samples"),
|
|
|
|
"api": {
|
|
|
|
"client_id": "fake_client_id",
|
|
|
|
"client_secret": "fake_secret",
|
|
|
|
"base_url": "fake_account.looker.com",
|
|
|
|
},
|
|
|
|
"parse_table_names_from_sql": True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"sink": {
|
|
|
|
"type": "file",
|
|
|
|
"config": {
|
|
|
|
"filename": f"{tmp_path}/{mce_out_file}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
pipeline.run()
|
|
|
|
pipeline.pretty_print_summary()
|
|
|
|
pipeline.raise_from_status(raise_warnings=True)
|
|
|
|
|
2021-10-14 18:09:32 -07:00
|
|
|
mce_helpers.check_golden_file(
|
|
|
|
pytestconfig,
|
|
|
|
output_path=tmp_path / mce_out_file,
|
|
|
|
golden_path=test_resources_dir / mce_out_file,
|
|
|
|
)
|
2021-10-03 20:04:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="lkml requires Python 3.7+")
|
|
|
|
def test_lookml_bad_sql_parser(pytestconfig, tmp_path, mock_time):
|
|
|
|
"""Incorrect specification of sql parser should not fail ingestion"""
|
|
|
|
test_resources_dir = pytestconfig.rootpath / "tests/integration/lookml"
|
|
|
|
mce_out = "lookml_mces_badsql_parser.json"
|
|
|
|
pipeline = Pipeline.create(
|
|
|
|
{
|
|
|
|
"run_id": "lookml-test",
|
|
|
|
"source": {
|
|
|
|
"type": "lookml",
|
|
|
|
"config": {
|
|
|
|
"base_folder": str(test_resources_dir / "lkml_samples"),
|
|
|
|
"connection_to_platform_map": {
|
|
|
|
"my_connection": {
|
|
|
|
"platform": "snowflake",
|
|
|
|
"default_db": "default_db",
|
|
|
|
"default_schema": "default_schema",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"parse_table_names_from_sql": True,
|
|
|
|
"project_name": "lkml_samples",
|
|
|
|
"sql_parser": "bad.sql.Parser",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"sink": {
|
|
|
|
"type": "file",
|
|
|
|
"config": {
|
|
|
|
"filename": f"{tmp_path}/{mce_out}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
pipeline.run()
|
|
|
|
pipeline.pretty_print_summary()
|
|
|
|
pipeline.raise_from_status(raise_warnings=False)
|
|
|
|
try:
|
|
|
|
pipeline.raise_from_status(raise_warnings=True)
|
|
|
|
assert False, "Pipeline should have generated warnings"
|
|
|
|
except PipelineExecutionError:
|
|
|
|
pass
|
|
|
|
|
2021-10-14 18:09:32 -07:00
|
|
|
mce_helpers.check_golden_file(
|
|
|
|
pytestconfig,
|
|
|
|
output_path=tmp_path / mce_out,
|
|
|
|
golden_path=test_resources_dir / mce_out,
|
|
|
|
)
|
2021-10-06 07:13:38 -07:00
|
|
|
|
|
|
|
|
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 7), reason="lkml requires Python 3.7+")
|
|
|
|
def test_lookml_github_info(pytestconfig, tmp_path, mock_time):
|
|
|
|
"""Add github info to config"""
|
|
|
|
test_resources_dir = pytestconfig.rootpath / "tests/integration/lookml"
|
|
|
|
mce_out = "lookml_mces_with_external_urls.json"
|
|
|
|
pipeline = Pipeline.create(
|
|
|
|
{
|
|
|
|
"run_id": "lookml-test",
|
|
|
|
"source": {
|
|
|
|
"type": "lookml",
|
|
|
|
"config": {
|
|
|
|
"base_folder": str(test_resources_dir / "lkml_samples"),
|
|
|
|
"connection_to_platform_map": {
|
|
|
|
"my_connection": {
|
|
|
|
"platform": "snowflake",
|
|
|
|
"default_db": "default_db",
|
|
|
|
"default_schema": "default_schema",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"parse_table_names_from_sql": True,
|
|
|
|
"project_name": "lkml_samples",
|
|
|
|
"github_info": {"repo": "datahub/looker-demo", "branch": "master"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"sink": {
|
|
|
|
"type": "file",
|
|
|
|
"config": {
|
|
|
|
"filename": f"{tmp_path}/{mce_out}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
pipeline.run()
|
|
|
|
pipeline.pretty_print_summary()
|
|
|
|
pipeline.raise_from_status(raise_warnings=True)
|
|
|
|
|
2021-10-14 18:09:32 -07:00
|
|
|
mce_helpers.check_golden_file(
|
|
|
|
pytestconfig,
|
|
|
|
output_path=tmp_path / mce_out,
|
|
|
|
golden_path=test_resources_dir / mce_out,
|
|
|
|
)
|