Exemplo n.º 1
0
    def test_try_to_specify_non_type_safe_return_type__simple_type(self):
        for type_safety in TypeSafety:
            with self.subTest():
                # Method
                with self.assertRaises(MockTypeSafetyError):
                    with tmock(MyThing, type_safety=type_safety) as my_thing_mock:
                        when(my_thing_mock.convert_int_to_str(1)).then_return(2)

                # Attribute get
                with self.assertRaises(MockTypeSafetyError):
                    with tmock(MyThing, type_safety=type_safety) as my_thing_mock:
                        when(my_thing_mock.a_hinted_str_attribute).then_return(1)
Exemplo n.º 2
0
    def test_mock__unmocked_method__NoBehaviourError(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                my_thing_mock: MyThing = tmock(mocked_thing)

                with self.assertRaises(NoBehaviourSpecifiedError):
                    my_thing_mock.return_a_str()
Exemplo n.º 3
0
    def test_mock__class_attribute__get__no_behaviour(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                my_thing_mock = tmock(mocked_thing)

                result = my_thing_mock.class_att_with_type

                self.assertEqual(mocked_thing.class_att_with_type, result)
Exemplo n.º 4
0
    def test_mock__try_to_mock_method_out_of_context(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                my_thing_mock: MyThing = tmock(mocked_thing)

                with self.assertRaises(NoBehaviourSpecifiedError):
                    when(my_thing_mock.return_a_str()).then_return(
                        "some string")
Exemplo n.º 5
0
    def test_specify_any_matcher_arg__called_with_correct_type__raise_error(
            self):
        with tmock(MyThing) as my_thing_mock:
            when(my_thing_mock.convert_int_to_str(
                match.anything())).then_raise(IOError)

        with self.assertRaises(IOError):
            my_thing_mock.convert_int_to_str(2)
Exemplo n.º 6
0
    def test_try_to_set_attribute_with_incorrect_type(self):
        for type_safety in TypeSafety:
            with self.subTest():
                my_thing_mock = tmock(MyThing, type_safety=type_safety)

                # Method
                with self.assertRaises(MockTypeSafetyError):
                    my_thing_mock.a_hinted_str_attribute = 1
Exemplo n.º 7
0
    def test_validate_class_type_hints__no_return_is_none_return(self):
        expected_missing_type_hints = [
            MissingHint(['class_att_with_unhinted_init'], MemberType.ATTRIBUTE),
            MissingHint(['unhinted_class_att'], MemberType.ATTRIBUTE),
            MissingHint(['instance_att_hinted_no_init'], MemberType.ATTRIBUTE),
            MissingHint(['instance_att_unhinted_no_init'], MemberType.ATTRIBUTE),
            MissingHint(['instance_att_with_unhinted_init'], MemberType.ATTRIBUTE),
            MissingHint(['method_with_args_and_kwargs', 'args'], MemberType.ARG),
            MissingHint(['method_with_args_and_kwargs', 'kwargs'], MemberType.ARG),
            MissingHint(['method_with_missing_arg_hint', 'something'], MemberType.ARG),
        ]

        with self.assertRaises(MissingTypeHintsError) as error:
            tmock(ClassWithMultipleUnHintedThings, type_safety=TypeSafety.NO_RETURN_IS_NONE_RETURN)
        actual_missing_type_hints = error.exception.args[1]

        self.assertEqual(expected_missing_type_hints, actual_missing_type_hints)
Exemplo n.º 8
0
    def test_when_we_have_matcher_based_behaviour_type_safety_is_enforced_on_call(
            self):
        expected = "a string"
        with tmock(MyThing) as my_thing_mock:
            when(my_thing_mock.convert_int_to_str(
                match.anything())).then_return(expected)

        with self.assertRaises(MockTypeSafetyError):
            my_thing_mock.convert_int_to_str("not an int")
Exemplo n.º 9
0
    async def test_we_can_mock_and_verify_an_async_method(self):
        expected = "Hello"

        with tmock(MyAsyncThing) as my_async_mock:
            when(await
                 my_async_mock.get_an_async_result()).then_return(expected)

        self.assertEqual(expected, await my_async_mock.get_an_async_result())

        verify(my_async_mock).get_an_async_result()
Exemplo n.º 10
0
    def test_specify_any_matcher_arg__called_with_correct_type__return_many(
            self):
        expected_many = ["a string"]
        with tmock(MyThing) as my_thing_mock:
            when(my_thing_mock.convert_int_to_str(
                match.anything())).then_return_many(expected_many)

        for expected in expected_many:
            actual = my_thing_mock.convert_int_to_str(2)
            self.assertEqual(expected, actual)
Exemplo n.º 11
0
    def test_mock__can_mock_method__no_args__no_return(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.do_something_with_side_effects()
                         ).then_return(None)

                my_thing_mock.do_something_with_side_effects()

                verify(my_thing_mock).do_something_with_side_effects()
Exemplo n.º 12
0
    def test_verify__any_matcher__not_called(self):
        with tmock(MyThing) as my_thing_mock:
            when(my_thing_mock.convert_int_to_str(1)).then_return("something")
            when(my_thing_mock.some_instance_attribute).then_return("hello")

        with self.assertRaises(VerifyError):
            verify(my_thing_mock).convert_int_to_str(match.anything())

        with self.assertRaises(VerifyError):
            verify(my_thing_mock).some_instance_attribute = match.anything()
Exemplo n.º 13
0
    def test_mock__can_mock_method__default_args(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.method_with_default_args(
                        first_number=1)).then_return(None)

                my_thing_mock.method_with_default_args(1)

                verify(my_thing_mock).method_with_default_args(1)
Exemplo n.º 14
0
    def test_mock__property__get__simple_return(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                expected = "hello"

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.derived_property).then_return(expected)

                actual = my_thing_mock.derived_property

                self.assertEqual(expected, actual)
Exemplo n.º 15
0
    def test_mock__then_raise(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_error = IOError()

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.do_something_with_side_effects()
                         ).then_raise(expected_error)

                with self.assertRaises(IOError):
                    my_thing_mock.do_something_with_side_effects()
Exemplo n.º 16
0
    def test_mock__generic_attribute__get__simple_return(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                expected = ["hello"]

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.generic_att).then_return(expected)

                actual = my_thing_mock.generic_att

                self.assertEqual(expected, actual)
Exemplo n.º 17
0
    def test_mock__class_attribute__get__then_raise(self):
        expected_error = IOError()

        for mocked_thing in mocked_things:
            with self.subTest():
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_raise(
                        expected_error)

                with self.assertRaises(IOError):
                    my_thing_mock.class_att_with_type
Exemplo n.º 18
0
    def test_mock__class_attribute__get__simple_return(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                expected = 3

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_return(
                        expected)

                actual = my_thing_mock.class_att_with_type

                self.assertEqual(expected, actual)
Exemplo n.º 19
0
    def test_mock__instance_attribute__get__simple_return(self):
        expected = 2

        for mocked_thing in mocked_things:
            with self.subTest():
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.instance_att_typed_init).then_return(
                        expected)

                actual = my_thing_mock.instance_att_typed_init

                self.assertEqual(expected, actual)
Exemplo n.º 20
0
    def test_mock__then_return_many__loop_true(self):
        expected_responses = ["first response", "second response"]

        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.return_a_str()).then_return_many(
                        expected_responses, True)

                for i in range(2):
                    for expected in expected_responses:
                        actual = my_thing_mock.return_a_str()
                        self.assertEqual(expected, actual)
Exemplo n.º 21
0
    def test_mock__can_mock_method__arg__returns(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = "a string"

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.convert_int_to_str(1)).then_return(
                        expected_result)

                actual = my_thing_mock.convert_int_to_str(1)

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock).convert_int_to_str(1)
Exemplo n.º 22
0
    def test_mock__class_attribute__get__many(self):
        expected_responses = [3, 4]

        for mocked_thing in mocked_things:
            with self.subTest():

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_return_many(
                        expected_responses)

                for expected in expected_responses:
                    actual = my_thing_mock.class_att_with_type
                    self.assertEqual(expected, actual)
Exemplo n.º 23
0
    def test_mock__class_attribute__set_then_get(self):
        for mocked_thing in mocked_things:
            with self.subTest():
                mocked_with = 1

                with tmock(MyThing) as my_thing_mock:
                    when(my_thing_mock.instance_att_typed_init).then_return(
                        mocked_with)

                setting_to = 3

                my_thing_mock.class_att_with_type = setting_to

                self.assertEqual(mocked_thing.class_att_with_type, mocked_with)
Exemplo n.º 24
0
    def test_mock__can_mock_method__multiple_args__mixed_with_kwargs_in_setup(
            self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = "a string"

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.multiple_arg(
                        number=1, prefix="p")).then_return(expected_result)

                actual = my_thing_mock.multiple_arg("p", 1)

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock).multiple_arg("p", 1)
Exemplo n.º 25
0
    def test_mock__class_attribute__get__then_do(self):
        expected = 3

        def handle_get():
            return expected

        for mocked_thing in mocked_things:
            with self.subTest():
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.class_att_with_type).then_do(handle_get)

                actual = my_thing_mock.class_att_with_type

                self.assertEqual(expected, actual)
