Merge lp:~cjohnston/ubuntu-ci-services-itself/write-ticket-resource into lp:ubuntu-ci-services-itself

Proposed by Chris Johnston
Status: Merged
Approved by: Chris Johnston
Approved revision: 56
Merged at revision: 57
Proposed branch: lp:~cjohnston/ubuntu-ci-services-itself/write-ticket-resource
Merge into: lp:ubuntu-ci-services-itself
Diff against target: 96 lines (+65/-1)
3 files modified
ticket_system/ticket/api.py (+3/-1)
ticket_system/ticket/tests/__init__.py (+1/-0)
ticket_system/ticket/tests/test_write_api.py (+61/-0)
To merge this branch: bzr merge lp:~cjohnston/ubuntu-ci-services-itself/write-ticket-resource
Reviewer Review Type Date Requested Status
Andy Doan (community) Approve
Review via email: mp+199714@code.launchpad.net

Commit message

Add write API and tests for the ticket resource

Description of the change

Add write API and tests for the ticket resource

To post a comment you must log in.
Revision history for this message
Andy Doan (doanac) wrote :

On 12/19/2013 02:28 PM, Chris Johnston wrote:

> === added file 'ticket_system/ticket/tests/test_write_api.py'

> + def test_post_ticket(self):
> + # Check how many are there first.
> + self.assertEqual(Ticket.objects.count(), 1)
> + self.post(resource=self.resource, params=self.post_ticket_data)
> + # Verify a new one has been added.
> + self.assertEqual(Ticket.objects.count(), 2)

might be worth taking the ID from self.post and making sure the
post_ticket_data matches the object that got created. not a big deal though.

> + def test_patch_detail(self):
> + # Grab the current data & modify it slightly.
> + original_data = self.getResource(self.detail_url)
> + new_data = original_data.copy()
> + new_data['owner'] = '<email address hidden>'
> +
> + self.assertEqual(Ticket.objects.count(), 1)
> + resp = self.client.patch('/api/v1/' + self.detail_url, data=new_data)
> + self.assertHttpMethodNotAllowed(resp)
> + # Make sure the count hasn't changed & we did an update.
> + self.assertEqual(Ticket.objects.count(), 1)
> + # Check for updated data.
> + self.assertEqual(Ticket.objects.get(pk=self.ticket.pk).owner,
> + self.ticket.owner)

Should this assert be:
   self.assertEqual(Ticket.objects.get(pk=self.ticket.pk).owner,
                    new_data['owner'])

Revision history for this message
Chris Johnston (cjohnston) wrote :

On Thu, Dec 19, 2013 at 4:27 PM, Andy Doan <email address hidden>wrote:

> > + def test_patch_detail(self):
> > + # Grab the current data & modify it slightly.
> > + original_data = self.getResource(self.detail_url)
> > + new_data = original_data.copy()
> > + new_data['owner'] = '<email address hidden>'
> > +
> > + self.assertEqual(Ticket.objects.count(), 1)
> > + resp = self.client.patch('/api/v1/' + self.detail_url,
> data=new_data)
> > + self.assertHttpMethodNotAllowed(resp)
> > + # Make sure the count hasn't changed & we did an update.
> > + self.assertEqual(Ticket.objects.count(), 1)
> > + # Check for updated data.
> > + self.assertEqual(Ticket.objects.get(pk=self.ticket.pk).owner,
> > + self.ticket.owner)
>
> Should this assert be:
> self.assertEqual(Ticket.objects.get(pk=self.ticket.pk).owner,
> new_data['owner'])
>

Nope... If you note:

+ allowed_methods = ['get', 'post']

You will notice that patch is not allowed. So I am verifying that patch
didn't work:

+ resp = self.client.patch('/api/v1/' + self.detail_url,
data=new_data)
+ self.assertHttpMethodNotAllowed(resp)

and then that the owner is still the same owner that it was originally.

Revision history for this message
Andy Doan (doanac) :
review: Approve
Revision history for this message
Vincent Ladeuil (vila) wrote :

74 + self.assertEqual(Ticket.objects.count(), 1)

Before the meme starts getting out of hand, *I* have a strong preference for:

 assert (expected, actual)

I *do* know there is an ongoing controversy on that topic but there is also an argument nobody was never able to properly answer: diff, patch, the good old guys, present their output (or input) in the old/new order. And nobody in their right mind will ever propose to change this.

When I read a failing test output, I expect the same order, 'old' is what was working and is what the test expect, so it's 'expected'. 'new' is why the test is failing right now, so it's 'actual'.

