Skip to content

Commit

Permalink
Convert runtime version specifier tests to pytest tests and move to t…
Browse files Browse the repository at this point in the history
…est_project

We've done two things in this commit, which arguably _should_ have been done in
two commits. First we moved the version specifier tests from `test_runtime.py::TestRuntimeConfig`
to `test_project.py::TestGetRequiredVersion` this is because what is really being
tested is the `_get_required_version` method. Doing it via `RuntimeConfig.from_parts` method
made actually testing it a lot harder as it requires setting up more of the world and
running with a _full_ project config dict.

The second thing we did was convert it from the old unittest implementation to a pytest
implementation. This saves us from having to create most of the world as we were doing
previously in these tests.

Of note, I did not move the test `test_unsupported_version_range_bad_config`. This test
is a bit different from the rest of the version specifier tests. It was introduced in
[1eb5857](1eb5857)
of [#2726](#2726) to resolve [#2638](#2638).
The focus of #2726 was to ensure the version specifier checks were run _before_ the validation
of the `dbt_project.yml`. Thus what this test is actually testing for is order of
operations at parse time. As such, this is really more a _functional_ test than a
unit test. In the next commit we'll get this test moved (and renamed)
  • Loading branch information
QMalcolm committed May 29, 2024
1 parent f003c3f commit ede371e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
53 changes: 52 additions & 1 deletion tests/unit/config/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import unittest
from copy import deepcopy
from typing import Any, Dict
from unittest import mock

import pytest
Expand All @@ -10,7 +11,7 @@
import dbt.exceptions
from dbt.adapters.contracts.connection import DEFAULT_QUERY_COMMENT, QueryComment
from dbt.adapters.factory import load_plugin
from dbt.config.project import Project
from dbt.config.project import Project, _get_required_version
from dbt.constants import DEPENDENCIES_FILE_NAME
from dbt.contracts.project import GitPackage, LocalPackage, PackageConfig
from dbt.flags import set_from_args
Expand Down Expand Up @@ -534,3 +535,53 @@ def setUp(self):
def test_setting_multiple_flags(self):
with pytest.raises(dbt.exceptions.DbtProjectError):
set_from_args(self.args, None)


class TestGetRequiredVersion:
@pytest.fixture
def project_dict(self) -> Dict[str, Any]:
return {
"name": "test_project",
"require-dbt-version": ">0.0.0",
}

def test_supported_version(self, project_dict: Dict[str, Any]) -> None:
specifiers = _get_required_version(project_dict=project_dict, verify_version=True)
assert set(x.to_version_string() for x in specifiers) == {">0.0.0"}

def test_unsupported_version(self, project_dict: Dict[str, Any]) -> None:
project_dict["require-dbt-version"] = ">99999.0.0"
with pytest.raises(
dbt.exceptions.DbtProjectError, match="This version of dbt is not supported"
):
_get_required_version(project_dict=project_dict, verify_version=True)

def test_unsupported_version_no_check(self, project_dict: Dict[str, Any]) -> None:
project_dict["require-dbt-version"] = ">99999.0.0"
specifiers = _get_required_version(project_dict=project_dict, verify_version=False)
assert set(x.to_version_string() for x in specifiers) == {">99999.0.0"}

def test_supported_version_range(self, project_dict: Dict[str, Any]) -> None:
project_dict["require-dbt-version"] = [">0.0.0", "<=99999.0.0"]
specifiers = _get_required_version(project_dict=project_dict, verify_version=True)
assert set(x.to_version_string() for x in specifiers) == {">0.0.0", "<=99999.0.0"}

def test_unsupported_version_range(self, project_dict: Dict[str, Any]) -> None:
project_dict["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
with pytest.raises(
dbt.exceptions.DbtProjectError, match="This version of dbt is not supported"
):
_get_required_version(project_dict=project_dict, verify_version=True)

def test_unsupported_version_range_no_check(self, project_dict: Dict[str, Any]) -> None:
project_dict["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
specifiers = _get_required_version(project_dict=project_dict, verify_version=False)
assert set(x.to_version_string() for x in specifiers) == {">0.0.0", "<=0.0.1"}

def test_impossible_version_range(self, project_dict: Dict[str, Any]) -> None:
project_dict["require-dbt-version"] = [">99999.0.0", "<=0.0.1"]
with pytest.raises(
dbt.exceptions.DbtProjectError,
match="The package version requirement can never be satisfied",
):
_get_required_version(project_dict=project_dict, verify_version=True)
45 changes: 0 additions & 45 deletions tests/unit/config/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,57 +85,12 @@ def from_parts(self, exc=None):
else:
return err

def test_supported_version(self):
self.default_project_data["require-dbt-version"] = ">0.0.0"
conf = self.from_parts()
self.assertEqual(set(x.to_version_string() for x in conf.dbt_version), {">0.0.0"})

def test_unsupported_version(self):
self.default_project_data["require-dbt-version"] = ">99999.0.0"
raised = self.from_parts(dbt.exceptions.DbtProjectError)
self.assertIn("This version of dbt is not supported", str(raised.exception))

def test_unsupported_version_no_check(self):
self.default_project_data["require-dbt-version"] = ">99999.0.0"
self.args.version_check = False
set_from_args(self.args, None)
conf = self.from_parts()
self.assertEqual(set(x.to_version_string() for x in conf.dbt_version), {">99999.0.0"})

def test_supported_version_range(self):
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=99999.0.0"]
conf = self.from_parts()
self.assertEqual(
set(x.to_version_string() for x in conf.dbt_version), {">0.0.0", "<=99999.0.0"}
)

def test_unsupported_version_range(self):
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
raised = self.from_parts(dbt.exceptions.DbtProjectError)
self.assertIn("This version of dbt is not supported", str(raised.exception))

def test_unsupported_version_range_bad_config(self):
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
self.default_project_data["some-extra-field-not-allowed"] = True
raised = self.from_parts(dbt.exceptions.DbtProjectError)
self.assertIn("This version of dbt is not supported", str(raised.exception))

def test_unsupported_version_range_no_check(self):
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
self.args.version_check = False
set_from_args(self.args, None)
conf = self.from_parts()
self.assertEqual(
set(x.to_version_string() for x in conf.dbt_version), {">0.0.0", "<=0.0.1"}
)

def test_impossible_version_range(self):
self.default_project_data["require-dbt-version"] = [">99999.0.0", "<=0.0.1"]
raised = self.from_parts(dbt.exceptions.DbtProjectError)
self.assertIn(
"The package version requirement can never be satisfied", str(raised.exception)
)

def test_unsupported_version_extra_config(self):
self.default_project_data["some-extra-field-not-allowed"] = True
raised = self.from_parts(dbt.exceptions.ProjectContractError)
Expand Down

0 comments on commit ede371e

Please sign in to comment.