source: main/branches/3D/openPLM/plmapp/tests/controllers/plmobject.py @ 662

Revision 662, 9.4 KB checked in by pcosquer, 8 years ago (diff)

3D branch: merge changes from trunk (rev [661])

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
36
37from openPLM.plmapp.tests.base import BaseTestCase
38
39class ControllerTest(BaseTestCase):
40    CONTROLLER = PLMObjectController
41    TYPE = "Part"
42    DATA = {}
43
44
45    def test_create(self):
46        controller = self.create()
47        self.assertEqual(controller.name, "")
48        self.assertEqual(controller.type, self.TYPE)
49        type_ = models.get_all_plmobjects()[self.TYPE]
50        self.assertEqual(type(controller.object), type_)
51        obj = type_.objects.get(reference=controller.reference,
52                revision=controller.revision, type=controller.type)
53        self.assertEqual(obj.owner, self.user)
54        self.assertEqual(obj.creator, self.user)
55        models.PLMObjectUserLink.objects.get(plmobject=obj, user=self.user,
56                role=models.ROLE_OWNER)
57        self.failUnless(obj.is_editable)
58
59    def test_create_error1(self):
60        # empty reference
61        self.assertRaises(ValueError, self.create, "")
62
63    def test_create_error2(self):
64        # empty revision
65        def fail():
66            controller = self.CONTROLLER.create("paer", self.TYPE, "",
67                                            self.user, self.DATA)
68        self.assertRaises(ValueError, fail)
69
70    def test_create_error3(self):
71        # empty reference
72        def fail():
73            controller = self.CONTROLLER.create("zeez", "", "a",
74                                            self.user, self.DATA)
75        self.assertRaises(ValueError, fail)
76
77    def test_create_error4(self):
78        # bad type
79        def fail():
80            controller = self.CONTROLLER.create("zee", "__", "a",
81                                            self.user, self.DATA)
82        self.assertRaises(ValueError, fail)
83   
84    def test_create_error5(self):
85        # bad type : PLMObject
86        def fail():
87            controller = self.CONTROLLER.create("zee", "PLMOBject_", "a",
88                                            self.user, self.DATA)
89        self.assertRaises(ValueError, fail)
90   
91    def test_create_error6(self):
92        """Create error test : user is not a contributor"""
93        self.user.get_profile().is_contributor = False
94        self.user.get_profile().save()
95        def fail():
96            controller = self.CONTROLLER.create("zee", "PLMOBject_", "a",
97                                            self.user, self.DATA)
98        self.assertRaises(exc.PermissionError, fail)
99
100    def test_keys(self):
101        controller = self.create("Part1")
102        controller2 = self.create("Part2")
103        self.assertRaises(IntegrityError, self.create, "Part1")
104
105    def test_getattr(self):
106        controller = self.create("Part1")
107        self.assertEqual(controller.name, "")
108        self.failUnless("name" in controller.attributes)
109        self.assertEqual(controller.state.name, "draft")
110        self.assertRaises(AttributeError, lambda: controller.unknown_attr)
111
112    def test_setattr(self):
113        controller = self.create("Part1")
114        self.assertEqual(controller.name, "")
115        controller.name = "a name"
116        self.assertEqual(controller.name, "a name")
117        controller.save()
118        self.assertEqual(controller.name, "a name")
119
120    def test_setattr_errors(self):
121        controller = self.create("Part1")
122        self.assertRaises(ValueError, setattr, controller, "owner", "error")
123        self.assertRaises(ValueError, setattr, controller, "state", "error")
124        self.assertRaises(ValueError, setattr, controller, "state", "draft")
125
126    def test_revise(self):
127        """
128        Test :meth:`revise`
129        """
130        controller = self.create("Part1")
131        rev = controller.revise("b")
132        self.assertEqual(rev.revision, "b")
133        def fail():
134            controller.revise("b2")
135        self.assertRaises(exc.RevisionError, fail)
136        for attr in controller.get_modification_fields():
137            self.assertEqual(getattr(controller, attr), getattr(rev, attr))
138
139    def test_revise_official(self):
140        ctrl = self.create("Part1")
141        ctrl.state = ctrl.lifecycle.official_state
142        ctrl.set_owner(self.cie)
143        ctrl.save()
144        self.failUnless(ctrl.is_revisable())
145        rev = ctrl.revise("b")
146        self.assertEqual(self.user, rev.owner)
147
148    def test_revise_error1(self):
149        "Revision : error : empty new revision"
150        controller = self.create("Part1")
151        self.assertRaises(exc.RevisionError, controller.revise, "")
152   
153    def test_revise_error2(self):
154        "Revision : error : same revision name"
155        controller = self.create("Part1")
156        self.assertRaises(exc.RevisionError, controller.revise, "a")
157
158    def test_set_owner(self):
159        controller = self.create("Part1")
160        user = User(username="user2")
161        user.save()
162        user.get_profile().is_contributor = True
163        user.get_profile().save()
164        controller.set_owner(user)
165        self.assertEqual(controller.owner, user)
166
167    def test_set_owner_error(self):
168        controller = self.create("Part1")
169        user = User(username="user2")
170        user.save()
171        self.assertRaises(exc.PermissionError, controller.set_owner, user)
172
173    def test_set_sign1(self):
174        controller = self.create("Part1")
175        user = User(username="user2")
176        user.save()
177        user.get_profile().is_contributor = True
178        user.get_profile().save()
179        controller.set_signer(user, level_to_sign_str(0))
180        link = models.PLMObjectUserLink.objects.get(role=level_to_sign_str(0),
181                                             plmobject=controller.object)
182        self.assertEqual(user, link.user)
183
184    def test_set_sign_error1(self):
185        """Test sign error : bad level"""
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        self.assertRaises(exc.PermissionError, controller.set_role, user,
192                          level_to_sign_str(1664))
193
194    def test_set_sign_error2(self):
195        """Test sign error : user is not a contributor"""   
196        controller = self.create("Part1")
197        user = User(username="user2")
198        user.save()
199        self.assertRaises(exc.PermissionError, controller.set_role, user,
200                          level_to_sign_str(0))
201
202    def test_add_notified(self):
203        controller = self.create("Part1")
204        user = User(username="user2")
205        user.save()
206        controller.add_notified(user)
207        models.PLMObjectUserLink.objects.get(user=user, plmobject=controller.object,
208                                      role="notified")
209
210    def test_remove_notified(self):
211        controller = self.create("Part1")
212        controller.add_notified(self.user)
213        models.PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object,
214                                      role="notified")
215        controller.remove_notified(self.user)
216        self.assertEqual(0, len(models.PLMObjectUserLink.objects.filter(
217            plmobject=controller.object, role="notified")))
218
219    def test_set_role(self):
220        controller = self.create("Part1")
221        user = User(username="user2")
222        user.save()
223        user.get_profile().is_contributor = True
224        user.get_profile().save()
225        controller.set_role(user, "owner")
226        self.assertEqual(controller.owner, user)
227        controller.set_role(self.user, "notified")
228        models.PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object,
229                                      role="notified")
230        controller.set_role(user, level_to_sign_str(0))
231        link = models.PLMObjectUserLink.objects.get(role=level_to_sign_str(0),
232                                             plmobject=controller.object)
233        self.assertEqual(user, link.user)
234
235    def test_promote_error(self):
236        """
237        Tests that a :exc:`.PromotionError` is raised when
238        :meth:`.PLMObject.is_promotable` returns False.
239        """
240        controller = self.create("Part1")
241        # fake function so that is_promotable returns False
242        def always_false():
243            return False
244        controller.object.is_promotable = always_false
245        self.assertRaises(exc.PromotionError, controller.promote)
246
Note: See TracBrowser for help on using the repository browser.