Changeset 434 in main


Ignore:
Timestamp:
10/31/11 17:14:00 (8 years ago)
Author:
pcosquer
Message:

csvimport:view: add a test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/plmapp/tests/csvimport.py

    r407 r434  
    11 
    2 import cStringIO 
     2import cStringIO, StringIO 
    33from django.contrib.auth.models import User 
    44from django.test import TransactionTestCase 
     
    1515from openPLM.plmapp.unicodecsv import * 
    1616from openPLM.plmapp.base_views import * 
     17from openPLM.plmapp.forms import CSVForm 
    1718 
    1819 
     
    2324 
    2425    def setUp(self): 
     26        super(CSVImportTestCase, self).setUp() 
    2527        self.cie = User.objects.create(username="company") 
    2628        p = self.cie.get_profile() 
     
    4042        self.user.groups.add(self.group) 
    4143        self.DATA["group"] = self.group 
     44        self.client.post("/login/", {'username' : 'user', 'password' : 'password'}) 
    4245 
    4346    def get_valid_rows(self): 
     
    185188        self.assertEquals(0, len(ParentChildLink.objects.all())) 
    186189 
     190    def test_view_init_get(self): 
     191        response = self.client.get("/import/csv/") 
     192        self.assertEqual(200, response.status_code) 
     193        self.assertEqual(1, response.context["step"]) 
     194        self.assertEqual("csv", response.context["target"]) 
     195        form = response.context["csv_form"] 
     196        self.assertTrue(isinstance(form, CSVForm)) 
     197 
     198    def test_view_csv_all(self): 
     199        """ 
     200        Complex test that simulate an upload of a csv file (complete process). 
     201        """ 
     202        # upload a csv file 
     203        csv_file = StringIO.StringIO() 
     204        csv_file.name = "data.csv" 
     205        UnicodeWriter(csv_file).writerows(self.get_valid_rows()) 
     206        csv_file.seek(0) 
     207        response = self.client.post("/import/csv/", {"encoding":"utf_8", 
     208            "filename":"data.csv", "file":csv_file}, follow=True) 
     209        csv_file.close() 
     210 
     211        # load the second page 
     212        url = response.redirect_chain[0][0] 
     213        response2 = self.client.get(url) 
     214        self.assertEquals(2, response2.context["step"]) 
     215        preview = response2.context["preview"] 
     216        self.assertFalse(None in preview.guessed_headers) 
     217        formset = response2.context["headers_formset"] 
     218 
     219        # validate and import the file 
     220        data = {} 
     221        for key, value in formset.management_form.initial.iteritems(): 
     222            data["form-" + key] = value or "" 
     223        for i, d in enumerate(formset.initial): 
     224            for key, value in d.iteritems(): 
     225                data["form-%d-%s" % (i, key)] = value 
     226            data['form-%d-ORDER' % i] = str(i) 
     227        response3 = self.client.post(url, data, follow=True) 
     228        url_done = response3.redirect_chain[-1][0] 
     229        self.assertEquals("http://testserver/import/done/", url_done) 
     230        # check an item 
     231        sp1 = get_obj("SinglePart", "sp1", "s", self.user) 
     232        self.assertEquals("SP1", sp1.name) 
     233 
     234 
     235 
Note: See TracChangeset for help on using the changeset viewer.