diff --git a/tests/conftest.py b/tests/conftest.py index 1def4b6..9de9e92 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)) @@ -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", diff --git a/tests/test_cli.py b/tests/test_cli.py index 81c7c45..99e65fd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,67 +4,77 @@ from dbt_cloud.cli import dbt_cloud as cli -@pytest.fixture +@pytest.fixture(scope="module") def runner(): return CliRunner() -@pytest.mark.account -@pytest.mark.integration -def test_cli_account_list_and_get(runner): - # Account list +@pytest.fixture(scope="module") +def dbt_cloud_project(runner, account_id): + project_name = "pytest project" + + # Project create result = runner.invoke( cli, - ["account", "list"], + ["project", "create", "--account-id", account_id, "--name", project_name], ) - assert result.exit_code == 0 + assert result.exit_code == 0, result.output response = json.loads(result.output) - assert len(response["data"]) > 0 + assert response["data"]["name"] == project_name + assert response["data"]["account_id"] == account_id - # Account get - account_id = response["data"][0]["id"] - print(account_id) + yield response["data"] + + # Project delete + project_id = response["data"]["id"] result = runner.invoke( cli, - ["account", "get", "--account-id", account_id], + ["project", "delete", "--account-id", account_id, "--project-id", project_id], ) - assert result.exit_code == 0 + assert result.exit_code == 0, result.output response = json.loads(result.output) - assert response["data"]["id"] == account_id + assert response["data"]["id"] == project_id + assert response["data"]["account_id"] == account_id -@pytest.mark.environment -@pytest.mark.integration -def test_cli_environment_list_and_get(runner, account_id, project_id): - # Environment list +@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", - "list", + "create", "--account-id", account_id, "--project-id", project_id, - "--limit", - 2, + "--name", + environment_name, + "--dbt-version", + "1.5.0-latest", ], ) - assert result.exit_code == 0, result.output + assert result.exit_code == 0 response = json.loads(result.output) - environment_id = response["data"][0]["id"] - assert len(response["data"]) > 0 - for environment in response["data"]: - assert environment["account_id"] == account_id + environment_id = response["data"]["id"] + assert response["data"]["name"] == environment_name + assert response["data"]["account_id"] == account_id + + yield response["data"] + # Environment delete result = runner.invoke( cli, [ "environment", - "get", + "delete", "--account-id", account_id, "--project-id", @@ -80,108 +90,155 @@ 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" +@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"] - # Environment create + # Job create result = runner.invoke( cli, [ - "environment", + "job", "create", "--account-id", account_id, "--project-id", project_id, + "--environment-id", + environment_id, "--name", - environment_name, - "--dbt-version", - "1.5.0-latest", + "pytest job", + "--settings-threads", + 4, + "--execute-steps", + '["dbt compile"]', ], ) - assert result.exit_code == 0 + assert result.exit_code == 0, result.output response = json.loads(result.output) - - assert response["data"]["name"] == environment_name + 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 - # Environment delete - environment_id = response["data"]["id"] + yield response["data"] + + # Job delete result = runner.invoke( cli, [ - "environment", + "job", "delete", "--account-id", account_id, - "--environment-id", - environment_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): + # Account list + result = runner.invoke( + cli, + ["account", "list"], + ) + assert result.exit_code == 0 response = json.loads(result.output) - assert response["data"]["id"] == environment_id - assert response["data"]["account_id"] == account_id + assert len(response["data"]) > 0 + # Account get + account_id = response["data"][0]["id"] + result = runner.invoke( + cli, + ["account", "get", "--account-id", account_id], + ) -@pytest.mark.project + assert result.exit_code == 0 + response = json.loads(result.output) + assert response["data"]["id"] == account_id + + +@pytest.mark.environment @pytest.mark.integration -def test_cli_project_list_and_get(runner, account_id): - # Project list +def test_cli_environment_list_and_get(runner, account_id, project_id): + # Environment list result = runner.invoke( cli, - ["project", "list", "--account-id", account_id, "--limit", 2], + [ + "environment", + "list", + "--account-id", + account_id, + "--project-id", + project_id, + "--limit", + 2, + ], ) assert result.exit_code == 0, result.output response = json.loads(result.output) + environment_id = response["data"][0]["id"] assert len(response["data"]) > 0 - for project in response["data"]: - assert project["account_id"] == account_id + for environment in response["data"]: + assert environment["account_id"] == account_id - # Project get - project_id = response["data"][0]["id"] result = runner.invoke( cli, - ["project", "get", "--account-id", account_id, "--project-id", project_id], + [ + "environment", + "get", + "--account-id", + account_id, + "--project-id", + project_id, + "--environment-id", + environment_id, + ], ) - assert result.exit_code == 0, result.output + assert result.exit_code == 0 response = json.loads(result.output) - assert response["data"]["id"] == project_id + assert response["data"]["id"] == environment_id + assert response["data"]["account_id"] == account_id @pytest.mark.project @pytest.mark.integration -def test_cli_project_create_and_delete(runner, account_id): - project_name = "pytest project" - - # Project create +def test_cli_project_list_and_get(runner, account_id): + # Project list result = runner.invoke( cli, - ["project", "create", "--account-id", account_id, "--name", project_name], + ["project", "list", "--account-id", account_id, "--limit", 2], ) 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 + assert len(response["data"]) > 0 + for project in response["data"]: + assert project["account_id"] == account_id - # Project delete - project_id = response["data"]["id"] + # Project get + project_id = response["data"][0]["id"] result = runner.invoke( cli, - ["project", "delete", "--account-id", account_id, "--project-id", project_id], + ["project", "get", "--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 @@ -273,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"]', - ], - ) +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) - 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, - ], - ) - - 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, @@ -375,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( @@ -431,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"],