diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-01-07 17:01:15 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-28 12:15:23 +0100 |
commit | dedd15f6cc661a46300012f65dc1bf37c0066bcb (patch) | |
tree | ca1a7659db18b4edc950e729ccfbd99fd0fd2260 /archaeological_finds/tests.py | |
parent | 3690d59483034b65eb74095c0fb40375efc61f67 (diff) | |
download | Ishtar-dedd15f6cc661a46300012f65dc1bf37c0066bcb.tar.bz2 Ishtar-dedd15f6cc661a46300012f65dc1bf37c0066bcb.zip |
Auto-generate labels from a base libreoffice template
Diffstat (limited to 'archaeological_finds/tests.py')
-rw-r--r-- | archaeological_finds/tests.py | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 548a74e7b..93548f8fc 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -38,7 +38,8 @@ from django.core.urlresolvers import reverse from django.test import tag from django.test.client import Client from ishtar_common.models import ImporterType, IshtarUser, ImporterColumn,\ - FormaterType, ImportTarget, IshtarSiteProfile, ProfileType + FormaterType, ImportTarget, IshtarSiteProfile, ProfileType, ImporterModel, \ + DocumentTemplate from django.utils.text import slugify from django.utils.translation import pgettext_lazy, gettext_lazy as _ @@ -2513,3 +2514,68 @@ class PublicAPITest(FindInit, APITestCase): value = value[key] self.assertEqual(value, result) + +class LabelTest(FindInit, TestCase): + fixtures = FIND_FIXTURES + model = models.Find + + def setUp(self): + templates = [ + settings.ROOT_PATH + '../archaeological_finds/tests/' + t + for t in ("etiquettes-mobilier.odt", "etiquettes-mobilier", + "etiquettes-mobilier.txt", "bad_lo.zip", + "truncated_xml.zip") + ] + self.templates = [] + for template in templates: + filename = template.split("/")[-1] + shutil.copy(template, + os.path.join(settings.MEDIA_ROOT, filename), + follow_symlinks=True) + self.templates.append( + os.path.join(settings.MEDIA_ROOT, filename)) + + def tearDown(self): + for tpl in self.templates: + if os.path.exists(tpl): + os.remove(tpl) + + def test_label(self): + base_targets = ";".join("Cadre{}".format(idx) for idx in range(1, 25)) + base_tpl, missing_ext, text_file, bad_lo, trunc_xml = self.templates + dataset = ( + (base_tpl, base_targets, True, "OK"), + (base_tpl, "", False, "no target"), + (base_tpl, "-;Cadre2;Cadre3", False, "bad first target"), + (base_tpl, "Cadre1;Frame2;Frame3", True, + "first target OK, silently failed other targets"), + (missing_ext, base_targets, True, "missing extension"), + (text_file, base_targets, False, "text file"), + (bad_lo, base_targets, False, "missing content.xml"), + (trunc_xml, base_targets, False, "truncated content.xml"), + ) + for tpl_file, targets, is_ok, msg in dataset: + with open(tpl_file, 'rb') as tpl: + template = SimpleUploadedFile("etiquettes-mobilier.odt", + tpl.read()) + model, __ = ImporterModel.objects.get_or_create( + klass='archaeological_finds.models.Find' + ) + q = DocumentTemplate.objects.filter(slug="test") + if q.count(): + q.all()[0].delete() + doc = DocumentTemplate.objects.create( + name="Test", + slug="test", + associated_model=model, + available=True, + label_targets=targets, + label_template=template) + self.templates.append(doc.label_template.path) + doc = DocumentTemplate.objects.get(pk=doc.pk) + msg = "Fail on dataset: " + msg + if is_ok: + self.assertTrue(doc.template.name, msg=msg) + self.templates.append(doc.template.path) + else: + self.assertFalse(doc.template.name, msg=msg) |