Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/basedate tests #305

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Changed
- Refactored tests for NullIndicator `#301 <https://github.com/lvgig/tubular/issues/301>`_
- Refactored BetweenDatesTransformer tests in new format `#294 <https://github.com/lvgig/tubular/issues/294>`_
- As part of above, edited dates file transformers to use BaseDropOriginalMixin in transform
- Edited base testing setup for dates file, created new BaseDatetimeTransformer class


1.3.1 (2024-07-18)
Expand Down
6 changes: 5 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ def minimal_attribute_dict():
"mappings": {"a": {1: 2, 3: 4}},
"adjust_column": "b",
},
"BaseDateTransformer": {
"BaseGenericDateTransformer": {
"columns": ["a"],
"new_column_name": "bla",
},
"BaseDatetimeTransformer": {
"columns": ["a"],
"new_column_name": "bla",
},
Expand Down
99 changes: 99 additions & 0 deletions tests/dates/test_BaseDatetimeTransformer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from copy import deepcopy

import numpy as np
import pandas as pd
import pytest

from tests.base_tests import (
ColumnStrListInitTests,
DropOriginalInitMixinTests,
GenericFitTests,
GenericTransformTests,
NewColumnNameInitMixintests,
OtherBaseBehaviourTests,
)


class DatetimeMixinTransformTests:
"""Generic tests for Datetime Transformers"""

@pytest.mark.parametrize(
("bad_value", "bad_type"),
[
(1, "int64"),
("a", "object"),
(np.nan, "float64"),
(pd.to_datetime("01/02/2020").date(), "date"),
],
)
def test_non_datetypes_error(
self,
uninitialized_transformers,
minimal_attribute_dict,
minimal_dataframe_lookup,
bad_value,
bad_type,
):
"Test that transform raises an error if columns contains non date types"

args = minimal_attribute_dict[self.transformer_name].copy()
columns = args["columns"]

for i in range(len(columns)):
df = deepcopy(minimal_dataframe_lookup[self.transformer_name])
print(df)
col = columns[i]
df[col] = bad_value

x = uninitialized_transformers[self.transformer_name](
columns=columns,
new_column_name="c",
)

msg = rf"{col} type should be in \['datetime64'\] but got {bad_type}"

with pytest.raises(
TypeError,
match=msg,
):
x.transform(df)


class TestInit(
NewColumnNameInitMixintests,
DropOriginalInitMixinTests,
ColumnStrListInitTests,
):
"""Generic tests for transformer.init()."""

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDatetimeTransformer"


class TestFit(GenericFitTests):
"""Generic tests for transformer.fit()"""

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDatetimeTransformer"


class TestTransform(GenericTransformTests, DatetimeMixinTransformTests):
"""Tests for BaseDatetimeTransformer.transform."""

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDatetimeTransformer"


class TestOtherBaseBehaviour(OtherBaseBehaviourTests):
"""
Class to run tests for BaseTransformerBehaviour outside the three standard methods.

May need to overwite specific tests in this class if the tested transformer modifies this behaviour.
"""

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDatetimeTransformer"
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
from copy import deepcopy

import numpy as np
import pandas as pd
import pytest

Expand Down Expand Up @@ -70,21 +72,62 @@ def create_date_diff_different_dtypes():
)


class DatesMixinTransformTests:
class GenericDatesMixinTransformTests:
"""Generic tests for Dates Transformers"""

@pytest.mark.parametrize(
("columns, datetime_col, date_col"),
("bad_value", "bad_type"),
[
(["date_col_1", "datetime_col_2"], 1, 0),
(["datetime_col_1", "date_col_2"], 0, 1),
(1, "int64"),
("a", "object"),
(np.nan, "float64"),
],
)
def test_non_datetypes_error(
self,
uninitialized_transformers,
minimal_attribute_dict,
minimal_dataframe_lookup,
bad_value,
bad_type,
):
"Test that transform raises an error if columns contains non date types"

args = minimal_attribute_dict[self.transformer_name].copy()
columns = args["columns"]

for i in range(len(columns)):
df = deepcopy(minimal_dataframe_lookup[self.transformer_name])
print(df)
col = columns[i]
df[col] = bad_value

x = uninitialized_transformers[self.transformer_name](
columns=columns,
new_column_name="c",
)

msg = (
rf"{col} type should be in \['datetime64', 'date'\] but got {bad_type}"
)

with pytest.raises(
TypeError,
match=msg,
):
x.transform(df)

@pytest.mark.parametrize(
("columns, datetime_col"),
[
(["date_col_1", "datetime_col_2"], 1),
(["datetime_col_1", "date_col_2"], 0),
],
)
def test_mismatched_datetypes_error(
self,
columns,
datetime_col,
date_col,
uninitialized_transformers,
):
"Test that transform raises an error if one column is a date and one is datetime"
Expand Down Expand Up @@ -122,23 +165,23 @@ class TestInit(

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDateTransformer"
cls.transformer_name = "BaseGenericDateTransformer"


class TestFit(GenericFitTests):
"""Generic tests for transformer.fit()"""

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDateTransformer"
cls.transformer_name = "BaseGenericDateTransformer"


class TestTransform(GenericTransformTests, DatesMixinTransformTests):
"""Tests for BaseDateTransformer.transform."""
class TestTransform(GenericTransformTests, GenericDatesMixinTransformTests):
"""Tests for BaseGenericDateTransformer.transform."""

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDateTransformer"
cls.transformer_name = "BaseGenericDateTransformer"


class TestOtherBaseBehaviour(OtherBaseBehaviourTests):
Expand All @@ -150,4 +193,4 @@ class TestOtherBaseBehaviour(OtherBaseBehaviourTests):

@classmethod
def setup_class(cls):
cls.transformer_name = "BaseDateTransformer"
cls.transformer_name = "BaseGenericDateTransformer"
4 changes: 2 additions & 2 deletions tests/dates/test_BaseTwocolumnDateTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
OtherBaseBehaviourTests,
TwoColumnListInitTests,
)
from tests.dates.test_BaseDateTransformer import DatesMixinTransformTests
from tests.dates.test_BaseGenericDateTransformer import GenericDatesMixinTransformTests


