def test_should_not_count_in_stubbed_invocations(self): when(self.tester).has_a_call(call.add('test')).then_return(False) when(self.tester).has_a_call(call.add('test')).then_return(True) self.tester.add('test') self.tester.add('test') verify(self.tester, times(2)).had_called_with(call.add('test'))
def test_should_allow_verifying_interaction_never_happened(self): self.tester.add('one') verify(self.tester, never()).had_called_with(call.add('two')) verify(self.tester, never()).had_called_with(call.clear()) with raises(NeverWantedButInvoked): verify(self.tester, never()).had_called_with(call.add('one'))
def test_move_rollback(self, search_response, defaults): """ Test move with rollback. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' dn2 = 'uid=tux,ou=Groups,dc=python-ldap,dc=org' old_base = 'ou=People,dc=python-ldap,dc=org' new_base = 'ou=Groups,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with pytest.raises(tldap.exceptions.TestFailure): with tldap.transaction.commit_on_success(): c.rename( dn, "uid=tux", "ou=Groups,dc=python-ldap,dc=org", ) c.fail() # raises TestFailure during commit causing rollback c.commit() expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.modify_dn(dn, 'uid=tux', new_superior=new_base), call.modify_dn(dn2, 'uid=tux', new_superior=old_base), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_rename_success(self, search_response, defaults): """ Test rename with success. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' dn2 = 'uid=tuz,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with tldap.transaction.commit_on_success(): c.rename( dn, 'uid=tuz', ) c.modify(dn2, { 'sn': [(ldap3.MODIFY_REPLACE, [b"Tuz"])] }) expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.modify_dn(dn, 'uid=tuz', new_superior=None), call.search(dn2, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn2, {'sn': [('MODIFY_REPLACE', [b'Tuz'])]}), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_rename_rollback(self, search_response, defaults): """ Test rename with rollback. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' dn2 = 'uid=tuz,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with pytest.raises(tldap.exceptions.TestFailure): with tldap.transaction.commit_on_success(): c.rename( dn, 'uid=tuz', ) c.modify(dn2, { "sn": [(ldap3.MODIFY_REPLACE, [b"Tuz"])] }) c.fail() # raises TestFailure during commit causing rollback c.commit() expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.modify_dn(dn, 'uid=tuz', new_superior=None), call.search(dn2, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn2, {'sn': [('MODIFY_REPLACE', [b'Tuz'])]}), call.modify(dn2, {'sn': [('MODIFY_REPLACE', [b'Torvalds'])]}), call.modify_dn(dn2, 'uid=tux', new_superior=None), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_third_statement_fails(self, search_response, defaults): """ Test success when 3rd statement fails; Need to roll back 2nd and 1st statements """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with pytest.raises(tldap.exceptions.TestFailure): with tldap.transaction.commit_on_success(): c.modify(dn, { "sn": [(ldap3.MODIFY_REPLACE, b"Milkshakes")] }) c.modify(dn, { "sn": [(ldap3.MODIFY_REPLACE, [b"Bannas"])] }) c.fail() # raises TestFailure during commit causing rollback c.commit() expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn, {'sn': [('MODIFY_REPLACE', b'Milkshakes')]}), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn, {'sn': [('MODIFY_REPLACE', [b'Bannas'])]}), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_dict_multi_key(self): mock = Mock() class DiffTuple(Diff): extract_imported = extract_existing = DictExtractor('k', 'k2') add = mock.add update = mock.update delete = mock.delete a = dict(k='a', k2=0, v=1) b = dict(k='b', k2=0, v=2) c = dict(k='c', k2=0, v=3) c_ = dict(k='c', k2=0, v=4) d = dict(k='d', k2=0, v=5) diff = DiffTuple([a, b, c], [b, c_, d]) diff.apply() compare([ call.delete(('a', 0), a, a), call.update(('c', 0), c, c, c_, c_), call.add(('d', 0), d, d), ], mock.mock_calls)
def test_named_tuple(self): mock = Mock() X = namedtuple('X', 'foo bar') Y = namedtuple('Y', 'foo bar') class DiffTuple(Diff): extract_imported = extract_existing = NamedTupleExtractor('foo') add = mock.add update = mock.update delete = mock.delete aX = X('a', 1) bX = X('b', 2) cX = X('c', 3) bY = Y('b', 2) cY = Y('c', 4) dY = Y('d', 5) daX = dict(foo='a', bar=1) dcX = dict(foo='c', bar=3) dcY = dict(foo='c', bar=4) ddY = dict(foo='d', bar=5) diff = DiffTuple([aX, bX, cX], [bY, cY, dY]) diff.apply() compare([ call.delete(('a',), aX, daX), call.update(('c',), cX, dcX, cY, dcY), call.add(('d', ), dY, ddY), ], mock.mock_calls)
def test_should_verify(self): self.tester.clear() verify(self.tester).had_called_with(call.clear()) self.tester.add("test") verify(self.tester).had_called_with(call.add("test")) verify_no_more_interactions(self.tester)
def test_should_fail_verification_on_method_argument(self): self.tester.clear() self.tester.add("foo") verify(self.tester).had_called_with(call.clear()) with raises(WantedButNotInvoked): verify(self.tester).had_called_with(call.add("bar"))
def test_should_detect_when_invoked_more_than_once(self): self.tester.add("foo") self.tester.clear() self.tester.clear() verify(self.tester).had_called_with(call.add("foo")) with raises(TooManyActualInvocations): verify(self.tester).had_called_with(call.clear())
def test_duplicate_imported_key_dealt_with_new_key(self): DiffTuple, mock = self.make_differ() def handle_imported_problem(self, key, dups): for raw, extracted in dups: yield key+str(raw[1]), raw, extracted DiffTuple.handle_imported_problem = handle_imported_problem diff = DiffTuple([], [('a', 1, 2), ('a', 3, 4)]) diff.apply() compare([ call.add('a1', ('a', 1, 2), ('a', 2)), call.add('a3', ('a', 3, 4), ('a', 4)), ], mock.mock_calls)
def test_commit_after_inserts(): # Given with patch('app.data_model.database.db_session', autospec=True) as db_session: # When db_session action within commit_or_rollback with commit_or_rollback(db_session): db_session.add('data') # Then .add() followed by .commit() db_session.assert_has_calls([call.add('data'), call.commit()])
def test_date_converter2(self): from lembrar.service import date_converter request = MagicMock() request.validated = {} request.params = {} request.params['created'] = 'crap' date_converter(request) assert [call.add('parameters', 'created', 'Cannot parse date format')] \ == request.errors.mock_calls
def test_delete_rollback(self, search_response, defaults): """ Test delete rollback. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with pytest.raises(tldap.exceptions.TestFailure): with tldap.transaction.commit_on_success(): c.delete(dn) c.fail() # raises TestFailure during commit causing rollback c.commit() expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.delete(dn), call.add(dn, None, defaults.modlist), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_post_actions(self): mock = Mock() class DiffTuple(Diff): def extract_existing(self, obj): return obj[0], obj extract_imported = extract_existing add = mock.add update = mock.update delete = mock.delete post_add = mock.post_add post_update = mock.post_update post_delete = mock.post_delete diff = DiffTuple( [('a1', 2), ('a2', 2), ('c1', 6), ('c2', 6)], [('c1', 7), ('c2', 7), ('d1', 8), ('d2', 8)] ) compare([], mock.mock_calls) diff.apply() compare([ call.delete('a1', ('a1', 2), ('a1', 2)), call.delete('a2', ('a2', 2), ('a2', 2)), call.post_delete(), call.update('c1', ('c1', 6), ('c1', 6), ('c1', 7), ('c1', 7)), call.update('c2', ('c2', 6), ('c2', 6), ('c2', 7), ('c2', 7)), call.post_update(), call.add('d1', ('d1', 8), ('d1', 8)), call.add('d2', ('d2', 8), ('d2', 8)), call.post_add(), ], mock.mock_calls)
def test_simple(self): mock_sess = Mock(spec_set=Session) pp = Mock(spec_set=BiweeklyPayPeriod) type(pp).start_date = date(2017, 1, 1) type(pp).end_date = date(2017, 1, 13) standing = Mock(spec_set=Budget, is_periodic=False) type(standing).id = 9 type(standing).name = 'standingBudget' budg1 = Mock(spec_set=Budget) type(budg1).id = 1 type(budg1).name = 'one' t1 = Mock() t2 = Mock() tr1 = Mock() tr2 = Mock() acct = Mock() desc = 'Budget Transfer - 123.45 from one (1) to standingBudget (9)' with patch('%s.Transaction' % pbm, autospec=True) as mock_t: with patch('%s.TxnReconcile' % pbm, autospec=True) as mock_tr: mock_t.side_effect = [t1, t2] mock_tr.side_effect = [tr1, tr2] res = do_budget_transfer(mock_sess, pp.start_date, Decimal('123.45'), acct, budg1, standing, notes='foo') assert res == [t1, t2] assert mock_t.mock_calls == [ call(date=pp.start_date, budget_amounts={budg1: Decimal('123.45')}, budgeted_amount=Decimal('123.45'), description=desc, account=acct, notes='foo', planned_budget=budg1), call(date=pp.start_date, budget_amounts={standing: Decimal('-123.45')}, budgeted_amount=Decimal('-123.45'), description=desc, account=acct, notes='foo', planned_budget=standing) ] assert mock_tr.mock_calls == [ call(transaction=t1, note=desc), call(transaction=t2, note=desc) ] assert mock_sess.mock_calls == [ call.add(t1), call.add(t2), call.add(t1), call.add(t2), call.add(tr1), call.add(tr2) ]
def test_delete_success(self, search_response, defaults): """ Test delete success. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with tldap.transaction.commit_on_success(): c.delete(dn) expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.delete(dn), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_tuple(self): DiffTuple, mock = self.make_differ() diff = DiffTuple( [('a', 1, 2), ('b', 3, 4), ('c', 5, 6)], [('b', 3, 4), ('c', 5, 7), ('d', 7, 8)] ) compare([], mock.mock_calls) diff.apply() compare([ call.delete('a', ('a', 1, 2), ('a', 2)), call.update('c', ('c', 5, 6), ('c', 6), ('c', 5, 7), ('c', 7)), call.add('d', ('d', 7, 8), ('d', 8)), ], mock.mock_calls)
def test_replace_attribute_success(self, search_response, defaults): """ Test change attribute with success. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with tldap.transaction.commit_on_success(): c.modify(dn, { 'sn': [(ldap3.MODIFY_REPLACE, [b"Gates"])] }) expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn, {'sn': [('MODIFY_REPLACE', [b'Gates'])]}), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_delete_attribute_success(self, search_response, defaults): """ Test deleting attribute *of new object* with success. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with tldap.transaction.commit_on_success(): c.modify(dn, { "telephoneNumber": [(ldap3.MODIFY_DELETE, [b'000'])] }) expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn, {'telephoneNumber': [('MODIFY_DELETE', [b'000'])]}), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_duplicate_imported_key_dealt_with(self): DiffTuple, mock = self.make_differ() def handle_imported_problem(self, key, dups): first, second = dups first_raw, first_extracted = first second_raw, second_extracted = second return [(key, first_raw, (key, first_raw[1]+second_raw[1]))] DiffTuple.handle_imported_problem = handle_imported_problem diff = DiffTuple([], [('a', 1, 2), ('a', 3, 4)]) diff.apply() compare([ call.add('a', ('a', 1, 2), ('a', 4)), ], mock.mock_calls)
def test_move_success(self, search_response, defaults): """ Test move with success. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' new_base = 'ou=Groups,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with tldap.transaction.commit_on_success(): c.rename( dn, "uid=tux", new_base, ) expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.modify_dn(dn, 'uid=tux', new_superior=new_base), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_skip(self): mock = Mock() class DiffTuple(Diff): extract_existing = DictExtractor('k') def extract_imported(self, obj): if obj[-1] == 4: return return obj[0], (obj[0], obj[-1]) add = mock.add update = mock.update delete = mock.delete diff = DiffTuple([], [('a', 1, 2), ('a', 3, 4)]) diff.apply() compare([ call.add('a', ('a', 1, 2), ('a', 2)), ], mock.mock_calls)
def test_roll_back_explicit(self, search_response, defaults): """ Test explicit roll back. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection with tldap.transaction.commit_on_success(): c.add(dn, defaults.modlist) c.modify(dn, { 'sn': [(ldap3.MODIFY_REPLACE, [b"Gates"])] }) c.rollback() expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn, {'sn': [('MODIFY_REPLACE', [b'Gates'])]}), call.modify(dn, {'sn': [('MODIFY_REPLACE', [b'Torvalds'])]}), call.delete(dn) ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_add_attribute_rollback(self, search_response, defaults): """ Test adding attribute with rollback. """ dn = 'uid=tux,ou=People,dc=python-ldap,dc=org' search_response.add(dn, defaults.modlist) c = tldap.backend.connection c.add(dn, defaults.modlist) with pytest.raises(tldap.exceptions.TestFailure): with tldap.transaction.commit_on_success(): c.modify(dn, { "telephoneNumber": [(ldap3.MODIFY_ADD, [b"111"])] }) c.fail() # raises TestFailure during commit causing rollback c.commit() expected_calls = [ call.open(), call.bind(), call.add(dn, None, defaults.modlist), call.search(dn, '(objectclass=*)', 'BASE', attributes=ANY), call.modify(dn, {'telephoneNumber': [('MODIFY_ADD', [b'111'])]}), call.modify(dn, {'telephoneNumber': [('MODIFY_DELETE', [b'111'])]}), ] defaults.mock_connection.assert_has_calls(expected_calls)
def test_should_pass_when_methods_actually_not_called(self): verify(self.tester, times(0)).had_called_with(call.clear()) verify(self.tester, times(0)).had_called_with(call.add("yes, I wasn't called"))
def test_should_add_file(self): self.client.add('/source') calls = [call.add('/source')] sub_mock = self.mock_pysvn.return_value sub_mock.assert_has_calls(calls)