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

Revision 662, 9.3 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
29import datetime
30
31from openPLM.plmapp.controllers import PLMObjectController, PartController, \
32        DocumentController
33import openPLM.plmapp.exceptions as exc
34import openPLM.plmapp.models as models
35from openPLM.plmapp.lifecycle import LifecycleList
36
37from openPLM.plmapp.tests.controllers.plmobject import ControllerTest
38
39
40class PartControllerTest(ControllerTest):
41    TYPE = "Part"
42    CONTROLLER = PartController
43   
44    def setUp(self):
45        super(PartControllerTest, self).setUp()
46        self.controller = self.CONTROLLER.create("aPart1", self.TYPE, "a",
47                                                 self.user, self.DATA)
48        self.controller2 = self.CONTROLLER.create("aPart2", self.TYPE, "a",
49                                                  self.user, self.DATA)
50        self.controller3 = self.CONTROLLER.create("aPart3", self.TYPE, "a",
51                                                  self.user, self.DATA)
52        self.controller4 = self.CONTROLLER.create("aPart4", self.TYPE, "a",
53                                                  self.user, self.DATA)
54        self.document = DocumentController.create("Doc1", "Document", "a",
55                self.user, self.DATA)
56        self.document.add_file(self.get_file())
57        self.document.promote()
58        for ctrl in (self.controller, self.controller2):
59            ctrl.attach_to_document(self.document)
60
61    def test_add_child(self):
62        children = self.controller.get_children()
63        self.assertEqual(len(children), 0)
64        self.controller.add_child(self.controller2, 10, 15)
65        children = self.controller.get_children()
66        self.assertEqual(len(children), 1)
67        level, link = children[0]
68        self.assertEqual(level, 1)
69        self.assertEqual(link.child.pk, self.controller2.object.pk)
70        self.assertEqual(link.parent.pk, self.controller.object.pk)
71        self.assertEqual(link.quantity, 10)
72        self.assertEqual(link.order, 15)
73
74    def test_add_child_error1(self):
75        def fail():
76            # bad quantity
77            self.controller.add_child(self.controller2, -10, 15)
78        self.assertRaises(ValueError, fail)
79
80    def test_add_child_error2(self):
81        def fail():
82            # bad order
83            self.controller.add_child(self.controller2, 10, -15)
84        self.assertRaises(ValueError, fail)
85   
86    def test_add_child_error3(self):
87        def fail():
88            # bad child : parent
89            self.controller2.add_child(self.controller, 10, 15)
90            self.controller.add_child(self.controller2, 10, 15)
91        self.assertRaises(ValueError, fail)
92   
93    def test_add_child_error4(self):
94        def fail():
95            # bad child : already a child
96            self.controller.add_child(self.controller2, 10, 15)
97            self.controller.add_child(self.controller2, 10, 15)
98        self.assertRaises(ValueError, fail)
99   
100    def test_add_child_error5(self):
101        def fail():
102            # bad child type
103            doc = PLMObjectController.create("e", "PLMObject", "1", self.user)
104            self.controller.add_child(doc, 10, 15)
105        self.assertRaises(ValueError, fail)
106
107    def test_add_child_error6(self):
108        def fail():
109            # bad child : add self
110            self.controller.add_child(self.controller, 10, 15)
111        self.assertRaises(ValueError, fail)
112
113    def test_modify_child(self):
114        self.controller.add_child(self.controller2, 10, 15, "-")
115        self.controller.modify_child(self.controller2, 3, 5, "kg")
116        children = self.controller.get_children()
117        level, link = children[0]
118        self.assertEqual(link.quantity, 3)
119        self.assertEqual(link.order, 5)
120        self.assertEqual(link.unit, "kg")
121
122    def test_delete_child(self):
123        self.controller.add_child(self.controller2, 10, 15)
124        self.controller.delete_child(self.controller2)
125        self.assertEqual(self.controller.get_children(), [])
126
127    def test_get_children(self):
128        self.controller.add_child(self.controller2, 10, 15)
129        date = datetime.datetime.now()
130        self.controller2.add_child(self.controller3, 10, 15)
131        self.controller.add_child(self.controller4, 10, 15)
132        wanted = [(1, self.controller2.object.pk),
133                  (2, self.controller3.object.pk),
134                  (1, self.controller4.object.pk)]
135        children = [(lvl, lk.child.pk) for lvl, lk in self.controller.get_children(-1)]
136        self.assertEqual(children, wanted)
137        wanted = [(1, self.controller2.object.pk),
138                  (1, self.controller4.object.pk)]
139        # first level
140        children = [(lvl, lk.child.pk) for lvl, lk in self.controller.get_children(1)]
141        self.assertEqual(children, wanted)
142        # date
143        wanted = [(1, self.controller2.object.pk)]
144        children = [(lvl, lk.child.pk) for lvl, lk in self.controller.get_children(date=date)]
145        self.assertEqual(children, wanted)
146
147    def test_get_parents(self):
148        self.controller.add_child(self.controller2, 10, 15)
149        date = datetime.datetime.now()
150        self.controller2.add_child(self.controller3, 10, 15)
151        self.controller.add_child(self.controller4, 10, 15)
152        wanted = [(1, self.controller2.object.pk),
153                  (2, self.controller.object.pk),]
154        parents = [(lvl, lk.parent.pk) for lvl, lk in self.controller3.get_parents(-1)]
155        self.assertEqual(parents, wanted)
156        wanted = [(1, self.controller2.object.pk)]
157        # first level
158        parents = [(lvl, lk.parent.pk) for lvl, lk in self.controller3.get_parents(1)]
159        self.assertEqual(parents, wanted)
160        # date
161        parents = [(lvl, lk.parent.pk) for lvl, lk in self.controller3.get_parents(date=date)]
162        self.assertEqual(parents, [])
163
164    def test_is_promotable1(self):
165        """Tests promotion from draft state, an official document is attached."""
166        self.failUnless(self.controller.is_promotable())
167
168    def test_is_promotable2(self):
169        """Tests promotion from official state."""
170        self.controller.promote()
171        self.failUnless(self.controller.is_promotable())
172   
173    def test_is_promotable3(self):
174        """Tests promotions with an official child."""
175        self.controller2.promote()
176        self.controller.add_child(self.controller2, 10, 15)
177        self.failUnless(self.controller.is_promotable())
178       
179    def test_is_promotable4(self):
180        """Tests promotion from official state, with a deprecated child."""
181        self.controller2.promote()
182        self.controller2.promote()
183        self.controller.add_child(self.controller2, 10, 15)
184        self.failUnless(self.controller.is_promotable())
185
186    def test_is_promotable_no_document(self):
187        "Tests that a part with no document attached is not promotable."""
188        self.failIf(self.controller3.is_promotable())
189
190    def test_is_promotable_no_official_document(self):
191        "Tests that a part with no official document attached is not promotable."
192        doc = DocumentController.create("doc_2", "Document", "a", self.user,
193                self.DATA)
194        self.controller3.attach_to_document(doc)
195        self.failIf(self.controller3.is_promotable())
196
197    def test_is_promotable_one_official_document(self):
198        """Tests that a part with one official document attached and another
199        not official is promotable."""
200        doc = DocumentController.create("doc_2", "Document", "a", self.user,
201                self.DATA)
202        self.controller3.attach_to_document(self.document)
203        self.controller3.attach_to_document(doc)
204        self.failUnless(self.controller3.is_promotable())
205
206    def test_promote(self):
207        controller = self.controller
208        self.assertEqual(controller.state.name, "draft")
209        controller.promote()
210        self.assertEqual(controller.state.name, "official")
211        self.failIf(controller.is_editable)
212        self.assertRaises(exc.PromotionError, controller.demote)
213        lcl = LifecycleList("diop", "official", "draft",
214                "issue1", "official", "deprecated")
215        lc = models.Lifecycle.from_lifecyclelist(lcl)
216        controller.lifecycle = lc
217        controller.state = models.State.objects.get(name="draft")
218        controller.save()
219        controller.promote()
220        self.assertEqual(controller.state.name, "issue1")
221        controller.demote()
222        self.assertEqual(controller.state.name, "draft")
223        self.failUnless(controller.is_editable)
224
225
Note: See TracBrowser for help on using the repository browser.