Skip to content

Commit

Permalink
Stop ignoring test directory for precommit (#7201)
Browse files Browse the repository at this point in the history
* reformat test directory to pass formatting checks

* remove test comment
  • Loading branch information
emmyoop authored Mar 22, 2023
1 parent 8225a00 commit ca23148
Show file tree
Hide file tree
Showing 46 changed files with 7,850 additions and 7,090 deletions.
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Configuration for pre-commit hooks (see https://pre-commit.com/).
# Eventually the hooks described here will be run as tests before merging each PR.

# TODO: remove global exclusion of tests when testing overhaul is complete
exclude: ^(test/|core/dbt/docs/build/)
exclude: ^(core/dbt/docs/build/)

# Force all unspecified python hooks to run python 3.8
default_language_version:
Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ mypy_path = "third-party-stubs/"
namespace_packages = true

[tool.black]
# TODO: remove global exclusion of tests when testing overhaul is complete
force-exclude = 'test/'
line-length = 99
target-version = ['py38']
4 changes: 2 additions & 2 deletions test/unit/mock_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

def adapter_factory():
class MockAdapter(BaseAdapter):
ConnectionManager = mock.MagicMock(TYPE='mock')
ConnectionManager = mock.MagicMock(TYPE="mock")
responder = mock.MagicMock()
# some convenient defaults
responder.quote.side_effect = lambda identifier: '"{}"'.format(identifier)
responder.date_function.side_effect = lambda: 'unitdate()'
responder.date_function.side_effect = lambda: "unitdate()"
responder.is_cancelable.side_effect = lambda: False

@contextmanager
Expand Down
61 changes: 36 additions & 25 deletions test/unit/test_adapter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,57 @@
from dbt.adapters.factory import AdapterContainer
from dbt.adapters.base.plugin import AdapterPlugin
from dbt.include.global_project import (
PACKAGE_PATH as GLOBAL_PROJECT_PATH,
PROJECT_NAME as GLOBAL_PROJECT_NAME,
)


class TestGetPackageNames(unittest.TestCase):
def setUp(self):
with mock.patch('dbt.adapters.base.plugin.project_name_from_path') as get_name:
get_name.return_value = 'root'
with mock.patch("dbt.adapters.base.plugin.project_name_from_path") as get_name:
get_name.return_value = "root"
self.root_plugin = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path='/path/to/root/plugin',
dependencies=['childa', 'childb'],
include_path="/path/to/root/plugin",
dependencies=["childa", "childb"],
)
get_name.return_value = 'pkg_childa'
get_name.return_value = "pkg_childa"
self.childa = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path='/path/to/childa',
include_path="/path/to/childa",
)
get_name.return_value = 'pkg_childb'
get_name.return_value = "pkg_childb"
self.childb = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path='/path/to/childb',
dependencies=['childc']
include_path="/path/to/childb",
dependencies=["childc"],
)
get_name.return_value = 'pkg_childc'
get_name.return_value = "pkg_childc"
self.childc = AdapterPlugin(
adapter=mock.MagicMock(),
credentials=mock.MagicMock(),
include_path='/path/to/childc',
include_path="/path/to/childc",
)

self._mock_modules = {
'root': self.root_plugin,
'childa': self.childa,
'childb': self.childb,
'childc': self.childc,
"root": self.root_plugin,
"childa": self.childa,
"childb": self.childb,
"childc": self.childc,
}

self.factory = AdapterContainer()

self.load_patch = mock.patch.object(AdapterContainer, 'load_plugin')
self.load_patch = mock.patch.object(AdapterContainer, "load_plugin")
self.mock_load = self.load_patch.start()


def mock_load_plugin(name: str):
try:
plugin = self._mock_modules[name]
except KeyError:
raise RuntimeError(f'test could not find adapter type {name}!')
raise RuntimeError(f"test could not find adapter type {name}!")
self.factory.plugins[name] = plugin
self.factory.packages[plugin.project_name] = Path(plugin.include_path)
for dep in plugin.dependencies:
Expand All @@ -71,13 +69,26 @@ def test_no_packages(self):
assert self.factory.get_adapter_package_names(None) == [GLOBAL_PROJECT_NAME]

def test_one_package(self):
self.factory.load_plugin('childc')
assert self.factory.get_adapter_package_names('childc') == ['pkg_childc', GLOBAL_PROJECT_NAME]
self.factory.load_plugin("childc")
assert self.factory.get_adapter_package_names("childc") == [
"pkg_childc",
GLOBAL_PROJECT_NAME,
]

def test_simple_child_packages(self):
self.factory.load_plugin('childb')
assert self.factory.get_adapter_package_names('childb') == ['pkg_childb', 'pkg_childc', GLOBAL_PROJECT_NAME]
self.factory.load_plugin("childb")
assert self.factory.get_adapter_package_names("childb") == [
"pkg_childb",
"pkg_childc",
GLOBAL_PROJECT_NAME,
]

