tests.py 2.63 KB
Newer Older
okhin's avatar
okhin committed
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
from unittest import mock

from django.test import TestCase
from django.core.files.uploadedfile import SimpleUploadedFile

from picampaign.importer.models import Importer
from picampaign.contact.models import Contact, Phone
from picampaign.campaign.models import Campaign, CampaignContact
from picampaign.organization.models import Group, GroupType, Organization

class ImporterTest(TestCase):
    def setUp(self):
        self.organization = Organization.objects.create(
            name='Majestic 12',
            sip_key='majestic-12'
        )
        self.campaign = Campaign.objects.create(
            title='Test campaign',
            start_date='2000-01-01',
            end_date='2100-12-31',
            organization=self.organization)
        GroupType.objects.create(
            organization=self.organization,
            name='group'
        )
        GroupType.objects.create(
            organization=self.organization,
            name='chamber'
        )
        GroupType.objects.create(
            organization=self.organization,
            name='committee'
        )

    def test_str(self):
        importer = Importer.objects.create(
            name='test importer',
            category='repr',
            campaign=self.campaign,
        )
        self.assertEqual(str(importer), u'%(name)s: %(format)s %(type)s importer' % {'format': importer.category,
                                                'type': importer.kind(),
                                                'name': importer.name})

    def test_kind(self):
        importer = Importer.objects.create(
            name='test importer',
            category='repr',
            campaign = self.campaign,
        )
        self.assertEqual(importer.kind(), None)
        importer.file = 'a'
        self.assertEqual(importer.kind(), 'file')
        importer.url = 'http://www.example.com/'
        self.assertEqual(importer.kind(), 'url')

    def test_representative(self):
        json_file = None
        with open('picampaign/importer/data_tests.json', u'rb') as f:
            json_file = SimpleUploadedFile('data_tests.json', f.read())

        importer = Importer.objects.create(
            name='test importer',
            category='repr',
            campaign = self.campaign,
            file=json_file
        )
        self.assertEqual(importer.kind(), 'file')
        importer.handle()
        importer.refresh_from_db()
        # The file import 3 of the 10 meps
        self.assertEqual(importer.last_count, 10)
        self.assertEqual(importer.last_imported, 3)
        self.assertEqual(len(Group.objects.all()), 13)
        self.assertEqual(len(Contact.objects.all()), 3)