def update_last_successful_build(project, build_tag): """Update last successful build.""" last_successful_build = ndb.Key(LastSuccessfulBuild, project['name'] + '-' + build_tag).get() if not last_successful_build and 'last_successful_build' not in project: return if 'last_successful_build' not in project: project['last_successful_build'] = { 'build_id': last_successful_build.build_id, 'finish_time': last_successful_build.finish_time } else: if last_successful_build: last_successful_build.build_id = project['last_successful_build'][ 'build_id'] last_successful_build.finish_time = project['last_successful_build'][ 'finish_time'] else: last_successful_build = LastSuccessfulBuild( id=project['name'] + '-' + build_tag, project=project['name'], build_id=project['last_successful_build']['build_id'], finish_time=project['last_successful_build']['finish_time']) last_successful_build.put()
def test_update_last_successful_build_datastore(self): """When last successful build is only available in datastore.""" with ndb.Client().context(): project = {'name': 'test-project'} LastSuccessfulBuild(id='test-project-fuzzing', build_tag='fuzzing', project='test-project', build_id='1', finish_time='test_time').put() update_build_status.update_last_successful_build( project, 'fuzzing') expected_project = { 'name': 'test-project', 'last_successful_build': { 'build_id': '1', 'finish_time': 'test_time' } } self.assertDictEqual(project, expected_project)
def test_update_last_successful_build(self): """When last successful build is available at both places.""" with ndb.Client().context(): project = { 'name': 'test-project', 'last_successful_build': { 'build_id': '2', 'finish_time': 'test_time' } } LastSuccessfulBuild(id='test-project-fuzzing', build_tag='fuzzing', project='test-project', build_id='1', finish_time='test_time').put() update_build_status.update_last_successful_build( project, 'fuzzing') expected_build_id = '2' self.assertEqual( expected_build_id, ndb.Key(LastSuccessfulBuild, 'test-project-fuzzing').get().build_id)