From bd317aed3144a2a9ede17cd7398ddb0365881a7c Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Fri, 1 Feb 2013 19:19:18 +0100 Subject: [PATCH] Wrote tests for openslides.motion.models.Motion --- openslides/motion/models.py | 24 ++------ tests/motion/__init__.py | 0 tests/motion/test_models.py | 117 ++++++++++++++++++++++++++++++++++++ tests/test_motion.py | 41 ------------- 4 files changed, 121 insertions(+), 61 deletions(-) create mode 100644 tests/motion/__init__.py create mode 100644 tests/motion/test_models.py delete mode 100644 tests/test_motion.py diff --git a/openslides/motion/models.py b/openslides/motion/models.py index e40aa91e4..5380ba6f8 100644 --- a/openslides/motion/models.py +++ b/openslides/motion/models.py @@ -113,7 +113,9 @@ class Motion(SlideMixin, models.Model): _attr = '_%s' % attr try: setattr(version, attr, getattr(self, _attr)) + delattr(self, _attr) except AttributeError: + # If the _attr was not set, use the value from last_version setattr(version, attr, getattr(self.last_version, attr)) version.save() @@ -190,14 +192,6 @@ class Motion(SlideMixin, models.Model): self._new_version = MotionVersion(motion=self) return self._new_version - def get_version(self, version_id): - """ - Return a specific version from version_id - """ - # TODO: Check case, if version_id is not one of this motion - return self.versions.get(pk=version_id) - - def get_version(self): """ Get the "active" version object. This version will be used to get the @@ -206,7 +200,6 @@ class Motion(SlideMixin, models.Model): try: return self._version except AttributeError: - # TODO: choose right version via config return self.last_version def set_version(self, version): @@ -245,10 +238,7 @@ class Motion(SlideMixin, models.Model): return self.new_version def is_supporter(self, person): - try: - return self.supporter.filter(person=person).exists() - except AttributeError: - return False + return self.supporter.filter(person=person).exists() def support(self, person): """ @@ -267,13 +257,7 @@ class Motion(SlideMixin, models.Model): Remove a supporter from the list of supporters of the motion """ if self.state.support: - try: - self.supporter.filter(person=person).delete() - except MotionSupporter.DoesNotExist: - # TODO: Don't do nothing but raise a precise exception for the view - pass - #else: - #self.writelog(_("Supporter: -%s") % (person)) + self.supporter.filter(person=person).delete() else: raise WorkflowError("You can not unsupport a motion in state %s" % self.state.name) diff --git a/tests/motion/__init__.py b/tests/motion/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/motion/test_models.py b/tests/motion/test_models.py new file mode 100644 index 000000000..eda00fdd2 --- /dev/null +++ b/tests/motion/test_models.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + Tests for openslides.motion.models + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: 2011, 2012 by OpenSlides team, see AUTHORS. + :license: GNU GPL, see LICENSE for more details. +""" + +from django.test import TestCase + +from openslides.participant.models import User +from openslides.config.models import config +from openslides.motion.models import Motion +from openslides.motion.workflow import WorkflowError + + +class ModelTest(TestCase): + def setUp(self): + self.motion = Motion.objects.create(title='v1') + self.test_user = User.objects.create(username='blub') + + def test_create_new_version(self): + config['motion_create_new_version'] = 'ALLWASY_CREATE_NEW_VERSION' + motion = Motion.objects.create(title='m1') + self.assertEqual(motion.versions.count(), 1) + + motion.new_version + motion.save() + self.assertEqual(motion.versions.count(), 2) + + motion.title = 'new title' + motion.save() + self.assertEqual(motion.versions.count(), 3) + + motion.save() + self.assertEqual(motion.versions.count(), 3) + + config['motion_create_new_version'] = 'NEVER_CREATE_NEW_VERSION' + motion.text = 'new text' + motion.save() + self.assertEqual(motion.versions.count(), 3) + + def test_version_data(self): + motion = Motion() + self.assertEqual(motion.title, '') + with self.assertRaises(AttributeError): + self._title + + motion.title = 'title' + self.assertEqual(motion._title, 'title') + + motion.reason = 'reason' + self.assertEqual(motion._reason, 'reason') + + def test_version(self): + motion = Motion.objects.create(title='v1') + motion.title = 'v2' + motion.save() + v2_version = motion.version + motion.title = 'v3' + motion.save() + with self.assertRaises(AttributeError): + self._title + self.assertEqual(motion.title, 'v3') + + motion.version = 0 + self.assertEqual(motion.title, 'v1') + + motion.version = v2_version + self.assertEqual(motion.title, 'v2') + + motion.version = None + motion.version = None # Test to set a version to None, which is already None + self.assertEqual(motion.title, 'v3') + + with self.assertRaises(ValueError): + motion.version = 'wrong' + + def test_absolute_url(self): + motion_id = self.motion.id + + self.assertEqual(self.motion.get_absolute_url('detail'), '/motion/%d/' % motion_id) + self.assertEqual(self.motion.get_absolute_url('edit'), '/motion/%d/edit/' % motion_id) + #self.assertEqual(self.motion.get_absolute_url('delete'), '/motion/%d/del/' % motion_id) + + def test_supporter(self): + self.assertFalse(self.motion.is_supporter(self.test_user)) + self.motion.support(self.test_user) + self.assertTrue(self.motion.is_supporter(self.test_user)) + self.motion.unsupport(self.test_user) + self.assertFalse(self.motion.is_supporter(self.test_user)) + self.motion.unsupport(self.test_user) + + def test_poll(self): + self.motion.state = 'per' + poll = self.motion.create_poll() + self.assertEqual(poll.poll_number, 1) + + def test_state(self): + self.motion.reset_state() + self.assertEqual(self.motion.state.id, 'pub') + + with self.assertRaises(WorkflowError): + self.motion.create_poll() + + self.motion.set_state('per') + self.assertEqual(self.motion.state.id, 'per') + with self.assertRaises(WorkflowError): + self.motion.support(self.test_user) + with self.assertRaises(WorkflowError): + self.motion.unsupport(self.test_user) + + with self.assertRaises(WorkflowError): + self.motion.set_state('per') + diff --git a/tests/test_motion.py b/tests/test_motion.py deleted file mode 100644 index 26c506c37..000000000 --- a/tests/test_motion.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" - openslides.motion.tests - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - Unit tests for the motion app. - - :copyright: 2011, 2012 by OpenSlides team, see AUTHORS. - :license: GNU GPL, see LICENSE for more details. -""" - -from django.test import TestCase - -from openslides.participant.models import User -from openslides.motion.models import Motion - - -class MotionTest(TestCase): - def setUp(self): - self.admin = User(username='testadmin') - self.admin.save() - self.anonym = User(username='testanoym') - self.anonym.save() - self.app1 = Motion(submitter=self.admin) - self.app1.save() - - def refresh(self): - self.app1 = Motion.objects.get(pk=self.app1.id) - - def testVersion(self): - self.assertTrue(self.app1.versions.exists()) - self.assertEqual(self.app1.last_version, self.app1.versions[0]) - self.assertEqual(self.app1.creation_time, self.app1.last_version.time) - - self.app1.title = "app1" - self.app1.save() - self.refresh() - - self.assertEqual(self.app1.versions.count(), 2) - self.assertEqual(self.app1.last_version, self.app1.versions[1])