And we don't want to have to read the test code to know which order has been used each time we encounter a test failure, so please, let's settle on expected, actual.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ticket_system/ticket/api.py'
2--- ticket_system/ticket/api.py 2013-12-19 16:07:20 +0000
3+++ ticket_system/ticket/api.py 2013-12-19 20:32:23 +0000
4@@ -14,6 +14,7 @@
5 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6
7 from tastypie import fields
8+from tastypie.authorization import Authorization
9 from tastypie.resources import ModelResource
10 from ticket.models import Ticket, SubTicket, SourcePackageUpload, Artifact
11
12@@ -24,7 +25,8 @@
13
14 class Meta:
15 queryset = Ticket.objects.all()
16- allowed_methods = ['get']
17+ allowed_methods = ['get', 'post']
18+ authorization = Authorization()
19
20
21 class SourcePackageUploadResource(ModelResource):
22
23=== modified file 'ticket_system/ticket/tests/__init__.py'
24--- ticket_system/ticket/tests/__init__.py 2013-12-13 04:56:17 +0000
25+++ ticket_system/ticket/tests/__init__.py 2013-12-19 20:32:23 +0000
26@@ -16,3 +16,4 @@
27 from test_models import *
28 from test_read_api import *
29 from test_full_read_api import *
30+from test_write_api import *
31
32=== added file 'ticket_system/ticket/tests/test_write_api.py'
33--- ticket_system/ticket/tests/test_write_api.py 1970-01-01 00:00:00 +0000
34+++ ticket_system/ticket/tests/test_write_api.py 2013-12-19 20:32:23 +0000
35@@ -0,0 +1,61 @@
36+# Ubuntu Continuous Integration Engine
37+# Copyright 2013 Canonical Ltd.
38+
39+# This program is free software: you can redistribute it and/or modify it
40+# under the terms of the GNU Affero General Public License version 3, as
41+# published by the Free Software Foundation.
42+
43+# This program is distributed in the hope that it will be useful, but
44+# WITHOUT ANY WARRANTY; without even the implied warranties of
45+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
46+# PURPOSE. See the GNU Affero General Public License for more details.
47+
48+# You should have received a copy of the GNU Affero General Public License
49+# along with this program. If not, see <http://www.gnu.org/licenses/>.
50+
51+from model_mommy import mommy
52+from ci_utils.tastypie.test import TastypieTestCase
53+from ticket.models import Ticket
54+
55+
56+class APICreateTicketResourceTest(TastypieTestCase):
57+
58+ def setUp(self):
59+ super(APICreateTicketResourceTest, self).setUp('/api/v1')
60+ self.ticket = mommy.make('Ticket')
61+ self.resource = 'ticket/'
62+ self.detail_url = self.resource + '{0}/'.format(self.ticket.pk)
63+ self.post_ticket_data = {
64+ 'owner': 'owner@example.com',
65+ 'title': 'My first ticket!',
66+ 'description': 'This if my first ticket. See what it can do',
67+ 'bug_id': '12345',
68+ 'added_binaries': 'binary-1,binary-2',
69+ 'removed_binaries': 'binary-3,binary-4',
70+ }
71+
72+ def test_post_ticket(self):
73+ # Check how many are there first.
74+ self.assertEqual(Ticket.objects.count(), 1)
75+ self.post(resource=self.resource, params=self.post_ticket_data)
76+ # Verify a new one has been added.
77+ self.assertEqual(Ticket.objects.count(), 2)
78+
79+ def test_patch_detail(self):
80+ # Grab the current data & modify it slightly.
81+ original_data = self.getResource(self.detail_url)
82+ new_data = original_data.copy()
83+ new_data['owner'] = 'ci@example.com'
84+
85+ self.assertEqual(Ticket.objects.count(), 1)
86+ resp = self.client.patch('/api/v1/' + self.detail_url, data=new_data)
87+ self.assertHttpMethodNotAllowed(resp)
88+ # Make sure the count hasn't changed & we did an update.
89+ self.assertEqual(Ticket.objects.count(), 1)
90+ # Check for updated data.
91+ self.assertEqual(Ticket.objects.get(pk=self.ticket.pk).owner,
92+ self.ticket.owner)
93+
94+ def test_delete_ticket_not_allowed(self):
95+ resp = self.delete(resource=self.detail_url)
96+ self.assertHttpMethodNotAllowed(resp)

Subscribers

People subscribed via source and target branches