def save_result(data): res, error = validate_result(data) if error: return res, True else: assert(isinstance(res, Environment)) env = res p, created = Project.objects.get_or_create(name=data["project"]) branch, created = Branch.objects.get_or_create(name=data["branch"], project=p) b, created = Benchmark.objects.get_or_create(name=data["benchmark"]) try: rev = branch.revisions.get(commitid=data['commitid']) except Revision.DoesNotExist: rev_date = data.get("revision_date") # "None" (as string) can happen when we urlencode the POST data if not rev_date or rev_date in ["", "None"]: rev_date = datetime.today() rev = Revision(branch=branch, project=p, commitid=data['commitid'], date=rev_date) try: rev.full_clean() except ValidationError as e: return str(e), True if p.repo_type not in ("N", ""): try: saverevisioninfo(rev) except RuntimeError as e: logger.warning("unable to save revision %s info: %s", rev, e, exc_info=True) rev.save() exe, created = Executable.objects.get_or_create( name=data['executable'], project=p ) try: r = Result.objects.get( revision=rev, executable=exe, benchmark=b, environment=env) except Result.DoesNotExist: r = Result(revision=rev, executable=exe, benchmark=b, environment=env) r.value = data["result_value"] if 'result_date' in data: r.date = data["result_date"] elif rev.date: r.date = rev.date else: r.date = datetime.now() r.std_dev = data.get('std_dev') r.val_min = data.get('min') r.val_max = data.get('max') r.full_clean() r.save() return (rev, exe, env), False
def save_result(data): res, error = validate_result(data) if error: return res, True else: assert(isinstance(res, Environment)) e = res p, created = Project.objects.get_or_create(name=data["project"]) branch, created = Branch.objects.get_or_create(name=data["branch"], project=p) b, created = Benchmark.objects.get_or_create(name=data["benchmark"]) try: rev = branch.revisions.get(commitid=data['commitid']) except Revision.DoesNotExist: rev_date = data.get("revision_date") # "None" (as string) can happen when we urlencode the POST data if not rev_date or rev_date in ["", "None"]: rev_date = datetime.today() rev = Revision(branch=branch, project=p, commitid=data['commitid'], date=rev_date) try: rev.full_clean() except ValidationError as e: return str(e), True if p.repo_type not in ("N", ""): try: saverevisioninfo(rev) except StandardError, e: logging.warning("unable to save revision %s info: %s", rev, e, exc_info=True) rev.save()
def setUp(self): DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S' self.branch1 = Branch.objects.get(pk=1) self.project1 = Project.objects.get(pk=1) self.revision1_data = dict( commitid="2a6306432e973cdcfd324e81169bb8029d47b736", tag="tag", date=datetime.now(), message="Commit message\n - all bugs fixed\n - code 130% faster", project=self.project1, author="Alan T. <alan@localhost>", branch=self.branch1, ) self.revision1 = Revision(**self.revision1_data) self.revision1.save() self.revision2_data = dict( commitid="4d3bea3cffe4edcd7d70fc46c5e19474cc4bd012", tag="v1.0", date=datetime.now().strftime(DATETIME_FORMAT), message="Commit message\n - cleanup\n - all FIXMEs removed", project='/api/v1/project/{0}/'.format(self.project1.id), author="Chuck N. <chuck@localhost>", branch='/api/v1/branch/{0}/'.format(self.branch1.id), ) self.client = Client() super(RevisionTest, self).setUp()
def save_result(data): res, error = validate_result(data) if error: return res, True else: assert(isinstance(res, Environment)) env = res p, created = Project.objects.get_or_create(name=data["project"]) branch, created = Branch.objects.get_or_create(name=data["branch"], project=p) b, created = Benchmark.objects.get_or_create(name=data["benchmark"]) try: rev = branch.revisions.get(commitid=data['commitid']) except Revision.DoesNotExist: rev_date = data.get("revision_date") # "None" (as string) can happen when we urlencode the POST data if not rev_date or rev_date in ["", "None"]: rev_date = datetime.today() rev = Revision(branch=branch, project=p, commitid=data['commitid'], date=rev_date) try: rev.full_clean() except ValidationError as e: return str(e), True if p.repo_type not in ("N", ""): try: saverevisioninfo(rev) except RuntimeError as e: logging.warning("unable to save revision %s info: %s", rev, e, exc_info=True) rev.save() exe, created = Executable.objects.get_or_create( name=data['executable'], project=p ) try: r = Result.objects.get( revision=rev,executable=exe,benchmark=b,environment=env) except Result.DoesNotExist: r = Result(revision=rev,executable=exe,benchmark=b,environment=env) r.value = data["result_value"] if 'result_date' in data: r.date = data["result_date"] elif rev.date: r.date = rev.date else: r.date = datetime.now() r.std_dev = data.get('std_dev') r.val_min = data.get('min') r.val_max = data.get('max') r.full_clean() r.save() return (rev, exe, env), False
def make_result(self, value, rev=None, benchmark=None): from uuid import uuid4 if not benchmark: benchmark = self.bench if not rev: commitdate = self.starttime + timedelta(days=self.days) cid = str(uuid4()) Revision(commitid=cid, date=commitdate, branch=self.b, project=self.pro).save() rev = Revision.objects.get(commitid=cid) Result(value=value, revision=rev, executable=self.exe, environment=self.env, benchmark=benchmark).save() self.days += 1 return rev
class RevisionTest(FixtureTestCase): """Test Revision() API""" def setUp(self): DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S' self.branch1 = Branch.objects.get(pk=1) self.project1 = Project.objects.get(pk=1) self.revision1_data = dict( commitid="2a6306432e973cdcfd324e81169bb8029d47b736", tag="tag", date=datetime.now(), message="Commit message\n - all bugs fixed\n - code 130% faster", project=self.project1, author="Alan T. <alan@localhost>", branch=self.branch1, ) self.revision1 = Revision(**self.revision1_data) self.revision1.save() self.revision2_data = dict( commitid="4d3bea3cffe4edcd7d70fc46c5e19474cc4bd012", tag="v1.0", date=datetime.now().strftime(DATETIME_FORMAT), message="Commit message\n - cleanup\n - all FIXMEs removed", project='/api/v1/project/{0}/'.format(self.project1.id), author="Chuck N. <chuck@localhost>", branch='/api/v1/branch/{0}/'.format(self.branch1.id), ) self.client = Client() super(RevisionTest, self).setUp() def test_get_revision(self): """Should get an existing revision""" response = self.client.get('/api/v1/revision/1/') self.assertEquals(response.status_code, 200) self.assertEqual(json.loads(response.content)['commitid'], "1") self.assertEqual( json.loads(response.content)['project'], "/api/v1/project/1/") def test_get_revision_all_fields(self): """Should get all fields for a revision""" response = self.client.get('/api/v1/revision/%s/' % (self.revision1.id, )) self.assertEquals(response.status_code, 200) self.assertEquals( json.loads(response.content)['commitid'], self.revision1.commitid) self.assertEquals( json.loads(response.content)['project'], '/api/v1/project/%s/' % (self.project1.pk)) self.assertEquals( json.loads(response.content)['branch'], '/api/v1/branch/%s/' % (self.branch1.pk)) self.assertEquals( json.loads(response.content)['tag'], self.revision1_data['tag']) self.assertEquals( json.loads(response.content)['message'], self.revision1_data['message']) def test_post(self): """Should save a new revision""" modified_data = copy.deepcopy(self.revision2_data) response = self.client.post('/api/v1/revision/', data=json.dumps(modified_data), content_type='application/json') self.assertEquals(response.status_code, 201) id = response['Location'].rsplit('/', 2)[-2] response = self.client.get('/api/v1/revision/{0}/'.format(id)) for k, v in self.revision2_data.items(): self.assertEqual(json.loads(response.content)[k], v) response = self.client.delete('/api/v1/revision/{0}/'.format(id), content_type='application/json') self.assertEquals(response.status_code, 204) def test_put(self): """Should modify an existing revision""" modified_data = copy.deepcopy(self.revision2_data) modified_data['tag'] = "v0.9.1" response = self.client.put('/api/v1/revision/1/', data=json.dumps(modified_data), content_type='application/json') self.assertEquals(response.status_code, 204) response = self.client.get('/api/v1/revision/1/') for k, v in modified_data.items(): self.assertEqual(json.loads(response.content)[k], v) def test_delete(self): """Should delete a revision""" response = self.client.get('/api/v1/revision/1/') self.assertEquals(response.status_code, 200) # from fixture response = self.client.delete('/api/v1/revision/1/', content_type='application/json') self.assertEquals(response.status_code, 204) response = self.client.get('/api/v1/revision/1/') self.assertEquals(response.status_code, 404)
class RevisionTest(FixtureTestCase): """Test Revision() API""" def setUp(self): DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S' self.branch1 = Branch.objects.get(pk=1) self.project1 = Project.objects.get(pk=1) self.revision1_data = dict( commitid="2a6306432e973cdcfd324e81169bb8029d47b736", tag="tag", date=datetime.now(), message="Commit message\n - all bugs fixed\n - code 130% faster", project=self.project1, author="Alan T. <alan@localhost>", branch=self.branch1, ) self.revision1 = Revision(**self.revision1_data) self.revision1.save() self.revision2_data = dict( commitid="4d3bea3cffe4edcd7d70fc46c5e19474cc4bd012", tag="v1.0", date=datetime.now().strftime(DATETIME_FORMAT), message="Commit message\n - cleanup\n - all FIXMEs removed", project='/api/v1/project/{0}/'.format(self.project1.id), author="Chuck N. <chuck@localhost>", branch='/api/v1/branch/{0}/'.format(self.branch1.id), ) self.client = Client() super(RevisionTest, self).setUp() def test_get_revision(self): """Should get an existing revision""" response = self.client.get('/api/v1/revision/1/') self.assertEquals(response.status_code, 200) self.assertEqual(json.loads(response.content)['commitid'], "1") self.assertEqual(json.loads(response.content)['project'], "/api/v1/project/1/") def test_get_revision_all_fields(self): """Should get all fields for a revision""" response = self.client.get('/api/v1/revision/%s/' % (self.revision1.id,)) self.assertEquals(response.status_code, 200) self.assertEquals(json.loads(response.content)['commitid'], self.revision1.commitid) self.assertEquals(json.loads(response.content)['project'], '/api/v1/project/%s/' % (self.project1.pk)) self.assertEquals(json.loads(response.content)['branch'], '/api/v1/branch/%s/' % (self.branch1.pk)) self.assertEquals(json.loads(response.content)['tag'], self.revision1_data['tag']) self.assertEquals(json.loads(response.content)['message'], self.revision1_data['message']) def test_post(self): """Should save a new revision""" modified_data = copy.deepcopy(self.revision2_data) response = self.client.post('/api/v1/revision/', data=json.dumps(modified_data), content_type='application/json') self.assertEquals(response.status_code, 201) id = response['Location'].rsplit('/', 2)[-2] response = self.client.get('/api/v1/revision/{0}/'.format(id)) for k, v in self.revision2_data.items(): self.assertEqual( json.loads(response.content)[k], v) response = self.client.delete('/api/v1/revision/{0}/'.format(id), content_type='application/json') self.assertEquals(response.status_code, 204) def test_put(self): """Should modify an existing revision""" modified_data = copy.deepcopy(self.revision2_data) modified_data['tag'] = "v0.9.1" response = self.client.put('/api/v1/revision/1/', data=json.dumps(modified_data), content_type='application/json') self.assertEquals(response.status_code, 204) response = self.client.get('/api/v1/revision/1/') for k, v in modified_data.items(): self.assertEqual( json.loads(response.content)[k], v) def test_delete(self): """Should delete a revision""" response = self.client.get('/api/v1/revision/1/') self.assertEquals(response.status_code, 200) # from fixture response = self.client.delete('/api/v1/revision/1/', content_type='application/json') self.assertEquals(response.status_code, 204) response = self.client.get('/api/v1/revision/1/') self.assertEquals(response.status_code, 404)