Exemplo n.º 1
0
 def test_can_be_used_with_various_iterators(self):
     side_effect = effect((call(1), itertools.repeat(10, 3)))
     for i in range(3):
         self.assertEqual(side_effect(1), 10)
     with self.assertRaises(TypeError):
         side_effect(1)
     #trying another iterator
     side_effect = effect((call(1), itertools.chain([1, 1, 1], [2, 2, 2])))
     for i in range(3):
         self.assertEqual(side_effect(1), 1)
     for i in range(3):
         self.assertEqual(side_effect(1), 2)
     with self.assertRaises(TypeError):
         side_effect(1)
Exemplo n.º 2
0
 def test_can_be_used_with_various_iterators(self):
     side_effect = effect((call(1), itertools.repeat(10, 3)))
     for i in range(3):
         self.assertEqual(side_effect(1), 10)
     with self.assertRaises(TypeError):
         side_effect(1)
     #trying another iterator    
     side_effect = effect((call(1), itertools.chain([1,1,1], [2,2,2])))
     for i in range(3):
         self.assertEqual(side_effect(1), 1)
     for i in range(3):
         self.assertEqual(side_effect(1), 2)
     with self.assertRaises(TypeError):
         side_effect(1)
Exemplo n.º 3
0
 def test_empty_sequential_config_raises_exception_on_first_call(self):
     config = (
         (call('Empty iterable, raises TypeError on first call'), []), 
     )
     side_effect = effect(*config)
     with self.assertRaises(TypeError):
         side_effect('Empty iterable, raises TypeError on first call')
Exemplo n.º 4
0
 def test_you_can_put_data_structures_into_infinite_sequential_configs(self):
     config = (
         (call('Always a dict'), itertools.cycle([{'this': 'is a dict in a list'}])), 
     )
     side_effect = effect(*config)
     self.assertEqual(side_effect('Always a dict'), {'this': 'is a dict in a list'})
     self.assertEqual(side_effect('Always a dict'), {'this': 'is a dict in a list'})
Exemplo n.º 5
0
 def test_sequential_config_with_one_element_called_only_once(self):
     config = ((call('with 2, followed by TypeError'), ['with 2']), )
     side_effect = effect(*config)
     self.assertEqual(side_effect('with 2, followed by TypeError'),
                      'with 2')
     with self.assertRaises(TypeError):
         side_effect('with 2, followed by TypeError')
Exemplo n.º 6
0
 def test_dicts_should_be_returned_as_is(self):
     config = (
         (call('A dict should always be returned as is'), {'this': 'is a dict'}), 
     )
     side_effect = effect(*config)
     self.assertEqual(side_effect('A dict should always be returned as is'), {'this': 'is a dict'})
     self.assertEqual(side_effect('A dict should always be returned as is'), {'this': 'is a dict'})
Exemplo n.º 7
0
    def test_can_be_used_to_return_class(self):
        class Some(object):
            pass

        side_effect = effect((call(1), Some))
        self.assertEqual(side_effect(1), Some)
        self.assertEqual(side_effect(1), Some)  #always returns Some
Exemplo n.º 8
0
 def test_sequential_config_with_one_element_called_only_once(self):
     config = (
         (call('with 2, followed by TypeError'), ['with 2']), 
     )
     side_effect = effect(*config)
     self.assertEqual(side_effect('with 2, followed by TypeError'), 'with 2')
     with self.assertRaises(TypeError):
         side_effect('with 2, followed by TypeError')
Exemplo n.º 9
0
    def test_creates_callable_mapped_to_config_dict(self):
        config = [(call(1), 2), (call("a"), 3), (call(1, b=2), 4), (call(c=3), 5)]
        side_effecet = effect(*config)

        self.assertEqual(side_effecet(1), 2)
        self.assertEqual(side_effecet("a"), 3)
        self.assertEqual(side_effecet(1, b=2), 4)
        self.assertEqual(side_effecet(c=3), 5)
Exemplo n.º 10
0
 def test_you_can_put_data_structures_into_sequential_configs(self):
     config = (
         (call('returns a dict and then TypeError'), [{'this': 'is a dict in a list'}]), 
     )
     side_effect = effect(*config)
     self.assertEqual(side_effect('returns a dict and then TypeError'), {'this': 'is a dict in a list'})
     with self.assertRaises(TypeError):
         side_effect('returns a dict and then TypeError')
Exemplo n.º 11
0
 def test_using_cycle_config_can_be_called_infinitely(self):
     config = ((call('Can be called forever'),
                itertools.cycle(['with 1', 'with 1 again'])), )
     side_effect = effect(*config)
     self.assertEqual(side_effect('Can be called forever'), 'with 1')
     self.assertEqual(side_effect('Can be called forever'), 'with 1 again')
     self.assertEqual(side_effect('Can be called forever'), 'with 1')
     self.assertEqual(side_effect('Can be called forever'), 'with 1 again')
