Skip to content

Commit da80503

Browse files
committed
Covered all RESTful API resources by tests, changed Report() api
- 100% coverage of all the resources provided by the tastypie API - changed ReportResource() to only accept GET requests. Reports are generated automatically. - fixed some typos
1 parent b6ec83c commit da80503

3 files changed

Lines changed: 169 additions & 18 deletions

File tree

codespeed/api.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454

5555

5656
class UserResource(ModelResource):
57-
"""Ressource for Django User()"""
57+
"""Resource for Django User()"""
58+
5859
class Meta:
5960
queryset = User.objects.filter(is_active=True)
6061
resource_name = 'user'
@@ -68,15 +69,15 @@ class Meta:
6869

6970

7071
class ProjectResource(ModelResource):
71-
"""Ressource for Project()"""
72+
"""Resource for Project()"""
7273

7374
class Meta:
7475
queryset = Project.objects.all()
7576
authorization= Authorization()
7677

7778

7879
class BranchResource(ModelResource):
79-
"""Ressource for Branch()"""
80+
"""Resource for Branch()"""
8081

8182
project = fields.ToOneField(ProjectResource, 'project')
8283

@@ -86,7 +87,7 @@ class Meta:
8687

8788

8889
class RevisionResource(ModelResource):
89-
"""Ressource for Revision()"""
90+
"""Resource for Revision()"""
9091

9192
project = fields.ToOneField(ProjectResource, 'project')
9293
branch = fields.ToOneField(BranchResource, 'branch')
@@ -97,7 +98,7 @@ class Meta:
9798

9899

99100
class ExecutableResource(ModelResource):
100-
"""Ressource for Executable()"""
101+
"""Resource for Executable()"""
101102

102103
project = fields.ToOneField(ProjectResource, 'project')
103104

@@ -107,15 +108,15 @@ class Meta:
107108

108109

109110
class BenchmarkResource(ModelResource):
110-
"""Ressource for Benchmark()"""
111+
"""Resource for Benchmark()"""
111112

112113
class Meta:
113114
queryset = Benchmark.objects.all()
114115
authorization= Authorization()
115116

116117

117118
class EnvironmentResource(ModelResource):
118-
"""Ressource for Enviroment()"""
119+
"""Resource for Enviroment()"""
119120

120121
class Meta:
121122
queryset = Environment.objects.all()
@@ -124,18 +125,23 @@ class Meta:
124125

125126

126127
class ResultResource(ModelResource):
127-
"""Ressource for Result()"""
128+
"""Resource for Result()"""
128129

129130
class Meta:
130131
queryset = Result.objects.all()
131132
authorization= Authorization()
132133

133134

134135
class ReportResource(ModelResource):
135-
"""Ressource for Report()"""
136+
"""Resource for Report()"""
137+
138+
revision = fields.ToOneField(RevisionResource, 'revision')
139+
environment = fields.ToOneField(EnvironmentResource, 'environment')
140+
executable = fields.ToOneField(ExecutableResource, 'executable')
136141

137142
class Meta:
138143
queryset = Report.objects.all()
144+
allowed_methods = ['get']
139145
authorization= Authorization()
140146

141147

@@ -331,7 +337,7 @@ def save(self):
331337

332338

333339
class ResultBundleResource(Resource):
334-
"""Ressource for all the data of a benchmark result.
340+
"""Resource for all the data of a benchmark result.
335341
336342
Primarily used to submit benchmark results
337343

codespeed/tests/tests_api.py

Lines changed: 151 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def setUp(self):
372372
super(RevisionTest, self).setUp()
373373

374374
def test_get_revision(self):
375-
"""Should get an existing branch"""
375+
"""Should get an existing revision"""
376376
response = self.client.get('/api/v1/revision/1/')
377377
self.assertEquals(response.status_code, 200)
378378
self.assertEqual(json.loads(response.content)['commitid'], "1")
@@ -411,7 +411,7 @@ def test_post(self):
411411
self.assertEquals(response.status_code, 204)
412412

