Commit 91953a8b authored by okhin's avatar okhin

Testing the importer

parent d931aa24
Pipeline #707 failed with stage
in 57 seconds
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -29,9 +29,9 @@ class Importer(models.Model):
'name': self.name}
def kind(self):
if self.url is '' and self.file is None:
if self.url is None and self.file.name is None:
return None
if self.url is '':
if self.url is None:
return 'file'
else:
return 'url'
......@@ -63,19 +63,16 @@ class Importer(models.Model):
if not (end_date == '' or end_date.startswith('9999')):
# This mandate has ended
continue
# We want to only use the groups nown by the organisation
try:
groupType = GroupType.objects.get(organization = self.organization,
name = mandate['group']['kind'])
except:
continue
if 'abbreviation' in mandate['group']:
group, added = Group.objects.get_or_create(name = mandate['group']['abbreviation'],
type = groupType)
else:
group, added = Group.objects.get_or_create(name = mandate['group']['name'],
type = groupType)
groups.append(group)
# We want to only use the groups own by the organisation
for groupType in GroupType.objects.filter(organization = self.organization,
name = mandate['group']['kind']):
if 'abbreviation' in mandate['group']:
group, added = Group.objects.get_or_create(name = mandate['group']['abbreviation'],
type = groupType)
else:
group, added = Group.objects.get_or_create(name = mandate['group']['name'],
type = groupType)
groups.append(group)
if groups == []:
# The contact have no groups active, he is not to be bothered.
continue
......@@ -122,7 +119,7 @@ class Importer(models.Model):
# Updating the last imported item
self.last_imported = inserted
self.save()
def handle(self):
"""
This function is used to import the data described by the Importer.
......@@ -155,5 +152,4 @@ class Importer(models.Model):
# Now, let's get the correct call
if self.category == 'repr':
self.representative(import_data)
return
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,
organization=self.organization
)
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,
organization = self.organization,
)
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,
organization = self.organization,
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment