From c5580a122abf753602ca03c61d3f5152a97cd4f8 Mon Sep 17 00:00:00 2001 From: Simo Tumelius Date: Fri, 4 Aug 2023 08:26:36 +0300 Subject: [PATCH 1/3] Add dbt_cloud_project and dbt_cloud_environment pytest fixtures --- tests/conftest.py | 2 +- tests/test_cli.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 1def4b6..1722325 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -37,7 +37,7 @@ RUN_ID = 36053848 -@pytest.fixture +@pytest.fixture(scope="module") def account_id(): return int(os.environ.get("DBT_CLOUD_ACCOUNT_ID", ACCOUNT_ID)) diff --git a/tests/test_cli.py b/tests/test_cli.py index 81c7c45..cab8c2d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,11 +4,90 @@ from dbt_cloud.cli import dbt_cloud as cli -@pytest.fixture +@pytest.fixture(scope="module") def runner(): return CliRunner() +@pytest.fixture(scope="module") +def dbt_cloud_project(runner, account_id): + project_name = "pytest project" + + # Project create + result = runner.invoke( + cli, + ["project", "create", "--account-id", account_id, "--name", project_name], + ) + + assert result.exit_code == 0, result.output + response = json.loads(result.output) + assert response["data"]["name"] == project_name + assert response["data"]["account_id"] == account_id + + yield response["data"] + + # Project delete + project_id = response["data"]["id"] + result = runner.invoke( + cli, + ["project", "delete", "--account-id", account_id, "--project-id", project_id], + ) + + assert result.exit_code == 0, result.output + response = json.loads(result.output) + assert response["data"]["id"] == project_id + assert response["data"]["account_id"] == account_id + +@pytest.fixture(scope="module") +def dbt_cloud_environment(dbt_cloud_project, runner, account_id): + environment_name = "pytest environment" + project_id = dbt_cloud_project["id"] + + # Environment create + result = runner.invoke( + cli, + [ + "environment", + "create", + "--account-id", + account_id, + "--project-id", + project_id, + "--name", + environment_name, + "--dbt-version", + "1.5.0-latest", + ], + ) + + assert result.exit_code == 0 + response = json.loads(result.output) + + assert response["data"]["name"] == environment_name + assert response["data"]["account_id"] == account_id + + yield response["data"] + + # Environment delete + environment_id = response["data"]["id"] + result = runner.invoke( + cli, + [ + "environment", + "delete", + "--account-id", + account_id, + "--environment-id", + environment_id, + ], + ) + + assert result.exit_code == 0 + response = json.loads(result.output) + assert response["data"]["id"] == environment_id + assert response["data"]["account_id"] == account_id + + @pytest.mark.account @pytest.mark.integration def test_cli_account_list_and_get(runner): From b6bcdc9c1db8e7c7b57d49e5d91761ca11a7f778 Mon Sep 17 00:00:00 2001 From: Simo Tumelius Date: Fri, 4 Aug 2023 08:42:29 +0300 Subject: [PATCH 2/3] Add dbt_cloud_job pytest fixture --- tests/test_cli.py | 206 +++++++++++++++------------------------------- 1 file changed, 65 insertions(+), 141 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index cab8c2d..99e65fd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -38,6 +38,7 @@ def dbt_cloud_project(runner, account_id): assert response["data"]["id"] == project_id assert response["data"]["account_id"] == account_id + @pytest.fixture(scope="module") def dbt_cloud_environment(dbt_cloud_project, runner, account_id): environment_name = "pytest environment" @@ -62,14 +63,13 @@ def dbt_cloud_environment(dbt_cloud_project, runner, account_id): assert result.exit_code == 0 response = json.loads(result.output) - + environment_id = response["data"]["id"] assert response["data"]["name"] == environment_name assert response["data"]["account_id"] == account_id yield response["data"] # Environment delete - environment_id = response["data"]["id"] result = runner.invoke( cli, [ @@ -77,6 +77,8 @@ def dbt_cloud_environment(dbt_cloud_project, runner, account_id): "delete", "--account-id", account_id, + "--project-id", + project_id, "--environment-id", environment_id, ], @@ -88,6 +90,60 @@ def dbt_cloud_environment(dbt_cloud_project, runner, account_id): assert response["data"]["account_id"] == account_id +@pytest.fixture(scope="module") +def dbt_cloud_job(runner, dbt_cloud_environment, account_id): + project_id = dbt_cloud_environment["project_id"] + environment_id = dbt_cloud_environment["id"] + + # Job create + result = runner.invoke( + cli, + [ + "job", + "create", + "--account-id", + account_id, + "--project-id", + project_id, + "--environment-id", + environment_id, + "--name", + "pytest job", + "--settings-threads", + 4, + "--execute-steps", + '["dbt compile"]', + ], + ) + + assert result.exit_code == 0, result.output + response = json.loads(result.output) + job_id = response["data"]["id"] + assert response["data"]["account_id"] == account_id + assert response["data"]["project_id"] == project_id + assert response["data"]["environment_id"] == environment_id + assert response["data"]["settings"]["threads"] == 4 + + yield response["data"] + + # Job delete + result = runner.invoke( + cli, + [ + "job", + "delete", + "--account-id", + account_id, + "--job-id", + job_id, + ], + ) + + assert result.exit_code == 0, result.output + response = json.loads(result.output) + assert response["data"]["id"] == job_id + + @pytest.mark.account @pytest.mark.integration def test_cli_account_list_and_get(runner): @@ -103,7 +159,6 @@ def test_cli_account_list_and_get(runner): # Account get account_id = response["data"][0]["id"] - print(account_id) result = runner.invoke( cli, ["account", "get", "--account-id", account_id], @@ -159,54 +214,6 @@ def test_cli_environment_list_and_get(runner, account_id, project_id): assert response["data"]["account_id"] == account_id -@pytest.mark.environment -@pytest.mark.integration -def test_cli_environment_create_and_delete(runner, account_id, project_id): - environment_name = "pytest environment" - - # Environment create - result = runner.invoke( - cli, - [ - "environment", - "create", - "--account-id", - account_id, - "--project-id", - project_id, - "--name", - environment_name, - "--dbt-version", - "1.5.0-latest", - ], - ) - - assert result.exit_code == 0 - response = json.loads(result.output) - - assert response["data"]["name"] == environment_name - assert response["data"]["account_id"] == account_id - - # Environment delete - environment_id = response["data"]["id"] - result = runner.invoke( - cli, - [ - "environment", - "delete", - "--account-id", - account_id, - "--environment-id", - environment_id, - ], - ) - - assert result.exit_code == 0 - response = json.loads(result.output) - assert response["data"]["id"] == environment_id - assert response["data"]["account_id"] == account_id - - @pytest.mark.project @pytest.mark.integration def test_cli_project_list_and_get(runner, account_id): @@ -234,35 +241,6 @@ def test_cli_project_list_and_get(runner, account_id): assert response["data"]["id"] == project_id -@pytest.mark.project -@pytest.mark.integration -def test_cli_project_create_and_delete(runner, account_id): - project_name = "pytest project" - - # Project create - result = runner.invoke( - cli, - ["project", "create", "--account-id", account_id, "--name", project_name], - ) - - assert result.exit_code == 0, result.output - response = json.loads(result.output) - assert response["data"]["name"] == project_name - assert response["data"]["account_id"] == account_id - - # Project delete - project_id = response["data"]["id"] - result = runner.invoke( - cli, - ["project", "delete", "--account-id", account_id, "--project-id", project_id], - ) - - assert result.exit_code == 0, result.output - response = json.loads(result.output) - assert response["data"]["id"] == project_id - assert response["data"]["account_id"] == account_id - - @pytest.mark.connection @pytest.mark.integration def test_cli_connection_list_and_get(runner, account_id, project_id): @@ -352,57 +330,9 @@ def test_cli_job_list_and_get(runner, account_id, project_id): @pytest.mark.job @pytest.mark.integration -def test_cli_job_create_and_delete(runner, account_id, project_id, environment_id): - # Job create - result = runner.invoke( - cli, - [ - "job", - "create", - "--account-id", - account_id, - "--project-id", - project_id, - "--environment-id", - environment_id, - "--name", - "pytest job", - "--settings-threads", - 4, - "--execute-steps", - '["dbt seed"]', - ], - ) - - assert result.exit_code == 0, result.output - response = json.loads(result.output) - job_id = response["data"]["id"] - assert response["data"]["account_id"] == account_id - assert response["data"]["project_id"] == project_id - assert response["data"]["environment_id"] == environment_id - assert response["data"]["settings"]["threads"] == 4 - - # Job delete - result = runner.invoke( - cli, - [ - "job", - "delete", - "--account-id", - account_id, - "--job-id", - job_id, - ], - ) +def test_cli_job_export_and_import(runner, account_id, dbt_cloud_job): + job_id = dbt_cloud_job["id"] - assert result.exit_code == 0, result.output - response = json.loads(result.output) - assert response["data"]["id"] == job_id - - -@pytest.mark.job -@pytest.mark.integration -def test_cli_job_export_and_import(runner, account_id, job_id): # Job export result = runner.invoke( cli, @@ -454,16 +384,10 @@ def test_cli_job_export_and_import(runner, account_id, job_id): @pytest.mark.job @pytest.mark.integration -def test_cli_job_delete_all(runner, account_id, project_id, environment_id): - # Job list - result = runner.invoke( - cli, - ["job", "list", "--account-id", account_id, "--project-id", project_id], - ) - - assert result.exit_code == 0, result.output - response = json.loads(result.output) - job_ids_to_keep = [job["id"] for job in response["data"]] +def test_cli_job_delete_all(runner, account_id, dbt_cloud_job): + project_id = dbt_cloud_job["project_id"] + environment_id = dbt_cloud_job["environment_id"] + job_ids_to_keep = [dbt_cloud_job["id"]] # Job create result = runner.invoke( @@ -510,7 +434,7 @@ def test_cli_job_delete_all(runner, account_id, project_id, environment_id): @pytest.mark.job @pytest.mark.integration -def test_cli_job_run_wait(runner, account_id, job_id): +def test_cli_job_run_wait(runner, job_id, account_id): result = runner.invoke( cli, ["job", "run", "--account-id", account_id, "--job-id", job_id, "--wait"], From f66d7d05abb4b6f7ac9b30d8f19deccd7a15e9c6 Mon Sep 17 00:00:00 2001 From: Simo Tumelius Date: Fri, 4 Aug 2023 08:45:27 +0300 Subject: [PATCH 3/3] Fix a test --- tests/conftest.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 1722325..9de9e92 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -211,7 +211,10 @@ def load_response(response_name): pytest.param( "environment_get", DbtCloudEnvironmentGetCommand( - api_token=API_TOKEN, account_id=ACCOUNT_ID, environment_id=ENVIRONMENT_ID + api_token=API_TOKEN, + account_id=ACCOUNT_ID, + project_id=PROJECT_ID, + environment_id=ENVIRONMENT_ID, ), load_response("environment_get_response"), "get",