def test_simple(self): repo = self.create_repo() result = RevisionResult( id='c' * 40, author='Foo Bar <*****@*****.**>', committer='Biz Baz <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), committer_date=datetime(2013, 9, 19, 22, 15, 23), message='Hello world!', parents=['a' * 40, 'b' * 40], ) revision, created = result.save(repo) assert created assert type(revision) == Revision assert revision.repository == repo assert revision.sha == 'c' * 40 assert revision.message == 'Hello world!' assert revision.author.name == 'Foo Bar' assert revision.author.email == '*****@*****.**' assert revision.committer.name == 'Biz Baz' assert revision.committer.email == '*****@*****.**' assert revision.parents == ['a' * 40, 'b' * 40] assert revision.date_created == datetime(2013, 9, 19, 22, 15, 22) assert revision.date_committed == datetime(2013, 9, 19, 22, 15, 23)
def test_simple(self): repo = self.create_repo() result = RevisionResult( id="c" * 40, author="Foo Bar <*****@*****.**>", committer="Biz Baz <*****@*****.**>", author_date=datetime(2013, 9, 19, 22, 15, 22), committer_date=datetime(2013, 9, 19, 22, 15, 23), message="Hello world!", parents=["a" * 40, "b" * 40], ) revision, created, _ = result.save(repo) assert created assert type(revision) == Revision assert revision.repository == repo assert revision.sha == "c" * 40 assert revision.message == "Hello world!" assert revision.author.name == "Foo Bar" assert revision.author.email == "*****@*****.**" assert revision.committer.name == "Biz Baz" assert revision.committer.email == "*****@*****.**" assert revision.parents == ["a" * 40, "b" * 40] assert revision.date_created == datetime(2013, 9, 19, 22, 15, 22) assert revision.date_committed == datetime(2013, 9, 19, 22, 15, 23)
def log_results(): yield RevisionResult( id='a' * 40, message='hello world', author='Foo <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), ) yield RevisionResult( id='b' * 40, message='biz', author='Bar <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 21), )
def result(id): return RevisionResult( id=id, message='hello world', author='Foo <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 21), )
def log_results(parent=None, offset=0, limit=100): results = [ RevisionResult( id='a' * 40, message='hello world', author='Foo <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), ), RevisionResult( id='b' * 40, message='biz', author='Bar <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 21), ), ] return iter(results[offset:offset + limit])
def log(parent): if parent is None: yield RevisionResult( id='a' * 40, message='hello world!', author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), )
def log_results(parent=None, branch=None, offset=0, limit=1): assert not branch return iter([ RevisionResult( id='a' * 40, message='hello world', author='Foo <*****@*****.**>', )])
def log_results(parent=None, branch=None, offset=0, limit=100): results = [ RevisionResult(id='a' * 40, message='hello world', author='Foo <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=['first', '2nd']) ] # Exclude one result for queries with '2nd' if branch != '2nd': results.append( RevisionResult(id='b' * 40, message='biz', author='Bar <*****@*****.**>', author_date=datetime( 2013, 9, 19, 22, 15, 21), branches=['first'])) return results
def log(parent, limit, first_parent): yield existing_revision yield existing_revision_branch_changed yield existing_revision_no_branches for i in range(3): yield RevisionResult(id=str(i) * 40, message='commit number' + str(i), author='Example <*****@*****.**>', author_date=datetime( 2013, 9, 19, 22, 15, 22), branches=['b'])
def test_save_again(self): mock_vcs = mock.MagicMock(spec=Vcs) repo = self.create_repo(backend=RepositoryBackend.git) with mock.patch.object(repo, 'get_vcs') as mock_get_vcs: mock_get_vcs.return_value = mock_vcs mock_vcs.get_patch_hash.return_value = 'a' * 40 result = RevisionResult( id='c' * 40, author='Foo Bar <*****@*****.**>', committer='Biz Baz <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), committer_date=datetime(2013, 9, 19, 22, 15, 23), message='Hello world!', parents=['a' * 40, 'b' * 40], ) revision1, created1, _ = result.save(repo) revision2, created2, _ = result.save(repo) assert type(revision1) == type(revision2) == Revision assert revision1.repository == revision2.repository == repo assert revision1.sha == revision2.sha == 'c' * 40 assert revision1.message == revision2.message == 'Hello world!' assert revision1.author.name == revision2.author.name == 'Foo Bar' assert revision1.author.email == revision2.author.email == '*****@*****.**' assert revision1.committer.name == revision2.committer.name == 'Biz Baz' assert revision1.committer.email == revision2.committer.email == '*****@*****.**' assert revision1.parents == revision2.parents == [ 'a' * 40, 'b' * 40 ] assert revision1.date_created == revision2.date_created == datetime( 2013, 9, 19, 22, 15, 22) assert revision1.date_committed == revision2.date_committed == datetime( 2013, 9, 19, 22, 15, 23) assert revision1.patch_hash == revision2.patch_hash == 'a' * 40 assert mock_vcs.get_patch_hash.called_once_with('c' * 40) assert created1 assert not created2
def _log_results(parent=None, branch=None, offset=0, limit=1): assert not branch if parent not in (None, _VALID_SHA): raise CommandError(cmd="test command", retcode=128) return iter([ RevisionResult( id=_VALID_SHA, message='hello world', author='Foo <*****@*****.**>', author_date=datetime.utcnow(), ) ])
def test_save_again(self): mock_vcs = mock.MagicMock(spec=Vcs) repo = self.create_repo(backend=RepositoryBackend.git) with mock.patch.object(repo, 'get_vcs') as mock_get_vcs: mock_get_vcs.return_value = mock_vcs mock_vcs.get_patch_hash.return_value = 'a' * 40 result = RevisionResult( id='c' * 40, author='Foo Bar <*****@*****.**>', committer='Biz Baz <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), committer_date=datetime(2013, 9, 19, 22, 15, 23), message='Hello world!', parents=['a' * 40, 'b' * 40], ) revision1, created1, _ = result.save(repo) revision2, created2, _ = result.save(repo) assert type(revision1) == type(revision2) == Revision assert revision1.repository == revision2.repository == repo assert revision1.sha == revision2.sha == 'c' * 40 assert revision1.message == revision2.message == 'Hello world!' assert revision1.author.name == revision2.author.name == 'Foo Bar' assert revision1.author.email == revision2.author.email == '*****@*****.**' assert revision1.committer.name == revision2.committer.name == 'Biz Baz' assert revision1.committer.email == revision2.committer.email == '*****@*****.**' assert revision1.parents == revision2.parents == ['a' * 40, 'b' * 40] assert revision1.date_created == revision2.date_created == datetime(2013, 9, 19, 22, 15, 22) assert revision1.date_committed == revision2.date_committed == datetime(2013, 9, 19, 22, 15, 23) assert revision1.patch_hash == revision2.patch_hash == 'a' * 40 assert mock_vcs.get_patch_hash.called_once_with('c' * 40) assert created1 assert not created2
def test_with_existing_revision(self, queue_delay, get_vcs_backend, mock_fire_signal): """ Ensure that sync_repo creates and fires signals for existing revisions only if we haven't done so before and there are branches. """ vcs_backend = mock.MagicMock(spec=Vcs) vcs_backend.get_patch_hash.return_value = 'a' * 40 get_vcs_backend.return_value = vcs_backend repo = self.create_repo(backend=RepositoryBackend.git) existing_revision_branch_changed = RevisionResult( id=str(3) * 40, message='latest commit', author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=['b']) existing_revision_branch_changed.save(repo) existing_revision_branch_changed.branches = ['b', 'c'] existing_revision = RevisionResult( id=str(4) * 40, message='latest commit', author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=['b'], ) r, _, _ = existing_revision.save(repo) r.date_created_signal = datetime.utcnow() db.session.commit() existing_revision_no_branches = RevisionResult( id=str(5) * 40, message='latest commit', author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=[]) existing_revision_no_branches.save(repo) db.session.commit() def log(parent, limit, first_parent): yield existing_revision yield existing_revision_branch_changed yield existing_revision_no_branches for i in range(3): yield RevisionResult(id=str(i) * 40, message='commit number' + str(i), author='Example <*****@*****.**>', author_date=datetime( 2013, 9, 19, 22, 15, 22), branches=['b']) vcs_backend.log.side_effect = log with mock.patch.object(sync_repo, 'allow_absent_from_db', True): sync_repo(repo_id=repo.id.hex, task_id=repo.id.hex) assert get_vcs_backend.called vcs_backend.log.assert_any_call(parent=None, limit=NUM_RECENT_COMMITS, first_parent=False) db.session.expire_all() repo = Repository.query.get(repo.id) assert repo.last_update_attempt is not None assert repo.last_update is not None for i in range(4): mock_fire_signal.delay.assert_any_call( signal='revision.created', kwargs={ 'repository_id': repo.id.hex, 'revision_sha': str(i) * 40, }, ) assert mock_fire_signal.delay.call_count == 4 # Now all the revisions have been handled. # Another call to sync_repo should do nothing. mock_fire_signal.delay.call_count = 0 with mock.patch.object(sync_repo, 'allow_absent_from_db', True): sync_repo(repo_id=repo.id.hex, task_id=repo.id.hex) assert mock_fire_signal.delay.call_count == 0
def test_with_existing_revision(self, queue_delay, get_vcs_backend, mock_fire_signal): """ Ensure that sync_repo creates and fires signals for existing revisions only if we haven't done so before and there are branches. """ vcs_backend = mock.MagicMock(spec=Vcs) get_vcs_backend.return_value = vcs_backend repo = self.create_repo(backend=RepositoryBackend.git) existing_revision_branch_changed = RevisionResult( id=str(3) * 40, message='latest commit', author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=['b'] ) existing_revision_branch_changed.save(repo) existing_revision_branch_changed.branches = ['b', 'c'] existing_revision = RevisionResult( id=str(4) * 40, message='latest commit', author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=['b'], ) r, _, _ = existing_revision.save(repo) r.date_created_signal = datetime.utcnow() db.session.commit() existing_revision_no_branches = RevisionResult( id=str(5) * 40, message='latest commit', author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=[] ) existing_revision_no_branches.save(repo) db.session.commit() def log(parent, limit): yield existing_revision yield existing_revision_branch_changed yield existing_revision_no_branches for i in range(3): yield RevisionResult( id=str(i) * 40, message='commit number' + str(i), author='Example <*****@*****.**>', author_date=datetime(2013, 9, 19, 22, 15, 22), branches=['b'] ) vcs_backend.log.side_effect = log with mock.patch.object(sync_repo, 'allow_absent_from_db', True): sync_repo(repo_id=repo.id.hex, task_id=repo.id.hex) get_vcs_backend.assert_called_once_with() vcs_backend.log.assert_any_call(parent=None, limit=NUM_RECENT_COMMITS) db.session.expire_all() repo = Repository.query.get(repo.id) assert repo.last_update_attempt is not None assert repo.last_update is not None for i in range(4): mock_fire_signal.delay.assert_any_call( signal='revision.created', kwargs={ 'repository_id': repo.id.hex, 'revision_sha': str(i) * 40, }, ) assert mock_fire_signal.delay.call_count == 4 # Now all the revisions have been handled. # Another call to sync_repo should do nothing. mock_fire_signal.delay.call_count = 0 with mock.patch.object(sync_repo, 'allow_absent_from_db', True): sync_repo(repo_id=repo.id.hex, task_id=repo.id.hex) assert mock_fire_signal.delay.call_count == 0