source: main/trunk/openPLM/plmapp/tests/controllers/plmobject.py @ 472

Revision 472, 10.0 KB checked in by pcosquer, 8 years ago (diff)

tests: move some testcases to their applications + clean up imports

Line 
1############################################################################
2# openPLM - open source PLM
3# Copyright 2010 Philippe Joulaud, Pierre Cosquer
4#
5# This file is part of openPLM.
6#
7#    openPLM is free software: you can redistribute it and/or modify
8#    it under the terms of the GNU General Public License as published by
9#    the Free Software Foundation, either version 3 of the License, or
10#    (at your option) any later version.
11#
12#    openPLM is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#    GNU General Public License for more details.
16#
17#    You should have received a copy of the GNU General Public License
18#    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
19#
20# Contact :
21#    Philippe Joulaud : ninoo.fr@gmail.com
22#    Pierre Cosquer : pierre.cosquer@insa-rennes.fr
23################################################################################
24
25"""
26This module contains some tests for openPLM.
27"""
28
29from django.db import IntegrityError
30from django.contrib.auth.models import User
31
32from openPLM.plmapp.utils import level_to_sign_str
33import openPLM.plmapp.exceptions as exc
34import openPLM.plmapp.models as models
35from openPLM.plmapp.controllers import PLMObjectController
36from openPLM.plmapp.lifecycle import LifecycleList
37
38from openPLM.plmapp.tests.base import BaseTestCase
39
40class ControllerTest(BaseTestCase):
41    CONTROLLER = PLMObjectController
42    TYPE = "Part"
43    DATA = {}
44
45
46    def test_create(self):
47        controller = self.create()
48        self.assertEqual(controller.name, "")
49        self.assertEqual(controller.type, self.TYPE)
50        self.assertEqual(type(controller.object),
51                models.get_all_plmobjects()[self.TYPE])
52        type_ = models.get_all_plmobjects()[self.TYPE]
53        obj = type_.objects.get(reference=controller.reference,
54                revision=controller.revision, type=controller.type)
55        self.assertEqual(obj.owner, self.user)
56        self.assertEqual(obj.creator, self.user)
57        models.PLMObjectUserLink.objects.get(plmobject=obj, user=self.user,
58                role=models.ROLE_OWNER)
59        self.failUnless(obj.is_editable)
60
61    def test_create_error1(self):
62        # empty reference
63        self.assertRaises(ValueError, self.create, "")
64
65    def test_create_error2(self):
66        # empty revision
67        def fail():
68            controller = self.CONTROLLER.create("paer", self.TYPE, "",
69                                            self.user, self.DATA)
70        self.assertRaises(ValueError, fail)
71
72    def test_create_error3(self):
73        # empty reference
74        def fail():
75            controller = self.CONTROLLER.create("zeez", "", "a",
76                                            self.user, self.DATA)
77        self.assertRaises(ValueError, fail)
78
79    def test_create_error4(self):
80        # bad type
81        def fail():
82            controller = self.CONTROLLER.create("zee", "__", "a",
83                                            self.user, self.DATA)
84        self.assertRaises(ValueError, fail)
85   
86    def test_create_error5(self):
87        # bad type : PLMObject
88        def fail():
89            controller = self.CONTROLLER.create("zee", "PLMOBject_", "a",
90                                            self.user, self.DATA)
91        self.assertRaises(ValueError, fail)
92   
93    def test_create_error6(self):
94        """Create error test : user is not a contributor"""
95        self.user.get_profile().is_contributor = False
96        self.user.get_profile().save()
97        def fail():
98            controller = self.CONTROLLER.create("zee", "PLMOBject_", "a",
99                                            self.user, self.DATA)
100        self.assertRaises(exc.PermissionError, fail)
101
102    def test_keys(self):
103        controller = self.create("Part1")
104        controller2 = self.create("Part2")
105        self.assertRaises(IntegrityError, self.create, "Part1")
106
107    def test_getattr(self):
108        controller = self.create("Part1")
109        self.assertEqual(controller.name, "")
110        self.failUnless("name" in controller.attributes)
111        self.assertEqual(controller.state.name, "draft")
112        self.assertRaises(AttributeError, lambda: controller.unknown_attr)
113
114    def test_setattr(self):
115        controller = self.create("Part1")
116        self.assertEqual(controller.name, "")
117        controller.name = "a name"
118        self.assertEqual(controller.name, "a name")
119        controller.save()
120        self.assertEqual(controller.name, "a name")
121
122    def test_setattr_errors(self):
123        controller = self.create("Part1")
124        self.assertRaises(ValueError, setattr, controller, "owner", "error")
125        self.assertRaises(ValueError, setattr, controller, "state", "error")
126        self.assertRaises(ValueError, setattr, controller, "state", "draft")
127
128    def test_promote(self):
129        controller = self.create("Part1")
130        self.assertEqual(controller.state.name, "draft")
131        controller.promote()
132        self.assertEqual(controller.state.name, "official")
133        self.failIf(controller.is_editable)
134        self.assertRaises(exc.PromotionError, controller.demote)
135        lcl = LifecycleList("diop", "official", "draft",
136                "issue1", "official", "deprecated")
137        lc = models.Lifecycle.from_lifecyclelist(lcl)
138        controller.lifecycle = lc
139        controller.state = models.State.objects.get(name="draft")
140        controller.save()
141        controller.promote()
142        self.assertEqual(controller.state.name, "issue1")
143        controller.demote()
144        self.assertEqual(controller.state.name, "draft")
145        self.failUnless(controller.is_editable)
146
147    def test_revise(self):
148        """
149        Test :meth:`revise`
150        """
151        controller = self.create("Part1")
152        rev = controller.revise("b")
153        self.assertEqual(rev.revision, "b")
154        def fail():
155            controller.revise("b2")
156        self.assertRaises(exc.RevisionError, fail)
157        for attr in controller.get_modification_fields():
158            self.assertEqual(getattr(controller, attr), getattr(rev, attr))
159
160    def test_revise_error1(self):
161        "Revision : error : empty name"
162        controller = self.create("Part1")
163        self.assertRaises(exc.RevisionError, controller.revise, "")
164   
165    def test_revise_error2(self):
166        "Revision : error : same revision name"
167        controller = self.create("Part1")
168        self.assertRaises(exc.RevisionError, controller.revise, "a")
169
170    def test_set_owner(self):
171        controller = self.create("Part1")
172        user = User(username="user2")
173        user.save()
174        user.get_profile().is_contributor = True
175        user.get_profile().save()
176        controller.set_owner(user)
177        self.assertEqual(controller.owner, user)
178
179    def test_set_owner_error(self):
180        controller = self.create("Part1")
181        user = User(username="user2")
182        user.save()
183        self.assertRaises(exc.PermissionError, controller.set_owner, user)
184
185    def test_set_sign1(self):
186        controller = self.create("Part1")
187        user = User(username="user2")
188        user.save()
189        user.get_profile().is_contributor = True
190        user.get_profile().save()
191        controller.set_signer(user, level_to_sign_str(0))
192        link = models.PLMObjectUserLink.objects.get(role=level_to_sign_str(0),
193                                             plmobject=controller.object)
194        self.assertEqual(user, link.user)
195
196    def test_set_sign_error1(self):
197        """Test sign error : bad level"""
198        controller = self.create("Part1")
199        user = User(username="user2")
200        user.save()
201        user.get_profile().is_contributor = True
202        user.get_profile().save()
203        self.assertRaises(exc.PermissionError, controller.set_role, user,
204                          level_to_sign_str(1664))
205
206    def test_set_sign_error2(self):
207        """Test sign error : user is not a contributor"""   
208        controller = self.create("Part1")
209        user = User(username="user2")
210        user.save()
211        self.assertRaises(exc.PermissionError, controller.set_role, user,
212                          level_to_sign_str(0))
213
214    def test_add_notified(self):
215        controller = self.create("Part1")
216        user = User(username="user2")
217        user.save()
218        controller.add_notified(user)
219        models.PLMObjectUserLink.objects.get(user=user, plmobject=controller.object,
220                                      role="notified")
221
222    def test_remove_notified(self):
223        controller = self.create("Part1")
224        controller.add_notified(self.user)
225        models.PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object,
226                                      role="notified")
227        controller.remove_notified(self.user)
228        self.assertEqual(0, len(models.PLMObjectUserLink.objects.filter(
229            plmobject=controller.object, role="notified")))
230
231    def test_set_role(self):
232        controller = self.create("Part1")
233        user = User(username="user2")
234        user.save()
235        user.get_profile().is_contributor = True
236        user.get_profile().save()
237        controller.set_role(user, "owner")
238        self.assertEqual(controller.owner, user)
239        controller.set_role(self.user, "notified")
240        models.PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object,
241                                      role="notified")
242        controller.set_role(user, level_to_sign_str(0))
243        link = models.PLMObjectUserLink.objects.get(role=level_to_sign_str(0),
244                                             plmobject=controller.object)
245        self.assertEqual(user, link.user)
246
247    def test_promote_error(self):
248        """
249        Tests that a :exc:`.PromotionError` is raised when
250        :meth:`.PLMObject.is_promotable` returns False.
251        """
252        controller = self.create("Part1")
253        # fake function so that is_promotable returns False
254        def always_false():
255            return False
256        controller.object.is_promotable = always_false
257        self.assertRaises(exc.PromotionError, controller.promote)
258
Note: See TracBrowser for help on using the repository browser.