Changeset 472 in main


Ignore:
Timestamp:
11/14/11 12:20:57 (8 years ago)
Author:
pcosquer
Message:

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

Location:
trunk/openPLM
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/cad/tests.py

    r261 r472  
    2626This file demonstrates two different styles of tests (one doctest and one 
    2727unittest). These will both pass when you run "manage.py test". 
    28  
    29 Replace these with more appropriate tests for your application. 
    3028""" 
    3129 
    32 from django.test import TestCase 
     30from openPLM.cad.models import DrawingController 
    3331 
    34 class SimpleTest(TestCase): 
    35     def test_basic_addition(self): 
    36         """ 
    37         Tests that 1 + 1 always equals 2. 
    38         """ 
    39         self.failUnlessEqual(1 + 1, 2) 
     32from openPLM.plmapp.tests.controllers.document import DocumentControllerTest 
    4033 
    41 __test__ = {"doctest": """ 
    42 Another way to test that 1 + 1 is equal to 2. 
    4334 
    44 >>> 1 + 1 == 2 
    45 True 
    46 """} 
     35class DesignTest(DocumentControllerTest): 
     36    TYPE = "Drawing" 
     37    CONTROLLER = DrawingController 
     38    DATA = {} 
    4739 
  • trunk/openPLM/computer/tests.py

    r261 r472  
    3030""" 
    3131 
    32 from django.test import TestCase 
     32from openPLM.computer.models import SinglePartController 
     33from openPLM.plmapp.tests.views import ViewTest 
     34from openPLM.plmapp.tests.controllers.part import PartControllerTest 
    3335 
    34 class SimpleTest(TestCase): 
    35     def test_basic_addition(self): 
    36         """ 
    37         Tests that 1 + 1 always equals 2. 
    38         """ 
    39         self.failUnlessEqual(1 + 1, 2) 
     36class HardDiskViewTest(ViewTest): 
     37    TYPE = "HardDisk" 
     38    DATA = {"capacity_in_go" : 500, 
     39            "supplier" : "ASupplier"} 
     40     
     41    def test_display_attributes2(self): 
     42        response = self.get(self.base_url + "attributes/") 
     43        self.failUnless(response.context["object_attributes"]) 
     44        attributes = dict(response.context["object_attributes"]) 
     45        self.assertEqual(attributes["capacity in go"], self.DATA["capacity_in_go"]) 
     46        self.assertEqual(attributes["supplier"], self.DATA["supplier"]) 
     47        self.assertEqual(attributes["tech details"], "") 
    4048 
    41 __test__ = {"doctest": """ 
    42 Another way to test that 1 + 1 is equal to 2. 
     49class HardDiskControllerTest(PartControllerTest): 
     50    TYPE = "HardDisk" 
     51    CONTROLLER = SinglePartController 
     52    DATA = {"capacity_in_go" : 500} 
    4353 
    44 >>> 1 + 1 == 2 
    45 True 
    46 """} 
    4754 
  • trunk/openPLM/office/tests.py

    r261 r472  
    2727unittest). These will both pass when you run "manage.py test". 
    2828 
    29 Replace these with more appropriate tests for your application. 
    3029""" 
    3130 
    32 from django.test import TestCase 
     31from django.core.files.base import File 
    3332 
    34 class SimpleTest(TestCase): 
    35     def test_basic_addition(self): 
    36         """ 
    37         Tests that 1 + 1 always equals 2. 
    38         """ 
    39         self.failUnlessEqual(1 + 1, 2) 
     33from openPLM.office.models import OfficeDocumentController 
     34from openPLM.plmapp.tests.controllers.document import DocumentControllerTest 
    4035 
    41 __test__ = {"doctest": """ 
    42 Another way to test that 1 + 1 is equal to 2. 
    4336 
    44 >>> 1 + 1 == 2 
    45 True 
    46 """} 
     37class OfficeTest(DocumentControllerTest): 
     38    TYPE = "OfficeDocument" 
     39    CONTROLLER = OfficeDocumentController 
     40    DATA = {} 
    4741 
     42    def test_add_odt(self): 
     43        # format a4, 3 pages 
     44        f = file("datatests/office_a4_3p.odt", "rb") 
     45        my_file = File(f) 
     46        self.controller.add_file(my_file) 
     47        self.assertEquals(self.controller.nb_pages, 3) 
     48        self.assertEquals(self.controller.format, "A4") 
     49        f2 = self.controller.files.all()[0] 
     50        self.failUnless(f2.file.path.endswith(".odt")) 
     51        self.controller.delete_file(f2) 
     52 
     53    def test_add_odt2(self): 
     54        # fake odt 
     55        # No exceptions should be raised 
     56        self.controller.add_file(self.get_file("plop.odt")) 
     57        f2 = self.controller.files.all()[0] 
     58        self.controller.delete_file(f2) 
     59 
     60    def test_add_odt3(self): 
     61        # do not update fields 
     62        f = file("datatests/office_a4_3p.odt", "rb") 
     63        my_file = File(f) 
     64        self.controller.add_file(my_file, False) 
     65        self.assertEquals(self.controller.nb_pages, None) 
     66        f2 = self.controller.files.all()[0] 
     67        self.controller.delete_file(f2) 
     68 
     69 
  • trunk/openPLM/plmapp/tests/api.py

    r437 r472  
    77 
    88class ApiTestCase(CommonViewTest): 
    9  
    109 
    1110    def get(self, url, **get): 
  • trunk/openPLM/plmapp/tests/base.py

    r433 r472  
    44from django.test import TestCase 
    55 
    6 from openPLM.plmapp.utils import * 
    7 from openPLM.plmapp.exceptions import * 
    8 from openPLM.plmapp.models import * 
    9 from openPLM.plmapp.controllers import * 
    10 from openPLM.plmapp.lifecycle import * 
    11 from openPLM.computer.models import * 
    12 from openPLM.office.models import * 
    13 from openPLM.cad.models import * 
     6from openPLM.plmapp.models import GroupInfo 
     7from openPLM.plmapp.controllers import PLMObjectController 
    148 
    159 
  • trunk/openPLM/plmapp/tests/controllers/document.py

    r433 r472  
    3131from django.contrib.auth.models import User 
    3232from django.core.files.base import ContentFile 
    33 from django.core.files import File 
    34  
    35 from openPLM.plmapp.utils import * 
    36 from openPLM.plmapp.exceptions import * 
    37 from openPLM.plmapp.models import * 
    38 from openPLM.plmapp.controllers import * 
    39 from openPLM.plmapp.lifecycle import * 
    40 from openPLM.computer.models import * 
    41 from openPLM.office.models import * 
    42 from openPLM.cad.models import * 
     33 
     34import openPLM.plmapp.exceptions as exc 
     35import openPLM.plmapp.models as models 
     36from openPLM.plmapp.controllers import PartController, DocumentController 
     37from openPLM.plmapp.lifecycle import LifecycleList 
    4338 
    4439from openPLM.plmapp.tests.controllers.plmobject import ControllerTest 
     
    7570        lcl = LifecycleList("diop", "official", "draft",  
    7671                "issue1", "official", "deprecated") 
    77         lc = Lifecycle.from_lifecyclelist(lcl) 
     72        lc = models.Lifecycle.from_lifecyclelist(lcl) 
    7873        self.controller.lifecycle = lc 
    79         self.controller.state = State.objects.get(name="draft") 
     74        self.controller.state = models.State.objects.get(name="draft") 
    8075        self.controller.save() 
    8176        self.controller.promote() 
     
    9590        d = self.controller.add_file(self.get_file()) 
    9691        self.controller.lock(d) 
    97         self.assertRaises(LockError, self.controller.lock, d) 
     92        self.assertRaises(exc.LockError, self.controller.lock, d) 
    9893     
    9994    def test_lock_error2(self): 
     
    114109    def test_unlock_error1(self): 
    115110        d = self.controller.add_file(self.get_file()) 
    116         self.assertRaises(UnlockError, self.controller.unlock, d) 
     111        self.assertRaises(exc.UnlockError, self.controller.unlock, d) 
    117112 
    118113    def test_unlock_error2(self): 
     
    123118        d = self.controller.add_file(self.get_file()) 
    124119        self.controller.lock(d) 
    125         self.assertRaises(UnlockError, controller.unlock, d) 
     120        self.assertRaises(exc.UnlockError, controller.unlock, d) 
    126121     
    127122    def test_unlock_error3(self): 
     
    183178        f2 = self.controller.files.all()[0] 
    184179        self.controller.lock(f2) 
    185         self.assertRaises(DeleteFileError, self.controller.delete_file, f2) 
     180        self.assertRaises(exc.DeleteFileError, self.controller.delete_file, f2) 
    186181 
    187182    def test_delete_file_error2(self): 
     
    203198 
    204199    def test_attach_to_part_error3(self): 
    205         obj = PLMObject.objects.create(reference="obj", type="PLMObject", 
     200        obj = models.PLMObject.objects.create(reference="obj", type="PLMObject", 
    206201                           revision="a", creator=self.user, owner=self.user, 
    207202                           group=self.group) 
     
    265260        user.groups.add(self.group) 
    266261        controller = self.CONTROLLER(self.controller.object, user) 
    267         PLMObjectUserLink.objects.create(user=user, role="owner", 
     262        models.PLMObjectUserLink.objects.create(user=user, role="owner", 
    268263                                         plmobject=controller.object) 
    269264        d = self.controller.add_file(self.get_file()) 
    270265        self.controller.lock(d) 
    271         self.assertRaises(UnlockError, controller.checkin, d, 
     266        self.assertRaises(exc.UnlockError, controller.checkin, d, 
    272267                          self.get_file()) 
    273268     
     
    276271        user.set_password("password") 
    277272        user.save() 
    278         DelegationLink.objects.create(delegator=self.user, delegatee=user, 
    279                 role=ROLE_OWNER) 
     273        models.DelegationLink.objects.create(delegator=self.user, delegatee=user, 
     274                role=models.ROLE_OWNER) 
    280275        controller = self.CONTROLLER(self.controller.object, user) 
    281276        d = self.controller.add_file(self.get_file()) 
    282         self.assertRaises(PermissionError, controller.checkin, d, 
     277        self.assertRaises(exc.PermissionError, controller.checkin, d, 
    283278                          self.get_file()) 
    284279 
     
    302297        self.assertNotEquals(f2.thumbnail.path, f3.thumbnail.path) 
    303298 
    304  
    305 class OfficeTest(DocumentControllerTest): 
    306     TYPE = "OfficeDocument" 
    307     CONTROLLER = OfficeDocumentController 
    308     DATA = {} 
    309  
    310     def test_add_odt(self): 
    311         # format a4, 3 pages 
    312         f = file("datatests/office_a4_3p.odt", "rb") 
    313         my_file = File(f) 
    314         self.controller.add_file(my_file) 
    315         self.assertEquals(self.controller.nb_pages, 3) 
    316         self.assertEquals(self.controller.format, "A4") 
    317         f2 = self.controller.files.all()[0] 
    318         self.failUnless(f2.file.path.endswith(".odt")) 
    319         self.controller.delete_file(f2) 
    320  
    321     def test_add_odt2(self): 
    322         # fake odt 
    323         # No exceptions should be raised 
    324         self.controller.add_file(self.get_file("plop.odt")) 
    325         f2 = self.controller.files.all()[0] 
    326         self.controller.delete_file(f2) 
    327  
    328     def test_add_odt3(self): 
    329         # do not update fields 
    330         f = file("datatests/office_a4_3p.odt", "rb") 
    331         my_file = File(f) 
    332         self.controller.add_file(my_file, False) 
    333         self.assertEquals(self.controller.nb_pages, None) 
    334         f2 = self.controller.files.all()[0] 
    335         self.controller.delete_file(f2) 
    336  
    337 class DesignTest(DocumentControllerTest): 
    338     TYPE = "Drawing" 
    339     CONTROLLER = DrawingController 
    340     DATA = {} 
    341  
  • trunk/openPLM/plmapp/tests/controllers/group.py

    r440 r472  
    3030from django.contrib.auth.models import User 
    3131 
    32 from openPLM.plmapp.utils import * 
    33 from openPLM.plmapp.exceptions import * 
    34 from openPLM.plmapp.models import * 
    35 from openPLM.plmapp.controllers import * 
     32from openPLM.plmapp.models import GroupInfo, Invitation, get_all_plmobjects 
     33from openPLM.plmapp.controllers import GroupController 
    3634from openPLM.plmapp.forms import get_creation_form, get_user_formset 
    3735 
  • trunk/openPLM/plmapp/tests/controllers/part.py

    r428 r472  
    2929import datetime 
    3030 
    31 from openPLM.plmapp.utils import * 
    32 from openPLM.plmapp.exceptions import * 
    33 from openPLM.plmapp.models import * 
    34 from openPLM.plmapp.controllers import * 
    35 from openPLM.plmapp.lifecycle import * 
    36 from openPLM.computer.models import * 
    37 from openPLM.office.models import * 
    38 from openPLM.cad.models import * 
     31from openPLM.plmapp.controllers import PLMObjectController, PartController 
    3932 
    4033from openPLM.plmapp.tests.controllers.plmobject import ControllerTest 
     
    180173 
    181174 
    182 class HardDiskControllerTest(PartControllerTest): 
    183     TYPE = "HardDisk" 
    184     CONTROLLER = SinglePartController 
    185     DATA = {"capacity_in_go" : 500} 
    186  
    187  
  • trunk/openPLM/plmapp/tests/controllers/plmobject.py

    r433 r472  
    3030from django.contrib.auth.models import User 
    3131 
    32 from openPLM.plmapp.utils import * 
    33 from openPLM.plmapp.exceptions import * 
    34 from openPLM.plmapp.models import * 
    35 from openPLM.plmapp.controllers import * 
    36 from openPLM.plmapp.lifecycle import * 
    37 from openPLM.computer.models import * 
    38 from openPLM.office.models import * 
    39 from openPLM.cad.models import * 
     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 
    4037 
    4138from openPLM.plmapp.tests.base import BaseTestCase 
     
    5148        self.assertEqual(controller.name, "") 
    5249        self.assertEqual(controller.type, self.TYPE) 
    53         self.assertEqual(type(controller.object), get_all_plmobjects()[self.TYPE]) 
    54         obj = get_all_plmobjects()[self.TYPE].objects.get(reference=controller.reference, 
     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, 
    5554                revision=controller.revision, type=controller.type) 
    5655        self.assertEqual(obj.owner, self.user) 
    5756        self.assertEqual(obj.creator, self.user) 
    58         PLMObjectUserLink.objects.get(plmobject=obj, user=self.user, role="owner") 
     57        models.PLMObjectUserLink.objects.get(plmobject=obj, user=self.user, 
     58                role=models.ROLE_OWNER) 
    5959        self.failUnless(obj.is_editable) 
    6060 
     
    9898            controller = self.CONTROLLER.create("zee", "PLMOBject_", "a", 
    9999                                            self.user, self.DATA) 
    100         self.assertRaises(PermissionError, fail) 
     100        self.assertRaises(exc.PermissionError, fail) 
    101101 
    102102    def test_keys(self): 
     
    132132        self.assertEqual(controller.state.name, "official") 
    133133        self.failIf(controller.is_editable) 
    134         self.assertRaises(PromotionError, controller.demote) 
     134        self.assertRaises(exc.PromotionError, controller.demote) 
    135135        lcl = LifecycleList("diop", "official", "draft",  
    136136                "issue1", "official", "deprecated") 
    137         lc = Lifecycle.from_lifecyclelist(lcl) 
     137        lc = models.Lifecycle.from_lifecyclelist(lcl) 
    138138        controller.lifecycle = lc 
    139         controller.state = State.objects.get(name="draft") 
     139        controller.state = models.State.objects.get(name="draft") 
    140140        controller.save() 
    141141        controller.promote() 
     
    154154        def fail(): 
    155155            controller.revise("b2") 
    156         self.assertRaises(RevisionError, fail) 
     156        self.assertRaises(exc.RevisionError, fail) 
    157157        for attr in controller.get_modification_fields(): 
    158158            self.assertEqual(getattr(controller, attr), getattr(rev, attr)) 
     
    161161        "Revision : error : empty name" 
    162162        controller = self.create("Part1") 
    163         self.assertRaises(RevisionError, controller.revise, "") 
     163        self.assertRaises(exc.RevisionError, controller.revise, "") 
    164164     
    165165    def test_revise_error2(self): 
    166166        "Revision : error : same revision name" 
    167167        controller = self.create("Part1") 
    168         self.assertRaises(RevisionError, controller.revise, "a") 
     168        self.assertRaises(exc.RevisionError, controller.revise, "a") 
    169169 
    170170    def test_set_owner(self): 
     
    181181        user = User(username="user2") 
    182182        user.save() 
    183         self.assertRaises(PermissionError, controller.set_owner, user) 
     183        self.assertRaises(exc.PermissionError, controller.set_owner, user) 
    184184 
    185185    def test_set_sign1(self): 
     
    190190        user.get_profile().save() 
    191191        controller.set_signer(user, level_to_sign_str(0)) 
    192         link = PLMObjectUserLink.objects.get(role=level_to_sign_str(0), 
     192        link = models.PLMObjectUserLink.objects.get(role=level_to_sign_str(0), 
    193193                                             plmobject=controller.object) 
    194194        self.assertEqual(user, link.user) 
     
    201201        user.get_profile().is_contributor = True 
    202202        user.get_profile().save() 
    203         self.assertRaises(PermissionError, controller.set_role, user, 
     203        self.assertRaises(exc.PermissionError, controller.set_role, user, 
    204204                          level_to_sign_str(1664)) 
    205205 
     
    209209        user = User(username="user2") 
    210210        user.save() 
    211         self.assertRaises(PermissionError, controller.set_role, user, 
     211        self.assertRaises(exc.PermissionError, controller.set_role, user, 
    212212                          level_to_sign_str(0)) 
    213213 
     
    217217        user.save() 
    218218        controller.add_notified(user) 
    219         PLMObjectUserLink.objects.get(user=user, plmobject=controller.object, 
     219        models.PLMObjectUserLink.objects.get(user=user, plmobject=controller.object, 
    220220                                      role="notified") 
    221221 
     
    223223        controller = self.create("Part1") 
    224224        controller.add_notified(self.user) 
    225         PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object, 
     225        models.PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object, 
    226226                                      role="notified") 
    227227        controller.remove_notified(self.user) 
    228         self.assertEqual(0, len(PLMObjectUserLink.objects.filter( 
     228        self.assertEqual(0, len(models.PLMObjectUserLink.objects.filter( 
    229229            plmobject=controller.object, role="notified"))) 
    230230 
     
    238238        self.assertEqual(controller.owner, user) 
    239239        controller.set_role(self.user, "notified") 
    240         PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object, 
     240        models.PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object, 
    241241                                      role="notified") 
    242242        controller.set_role(user, level_to_sign_str(0)) 
    243         link = PLMObjectUserLink.objects.get(role=level_to_sign_str(0), 
     243        link = models.PLMObjectUserLink.objects.get(role=level_to_sign_str(0), 
    244244                                             plmobject=controller.object) 
    245245        self.assertEqual(user, link.user) 
     
    255255            return False 
    256256        controller.object.is_promotable = always_false 
    257         self.assertRaises(PromotionError, controller.promote) 
    258  
     257        self.assertRaises(exc.PromotionError, controller.promote) 
     258 
  • trunk/openPLM/plmapp/tests/csvimport.py

    r434 r472  
    44from django.test import TransactionTestCase 
    55 
    6 from openPLM.plmapp.utils import * 
    7 from openPLM.plmapp.exceptions import * 
    8 from openPLM.plmapp.models import * 
    9 from openPLM.plmapp.controllers import * 
    10 from openPLM.plmapp.lifecycle import * 
    11 from openPLM.computer.models import * 
    12 from openPLM.office.models import * 
    13 from openPLM.cad.models import * 
    14 from openPLM.plmapp.csvimport import * 
    15 from openPLM.plmapp.unicodecsv import * 
    16 from openPLM.plmapp.base_views import * 
     6from openPLM.plmapp.models import GroupInfo, PLMObject, ParentChildLink 
     7from openPLM.plmapp.csvimport import PLMObjectsImporter, BOMImporter,\ 
     8        CSVImportError 
     9from openPLM.plmapp.base_views import get_obj 
     10from openPLM.plmapp.unicodecsv import UnicodeWriter 
    1711from openPLM.plmapp.forms import CSVForm 
    1812 
    1913 
    2014class CSVImportTestCase(TransactionTestCase): 
    21     CONTROLLER = PLMObjectController 
    22     TYPE = "Part" 
    23     DATA = {} 
    2415 
    2516    def setUp(self): 
     
    4132        self.group.save() 
    4233        self.user.groups.add(self.group) 
    43         self.DATA["group"] = self.group 
    4434        self.client.post("/login/", {'username' : 'user', 'password' : 'password'}) 
    4535 
  • trunk/openPLM/plmapp/tests/filehandlers.py

    r261 r472  
    3030from django.test import TestCase 
    3131 
    32 from openPLM.plmapp.filehandlers import * 
     32from openPLM.plmapp.filehandlers import HandlersManager, ODFHandler 
    3333 
    3434class FileHandlerTest(TestCase): 
  • trunk/openPLM/plmapp/tests/lifecycle.py

    r261 r472  
    2929from django.test import TestCase 
    3030 
    31 from openPLM.plmapp.utils import * 
    32 from openPLM.plmapp.models import * 
     31from openPLM.plmapp.models import Lifecycle, get_default_lifecycle, \ 
     32        get_default_state, LifecycleStates 
     33from openPLM.plmapp.lifecycle import LifecycleList 
    3334 
    3435class LifecycleTest(TestCase): 
  • trunk/openPLM/plmapp/tests/views.py

    r470 r472  
    3838        UserController 
    3939from openPLM.plmapp.lifecycle import LifecycleList 
    40 #from openPLM.computer.models import * 
    41 #from openPLM.office.models import * 
    42 #from openPLM.cad.models import * 
    4340 
    4441from openPLM.plmapp.tests.base import BaseTestCase 
     
    667664 
    668665 
    669 class HardDiskViewTest(ViewTest): 
    670     TYPE = "HardDisk" 
    671     DATA = {"capacity_in_go" : 500, 
    672             "supplier" : "ASupplier"} 
    673      
    674     def test_display_attributes2(self): 
    675         response = self.get(self.base_url + "attributes/") 
    676         self.failUnless(response.context["object_attributes"]) 
    677         attributes = dict(response.context["object_attributes"]) 
    678         self.assertEqual(attributes["capacity in go"], self.DATA["capacity_in_go"]) 
    679         self.assertEqual(attributes["supplier"], self.DATA["supplier"]) 
    680         self.assertEqual(attributes["tech details"], "") 
    681  
    682666class MechantUserViewTest(TestCase): 
    683667    """ 
Note: See TracChangeset for help on using the changeset viewer.