413413
def test_put(self):
414-
"""Should modify an existing environment"""
414+
"""Should modify an existing revision"""
415415
modified_data = copy.deepcopy(self.revision2_data)
416416
modified_data['tag'] = "v0.9.1"
417417
response = self.client.put('/api/v1/revision/1/',
@@ -437,7 +437,7 @@ def test_delete(self):
437437

438438

439439
class ExecutableTest(FixtureTestCase):
440-
"""Test Branch() API"""
440+
"""Test Executable() API"""
441441

442442
def setUp(self):
443443
self.executable1 = Executable.objects.get(pk=1)
@@ -514,13 +514,157 @@ def test_delete(self):
514514

515515

516516
class BenchmarkTest(FixtureTestCase):
517-
"""Test Branch() API"""
518-
pass
517+
"""Test Benchmark() API"""
518+
519+
def setUp(self):
520+
self.benchmark1 = Benchmark.objects.get(pk=1)
521+
self.benchmark2_data = dict(
522+
name="sleep",
523+
benchmark_type = 'C',
524+
description = 'fast faster fastest',
525+
units_title = 'Time',
526+
units = 'seconds',
527+
lessisbetter = True,
528+
default_on_comparison = True,
529+
)
530+
self.benchmark2 = Benchmark(**self.benchmark2_data)
531+
self.benchmark2.save()
532+
self.client = Client()
533+
super(BenchmarkTest, self).setUp()
534+
535+
def test_get_benchmark(self):
536+
"""Should get an existing benchmark"""
537+
response = self.client.get('/api/v1/benchmark/1/')
538+
self.assertEquals(response.status_code, 200)
539+
self.assertEqual(json.loads(response.content)['name'],
540+
'float')
541+
self.assertEqual(json.loads(response.content)['units'],
542+
"seconds")
543+
544+
def test_get_benchmark_all_fields(self):
545+
"""Should get all fields for an benchmark"""
546+
response = self.client.get('/api/v1/benchmark/{0}/'.format(
547+
self.benchmark2.id,))
548+
self.assertEquals(response.status_code, 200)
549+
for k, v in self.benchmark2_data.items():
550+
self.assertEqual(json.loads(response.content)[k], v)
551+
552+
def test_post(self):
553+
"""Should save a new benchmark"""
554+
modified_data = copy.deepcopy(self.benchmark2_data)
555+
modified_data['name'] = 'wake'
556+
response = self.client.post('/api/v1/benchmark/',
557+
data=json.dumps(modified_data),
558+
content_type='application/json')
559+
self.assertEquals(response.status_code, 201)
560+
id = response['Location'].rsplit('/', 2)[-2]
561+
response = self.client.get('/api/v1/benchmark/{0}/'.format(id))
562+
for k, v in modified_data.items():
563+
self.assertEqual(
564+
json.loads(response.content)[k], v)
565+
response = self.client.delete('/api/v1/benchmark/{0}/'.format(id),
566+
content_type='application/json')
567+
self.assertEquals(response.status_code, 204)
568+
569+
def test_put(self):
570+
"""Should modify an existing benchmark"""
571+
modified_data = copy.deepcopy(self.benchmark2_data)
572+
modified_data['name'] = "django"
573+
response = self.client.put('/api/v1/benchmark/1/',
574+
data=json.dumps(modified_data),
575+
content_type='application/json')
576+
self.assertEquals(response.status_code, 204)
577+
response = self.client.get('/api/v1/benchmark/1/')
578+
for k, v in modified_data.items():
579+
self.assertEqual(
580+
json.loads(response.content)[k], v)
581+
582+
def test_delete(self):
583+
"""Should delete a benchmark"""
584+
response = self.client.get('/api/v1/benchmark/1/')
585+
self.assertEquals(response.status_code, 200)
586+
# from fixture
587+
response = self.client.delete('/api/v1/benchmark/1/',
588+
content_type='application/json')
589+
self.assertEquals(response.status_code, 204)
590+
591+
response = self.client.get('/api/v1/benchmark/1/')
592+
self.assertEquals(response.status_code, 404)
519593

520594

521595
class ReportTest(FixtureTestCase):
522-
"""Test Branch() API"""
523-
pass
596+
"""Test Report() API"""
597+
598+
def setUp(self):
599+
self.report1 = Report.objects.get(pk=1)
600+
self.revision1 = Revision.objects.get(pk=1)
601+
self.executable1 = Executable.objects.get(pk=1)
602+
self.environment1 = Environment.objects.get(pk=1)
603+
self.executable2_data = dict(
604+
name="Fibo",
605+
description="Fibonacci the Lame",
606+
)
607+
self.project=Project.objects.get(pk=1)
608+
self.executable2 = Executable(project=self.project,
609+
**self.executable2_data)
610+
self.executable2.save()
611+
self.report2_data = dict(
612+
revision=self.revision1,
613+
environment=self.environment1,
614+
executable=self.executable2,
615+
)
616+
self.report2 = Report(**self.report2_data)
617+
self.report2.save()
618+
self.report2_data = dict(
619+
revision='/api/v1/revision/{0}/'.format(self.revision1.id),
620+
environment='/api/v1/environment/{0}/'.format(self.environment1.id),
621+
executable='/api/v1/executable/{0}/'.format(self.executable2.id),
622+
)
623+
self.client = Client()
624+
super(ReportTest, self).setUp()
625+
626+
def test_get_report(self):
627+
"""Should get an existing report"""
628+
response = self.client.get('/api/v1/report/1/')
629+
self.assertEquals(response.status_code, 200)
630+
self.assertEqual(json.loads(response.content)['summary'],
631+
'float -50.0%')
632+
self.assertEqual(json.loads(response.content)['colorcode'],
633+
"green")
634+
635+
def test_get_report_all_fields(self):
636+
"""Should get all fields for an report"""
637+
response = self.client.get('/api/v1/report/{0}/'.format(
638+
self.report2.id,))
639+
self.assertEquals(response.status_code, 200)
640+
for k, v in self.report2_data.items():
641+
self.assertEqual(json.loads(response.content)[k], v)
642+
643+
def test_post(self):
644+
"""Should save a new report"""
645+
modified_data = copy.deepcopy(self.report2_data)
646+
response = self.client.post('/api/v1/report/',
647+
data=json.dumps(modified_data),
648+
content_type='application/json')
649+
# next has to be 405, otherwise would raise IntegrityError
650+
self.assertEquals(response.status_code, 405)
651+
652+
def test_put(self):
653+
"""Should modify an existing report"""
654+
modified_data = copy.deepcopy(self.report2_data)
655+
response = self.client.put('/api/v1/report/1/',
656+
data=json.dumps(modified_data),
657+
content_type='application/json')
658+
self.assertEquals(response.status_code, 405)
659+
660+
def test_delete(self):
661+
"""Should delete a report"""
662+
response = self.client.get('/api/v1/report/1/')
663+
self.assertEquals(response.status_code, 200)
664+
# from fixture
665+
response = self.client.delete('/api/v1/report/1/',
666+
content_type='application/json')
667+
self.assertEquals(response.status_code, 405)
524668

525669

526670
class UserTest(FixtureTestCase):

codespeed/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from tastypie.api import Api
66
from codespeed.feeds import LatestEntries
77
from codespeed.api import (UserResource, EnvironmentResource,
8-
ProjectResource, ExecutableResource,
8+
ProjectResource, ExecutableResource, ReportResource,
99
BenchmarkResource, ResultResource, BranchResource,
1010
RevisionResource, ResultBundleResource)
1111

@@ -20,6 +20,7 @@
2020
rest_api.register(ResultResource())
2121
rest_api.register(BranchResource())
2222
rest_api.register(RevisionResource())
23+
rest_api.register(ReportResource())
2324
rest_api.register(ResultBundleResource())
2425

2526
urlpatterns = patterns('',

0 commit comments

Comments
 (0)