source: main/trunk/openPLM/plmapp/tests/controllers/document.py @ 428

Revision 428, 12.6 KB checked in by pcosquer, 8 years ago (diff)

clean up some tests

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 os
30from django.conf import settings
31from django.contrib.auth.models import User
32from django.core.files.base import ContentFile
33from django.core.files import File
34
35from openPLM.plmapp.utils import *
36from openPLM.plmapp.exceptions import *
37from openPLM.plmapp.models import *
38from openPLM.plmapp.controllers import *
39from openPLM.plmapp.lifecycle import *
40from openPLM.computer.models import *
41from openPLM.office.models import *
42from openPLM.cad.models import *
43
44from openPLM.plmapp.tests.controllers.plmobject import ControllerTest
45
46class DocumentControllerTest(ControllerTest):
47    TYPE = "Document"
48    CONTROLLER = DocumentController
49    DATA = {}
50
51    def setUp(self):
52        super(DocumentControllerTest, self).setUp()
53        self.controller = self.CONTROLLER.create("adoc", self.TYPE, "a",
54                                                 self.user, self.DATA)
55        self.part = PartController.create("mpart", "Part", "a", self.user,
56                self.DATA)
57        self.old_files = []
58
59    def tearDown(self):
60        for f in list(self.controller.files.all()) + self.old_files:
61            os.chmod(f.file.path, 0700)
62            os.remove(f.file.path)
63
64    def test_initial_lock(self):
65        d = self.controller.add_file(self.get_file())
66        self.assertEqual(d.locked, False)
67        self.assertEqual(d.locker, None)
68
69    def test_promote(self):
70        self.controller.add_file(self.get_file())
71        self.assertEqual(self.controller.state.name, "draft")
72        self.controller.promote()
73        self.assertEqual(self.controller.state.name, "official")
74        self.failIf(self.controller.is_editable)
75        lcl = LifecycleList("diop", "official", "draft",
76                "issue1", "official", "deprecated")
77        lc = Lifecycle.from_lifecyclelist(lcl)
78        self.controller.lifecycle = lc
79        self.controller.state = State.objects.get(name="draft")
80        self.controller.save()
81        self.controller.promote()
82        self.assertEqual(self.controller.state.name, "issue1")
83        self.controller.demote()
84        self.assertEqual(self.controller.state.name, "draft")
85        self.failUnless(self.controller.is_editable)
86
87    def test_lock(self):
88        d = self.controller.add_file(self.get_file())
89        self.controller.lock(d)
90        self.assertEqual(d.locked, True)
91        self.assertEqual(d.locker, self.user)
92
93    def test_lock_error1(self):
94        "Error : already locked"
95        d = self.controller.add_file(self.get_file())
96        self.controller.lock(d)
97        self.assertRaises(LockError, self.controller.lock, d)
98   
99    def test_lock_error2(self):
100        "Error : bad file"
101        controller = self.CONTROLLER.create("adoc2", self.TYPE, "a",
102                                                 self.user, self.DATA)
103        d = controller.add_file(self.get_file())
104        self.old_files.append(d)
105        self.assertRaises(ValueError, self.controller.lock, d)
106   
107    def test_unlock(self):
108        d = self.controller.add_file(self.get_file())
109        self.controller.lock(d)
110        self.controller.unlock(d)
111        self.assertEqual(d.locked, False)
112        self.assertEqual(d.locker, None)
113   
114    def test_unlock_error1(self):
115        d = self.controller.add_file(self.get_file())
116        self.assertRaises(UnlockError, self.controller.unlock, d)
117
118    def test_unlock_error2(self):
119        user = User(username="baduser")
120        user.set_password("password")
121        user.save()
122        controller = self.CONTROLLER(self.controller.object, user)
123        d = self.controller.add_file(self.get_file())
124        self.controller.lock(d)
125        self.assertRaises(UnlockError, controller.unlock, d)
126   
127    def test_unlock_error3(self):
128        "Error : bad file"
129        controller = self.CONTROLLER.create("adoc2", self.TYPE, "a",
130                                                 self.user, self.DATA)
131        d = controller.add_file(self.get_file())
132        self.assertRaises(ValueError, self.controller.unlock, d)
133        self.old_files.append(d)
134   
135    def get_file(self, name="temp.txt", data="data"):
136        f = ContentFile(data)
137        f.name = name
138        return f
139
140    def test_add_file(self):
141        f = self.get_file()
142        self.controller.add_file(f)
143        files = self.controller.files.all()
144        self.assertEqual(len(files), 1)
145        f2 = files[0]
146        self.assertEqual(f2.filename, f.name)
147        self.assertEqual(f2.size, f.size)
148        self.assertEqual(f2.file.read(), "data")
149        self.assertEqual(file(f2.file.path).read(), "data")
150        self.assertEqual(os.path.splitext(f2.file.name)[1], ".txt")
151        self.failIf("temp" in f2.file.path)
152        self.failUnless(f2.file.path.startswith(settings.DOCUMENTS_DIR))
153        self.failUnless(os.access(f2.file.path, os.F_OK))
154        self.failUnless(os.access(f2.file.path, os.R_OK))
155        self.failUnless(not os.access(f2.file.path, os.W_OK))
156        self.failUnless(not os.access(f2.file.path, os.X_OK))
157
158    def test_add_several_files(self):
159        nb = 5
160        for i in xrange(nb):
161            f = self.get_file("temp%d.txt" % i, "data%d" % i)
162            self.controller.add_file(f)
163        files = self.controller.files.all().order_by('filename')
164        self.assertEqual(len(files), nb)
165        for i, f2 in enumerate(files):
166            self.assertEqual(f2.filename, "temp%d.txt" % i)
167            self.assertEqual(f2.file.read(), "data%d" % i)
168 
169    def test_add_file_error1(self):
170        """
171        test add_file : file too big
172        """
173        f = self.get_file("temp.txt", "x" * 500)
174        old, settings.MAX_FILE_SIZE = settings.MAX_FILE_SIZE, 400
175        self.assertRaises(ValueError, self.controller.add_file, f)
176        settings.MAX_FILE_SIZE = old
177
178    def test_delete_file(self):
179        self.controller.add_file(self.get_file())
180        f2 = self.controller.files.all()[0]
181        path = f2.file.path
182        self.controller.delete_file(f2)
183        self.assertEqual([], list(self.controller.files.all()))
184        self.failIf(os.path.exists(path))
185 
186    def test_delete_file_error(self):
187        self.controller.add_file(self.get_file())
188        f2 = self.controller.files.all()[0]
189        self.controller.lock(f2)
190        self.assertRaises(DeleteFileError, self.controller.delete_file, f2)
191
192    def test_delete_file_error2(self):
193        "Error : bad file"
194        controller = self.CONTROLLER.create("adoc2", self.TYPE, "a",
195                                                 self.user, self.DATA)
196        d = controller.add_file(self.get_file())
197        self.assertRaises(ValueError, self.controller.delete_file, d)
198        self.old_files.append(d)
199
200    def test_attach_to_part(self):
201        self.controller.attach_to_part(self.part)
202   
203    def test_attach_to_part_error1(self):
204        self.assertRaises(TypeError, self.controller.attach_to_part, None)
205   
206    def test_attach_to_part_error2(self):
207        self.assertRaises(TypeError, self.controller.attach_to_part, self)
208
209    def test_attach_to_part_error3(self):
210        obj = PLMObject.objects.create(reference="obj", type="PLMObject",
211                           revision="a", creator=self.user, owner=self.user,
212                           group=self.group)
213        self.assertRaises(TypeError, self.controller.attach_to_part, obj)
214   
215    def test_attach_to_part_error4(self):
216        obj = self.CONTROLLER.create("ob", self.TYPE, "a", self.user, self.DATA)
217        self.assertRaises(TypeError, self.controller.attach_to_part, obj)
218   
219    def test_detach_part(self):
220        self.controller.attach_to_part(self.part)
221        self.controller.detach_part(self.part)
222        self.assertEqual(len(self.controller.get_attached_parts()), 0)
223
224    def test_get_attached_parts(self):
225        self.controller.attach_to_part(self.part)
226        links = list(self.controller.get_attached_parts())
227        self.assertEqual([l.part for l in links], [self.part.object])
228       
229    def test_get_attached_parts_empty(self):
230        links = list(self.controller.get_attached_parts())
231        self.assertEqual(links, [])
232
233    def test_revise2(self):
234        self.controller.attach_to_part(self.part)
235        self.controller.add_file(self.get_file())
236        f1 = self.controller.files.all()[0]
237        rev = self.controller.revise("new_name")
238        links = list(rev.get_attached_parts())
239        self.assertEqual(links, [])
240        self.assertEqual(len(rev.files.all()), 1)
241        self.assertEqual(len(self.controller.files.all()), 1)
242        f2 = rev.files.all()[0]
243        self.assertEqual(f2.locker, None)
244        self.assertEqual(f2.locked, False)
245        self.assertEqual(f1.filename, f2.filename)
246        self.assertEqual(f1.size, f2.size)
247        self.assertEqual(f1.file.read(), f2.file.read())
248        self.failIf(f1.file.path == f2.file.path)
249        self.old_files.append(f2)
250
251    def test_checkin(self):
252        d = self.controller.add_file(self.get_file())
253        self.controller.checkin(d, self.get_file(data="new_data"))
254        self.assertEqual(len(self.controller.files), 1)
255        f = self.controller.files.all()[0]
256        self.assertEqual(f.file.read(), "new_data")
257
258    def test_checkin_error1(self):
259        controller = self.CONTROLLER.create("adoc2", self.TYPE, "a",
260                                                 self.user, self.DATA)
261        d = controller.add_file(self.get_file())
262        self.old_files.append(d)
263        self.assertRaises(ValueError, self.controller.checkin,
264                          d, self.get_file(data="new_data"))
265       
266    def test_checkin_error2(self):
267        user = User(username="baduser")
268        user.set_password("password")
269        user.save()
270        user.groups.add(self.group)
271        controller = self.CONTROLLER(self.controller.object, user)
272        PLMObjectUserLink.objects.create(user=user, role="owner",
273                                         plmobject=controller.object)
274        d = self.controller.add_file(self.get_file())
275        self.controller.lock(d)
276        self.assertRaises(UnlockError, controller.checkin, d,
277                          self.get_file())
278   
279    def test_checkin_errors3(self):
280        user = User(username="baduser")
281        user.set_password("password")
282        user.save()
283        DelegationLink.objects.create(delegator=self.user, delegatee=user,
284                role=ROLE_OWNER)
285        controller = self.CONTROLLER(self.controller.object, user)
286        d = self.controller.add_file(self.get_file())
287        self.assertRaises(PermissionError, controller.checkin, d,
288                          self.get_file())
289
290
291class OfficeTest(DocumentControllerTest):
292    TYPE = "OfficeDocument"
293    CONTROLLER = OfficeDocumentController
294    DATA = {}
295
296    def test_add_odt(self):
297        # format a4, 3 pages
298        f = file("datatests/office_a4_3p.odt", "rb")
299        my_file = File(f)
300        self.controller.add_file(my_file)
301        self.assertEquals(self.controller.nb_pages, 3)
302        self.assertEquals(self.controller.format, "A4")
303        f2 = self.controller.files.all()[0]
304        self.failUnless(f2.file.path.endswith(".odt"))
305        self.controller.delete_file(f2)
306
307    def test_add_odt2(self):
308        # fake odt
309        # No exceptions should be raised
310        self.controller.add_file(self.get_file("plop.odt"))
311        f2 = self.controller.files.all()[0]
312        self.controller.delete_file(f2)
313
314    def test_add_odt3(self):
315        # do not update fields
316        f = file("datatests/office_a4_3p.odt", "rb")
317        my_file = File(f)
318        self.controller.add_file(my_file, False)
319        self.assertEquals(self.controller.nb_pages, None)
320        f2 = self.controller.files.all()[0]
321        self.controller.delete_file(f2)
322
323class DesignTest(DocumentControllerTest):
324    TYPE = "Drawing"
325    CONTROLLER = DrawingController
326    DATA = {}
327
Note: See TracBrowser for help on using the repository browser.