Merge lp:~milo/linaro-ci-dashboard/simple-mock into lp:linaro-ci-dashboard

Proposed by Milo Casagrande
Status: Merged
Approved by: Stevan Radaković
Approved revision: 50
Merged at revision: 50
Proposed branch: lp:~milo/linaro-ci-dashboard/simple-mock
Merge into: lp:linaro-ci-dashboard
Diff against target: 650 lines (+328/-152)
12 files modified
Makefile (+2/-2)
dashboard/frontend/android_build/tests/test_android_build_views.py (+9/-15)
dashboard/frontend/android_textfield_loop/tests/test_android_textfield_views.py (+9/-9)
dashboard/frontend/integration_loop/tests/__init__.py (+27/-0)
dashboard/frontend/integration_loop/tests/test_integration_loop_clientresponse.py (+75/-0)
dashboard/frontend/integration_loop/tests/test_integration_loop_views.py (+56/-0)
dashboard/frontend/kernel_build/tests/test_kernel_build_views.py (+9/-13)
dashboard/frontend/tests/__init__.py (+0/-3)
dashboard/frontend/tests/loop_mock.py (+125/-0)
dashboard/frontend/tests/test_clientresponse.py (+0/-54)
dashboard/frontend/tests/test_views.py (+15/-53)
dashboard/frontend/views/loop_detail_view.py (+1/-3)
To merge this branch: bzr merge lp:~milo/linaro-ci-dashboard/simple-mock
Reviewer Review Type Date Requested Status
Stevan Radaković Approve
Linaro Infrastructure Pending
Review via email: mp+124358@code.launchpad.net

Description of the change

In this branch I worked out a little issue we had when running the tests. Basically, with the new refactoring of the views, we are looking for objects with the query 'Loop.objects.get' that accepts parameters (in our case the primary key). The problem is that during the test we mock the other objects present in the view, so the primary key during the test is not a valid primary key for the query set.

Here I introduced a couple of very simple mocks, Python classes that inherits from MagicMock, that mimicks a Loop instance, the 'Loop.objects' query, the '_meta' field and a generic 'field'.

I refactored the tests in order to use the new mocks, and also moved the integration_loop tests under the integration_loop sub-app (before they were under the frontend main app).

To post a comment you must log in.
Revision history for this message
Stevan Radaković (stevanr) wrote :

