def test___missing___returns_key():
    """Test that __missing__ returns the passed key."""

    x = ReturnKeyDict()

    k = "a"

    result = x.__missing__(k)

    assert k == result, "passed key not returned from __missing__"
Exemplo n.º 2
0
    def test_super_init_called(self, mocker):
        """Test that init calls BaseMappingTransformer.init."""

        spy = mocker.spy(tubular.mapping.BaseMappingTransformer, "__init__")

        x = MappingTransformer(mappings={"a": {"a": 1}}, verbose=True, copy=True)

        assert (
            spy.call_count == 1
        ), "unexpected number of calls to BaseMappingTransformer.__init__"

        call_args = spy.call_args_list[0]
        call_pos_args = call_args[0]
        call_kwargs = call_args[1]

        expected_kwargs = {
            "mappings": {"a": ReturnKeyDict({"a": 1})},
            "verbose": True,
            "copy": True,
        }

        assert (
            call_kwargs == expected_kwargs
        ), "unexpected kwargs in BaseMappingTransformer.__init__ call"

        expected_pos_args = (x,)

        assert (
            expected_pos_args == call_pos_args
        ), "unexpected positional args in BaseMappingTransformer.__init__ call"
def test_has___missing___method():
    """Test that ReturnKeyDict has a __missing__ method."""

    x = ReturnKeyDict()

    h.test_object_method(x, "__missing__",
                         "ReturnKeyDict does not have __missing__ method")
def test_keyerror_not_raised():
    """Test that a key error is not raised, instead key is returned, in attempt to access key not present in dict."""

    d = ReturnKeyDict({"a": 1})

    result = d["b"]

    assert (
        result == "b"
    ), "passed key not returned from when attempting to lookup but not present in dict"
Exemplo n.º 5
0
    def test_mapping_arg_conversion(self):
        """Test that sub dict in mappings arg are converted into ReturnKeyDict objects."""

        mappings = {
            "a": {"a": 1, "b": 2},
            "b": {1: 4.5, 2: 3.1},
            "c": {False: None, True: 1},
        }

        expected_mappings = {
            "a": ReturnKeyDict(mappings["a"]),
            "b": ReturnKeyDict(mappings["b"]),
            "c": ReturnKeyDict(mappings["c"]),
        }

        x = MappingTransformer(mappings=mappings)

        h.assert_equal_dispatch(
            expected_mappings,
            x.mappings,
            "mappings attribute not correctly converted sub dicts to ReturnKeyDict",
        )
Exemplo n.º 6
0
    def test_mappings_unchanged(self):
        """Test that mappings is unchanged in transform."""

        df = d.create_df_1()

        mapping = {
            "a": {1: "a", 2: "b", 3: "c", 4: "d", 5: "e", 6: "f"},
            "b": {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6},
        }

        preserve_original_value_mapping = {
            "a": ReturnKeyDict(mapping["a"]),
            "b": ReturnKeyDict(mapping["b"]),
        }

        x = MappingTransformer(mappings=mapping)

        x.transform(df)

        h.assert_equal_dispatch(
            actual=x.mappings,
            expected=preserve_original_value_mapping,
            msg="MappingTransformer.transform has changed self.mappings unexpectedly",
        )
Exemplo n.º 7
0
    def __init__(self, mappings, **kwargs):

        return_key_mappings = copy.deepcopy(mappings)

        for k, v in mappings.items():

            if type(v) is dict:

                return_key_mappings[k] = ReturnKeyDict(v)

            else:

                raise TypeError(
                    f"each item in mappings should be a dict but got type {type(v)} for key {k}"
                )

        BaseMappingTransformer.__init__(self,
                                        mappings=return_key_mappings,
                                        **kwargs)
Exemplo n.º 8
0
        assert (
            mocked_function_not_call.call_count == 0
        ), f"Unexpected number of calls to {test_function_not_call} -\n  Expected:  0\n  Actual:  {mocked_function_not_call.call_count}"


@pytest.mark.parametrize(
    "expected_function_called, value_to_pass",
    [
        ("tubular.testing.helpers.assert_list_tuple_equal_msg", [1, 2]),
        ("tubular.testing.helpers.assert_list_tuple_equal_msg", (1, 2)),
        ("tubular.testing.helpers.assert_dict_equal_msg", {
            "a": 1
        }),
        (
            "tubular.testing.helpers.assert_dict_equal_msg",
            ReturnKeyDict({"a": 1}),
        ),
        ("tubular.testing.helpers.assert_equal_msg", 1),
        ("tubular.testing.helpers.assert_equal_msg", 1.0),
        ("tubular.testing.helpers.assert_equal_msg", "a"),
        ("tubular.testing.helpers.assert_equal_msg", False),
        ("tubular.testing.helpers.assert_equal_msg", None),
        ("tubular.testing.helpers.assert_equal_msg", np.float64(1)),
        ("tubular.testing.helpers.assert_equal_msg", np.int64(1)),
        ("tubular.testing.helpers.assert_np_nan_eqal_msg", np.NaN),
        ("tubular.testing.helpers.assert_np_nan_eqal_msg", np.float64(np.NaN)),
    ],
)
def test_non_dataframe_correct_function_call(mocker, expected_function_called,
                                             value_to_pass):
    """Test that the correct 'sub' assert function is called if expected for the given input type - and none
def test_inheritance():
    """Test ReturnKeyDict inherits from dict."""

    x = ReturnKeyDict()

    h.assert_inheritance(x, dict)