2013-05-08 18:07:09 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Tests for openslides.motion.models
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
2013-09-25 10:01:01 +02:00
|
|
|
|
import StringIO
|
2013-05-08 18:07:09 +02:00
|
|
|
|
|
|
|
|
|
from django.test.client import Client
|
|
|
|
|
|
|
|
|
|
from openslides.motion.csv_import import import_motions
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from openslides.motion.models import Category, Motion
|
2013-05-12 00:47:49 +02:00
|
|
|
|
from openslides.participant.models import User
|
|
|
|
|
from openslides.utils.test import TestCase
|
2013-05-08 18:07:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CSVImport(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
# Admin
|
2013-05-12 00:47:49 +02:00
|
|
|
|
self.admin = User.objects.create_superuser('Admin_ieY0Eereimeimeizuosh', 'admin@admin.admin', 'eHiK1aiRahxaix0Iequ2')
|
2013-05-08 18:07:09 +02:00
|
|
|
|
self.admin_client = Client()
|
2013-05-12 00:47:49 +02:00
|
|
|
|
self.admin_client.login(username='Admin_ieY0Eereimeimeizuosh', password='eHiK1aiRahxaix0Iequ2')
|
|
|
|
|
|
|
|
|
|
# Normal user
|
|
|
|
|
self.normal_user = User.objects.create_user('User_CiuNgo4giqueeChie5oi', 'user@user.user', 'eihi1Eequaek4eagaiKu')
|
|
|
|
|
self.normal_client = Client()
|
|
|
|
|
self.normal_client.login(username='User_CiuNgo4giqueeChie5oi', password='eihi1Eequaek4eagaiKu')
|
|
|
|
|
|
|
|
|
|
# Category
|
2013-07-08 10:11:48 +02:00
|
|
|
|
self.category1 = Category.objects.create(name='Satzungsanträge', prefix='SA')
|
|
|
|
|
self.category2 = Category.objects.create(name='Bildung', prefix='B1')
|
|
|
|
|
self.category3 = Category.objects.create(name='Bildung', prefix='B2')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
|
|
def test_example_file_de(self):
|
2013-07-08 10:11:48 +02:00
|
|
|
|
special_user = User.objects.create_user(username='Harry_Holland',
|
2013-05-12 00:47:49 +02:00
|
|
|
|
password='iegheeChaje7guthie4a',
|
2013-07-08 10:11:48 +02:00
|
|
|
|
first_name='Harry',
|
|
|
|
|
last_name='Holland')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
for i in range(2):
|
2013-07-08 10:11:48 +02:00
|
|
|
|
username = 'John_Doe_%d' % i
|
2013-05-12 00:47:49 +02:00
|
|
|
|
User.objects.create_user(username=username,
|
|
|
|
|
password='default',
|
2013-07-08 10:11:48 +02:00
|
|
|
|
first_name='John',
|
|
|
|
|
last_name='Doe')
|
2013-05-08 18:07:09 +02:00
|
|
|
|
|
|
|
|
|
csv_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'extras', 'csv-examples')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
self.assertEqual(Motion.objects.count(), 0)
|
2013-05-08 18:07:09 +02:00
|
|
|
|
with open(csv_dir + '/motions-demo_de.csv') as f:
|
2013-05-29 22:29:08 +02:00
|
|
|
|
count_success, count_lines, error_messages, warning_messages = import_motions(csv_file=f, default_submitter=self.normal_user.person_id)
|
2013-07-08 10:11:48 +02:00
|
|
|
|
self.assertEqual(Motion.objects.count(), 11)
|
|
|
|
|
self.assertEqual(count_success, 11)
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
|
|
motion1 = Motion.objects.get(pk=1)
|
|
|
|
|
self.assertEqual(motion1.identifier, '1')
|
|
|
|
|
self.assertEqual(motion1.title, u'Entlastung des Vorstandes')
|
|
|
|
|
self.assertEqual(motion1.text, u'Die Versammlung möge beschließen, den Vorstand für seine letzte Legislaturperiode zu entlasten.')
|
|
|
|
|
self.assertEqual(motion1.reason, u'Bericht erfolgt mündlich.')
|
|
|
|
|
self.assertEqual(len(motion1.submitter.all()), 1)
|
|
|
|
|
self.assertEqual(motion1.submitter.all()[0].person, self.normal_user)
|
|
|
|
|
self.assertTrue(motion1.category is None)
|
2013-05-29 22:29:08 +02:00
|
|
|
|
self.assertTrue(any('Submitter unknown.' in w for w in warning_messages))
|
|
|
|
|
self.assertTrue(any('Category unknown.' in w for w in warning_messages))
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
|
|
motion2 = Motion.objects.get(pk=2)
|
2013-07-08 10:11:48 +02:00
|
|
|
|
self.assertEqual(motion2.identifier, 'SA 1')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
self.assertEqual(motion2.title, u'Satzungsänderung § 2 Abs. 3')
|
2013-07-08 10:11:48 +02:00
|
|
|
|
self.assertHTMLEqual(motion2.text, u'''<p>Die Versammlung möge beschließen, die Satzung in § 2 Abs. 3 wie folgt zu ändern:</p>
|
|
|
|
|
<p>Es wird vor dem Wort "Zweck" das Wort "gemeinnütziger" eingefügt.</p>''')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
self.assertEqual(motion2.reason, u'Die Änderung der Satzung ist aufgrund der letzten Erfahrungen eine sinnvolle Maßnahme, weil ...')
|
|
|
|
|
self.assertEqual(len(motion2.submitter.all()), 1)
|
|
|
|
|
self.assertEqual(motion2.submitter.all()[0].person, special_user)
|
|
|
|
|
self.assertEqual(motion2.category, self.category1)
|
|
|
|
|
|
2013-07-08 10:11:48 +02:00
|
|
|
|
# check user 'John Doe'
|
2013-05-29 22:29:08 +02:00
|
|
|
|
self.assertTrue(any('Several suitable submitters found.' in w for w in warning_messages))
|
2013-07-08 10:11:48 +02:00
|
|
|
|
# check category 'Bildung'
|
2013-05-29 22:29:08 +02:00
|
|
|
|
self.assertTrue(any('Several suitable categories found.' in w for w in warning_messages))
|
2013-05-08 18:07:09 +02:00
|
|
|
|
|
2013-05-12 00:47:49 +02:00
|
|
|
|
def test_malformed_file(self):
|
|
|
|
|
csv_file = StringIO.StringIO()
|
|
|
|
|
csv_file.write('Header\nMalformed data,\n,Title,Text,,,\n')
|
2013-05-29 22:29:08 +02:00
|
|
|
|
count_success, count_lines, error_messages, warning_messages = import_motions(csv_file=csv_file, default_submitter=self.normal_user.person_id)
|
|
|
|
|
self.assertEqual(count_success, 0)
|
|
|
|
|
self.assertTrue(any('Line is malformed.' in e for e in error_messages))
|
2013-05-08 18:07:09 +02:00
|
|
|
|
|
2013-05-12 00:47:49 +02:00
|
|
|
|
def test_wrong_encoding(self):
|
|
|
|
|
csv_file = StringIO.StringIO()
|
|
|
|
|
text = u'Müller'.encode('iso-8859-15')
|
|
|
|
|
csv_file.write(text)
|
|
|
|
|
csv_file.seek(0)
|
|
|
|
|
count_success, error_messages, warning_messages = import_motions(csv_file=csv_file, default_submitter=self.normal_user.person_id)
|
|
|
|
|
self.assertEqual(count_success, 0)
|
2013-05-29 22:29:08 +02:00
|
|
|
|
self.assertTrue('Import file has wrong character encoding, only UTF-8 is supported!' in error_messages)
|