Exemplo n.º 26
0
    def test_verify__any_matcher(self):
        with tmock(MyThing) as my_thing_mock:
            when(my_thing_mock.convert_int_to_str(1)).then_return("something")
            when(my_thing_mock.some_instance_attribute).then_return("hello")

        # Method
        my_thing_mock.convert_int_to_str(1)

        verify(my_thing_mock).convert_int_to_str(match.anything())

        # Set Attribute
        my_thing_mock.some_instance_attribute = "bye"

        verify(my_thing_mock).some_instance_attribute = match.anything()
Exemplo n.º 27
0
    def test_mock__can_mock_method__object_arg__returns(self):
        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                expected_result = 2

                object_arg = NestedThing()

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.method_with_object(
                        object_arg)).then_return(expected_result)

                actual = my_thing_mock.method_with_object(object_arg)

                self.assertEqual(expected_result, actual)
                verify(my_thing_mock).method_with_object(object_arg)
Exemplo n.º 28
0
    def test_mock__then_return_many__loop_false(self):
        expected_responses = ["first response", "second response"]

        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):

                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.return_a_str()).then_return_many(
                        expected_responses, False)

                for expected in expected_responses:
                    actual = my_thing_mock.return_a_str()
                    self.assertEqual(expected, actual)

                # Not looping, and responses have run out.
                with self.assertRaises(NoBehaviourSpecifiedError):
                    my_thing_mock.return_a_str()
Exemplo n.º 29
0
    def test_verify__exactly__not_matched__other_calls__verify_error(self):
        with tmock(MyThing) as my_thing_mock:
            when(my_thing_mock.some_instance_attribute).then_return("Hello")
            when(my_thing_mock.convert_int_to_str(
                match.anything())).then_return("A string")

        my_thing_mock.convert_int_to_str(1)

        # Method
        with self.assertRaises(VerifyError):
            verify(my_thing_mock, exactly=2).convert_int_to_str(2)

        my_thing_mock.some_instance_attribute = "something else"

        # Set attribute
        with self.assertRaises(VerifyError):
            verify(my_thing_mock, exactly=2).some_instance_attribute = "bye"
Exemplo n.º 30
0
    def test_mock__then_do(self):

        expected_arg = 1

        def bounce_back_handler(number: int):
            assert number == expected_arg
            return "{}".format(number)

        for mocked_thing in mocked_things:
            with self.subTest("{}".format(mocked_thing)):
                with tmock(mocked_thing) as my_thing_mock:
                    when(my_thing_mock.convert_int_to_str(
                        match.anything())).then_do(bounce_back_handler)

            actual = my_thing_mock.convert_int_to_str(expected_arg)

            self.assertEqual("1", actual)