test_run_hooks.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License in the LICENSE file at the
  6. # root of this repository, or online at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import pytest
  17. from dbt.tests.adapter.hooks.test_run_hooks import (
  18. BaseAfterRunHooks,
  19. BasePrePostRunHooks,
  20. )
  21. from dbt.tests.util import run_dbt
  22. from dbt_common.exceptions import DbtDatabaseError
  23. from fixtures import run_hook, test_run_operation
  24. class TestPrePostRunHooksMaterialize(BasePrePostRunHooks):
  25. @pytest.fixture(scope="function")
  26. def setUp(self, project):
  27. project.run_sql(f"drop table if exists { project.test_schema }.on_run_hook")
  28. project.run_sql(run_hook.format(schema=project.test_schema))
  29. project.run_sql(f"drop table if exists { project.test_schema }.schemas")
  30. project.run_sql(f"drop table if exists { project.test_schema }.db_schemas")
  31. os.environ["TERM_TEST"] = "TESTING"
  32. @pytest.fixture(scope="class")
  33. def project_config_update(self):
  34. return test_run_operation
  35. def check_hooks(self, state, project, host):
  36. ctx = self.get_ctx_vars(state, project)
  37. assert ctx["test_state"] == state
  38. assert ctx["target_dbname"] == "materialize"
  39. assert ctx["target_host"] == "materialized"
  40. assert ctx["target_name"] == "default"
  41. assert ctx["target_schema"] == project.test_schema
  42. assert ctx["target_threads"] == 1
  43. assert ctx["target_type"] == project.adapter_type
  44. assert (
  45. ctx["run_started_at"] is not None and len(ctx["run_started_at"]) > 0
  46. ), "run_started_at was not set"
  47. assert (
  48. ctx["invocation_id"] is not None and len(ctx["invocation_id"]) > 0
  49. ), "invocation_id was not set"
  50. class TestAfterRunHooksMaterialize(BaseAfterRunHooks):
  51. def test_missing_column_pre_hook(self, project):
  52. with pytest.raises(DbtDatabaseError):
  53. run_dbt(["run"], expect_pass=False)
  54. pass