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

Revision 472, 6.9 KB checked in by pcosquer, 9 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
29import datetime
30
31from openPLM.plmapp.controllers import PLMObjectController, PartController
32
33from openPLM.plmapp.tests.controllers.plmobject import ControllerTest
34
35
36class PartControllerTest(ControllerTest):
37    TYPE = "Part"
38    CONTROLLER = PartController
39   
40    def setUp(self):
41        super(PartControllerTest, self).setUp()
42        self.controller = self.CONTROLLER.create("aPart1", self.TYPE, "a",
43                                                 self.user, self.DATA)
44        self.controller2 = self.CONTROLLER.create("aPart2", self.TYPE, "a",
45                                                  self.user, self.DATA)
46        self.controller3 = self.CONTROLLER.create("aPart3", self.TYPE, "a",
47                                                  self.user, self.DATA)
48        self.controller4 = self.CONTROLLER.create("aPart4", self.TYPE, "a",
49                                                  self.user, self.DATA)
50
51    def test_add_child(self):
52        children = self.controller.get_children()
53        self.assertEqual(len(children), 0)
54        self.controller.add_child(self.controller2, 10, 15)
55        children = self.controller.get_children()
56        self.assertEqual(len(children), 1)
57        level, link = children[0]
58        self.assertEqual(level, 1)
59        self.assertEqual(link.child.pk, self.controller2.object.pk)
60        self.assertEqual(link.parent.pk, self.controller.object.pk)
61        self.assertEqual(link.quantity, 10)
62        self.assertEqual(link.order, 15)
63
64    def test_add_child_error1(self):
65        def fail():
66            # bad quantity
67            self.controller.add_child(self.controller2, -10, 15)
68        self.assertRaises(ValueError, fail)
69
70    def test_add_child_error2(self):
71        def fail():
72            # bad order
73            self.controller.add_child(self.controller2, 10, -15)
74        self.assertRaises(ValueError, fail)
75   
76    def test_add_child_error3(self):
77        def fail():
78            # bad child : parent
79            self.controller2.add_child(self.controller, 10, 15)
80            self.controller.add_child(self.controller2, 10, 15)
81        self.assertRaises(ValueError, fail)
82   
83    def test_add_child_error4(self):
84        def fail():
85            # bad child : already a child
86            self.controller.add_child(self.controller2, 10, 15)
87            self.controller.add_child(self.controller2, 10, 15)
88        self.assertRaises(ValueError, fail)
89   
90    def test_add_child_error5(self):
91        def fail():
92            # bad child type
93            doc = PLMObjectController.create("e", "PLMObject", "1", self.user)
94            self.controller.add_child(doc, 10, 15)
95        self.assertRaises(ValueError, fail)
96
97    def test_add_child_error6(self):
98        def fail():
99            # bad child : add self
100            self.controller.add_child(self.controller, 10, 15)
101        self.assertRaises(ValueError, fail)
102
103    def test_modify_child(self):
104        self.controller.add_child(self.controller2, 10, 15)
105        self.controller.modify_child(self.controller2, 3, 5)
106        children = self.controller.get_children()
107        level, link = children[0]
108        self.assertEqual(link.quantity, 3)
109        self.assertEqual(link.order, 5)
110
111    def test_delete_child(self):
112        self.controller.add_child(self.controller2, 10, 15)
113        self.controller.delete_child(self.controller2)
114        self.assertEqual(self.controller.get_children(), [])
115
116    def test_get_children(self):
117        self.controller.add_child(self.controller2, 10, 15)
118        date = datetime.datetime.now()
119        self.controller2.add_child(self.controller3, 10, 15)
120        self.controller.add_child(self.controller4, 10, 15)
121        wanted = [(1, self.controller2.object.pk),
122                  (2, self.controller3.object.pk),
123                  (1, self.controller4.object.pk)]
124        children = [(lvl, lk.child.pk) for lvl, lk in self.controller.get_children(-1)]
125        self.assertEqual(children, wanted)
126        wanted = [(1, self.controller2.object.pk),
127                  (1, self.controller4.object.pk)]
128        # first level
129        children = [(lvl, lk.child.pk) for lvl, lk in self.controller.get_children(1)]
130        self.assertEqual(children, wanted)
131        # date
132        wanted = [(1, self.controller2.object.pk)]
133        children = [(lvl, lk.child.pk) for lvl, lk in self.controller.get_children(date=date)]
134        self.assertEqual(children, wanted)
135
136    def test_get_parents(self):
137        self.controller.add_child(self.controller2, 10, 15)
138        date = datetime.datetime.now()
139        self.controller2.add_child(self.controller3, 10, 15)
140        self.controller.add_child(self.controller4, 10, 15)
141        wanted = [(1, self.controller2.object.pk),
142                  (2, self.controller.object.pk),]
143        parents = [(lvl, lk.parent.pk) for lvl, lk in self.controller3.get_parents(-1)]
144        self.assertEqual(parents, wanted)
145        wanted = [(1, self.controller2.object.pk)]
146        # first level
147        parents = [(lvl, lk.parent.pk) for lvl, lk in self.controller3.get_parents(1)]
148        self.assertEqual(parents, wanted)
149        # date
150        parents = [(lvl, lk.parent.pk) for lvl, lk in self.controller3.get_parents(date=date)]
151        self.assertEqual(parents, [])
152
153    def test_is_promotable1(self):
154        self.failUnless(self.controller.is_promotable())
155
156    def test_is_promotable2(self):
157        self.controller.promote()
158        self.failUnless(self.controller.is_promotable())
159   
160    def test_is_promotable3(self):
161        self.controller.add_child(self.controller2, 10, 15)
162        self.failUnless(self.controller.is_promotable())
163       
164    def test_is_promotable4(self):
165        self.controller2.promote()
166        self.controller.add_child(self.controller2, 10, 15)
167        self.failUnless(self.controller.is_promotable())
168
169    def test_is_promotable5(self):
170        self.controller.add_child(self.controller2, 10, 15)
171        self.controller.promote()
172        self.failIf(self.controller.is_promotable())
173
174
Note: See TracBrowser for help on using the repository browser.