Skip to content

Commit 18e4308

Browse files
committed
Convert runtime version specifier tests to pytest tests and move to test_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)
1 parent cb97686 commit 18e4308

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

tests/unit/config/test_project.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import unittest
44
from copy import deepcopy
5+
from typing import Any, Dict
56
from unittest import mock
67

78
import pytest
@@ -10,7 +11,7 @@
1011
import dbt.exceptions
1112
from dbt.adapters.contracts.connection import DEFAULT_QUERY_COMMENT, QueryComment
1213
from dbt.adapters.factory import load_plugin
13-
from dbt.config.project import Project
14+
from dbt.config.project import Project, _get_required_version
1415
from dbt.constants import DEPENDENCIES_FILE_NAME
1516
from dbt.contracts.project import GitPackage, LocalPackage, PackageConfig
1617
from dbt.flags import set_from_args
@@ -534,3 +535,53 @@ def setUp(self):
534535
def test_setting_multiple_flags(self):
535536
with pytest.raises(dbt.exceptions.DbtProjectError):
536537
set_from_args(self.args, None)
538+
539+
540+
class TestGetRequiredVersion:
541+
@pytest.fixture
542+
def project_dict(self) -> Dict[str, Any]:
543+
return {
544+
"name": "test_project",
545+
"require-dbt-version": ">0.0.0",
546+
}
547+
548+
def test_supported_version(self, project_dict: Dict[str, Any]) -> None:
549+
specifiers = _get_required_version(project_dict=project_dict, verify_version=True)
550+
assert set(x.to_version_string() for x in specifiers) == {">0.0.0"}
551+
552+
def test_unsupported_version(self, project_dict: Dict[str, Any]) -> None:
553+
project_dict["require-dbt-version"] = ">99999.0.0"
554+
with pytest.raises(
555+
dbt.exceptions.DbtProjectError, match="This version of dbt is not supported"
556+
):
557+
_get_required_version(project_dict=project_dict, verify_version=True)
558+
559+
def test_unsupported_version_no_check(self, project_dict: Dict[str, Any]) -> None:
560+
project_dict["require-dbt-version"] = ">99999.0.0"
561+
specifiers = _get_required_version(project_dict=project_dict, verify_version=False)
562+
assert set(x.to_version_string() for x in specifiers) == {">99999.0.0"}
563+
564+
def test_supported_version_range(self, project_dict: Dict[str, Any]) -> None:
565+
project_dict["require-dbt-version"] = [">0.0.0", "<=99999.0.0"]
566+
specifiers = _get_required_version(project_dict=project_dict, verify_version=True)
567+
assert set(x.to_version_string() for x in specifiers) == {">0.0.0", "<=99999.0.0"}
568+
569+
def test_unsupported_version_range(self, project_dict: Dict[str, Any]) -> None:
570+
project_dict["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
571+
with pytest.raises(
572+
dbt.exceptions.DbtProjectError, match="This version of dbt is not supported"
573+
):
574+
_get_required_version(project_dict=project_dict, verify_version=True)
575+
576+
def test_unsupported_version_range_no_check(self, project_dict: Dict[str, Any]) -> None:
577+
project_dict["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
578+
specifiers = _get_required_version(project_dict=project_dict, verify_version=False)
579+
assert set(x.to_version_string() for x in specifiers) == {">0.0.0", "<=0.0.1"}
580+
581+
def test_impossible_version_range(self, project_dict: Dict[str, Any]) -> None:
582+
project_dict["require-dbt-version"] = [">99999.0.0", "<=0.0.1"]
583+
with pytest.raises(
584+
dbt.exceptions.DbtProjectError,
585+
match="The package version requirement can never be satisfied",
586+
):
587+
_get_required_version(project_dict=project_dict, verify_version=True)

tests/unit/config/test_runtime.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -85,57 +85,12 @@ def from_parts(self, exc=None):
8585
else:
8686
return err
8787

88-
def test_supported_version(self):
89-
self.default_project_data["require-dbt-version"] = ">0.0.0"
90-
conf = self.from_parts()
91-
self.assertEqual(set(x.to_version_string() for x in conf.dbt_version), {">0.0.0"})
92-
93-
def test_unsupported_version(self):
94-
self.default_project_data["require-dbt-version"] = ">99999.0.0"
95-
raised = self.from_parts(dbt.exceptions.DbtProjectError)
96-
self.assertIn("This version of dbt is not supported", str(raised.exception))
97-
98-
def test_unsupported_version_no_check(self):
99-
self.default_project_data["require-dbt-version"] = ">99999.0.0"
100-
self.args.version_check = False
101-
set_from_args(self.args, None)
102-
conf = self.from_parts()
103-
self.assertEqual(set(x.to_version_string() for x in conf.dbt_version), {">99999.0.0"})
104-
105-
def test_supported_version_range(self):
106-
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=99999.0.0"]
107-
conf = self.from_parts()
108-
self.assertEqual(
109-
set(x.to_version_string() for x in conf.dbt_version), {">0.0.0", "<=99999.0.0"}
110-
)
111-
112-
def test_unsupported_version_range(self):
113-
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
114-
raised = self.from_parts(dbt.exceptions.DbtProjectError)
115-
self.assertIn("This version of dbt is not supported", str(raised.exception))
116-
11788
def test_unsupported_version_range_bad_config(self):
11889
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
11990
self.default_project_data["some-extra-field-not-allowed"] = True
12091
raised = self.from_parts(dbt.exceptions.DbtProjectError)
12192
self.assertIn("This version of dbt is not supported", str(raised.exception))
12293

123-
def test_unsupported_version_range_no_check(self):
124-
self.default_project_data["require-dbt-version"] = [">0.0.0", "<=0.0.1"]
125-
self.args.version_check = False
126-
set_from_args(self.args, None)
127-
conf = self.from_parts()
128-
self.assertEqual(
129-
set(x.to_version_string() for x in conf.dbt_version), {">0.0.0", "<=0.0.1"}
130-
)
131-
132-
def test_impossible_version_range(self):
133-
self.default_project_data["require-dbt-version"] = [">99999.0.0", "<=0.0.1"]
134-
raised = self.from_parts(dbt.exceptions.DbtProjectError)
135-
self.assertIn(
136-
"The package version requirement can never be satisfied", str(raised.exception)
137-
)
138-
13994
def test_unsupported_version_extra_config(self):
14095
self.default_project_data["some-extra-field-not-allowed"] = True
14196
raised = self.from_parts(dbt.exceptions.ProjectContractError)

0 commit comments

Comments
 (0)