Exemplo n.º 12
0
 def test_can_be_used_to_return_mock_instance(self):
     side_effect = effect((call(1), Mock()))
     self.assertEqual(
         side_effect(1).__class__.__name__,
         Mock().__class__.__name__)
     self.assertEqual(
         side_effect(1).__class__.__name__,
         Mock().__class__.__name__)  #always returns a Mock instance
Exemplo n.º 13
0
    def test_can_be_used_to_return_instance(self):
        class Some(object):
            pass

        side_effect = effect((call(1), Some()))
        self.assertEqual(side_effect(1).__class__, Some().__class__)
        self.assertEqual(side_effect(1).__class__,
                         Some().__class__)  #always returns a Some instance
Exemplo n.º 14
0
 def test_sequential_config_with_multiple_elements_each_called_only_once(self):
     config = (
         (call('with 1, again and then TypeError'), ['with 1', 'with 1 again']), 
     )
     side_effect = effect(*config)
     self.assertEqual(side_effect('with 1, again and then TypeError'), 'with 1')
     self.assertEqual(side_effect('with 1, again and then TypeError'), 'with 1 again')
     with self.assertRaises(TypeError):
         side_effect('with 1, again and then TypeError')
Exemplo n.º 15
0
 def test_using_cycle_config_can_be_called_infinitely(self):
     config = (
         (call('Can be called forever'), itertools.cycle(['with 1', 'with 1 again'])), 
     )
     side_effect = effect(*config)
     self.assertEqual(side_effect('Can be called forever'), 'with 1')
     self.assertEqual(side_effect('Can be called forever'), 'with 1 again')
     self.assertEqual(side_effect('Can be called forever'), 'with 1')
     self.assertEqual(side_effect('Can be called forever'), 'with 1 again')
Exemplo n.º 16
0
    def test_creates_callable_mapped_to_config_dict(self):
        config = [(call(1), 2), (call('a'), 3), (call(1, b=2), 4),
                  (call(c=3), 5)]
        side_effect = effect(*config)

        self.assertEqual(side_effect(1), 2)
        self.assertEqual(side_effect('a'), 3)
        self.assertEqual(side_effect(1, b=2), 4)
        self.assertEqual(side_effect(c=3), 5)
Exemplo n.º 17
0
 def test_dicts_should_be_returned_as_is(self):
     config = ((call('A dict should always be returned as is'), {
         'this': 'is a dict'
     }), )
     side_effect = effect(*config)
     self.assertEqual(side_effect('A dict should always be returned as is'),
                      {'this': 'is a dict'})
     self.assertEqual(side_effect('A dict should always be returned as is'),
                      {'this': 'is a dict'})
Exemplo n.º 18
0
 def test_you_can_put_data_structures_into_sequential_configs(self):
     config = ((call('returns a dict and then TypeError'), [{
         'this':
         'is a dict in a list'
     }]), )
     side_effect = effect(*config)
     self.assertEqual(side_effect('returns a dict and then TypeError'),
                      {'this': 'is a dict in a list'})
     with self.assertRaises(TypeError):
         side_effect('returns a dict and then TypeError')
Exemplo n.º 19
0
 def test_you_can_put_data_structures_into_infinite_sequential_configs(
         self):
     config = ((call('Always a dict'),
                itertools.cycle([{
                    'this': 'is a dict in a list'
                }])), )
     side_effect = effect(*config)
     self.assertEqual(side_effect('Always a dict'),
                      {'this': 'is a dict in a list'})
     self.assertEqual(side_effect('Always a dict'),
                      {'this': 'is a dict in a list'})
Exemplo n.º 20
0
 def test_sequential_config_with_multiple_elements_each_called_only_once(
         self):
     config = ((call('with 1, again and then TypeError'),
                ['with 1', 'with 1 again']), )
     side_effect = effect(*config)
     self.assertEqual(side_effect('with 1, again and then TypeError'),
                      'with 1')
     self.assertEqual(side_effect('with 1, again and then TypeError'),
                      'with 1 again')
     with self.assertRaises(TypeError):
         side_effect('with 1, again and then TypeError')
Exemplo n.º 21
0
 def test_sequential_config_with_generator(self):
     def ten_times():
         for i in range(9):
             yield i
     config = (
         (call('can be called ten times'), ten_times()),#note generator must be called
     )
     side_effect = effect(*config)
     for i in range(9):
         self.assertEqual(side_effect('can be called ten times'), i)
     with self.assertRaises(TypeError):
         side_effect('can be called ten times')