Thanks Milo, it looks great!
Approve +1.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'Makefile'
--- Makefile 2012-09-10 16:49:11 +0000
+++ Makefile 2012-09-14 08:22:19 +0000
@@ -33,7 +33,7 @@
3333
34test:34test:
35 python dashboard/manage.py jenkins start35 python dashboard/manage.py jenkins start
36 python dashboard/manage.py test frontend android_build kernel_build \36 python dashboard/manage.py test frontend android_build integration_loop \
37 android_textfield_loop jenkinsserver37 kernel_build android_textfield_loop jenkinsserver
3838
39.PHONY: help migration test39.PHONY: help migration test
4040
=== modified file 'dashboard/frontend/android_build/tests/test_android_build_views.py'
--- dashboard/frontend/android_build/tests/test_android_build_views.py 2012-09-12 14:31:41 +0000
+++ dashboard/frontend/android_build/tests/test_android_build_views.py 2012-09-14 08:22:19 +0000
@@ -14,8 +14,6 @@
14#14#
15# You should have received a copy of the GNU Affero General Public License15# You should have received a copy of the GNU Affero General Public License
16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
17from django.test import TestCase
18from mock import MagicMock
1917
20from frontend.android_build.views.android_loop_create_view \18from frontend.android_build.views.android_loop_create_view \
21 import AndroidLoopCreateView19 import AndroidLoopCreateView
@@ -23,18 +21,13 @@
23 import AndroidLoopDetailView21 import AndroidLoopDetailView
24from frontend.android_build.views.android_loop_update_view\22from frontend.android_build.views.android_loop_update_view\
25 import AndroidLoopUpdateView23 import AndroidLoopUpdateView
2624from frontend.tests.test_views import (
2725 ViewsTestsGeneral,
28class AndroidBuildViewsTest(TestCase):26 LoopMock
2927)
30 def setUp(self):28
31 self.mock_loop = MagicMock()29
32 self.mock_loop.loop_ptr = MagicMock()30class AndroidBuildViewsTest(ViewsTestsGeneral):
33 self.mock_loop.loop_ptr.id = 0
34 self.mock_loop.loop_ptr.loopbuild_set = MagicMock()
35 self.mock_loop.loop_ptr.loopbuild_set.all = MagicMock()
36 self.mock_loop.next_loop = MagicMock()
37 self.mock_loop.next_loop.id = 1
3831
39 def test_create_view_get_context_data(self):32 def test_create_view_get_context_data(self):
40 android_loop_create_view = AndroidLoopCreateView()33 android_loop_create_view = AndroidLoopCreateView()
@@ -51,7 +44,8 @@
51 context = AndroidLoopDetailView.get_context_data(44 context = AndroidLoopDetailView.get_context_data(
52 android_loop_detail_view)45 android_loop_detail_view)
53 self.assertEqual(context['request'], "some request data")46 self.assertEqual(context['request'], "some request data")
54 self.assertEqual(context['builds'].__class__.__name__, "MagicMock")47 self.assertEqual(context['builds'].__class__.__name__,
48 LoopMock.__name__)
5549
56 def test_update_view_get_context_data(self):50 def test_update_view_get_context_data(self):
57 android_loop_update_view = AndroidLoopUpdateView()51 android_loop_update_view = AndroidLoopUpdateView()
5852
=== modified file 'dashboard/frontend/android_textfield_loop/tests/test_android_textfield_views.py'
--- dashboard/frontend/android_textfield_loop/tests/test_android_textfield_views.py 2012-09-10 07:45:21 +0000
+++ dashboard/frontend/android_textfield_loop/tests/test_android_textfield_views.py 2012-09-14 08:22:19 +0000
@@ -14,8 +14,6 @@
14#14#
15# You should have received a copy of the GNU Affero General Public License15# You should have received a copy of the GNU Affero General Public License
16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
17from django.test import TestCase
18from mock import MagicMock
1917
20from frontend.android_textfield_loop.views.android_textfield_loop_create_view \18from frontend.android_textfield_loop.views.android_textfield_loop_create_view \
21 import AndroidTextFieldLoopCreateView19 import AndroidTextFieldLoopCreateView
@@ -23,12 +21,13 @@
23 import AndroidTextFieldLoopDetailView21 import AndroidTextFieldLoopDetailView
24from frontend.android_textfield_loop.views.android_textfield_loop_update_view \22from frontend.android_textfield_loop.views.android_textfield_loop_update_view \
25 import AndroidTextFieldLoopUpdateView23 import AndroidTextFieldLoopUpdateView
2624from frontend.tests.test_views import (
2725 ViewsTestsGeneral,
28class AndroidTextFieldViewsTest(TestCase):26 LoopMock,
2927)
30 def setUp(self):28
31 self.mock_loop = MagicMock()29
30class AndroidTextFieldViewsTest(ViewsTestsGeneral):
3231
33 def test_create_view_get_context_data(self):32 def test_create_view_get_context_data(self):
34 android_textfield_create_view = AndroidTextFieldLoopCreateView()33 android_textfield_create_view = AndroidTextFieldLoopCreateView()
@@ -45,7 +44,8 @@
45 context = AndroidTextFieldLoopDetailView.get_context_data(44 context = AndroidTextFieldLoopDetailView.get_context_data(
46 android_textfield_detail_view)45 android_textfield_detail_view)
47 self.assertEqual(context['request'], "some request data")46 self.assertEqual(context['request'], "some request data")
48 self.assertEqual(context['builds'].__class__.__name__, "MagicMock")47 self.assertEqual(context['builds'].__class__.__name__,
48 LoopMock.__name__)
4949
50 def test_update_view_get_context_data(self):50 def test_update_view_get_context_data(self):
51 android_textfield_update_view = AndroidTextFieldLoopUpdateView()51 android_textfield_update_view = AndroidTextFieldLoopUpdateView()
5252
=== added directory 'dashboard/frontend/integration_loop/tests'
=== added file 'dashboard/frontend/integration_loop/tests/__init__.py'
--- dashboard/frontend/integration_loop/tests/__init__.py 1970-01-01 00:00:00 +0000
+++ dashboard/frontend/integration_loop/tests/__init__.py 2012-09-14 08:22:19 +0000
@@ -0,0 +1,27 @@
1# Copyright (C) 2012 Linaro
2#
3# This file is part of linaro-ci-dashboard.
4#
5# linaro-ci-dashboard is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Affero General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# linaro-ci-dashboard is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU Affero General Public License for more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
17
18from dashboard.frontend.integration_loop.tests.test_integration_loop_views \
19 import *
20from dashboard.frontend.integration_loop.tests.\
21 test_integration_loop_clientresponse import *
22
23
24__test__ = {
25 'IntegrationLoopViewsTest': IntegrationLoopViewsTest,
26 'IntegrationLoopClientresponseTest': IntegrationLoopClientresponseTest,
27}
0\ No newline at end of file28\ No newline at end of file
129
=== added file 'dashboard/frontend/integration_loop/tests/test_integration_loop_clientresponse.py'
--- dashboard/frontend/integration_loop/tests/test_integration_loop_clientresponse.py 1970-01-01 00:00:00 +0000
+++ dashboard/frontend/integration_loop/tests/test_integration_loop_clientresponse.py 2012-09-14 08:22:19 +0000
@@ -0,0 +1,75 @@
1# Copyright (C) 2012 Linaro
2#
3# This file is part of linaro-ci-dashboard.
4#
5# linaro-ci-dashboard is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Affero General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# linaro-ci-dashboard is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU Affero General Public License for more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
17
18import urllib
19from frontend.tests.test_clientresponse import ClientResponseTestsGeneral
20
21
22class IntegrationLoopClientresponseTest(ClientResponseTestsGeneral):
23
24 def setUp(self):
25 super(IntegrationLoopClientresponseTest, self).setUp()
26 self.integration_loop_data = {"name": "testjob",
27 "branch": "lp:linaro-ci-dashboard",
28 "precommand": "make migrate",
29 "command": "make test",
30 "next_loop_0": "",
31 }
32
33 def test_integration_loop_create_get(self):
34 self.client.login(username=self.user, password=self.passwd)
35 response = self.client.get("/integration_loop/create/")
36 self.assertIn("/integration_loop/create/", response.content)
37 template_names = [template.name for template in
38 response.templates]
39 self.assertEquals(template_names, ["integration_loop_create.html",
40 "loop_create.html",
41 "base.html",
42 "login.html",
43 ])
44
45 def test_integration_loop_create_post_no_data(self):
46 self.client.login(username=self.user, password=self.passwd)
47 response = self.client.post("/integration_loop/create/", {})
48 self.assertEquals(response.status_code, 200)
49 self.assertIn("This field is required", response.content)
50
51 def test_integration_loop_create_post(self):
52 self.client.login(username=self.user, password=self.passwd)
53
54 # Follow redirect after save.
55 response = self.client.post("/integration_loop/create/",
56 self.integration_loop_data, follow=True)
57 # Assert response code.
58 self.assertEquals(response.status_code, 200)
59 # Assert redirect chain.
60 self.assertEquals(response.redirect_chain[0][1], 302)
61 self.assertIn(urllib.quote(self.integration_loop_data["name"]),
62 response.redirect_chain[0][0])
63 # Assert template names for detail page.
64 template_names = [template.name for template in
65 response.templates]
66 self.assertEquals(template_names, ["loop_detail.html",
67 "base.html",
68 "login.html",
69 ])
70 # Assert if everything is present on detail page.
71 self.assertIn(self.integration_loop_data["name"], response.content)
72 self.assertIn(self.integration_loop_data["branch"], response.content)
73 self.assertIn(self.integration_loop_data["precommand"],
74 response.content)
75 self.assertIn(self.integration_loop_data["command"], response.content)
0\ No newline at end of file76\ No newline at end of file
177
=== added file 'dashboard/frontend/integration_loop/tests/test_integration_loop_views.py'
--- dashboard/frontend/integration_loop/tests/test_integration_loop_views.py 1970-01-01 00:00:00 +0000
+++ dashboard/frontend/integration_loop/tests/test_integration_loop_views.py 2012-09-14 08:22:19 +0000
@@ -0,0 +1,56 @@
1# Copyright (C) 2012 Linaro
2#
3# This file is part of linaro-ci-dashboard.
4#
5# linaro-ci-dashboard is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Affero General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# linaro-ci-dashboard is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU Affero General Public License for more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
17
18from frontend.integration_loop.views.integration_loop_create_view \
19 import IntegrationLoopCreateView
20from frontend.integration_loop.views.integration_loop_update_view \
21 import IntegrationLoopUpdateView
22from frontend.integration_loop.views.integration_loop_detail_view \
23 import IntegrationLoopDetailView
24from frontend.tests.test_views import (
25 ViewsTestsGeneral,
26 LoopMock,
27)
28
29
30class IntegrationLoopViewsTest(ViewsTestsGeneral):
31
32 def test_create_view_get_context_data(self):
33 integration_loop_create_view = IntegrationLoopCreateView()
34 integration_loop_create_view.request = "some request data"
35 integration_loop_create_view.object = self.mock_loop
36 context = IntegrationLoopCreateView.get_context_data(
37 integration_loop_create_view)
38 self.assertEqual(context['request'], "some request data")
39
40 def test_detail_view_get_context_data(self):
41 integration_loop_detail_view = IntegrationLoopDetailView()
42 integration_loop_detail_view.request = "some request data"
43 integration_loop_detail_view.object = self.mock_loop
44 context = IntegrationLoopDetailView.get_context_data(
45 integration_loop_detail_view)
46 self.assertEqual(context['request'], "some request data")
47 self.assertEqual(context['builds'].__class__.__name__,
48 LoopMock.__name__)
49
50 def test_update_view_get_context_data(self):
51 integration_loop_update_view = IntegrationLoopUpdateView()
52 integration_loop_update_view.request = "some request data"
53 integration_loop_update_view.object = self.mock_loop
54 context = IntegrationLoopUpdateView.get_context_data(
55 integration_loop_update_view)
56 self.assertEqual(context['request'], "some request data")
057
=== modified file 'dashboard/frontend/kernel_build/tests/test_kernel_build_views.py'
--- dashboard/frontend/kernel_build/tests/test_kernel_build_views.py 2012-08-23 11:22:21 +0000
+++ dashboard/frontend/kernel_build/tests/test_kernel_build_views.py 2012-09-14 08:22:19 +0000
@@ -14,8 +14,6 @@
14#14#
15# You should have received a copy of the GNU Affero General Public License15# You should have received a copy of the GNU Affero General Public License
16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
17from django.test import TestCase
18from mock import MagicMock
1917
20from frontend.kernel_build.views.kernel_loop_create_view \18from frontend.kernel_build.views.kernel_loop_create_view \
21 import KernelLoopCreateView19 import KernelLoopCreateView
@@ -23,16 +21,13 @@
23 import KernelLoopDetailView21 import KernelLoopDetailView
24from frontend.kernel_build.views.kernel_loop_update_view\22from frontend.kernel_build.views.kernel_loop_update_view\
25 import KernelLoopUpdateView23 import KernelLoopUpdateView
2624from frontend.tests.test_views import (
2725 ViewsTestsGeneral,
28class KernelBuildViewsTest(TestCase):26 LoopMock,
2927)
30 def setUp(self):28
31 self.mock_loop = MagicMock()29
32 self.mock_loop.loop_ptr = MagicMock()30class KernelBuildViewsTest(ViewsTestsGeneral):
33 self.mock_loop.loop_ptr.id = 0
34 self.mock_loop.loop_ptr.loopbuild_set = MagicMock()
35 self.mock_loop.loop_ptr.loopbuild_set.all = MagicMock()
3631
37 def test_create_view_get_context_data(self):32 def test_create_view_get_context_data(self):
38 kernel_loop_create_view = KernelLoopCreateView()33 kernel_loop_create_view = KernelLoopCreateView()
@@ -49,7 +44,8 @@
49 context = KernelLoopDetailView.get_context_data(44 context = KernelLoopDetailView.get_context_data(
50 kernel_loop_detail_view)45 kernel_loop_detail_view)
51 self.assertEqual(context['request'], "some request data")46 self.assertEqual(context['request'], "some request data")
52 self.assertEqual(context['builds'].__class__.__name__, "MagicMock")47 self.assertEqual(context['builds'].__class__.__name__,
48 LoopMock.__name__)
5349
54 def test_update_view_get_context_data(self):50 def test_update_view_get_context_data(self):
55 kernel_loop_update_view = KernelLoopUpdateView()51 kernel_loop_update_view = KernelLoopUpdateView()
5652
=== modified file 'dashboard/frontend/tests/__init__.py'
--- dashboard/frontend/tests/__init__.py 2012-09-05 17:32:44 +0000
+++ dashboard/frontend/tests/__init__.py 2012-09-14 08:22:19 +0000
@@ -9,9 +9,6 @@
9__test__ = {9__test__ = {
10 'HomePageViewTests': HomePageViewTest,10 'HomePageViewTests': HomePageViewTest,
11 'IndexViewTests': IndexViewTest,11 'IndexViewTests': IndexViewTest,
12 'IntegrationLoopCreateViewTests': IntegrationLoopCreateViewTest,
13 'IntegrationLoopDetailViewTests': IntegrationLoopDetailViewTest,
14 'IntegrationLoopUpdateViewTests': IntegrationLoopUpdateViewTest,
15 'LoopTests': LoopTest,12 'LoopTests': LoopTest,
16 'ClientResponseTests': ClientResponseTests,13 'ClientResponseTests': ClientResponseTests,
17 'JenkinsCommandTest': JenkinsCommandTest,14 'JenkinsCommandTest': JenkinsCommandTest,
1815
=== added file 'dashboard/frontend/tests/loop_mock.py'
--- dashboard/frontend/tests/loop_mock.py 1970-01-01 00:00:00 +0000
+++ dashboard/frontend/tests/loop_mock.py 2012-09-14 08:22:19 +0000
@@ -0,0 +1,125 @@
1# Copyright (C) 2012 Linaro
2#
3# This file is part of linaro-ci-dashboard.
4#
5# linaro-ci-dashboard is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Affero General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# linaro-ci-dashboard is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU Affero General Public License for more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
17
18from mock import MagicMock
19
20
21class ObjectsMock(MagicMock):
22 """
23 This mock represents the 'objects' queryset of a Django model. It is
24 basically used to mock access like 'MODEL.objects' when looking up
25 a specific model.
26 """
27 def __init__(self, *args, **kwargs):
28 super(ObjectsMock, self).__init__(*args, **kwargs)
29
30 def get(self, *args, **kwargs):
31 return LoopMock()
32
33 def all(self):
34 return [LoopMock()]
35
36
37class LoopMock(MagicMock):
38 """
39 Class that mocks a generic Loop object. This class should be used when it
40 is necessary to pass an actual object.
41 """
42 def __init__(self, *args, **kwargs):
43 super(LoopMock, self).__init__(*args, **kwargs)
44
45 @property
46 def name(self):
47 return self.__class__.__name__
48
49 @property
50 def type(self):
51 return self.__class__.__name__
52
53 @property
54 def is_restricted(self):
55 return False
56
57 @property
58 def is_protected(self):
59 return False
60
61 @property
62 def _meta(self):
63 return MetaMock()
64
65
66class MetaMock(MagicMock):
67 """
68 Mock class that represents the Meta class of a Django model, accessed via
69 the _meta attribute of a model instance.
70 """
71 def __init__(self, *args, **kwargs):
72 super(MetaMock, self).__init__(*args, **kwargs)
73
74 @property
75 def app_label(self):
76 return "label"
77
78 def get_field(self, name=None):
79 """
80 Mocks the get_field method of _meta.
81
82 :param name: The name of the field to get back.
83 :type name str
84 :return A FieldMock object mocking a field.
85 """
86 return FieldMock(name=name)
87
88
89class FieldMock(MagicMock):
90 """
91 Mock class that represents a field in a class model.
92 """
93 def __init__(self, name=None, verbose_name=None, *args, **kwargs):
94 """
95 Initialized the field mock. By default, if passed with no arguments,
96 the field name and verbose name will be set to the class name.
97
98 :param name: The name associated with this field.
99 :type name str
100 :param verbose_name: The verbose name associated with this field.
101 :type verbose_name str
102 """
103 super(FieldMock, self).__init__(*args, **kwargs)
104 if name is not None:
105 self._name = name
106 else:
107 self._name = self.__class__.__name__
108 if verbose_name is not None:
109 self._verbose_name = verbose_name
110 else:
111 self._verbose_name = self.__class__.__name__
112
113 @property
114 def name(self):
115 """
116 :return The name of the field.
117 """
118 return self._name
119
120 @property
121 def verbose_name(self):
122 """
123 :return The verbose name associated with the field.
124 """
125 return self._verbose_name
0126
=== modified file 'dashboard/frontend/tests/test_clientresponse.py'
--- dashboard/frontend/tests/test_clientresponse.py 2012-09-12 13:15:13 +0000
+++ dashboard/frontend/tests/test_clientresponse.py 2012-09-14 08:22:19 +0000
@@ -19,7 +19,6 @@
19from django.test import TestCase19from django.test import TestCase
20from django.contrib.auth.models import User, Group20from django.contrib.auth.models import User, Group
21from django.test.client import Client21from django.test.client import Client
22import urllib
2322
2423
25class ClientResponseTestsGeneral(TestCase):24class ClientResponseTestsGeneral(TestCase):
@@ -44,15 +43,6 @@
4443
45class ClientResponseTests(ClientResponseTestsGeneral):44class ClientResponseTests(ClientResponseTestsGeneral):
4645
47 def setUp(self):
48 super(ClientResponseTests, self).setUp()
49 self.integration_loop_data = {"name": "testjob",
50 "branch": "lp:linaro-ci-dashboard",
51 "precommand": "make migrate",
52 "command": "make test",
53 "next_loop_0": "",
54 }
55
56 # Test case should fail if the login_required decorator source code46 # Test case should fail if the login_required decorator source code
57 # in index_view.py is removed.47 # in index_view.py is removed.
58 def test_response_index_when_user_not_auth_by_login_required(self):48 def test_response_index_when_user_not_auth_by_login_required(self):
@@ -74,47 +64,3 @@
74 self.assertIn(str(self.user), response.content)64 self.assertIn(str(self.user), response.content)
75 response = self.client.get('/home/')65 response = self.client.get('/home/')
76 self.assertIn(str(self.user), response.content)66 self.assertIn(str(self.user), response.content)
77
78 def test_integration_loop_create_get(self):
79 self.client.login(username=self.user, password=self.passwd)
80 response = self.client.get("/integration_loop/create/")
81 self.assertIn("/integration_loop/create/", response.content)
82 template_names = [template.name for template in
83 response.templates]
84 self.assertEquals(template_names, ["integration_loop_create.html",
85 "loop_create.html",
86 "base.html",
87 "login.html",
88 ])
89
90 def test_integration_loop_create_post_no_data(self):
91 self.client.login(username=self.user, password=self.passwd)
92 response = self.client.post("/integration_loop/create/", {})
93 self.assertEquals(response.status_code, 200)
94 self.assertIn("This field is required", response.content)
95
96 def test_integration_loop_create_post(self):
97 self.client.login(username=self.user, password=self.passwd)
98
99 # Follow redirect after save.
100 response = self.client.post("/integration_loop/create/",
101 self.integration_loop_data, follow=True)
102 # Assert response code.
103 self.assertEquals(response.status_code, 200)
104 # Assert redirect chain.
105 self.assertEquals(response.redirect_chain[0][1], 302)
106 self.assertIn(urllib.quote(self.integration_loop_data["name"]),
107 response.redirect_chain[0][0])
108 # Assert template names for detail page.
109 template_names = [template.name for template in
110 response.templates]
111 self.assertEquals(template_names, ["loop_detail.html",
112 "base.html",
113 "login.html",
114 ])
115 # Assert if everything is present on detail page.
116 self.assertIn(self.integration_loop_data["name"], response.content)
117 self.assertIn(self.integration_loop_data["branch"], response.content)
118 self.assertIn(self.integration_loop_data["precommand"],
119 response.content)
120 self.assertIn(self.integration_loop_data["command"], response.content)
12167
=== modified file 'dashboard/frontend/tests/test_views.py'
--- dashboard/frontend/tests/test_views.py 2012-08-16 15:38:48 +0000
+++ dashboard/frontend/tests/test_views.py 2012-09-14 08:22:19 +0000
@@ -15,17 +15,25 @@
15#15#
16# You should have received a copy of the GNU Affero General Public License16# You should have received a copy of the GNU Affero General Public License
17# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.17# along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
18
18from django.test import TestCase19from django.test import TestCase
19from mock import Mock, patch
2020
21from frontend.views.home_page_view import HomePageView21from frontend.views.home_page_view import HomePageView
22from frontend.views.index_view import IndexView22from frontend.views.index_view import IndexView
23from frontend.integration_loop.views.integration_loop_create_view \23from frontend.tests.loop_mock import (
24 import IntegrationLoopCreateView24 ObjectsMock,
25from frontend.integration_loop.views.integration_loop_update_view \25 LoopMock,
26 import IntegrationLoopUpdateView26)
27from frontend.integration_loop.views.integration_loop_detail_view \27from frontend.models.loop import Loop
28 import IntegrationLoopDetailView28
29
30class ViewsTestsGeneral(TestCase):
31 def setUp(self):
32 self.mock_loop = LoopMock()
33 Loop.objects = ObjectsMock()
34
35 def tearDown(self):
36 super(ViewsTestsGeneral, self).tearDown()
2937
3038
31class HomePageViewTest(TestCase):39class HomePageViewTest(TestCase):
@@ -44,49 +52,3 @@
44 index_view.request = "some request data"52 index_view.request = "some request data"
45 context = IndexView.get_context_data(index_view)53 context = IndexView.get_context_data(index_view)
46 self.assertEqual(context['request'], "some request data")54 self.assertEqual(context['request'], "some request data")
47
48
49class IntegrationLoopCreateViewTest(TestCase):
50
51 def setUp(self):
52 self.mock_loop = Mock()
53
54 def test_get_context_data(self):
55 integration_loop_create_view = IntegrationLoopCreateView()
56 integration_loop_create_view.request = "some request data"
57 integration_loop_create_view.object = self.mock_loop
58 context = IntegrationLoopCreateView.get_context_data(
59 integration_loop_create_view)
60 self.assertEqual(context['request'], "some request data")
61
62
63class IntegrationLoopDetailViewTest(TestCase):
64
65 def setUp(self):
66 self.mock_loop = Mock()
67 self.mock_loop.loop_ptr = Mock()
68 self.mock_loop.loop_ptr.loopbuild_set = Mock()
69 self.mock_loop.loop_ptr.loopbuild_set.all = Mock()
70
71 def test_get_context_data(self):
72 integration_loop_detail_view = IntegrationLoopDetailView()
73 integration_loop_detail_view.request = "some request data"
74 integration_loop_detail_view.object = self.mock_loop
75 context = IntegrationLoopDetailView.get_context_data(
76 integration_loop_detail_view)
77 self.assertEqual(context['request'], "some request data")
78 self.assertEqual(context['builds'].__class__.__name__, "Mock")
79
80
81class IntegrationLoopUpdateViewTest(TestCase):
82
83 def setUp(self):
84 self.mock_loop = Mock()
85
86 def test_get_context_data(self):
87 integration_loop_update_view = IntegrationLoopUpdateView()
88 integration_loop_update_view.request = "some request data"
89 integration_loop_update_view.object = self.mock_loop
90 context = IntegrationLoopUpdateView.get_context_data(
91 integration_loop_update_view)
92 self.assertEqual(context['request'], "some request data")
9355
=== modified file 'dashboard/frontend/views/loop_detail_view.py'
--- dashboard/frontend/views/loop_detail_view.py 2012-09-12 14:31:41 +0000
+++ dashboard/frontend/views/loop_detail_view.py 2012-09-14 08:22:19 +0000
@@ -55,9 +55,7 @@
55 field = self.object._meta.get_field('next_loop')55 field = self.object._meta.get_field('next_loop')
56 pk = self.object.next_loop_id56 pk = self.object.next_loop_id
57 verbose_name = field.verbose_name.capitalize()57 verbose_name = field.verbose_name.capitalize()
58 # XXX changed to isinstance, since tests were failing: we need to58 if pk is not None:
59 # mock Loop, not the model variable, in a better way in our tests.
60 if isinstance(pk, int):
61 # Get the pointer to the object we want to retrieve, and retrieve59 # Get the pointer to the object we want to retrieve, and retrieve
62 # the actual instance. We can do this since all our loops inherits60 # the actual instance. We can do this since all our loops inherits
63 # from the same base class (Loop), and we have the information61 # from the same base class (Loop), and we have the information

Subscribers

People subscribed via source and target branches