class TestInit(
Expand All @@ -29,7 +29,7 @@ def setup_class(cls):
cls.transformer_name = "BaseDateTwoColumnTransformer"


class TestTransform(GenericTransformTests, DatesMixinTransformTests):
class TestTransform(GenericTransformTests, GenericDatesMixinTransformTests):
"""Tests for BaseTwoColumnDateTransformer.transform."""

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions tests/dates/test_BetweenDatesTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
NewColumnNameInitMixintests,
OtherBaseBehaviourTests,
)
from tests.dates.test_BaseDateTransformer import (
DatesMixinTransformTests,
from tests.dates.test_BaseGenericDateTransformer import (
GenericDatesMixinTransformTests,
create_date_diff_different_dtypes,
)
from tubular.dates import BetweenDatesTransformer
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_wrong_col_count_error(self, columns):

class TestTransform(
GenericTransformTests,
DatesMixinTransformTests,
GenericDatesMixinTransformTests,
DropOriginalTransformMixinTests,
):
"""Tests for BetweenDatesTransformer.transform."""
Expand Down
18 changes: 9 additions & 9 deletions tests/dates/test_DateTimeInfoExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,22 @@ def test_valid_nan_output(self, timeofday_extractor):

class TestTransform:
@pytest.mark.parametrize(
("columns"),
("bad_column", "bad_type"),
[
["numeric_col"],
["string_col"],
["bool_col"],
["empty_col"],
["date_col"],
["numeric_col", "int64"],
["string_col", "object"],
["bool_col", "bool"],
["empty_col", "object"],
["date_col", "date"],
],
)
def test_input_data_check_column_errors(self, columns):
def test_input_data_check_column_errors(self, bad_column, bad_type):
"""Check that errors are raised on a variety of different non datatypes"""
x = DatetimeInfoExtractor(columns=columns)
x = DatetimeInfoExtractor(columns=bad_column)

df = d.create_date_diff_incorrect_dtypes()

msg = rf"{x.classname()}: {columns[0]} type should be in \['datetime64'\] but got {df[columns[0]].dtype}"
msg = rf"{x.classname()}: {bad_column} type should be in \['datetime64'\] but got {bad_type}"

with pytest.raises(TypeError, match=msg):
x.transform(df)
Expand Down
18 changes: 9 additions & 9 deletions tests/dates/test_DatetimeSinusoidCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,27 +219,27 @@ def test_attributes(self, example_transformer):

class TestDatetimeSinusoidCalculatorTransform:
@pytest.mark.parametrize(
("columns"),
("bad_column", "bad_type"),
[
["numeric_col"],
["string_col"],
["bool_col"],
["empty_col"],
["date_col"],
["numeric_col", "int64"],
["string_col", "object"],
["bool_col", "bool"],
["empty_col", "object"],
["date_col", "date"],
],
)
def test_input_data_check_column_errors(self, columns):
def test_input_data_check_column_errors(self, bad_column, bad_type):
"""Check that errors are raised on a variety of different non datatypes"""
x = DatetimeSinusoidCalculator(
columns,
bad_column,
"cos",
"month",
12,
)

df = d.create_date_diff_incorrect_dtypes()

msg = rf"{x.classname()}: {columns[0]} type should be in \['datetime64'\] but got {df[columns[0]].dtype}"
msg = rf"{x.classname()}: {bad_column} type should be in \['datetime64'\] but got {bad_type}"

with pytest.raises(TypeError, match=msg):
x.transform(df)
Expand Down
18 changes: 9 additions & 9 deletions tests/dates/test_SeriesDtMethodTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,26 @@ def expected_df_3():
return df

@pytest.mark.parametrize(
("columns"),
("bad_column", "bad_type"),
[
["numeric_col"],
["string_col"],
["bool_col"],
["empty_col"],
["date_col"],
("numeric_col", "int64"),
("string_col", "object"),
("bool_col", "bool"),
("empty_col", "object"),
("date_col", "date"),
],
)
def test_input_data_check_column_errors(self, columns):
def test_input_data_check_column_errors(self, bad_column, bad_type):
"""Check that errors are raised on a variety of different non datatypes"""
x = SeriesDtMethodTransformer(
new_column_name="a2",
pd_method_name="year",
column=columns[0],
column=bad_column,
)

df = d.create_date_diff_incorrect_dtypes()

msg = rf"{x.classname()}: {columns[0]} type should be in \['datetime64'\] but got {df[columns[0]].dtype}"
msg = rf"{x.classname()}: {x.columns[0]} type should be in \['datetime64'\] but got {bad_type}"

with pytest.raises(TypeError, match=msg):
x.transform(df)
Expand Down
Loading
Loading