diff options
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r-- | ishtar_common/tests.py | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 7e852a770..6902854a2 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -18,7 +18,9 @@ # See the file COPYING for details. from bs4 import BeautifulSoup as Soup +import csv import datetime +import io import os import shutil from StringIO import StringIO @@ -33,6 +35,8 @@ from django.core.exceptions import ValidationError from django.core.files import File as DjangoFile from django.core.management import call_command from django.core.urlresolvers import reverse +from django.db.models.fields import BooleanField +from django.db.models.fields.related import ForeignKey from django.template.defaultfilters import slugify from django.test import TestCase as BaseTestCase from django.test.client import Client @@ -478,10 +482,114 @@ class AdminGenTypeTest(TestCase): for model in self.gen_models: url = '/admin/{}/{}/'.format(self.module_name, model.__name__.lower()) - response = self.client.post(url, {'action': 'export_as_csv'}) + q = model.objects + if not q.count(): + continue + response = self.client.post( + url, {'action': 'export_as_csv', + '_selected_action': [str(o.pk) for o in q.all()]}) self.assertEqual( response.status_code, 200, msg="Can not export as CSV for {}.".format(model)) + try: + f = io.BytesIO(response.content) + reader = csv.DictReader(f) + for row in reader: + if 'txt_idx' in row: + slug_name = 'txt_idx' + elif 'slug' in row: + slug_name = 'slug' + else: + continue + obj = model.objects.get(**{slug_name: row[slug_name]}) + for k in row: + current_value = getattr(obj, k) + if not row[k]: + self.assertIn(current_value, [None, ''], + msg="Export CSV for {} - {}: CSV value is " + "null whereas value for object is {}" + ".".format(model, k, current_value)) + continue + field = model._meta.get_field(k) + if isinstance(field, BooleanField): + if current_value: + self.assertEqual( + row[k], 'True', + msg="Export CSV for {} - {}: CSV value is " + "{} whereas value for " + "object is True.".format(model, k, + row[k])) + continue + else: + self.assertEqual( + row[k], 'False', + msg="Export CSV for {} - {}: CSV value is " + "{} whereas value for " + "object is False.".format( + model, k, row[k])) + continue + elif isinstance(field, ForeignKey): + fmodel = field.rel.to + try: + value = fmodel.objects.get( + **{slug_name: row[k]} + ) + except fmodel.DoesNotExist: + msg = "Export CSV for {} - {}: CSV value is "\ + "{} but it is not a vaild slug for {}" \ + ".".format(model, k, row[k], fmodel) + raise ValidationError(msg) + self.assertEqual( + value, current_value, + msg="Export CSV for {} - {}: CSV value is " + "{} whereas value for " + "object is {}.".format( + model, k, value, current_value)) + elif type(current_value) == float: + self.assertEqual( + float(row[k]), current_value, + msg="Export CSV for {} - {}: CSV value is " + "{} whereas value for " + "object is {}.".format( + model, k, row[k], current_value)) + elif type(current_value) == int: + self.assertEqual( + int(row[k]), current_value, + msg="Export CSV for {} - {}: CSV value is " + "{} whereas value for " + "object is {}.".format( + model, k, row[k], current_value)) + + finally: + f.close() + + def test_csv_import(self): + for model in self.gen_models: + q = model.objects + nb = q.count() + if not nb: + continue + base_url = '/admin/{}/{}/'.format(self.module_name, + model.__name__.lower()) + response = self.client.post( + base_url, {'action': 'export_as_csv', + '_selected_action': [str(o.pk) for o in q.all()]}) + self.assertEqual( + response.status_code, 200, + msg="Can not export as CSV for {}.".format(model)) + + for obj in q.all(): + obj.delete() + + url = base_url + 'import-from-csv/' + try: + f = io.BytesIO(response.content) + response = self.client.post(url, {'csv_file': f, 'apply': True}) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, base_url) + self.assertEqual(nb, model.objects.count()) + finally: + f.close() def test_str(self): # test __str__ |