summaryrefslogtreecommitdiff
path: root/ishtar_common/tests.py
blob: 000abc2684c6e3b22a09186596a7c0a7c1700501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

import tempfile
from zipfile import ZipFile, ZIP_DEFLATED

from django.contrib.auth.models import User
from django.test import TestCase

from ishtar_common import models, ooo_replace
from ishtar_common.ooo_replace import ooo_replace

class OOOGenerationTest(TestCase):
    def testGeneration(self):
        context = {'test-var':u"Testé", 'test-var2':u""}
        tmp = tempfile.TemporaryFile()
        ooo_replace("../ishtar_common/tests/test-file.odt", tmp, context)
        inzip = ZipFile(tmp, 'r', ZIP_DEFLATED)
        value = inzip.read('content.xml')
        self.assertTrue("Testé" in value)
        self.assertTrue("testé 2" not in value)

class MergeTest(TestCase):
    def setUp(self):
        self.user, created = User.objects.get_or_create(username='username')
        self.organisation_types = models.OrganizationType.create_default_for_test()

        self.person_types = [models.PersonType.objects.create(label='Admin'),
                             models.PersonType.objects.create(label='User')]
        self.author_types = [models.AuthorType.objects.create(label='1'),
                             models.AuthorType.objects.create(label='2'),]

        self.company_1 = models.Organization.objects.create(
                    history_modifier=self.user, name='Franquin Comp.',
                    organization_type=self.organisation_types[0])
        self.person_1 = models.Person.objects.create(name='Boule',
                surname=' ',
                history_modifier=self.user, attached_to=self.company_1)
        self.person_1.person_types.add(self.person_types[0])
        self.author_1_pk = models.Author.objects.create(person=self.person_1,
                                            author_type=self.author_types[0]).pk

        self.company_2 = models.Organization.objects.create(
                    history_modifier=self.user, name='Goscinny Corp.',
                    organization_type=self.organisation_types[1])
        self.person_2 = models.Person.objects.create(name='Bill',
                history_modifier=self.user, surname='Peyo', title='Mr',
                attached_to=self.company_2)
        self.person_2.person_types.add(self.person_types[1])
        self.author_2_pk = models.Author.objects.create(person=self.person_2,
                                            author_type=self.author_types[1]).pk
        self.person_3 = models.Person.objects.create(name='George',
                history_modifier=self.user, attached_to=self.company_1)

    def testPersonMerge(self):
        self.person_1.merge(self.person_2)
        # preserve existing fields
        self.assertEqual(self.person_1.name, 'Boule')
        # fill missing fields
        self.assertEqual(self.person_1.title, 'Mr')
        # string field with only spaces is an empty field
        self.assertEqual(self.person_1.surname, 'Peyo')
        # preserve existing foreign key
        self.assertEqual(self.person_1.attached_to, self.company_1)
        # preserve existing many to many
        self.assertTrue(self.person_types[0] in self.person_1.person_types.all())
        # add new many to many
        self.assertTrue(self.person_types[1] in self.person_1.person_types.all())
        # update reverse foreign key association and dont break the existing
        self.assertEqual(models.Author.objects.get(pk=self.author_1_pk).person,
                         self.person_1)
        self.assertEqual(models.Author.objects.get(pk=self.author_2_pk).person,
                         self.person_1)

        self.person_3.merge(self.person_1)
        # manage well empty many to many fields
        self.assertTrue(self.person_types[1] in self.person_3.person_types.all())