def test_layered_child_packages(self):
self.factory.load_plugin('root')
assert self.factory.get_adapter_package_names('root') == ['root', 'pkg_childa', 'pkg_childb', 'pkg_childc', GLOBAL_PROJECT_NAME]
self.factory.load_plugin("root")
assert self.factory.get_adapter_package_names("root") == [
"root",
"pkg_childa",
"pkg_childb",
"pkg_childc",
GLOBAL_PROJECT_NAME,
]
113 changes: 67 additions & 46 deletions test/unit/test_agate_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,34 @@
1,n,test,3.2,20180806T11:33:29.320Z,True,NULL
2,y,asdf,900,20180806T11:35:29.320Z,False,a string"""

SAMPLE_CSV_BOM_DATA = u'\ufeff' + SAMPLE_CSV_DATA
SAMPLE_CSV_BOM_DATA = "\ufeff" + SAMPLE_CSV_DATA


EXPECTED = [
[
1, 'n', 'test', Decimal('3.2'),
1,
"n",
"test",
Decimal("3.2"),
datetime(2018, 8, 6, 11, 33, 29, 320000, tzinfo=tzinfo.Utc()),
True, None,
True,
None,
],
[
2, 'y', 'asdf', 900,
2,
"y",
"asdf",
900,
datetime(2018, 8, 6, 11, 35, 29, 320000, tzinfo=tzinfo.Utc()),
False, 'a string',
False,
"a string",
],
]


EXPECTED_STRINGS = [
['1', 'n', 'test', '3.2', '20180806T11:33:29.320Z', 'True', None],
['2', 'y', 'asdf', '900', '20180806T11:35:29.320Z', 'False', 'a string'],
["1", "n", "test", "3.2", "20180806T11:33:29.320Z", "True", None],
["2", "y", "asdf", "900", "20180806T11:35:29.320Z", "False", "a string"],
]


Expand All @@ -45,90 +53,104 @@ def tearDown(self):
rmtree(self.tempdir)

def test_from_csv(self):
path = os.path.join(self.tempdir, 'input.csv')
with open(path, 'wb') as fp:
fp.write(SAMPLE_CSV_DATA.encode('utf-8'))
path = os.path.join(self.tempdir, "input.csv")
with open(path, "wb") as fp:
fp.write(SAMPLE_CSV_DATA.encode("utf-8"))
tbl = agate_helper.from_csv(path, ())
self.assertEqual(len(tbl), len(EXPECTED))
for idx, row in enumerate(tbl):
self.assertEqual(list(row), EXPECTED[idx])

def test_bom_from_csv(self):
path = os.path.join(self.tempdir, 'input.csv')
with open(path, 'wb') as fp:
fp.write(SAMPLE_CSV_BOM_DATA.encode('utf-8'))
path = os.path.join(self.tempdir, "input.csv")
with open(path, "wb") as fp:
fp.write(SAMPLE_CSV_BOM_DATA.encode("utf-8"))
tbl = agate_helper.from_csv(path, ())
self.assertEqual(len(tbl), len(EXPECTED))
for idx, row in enumerate(tbl):
self.assertEqual(list(row), EXPECTED[idx])

def test_from_csv_all_reserved(self):
path = os.path.join(self.tempdir, 'input.csv')
with open(path, 'wb') as fp:
fp.write(SAMPLE_CSV_DATA.encode('utf-8'))
tbl = agate_helper.from_csv(path, tuple('abcdefg'))
path = os.path.join(self.tempdir, "input.csv")
with open(path, "wb") as fp:
fp.write(SAMPLE_CSV_DATA.encode("utf-8"))
tbl = agate_helper.from_csv(path, tuple("abcdefg"))
self.assertEqual(len(tbl), len(EXPECTED_STRINGS))
for expected, row in zip(EXPECTED_STRINGS, tbl):
self.assertEqual(list(row), expected)

def test_from_data(self):
column_names = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
column_names = ["a", "b", "c", "d", "e", "f", "g"]
data = [
{'a': '1', 'b': 'n', 'c': 'test', 'd': '3.2',
'e': '20180806T11:33:29.320Z', 'f': 'True', 'g': 'NULL'},
{'a': '2', 'b': 'y', 'c': 'asdf', 'd': '900',
'e': '20180806T11:35:29.320Z', 'f': 'False', 'g': 'a string'}
{
"a": "1",
"b": "n",
"c": "test",
"d": "3.2",
"e": "20180806T11:33:29.320Z",
"f": "True",
"g": "NULL",
},
{
"a": "2",
"b": "y",
"c": "asdf",
"d": "900",
"e": "20180806T11:35:29.320Z",
"f": "False",
"g": "a string",
},
]
tbl = agate_helper.table_from_data(data, column_names)
self.assertEqual(len(tbl), len(EXPECTED))
for idx, row in enumerate(tbl):
self.assertEqual(list(row), EXPECTED[idx])

def test_datetime_formats(self):
path = os.path.join(self.tempdir, 'input.csv')
path = os.path.join(self.tempdir, "input.csv")
datetimes = [
'20180806T11:33:29.000Z',
'20180806T11:33:29Z',
'20180806T113329Z',
"20180806T11:33:29.000Z",
"20180806T11:33:29Z",
"20180806T113329Z",
]
expected = datetime(2018, 8, 6, 11, 33, 29, 0, tzinfo=tzinfo.Utc())
for dt in datetimes:
with open(path, 'wb') as fp:
fp.write('a\n{}'.format(dt).encode('utf-8'))
with open(path, "wb") as fp:
fp.write("a\n{}".format(dt).encode("utf-8"))
tbl = agate_helper.from_csv(path, ())
self.assertEqual(tbl[0][0], expected)

def test_merge_allnull(self):
t1 = agate.Table([(1, 'a', None), (2, 'b', None)], ('a', 'b', 'c'))
t2 = agate.Table([(3, 'c', None), (4, 'd', None)], ('a', 'b', 'c'))
t1 = agate.Table([(1, "a", None), (2, "b", None)], ("a", "b", "c"))
t2 = agate.Table([(3, "c", None), (4, "d", None)], ("a", "b", "c"))
result = agate_helper.merge_tables([t1, t2])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
self.assertEqual(result.column_names, ("a", "b", "c"))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Number)
self.assertEqual(len(result), 4)

def test_merge_mixed(self):
t1 = agate.Table([(1, 'a', None), (2, 'b', None)], ('a', 'b', 'c'))
t2 = agate.Table([(3, 'c', 'dog'), (4, 'd', 'cat')], ('a', 'b', 'c'))
t3 = agate.Table([(3, 'c', None), (4, 'd', None)], ('a', 'b', 'c'))
t1 = agate.Table([(1, "a", None), (2, "b", None)], ("a", "b", "c"))
t2 = agate.Table([(3, "c", "dog"), (4, "d", "cat")], ("a", "b", "c"))
t3 = agate.Table([(3, "c", None), (4, "d", None)], ("a", "b", "c"))

result = agate_helper.merge_tables([t1, t2])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
self.assertEqual(result.column_names, ("a", "b", "c"))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Text)
self.assertEqual(len(result), 4)

result = agate_helper.merge_tables([t2, t3])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
self.assertEqual(result.column_names, ("a", "b", "c"))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Text)
self.assertEqual(len(result), 4)

result = agate_helper.merge_tables([t1, t2, t3])
self.assertEqual(result.column_names, ('a', 'b', 'c'))
self.assertEqual(result.column_names, ("a", "b", "c"))
assert isinstance(result.column_types[0], agate.data_types.Number)
assert isinstance(result.column_types[1], agate.data_types.Text)
assert isinstance(result.column_types[2], agate.data_types.Text)
Expand All @@ -138,18 +160,18 @@ def test_nocast_string_types(self):
# String fields should not be coerced into a representative type
# See: https://github.com/dbt-labs/dbt-core/issues/2984

column_names = ['a', 'b', 'c', 'd', 'e']
column_names = ["a", "b", "c", "d", "e"]
result_set = [
{'a': '0005', 'b': '01T00000aabbccdd', 'c': 'true', 'd': 10, 'e': False},
{'a': '0006', 'b': '01T00000aabbccde', 'c': 'false', 'd': 11, 'e': True},
{"a": "0005", "b": "01T00000aabbccdd", "c": "true", "d": 10, "e": False},
{"a": "0006", "b": "01T00000aabbccde", "c": "false", "d": 11, "e": True},
]

tbl = agate_helper.table_from_data_flat(data=result_set, column_names=column_names)
self.assertEqual(len(tbl), len(result_set))

expected = [
['0005', '01T00000aabbccdd', 'true', Decimal(10), False],
['0006', '01T00000aabbccde', 'false', Decimal(11), True],
["0005", "01T00000aabbccdd", "true", Decimal(10), False],
["0006", "01T00000aabbccde", "false", Decimal(11), True],
]

for i, row in enumerate(tbl):
Expand All @@ -159,10 +181,10 @@ def test_nocast_bool_01(self):
# True and False values should not be cast to 1 and 0, and vice versa
# See: https://github.com/dbt-labs/dbt-core/issues/4511

column_names = ['a', 'b']
column_names = ["a", "b"]
result_set = [
{'a': True, 'b': 1},
{'a': False, 'b': 0},
{"a": True, "b": 1},
{"a": False, "b": 0},
]

tbl = agate_helper.table_from_data_flat(data=result_set, column_names=column_names)
Expand All @@ -178,4 +200,3 @@ def test_nocast_bool_01(self):

for i, row in enumerate(tbl):
self.assertEqual(list(row), expected[i])

Loading

0 comments on commit ca23148

Please sign in to comment.