Exemplo n.º 1
0
    def test_starting_patcher_already_started_raises(self):
        domain = get_domain()
        patcher = patch_port(domain, port_name='c')
        patcher.start()

        with self.assertRaisesRegexp(RuntimeError, 'patch already started'):
            patcher.start()
Exemplo n.º 2
0
    def test_stopping_patcher_twice_raises(self):
        domain = get_domain()
        patcher = patch_port(domain, port_name='c')
        patcher.start()
        patcher.stop()

        with self.assertRaisesRegexp(RuntimeError, 'patcher not yet started'):
            patcher.stop()
Exemplo n.º 3
0
    def test_patch_port_as_context_manager(self):
        domain = get_domain()

        self.assertEqual(9 * 10 + 5, domain.a(9))  # before patch

        with patch_port(component=domain, port_name='b') as mock_b:
            mock_b.side_effect = lambda value: 100 * value
            self.assertEqual(100 * 9, domain.a(9))  # patched

        mock_b.assert_called_once_with(value=9)
        self.assertEqual(9 * 10 + 5, domain.a(9))  # patch removed outside of context
Exemplo n.º 4
0
    def test_patch_port_with_manual_start_stop(self):
        domain = get_domain()
        patcher = patch_port(component=domain, port_name='b')

        self.assertEqual(9 * 10 + 5, domain.a(9))  # before patch started

        mock_b = patcher.start()
        mock_b.side_effect = lambda value: 100 * value
        self.assertEqual(100 * 9, domain.a(9))  # patched

        patcher.stop()
        self.assertEqual(9 * 10 + 5, domain.a(9))  # patch removed once stopped
Exemplo n.º 5
0
    def test_patch_port_affects_all_consumers_of_a_port(self):
        domain = get_domain()

        # before patch
        self.assertEqual(9 * 10 + 5, domain.a(9))
        self.assertEqual(2 * 9 * 10 + 5, domain.e(9))

        with patch_port(component=domain, port_name='b') as mock_b:
            mock_b.side_effect = lambda value: 100 * value
            self.assertEqual(100 * 9, domain.a(9))  # patched
            self.assertEqual(100 * 2 * 9, domain.e(9))  # patched

        mock_b.assert_has_calls([mock.call(value=9), mock.call(value=2 * 9)], any_order=False)

        # patch removed outside of context
        self.assertEqual(9 * 10 + 5, domain.a(9))
        self.assertEqual(2 * 9 * 10 + 5, domain.e(9))
Exemplo n.º 6
0
 def test_return_value_can_be_specified_during_patching(self):
     domain = get_domain()
     with patch_port(domain, port_name='c', return_value=100):
         self.assertEqual(10 * 9 + 100, domain.a(9))
Exemplo n.º 7
0
 def test_side_effect_can_be_specified_during_patching(self):
     domain = get_domain()
     with patch_port(domain, port_name='c', side_effect=ValueError):
         with self.assertRaises(ValueError):
             domain.a(9)
Exemplo n.º 8
0
 def test_default_behaviour_of_mock_obj(self):
     domain = get_domain()
     with patch_port(domain, port_name='c') as m:
         self.assertIsNone(m.side_effect)
         self.assertIsInstance(m.return_value, mock.mock.Mock)
Exemplo n.º 9
0
    def test_patch_port_with_disconnected_port_raises(self):
        domain = get_domain()
        domain2 = domain.__class__()  # get new instance of domain where 'c' port is not yet connected

        with self.assertRaisesRegexp(DisconnectedPort, 'B.c is disconnected'):
            patch_port(domain2, port_name='c')
Exemplo n.º 10
0
    def test_patch_port_with_unknown_port_raises(self):
        domain = get_domain()

        with self.assertRaisesRegexp(UnknownPort, 'Could not find instances of port "unknown_port"'):
            patch_port(domain, port_name='unknown_port')