Exemplo n.º 22
0
    def test_sequential_config_with_generator(self):
        def ten_times():
            for i in range(9):
                yield i

        config = (
            (call('can be called ten times'),
             ten_times()),  #note generator must be called
        )
        side_effect = effect(*config)
        for i in range(9):
            self.assertEqual(side_effect('can be called ten times'), i)
        with self.assertRaises(TypeError):
            side_effect('can be called ten times')
Exemplo n.º 23
0
 def test_sequential_callable_with_a_bunch_of_configs(self):
     config = ((call(1), itertools.cycle(['with 1', 'with 1 again'])),
               (call(2), 'with 2'), (call(3), [
                   'with 3',
               ]), (call(4), []), (call(5), [{
                   'this': 'is a dict in a list'
               }]), (call(6), {
                   'this': 'is a dict'
               }))
     side_effect = effect(*config)
     self.assertEqual(side_effect(1), 'with 1')
     self.assertEqual(side_effect(2), 'with 2')
     self.assertEqual(side_effect(3), 'with 3')
     self.assertEqual(side_effect(1), 'with 1 again')
     self.assertEqual(side_effect(1), 'with 1')
     self.assertEqual(side_effect(2), 'with 2')  #second call
     with self.assertRaises(TypeError):
         side_effect(3)
     with self.assertRaises(TypeError):
         side_effect(4)
     self.assertEqual(side_effect(5), {'this': 'is a dict in a list'})
     with self.assertRaises(TypeError):
         side_effect(5)
     self.assertEqual(side_effect(6), {'this': 'is a dict'})
Exemplo n.º 24
0
 def test_sequential_callable_with_a_bunch_of_configs(self):
     config = (
         (call(1), itertools.cycle(['with 1', 'with 1 again'])), 
         (call(2), 'with 2'), 
         (call(3), ['with 3',]),
         (call(4), []),
         (call(5), [{'this': 'is a dict in a list'}]),
         (call(6), {'this': 'is a dict'})
     )
     side_effect = effect(*config)
     self.assertEqual(side_effect(1), 'with 1')
     self.assertEqual(side_effect(2), 'with 2')
     self.assertEqual(side_effect(3), 'with 3')
     self.assertEqual(side_effect(1), 'with 1 again')
     self.assertEqual(side_effect(1), 'with 1')
     self.assertEqual(side_effect(2), 'with 2')#second call
     with self.assertRaises(TypeError):
         side_effect(3)
     with self.assertRaises(TypeError):
         side_effect(4)
     self.assertEqual(side_effect(5), {'this': 'is a dict in a list'})
     with self.assertRaises(TypeError):
         side_effect(5)
     self.assertEqual(side_effect(6), {'this': 'is a dict'})
Exemplo n.º 25
0
 def test_empty_sequential_config_raises_exception_on_first_call(self):
     config = ((call('Empty iterable, raises TypeError on first call'),
                []), )
     side_effect = effect(*config)
     with self.assertRaises(TypeError):
         side_effect('Empty iterable, raises TypeError on first call')
Exemplo n.º 26
0
 def test_can_be_used_to_return_mock_instance(self):
     side_effect = effect((call(1), Mock()))
     self.assertEqual(side_effect(1).__class__.__name__, Mock().__class__.__name__)
     self.assertEqual(side_effect(1).__class__.__name__, Mock().__class__.__name__)#always returns a Mock instance
Exemplo n.º 27
0
 def test_raises_type_error_when_called_with_unknown_args(self):
     side_effect = effect((call(1), 5))
     self.assertRaises(TypeError, side_effect, 'junk')
Exemplo n.º 28
0
 def test_can_be_used_with_mutable_data_structs(self):
     side_effect = effect((call([1, 2, 3]), 'list'))
     self.assertEqual(side_effect([1, 2, 3]), 'list')
Exemplo n.º 29
0
 def test_can_be_used_to_return_instance(self):
     class Some(object):
         pass
     side_effect = effect((call(1), Some()))
     self.assertEqual(side_effect(1).__class__, Some().__class__)
     self.assertEqual(side_effect(1).__class__, Some().__class__)#always returns a Some instance
Exemplo n.º 30
0
 def test_raises_type_error_when_called_with_unknown_args(self):
     side_effect = effect((call(1), 5))
     self.assertRaises(TypeError, side_effect, 'junk')
Exemplo n.º 31
0
 def test_can_be_used_with_mutable_data_structs(self):
     side_effect = effect((call([1, 2, 3]), 'list'))
     self.assertEqual(side_effect([1, 2, 3]), 'list')
Exemplo n.º 32
0
 def test_can_be_used_to_return_class(self):
     class Some(object):
         pass
     side_effect = effect((call(1), Some))
     self.assertEqual(side_effect(1), Some)
     self.assertEqual(side_effect(1), Some)#always returns Some