From b2073cea0bcbaa0d3398125523efc03a02a8ce7f Mon Sep 17 00:00:00 2001 From: Mantas Date: Wed, 5 Aug 2015 18:12:11 +0300 Subject: [PATCH] Fix failing tests and hardcoded fixtures The reason why tests where failing is that user role forums fixture was hardcoded with forum ids. Since tests run on same database many times creating same objects again and again, new objects get different ids, so role fixture references wrong forums and forum roles. To fix that, added helper function load_fixtures that automaticall converts hardcoded ids to correct values. Also to not reference model instances by value, added factory classes for forums, forum roles and user roles. --- misago/factories/forummodel.py | 35 ++++++++++++++++++++ misago/factories/forumrolemodel.py | 18 +++++++++++ misago/factories/rolemodel.py | 18 +++++++++++ misago/settings/testing.py | 16 ++++++++++ misago/tests/apps/test_index.py | 6 ++-- misago/tests/apps/threads/test_list.py | 6 ++-- misago/utils/testing.py | 44 ++++++++++++++++++++++++++ 7 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 misago/factories/forumrolemodel.py create mode 100644 misago/factories/rolemodel.py create mode 100644 misago/utils/testing.py diff --git a/misago/factories/forummodel.py b/misago/factories/forummodel.py index cda5998f01..378b6f78b2 100644 --- a/misago/factories/forummodel.py +++ b/misago/factories/forummodel.py @@ -2,8 +2,43 @@ import factory.django +class RootForum(factory.django.DjangoModelFactory): + special = 'root' + name = 'root' + slug = 'root' + + class Meta: + model = 'misago.Forum' + django_get_or_create = ('slug',) + + +class CategoryForum(factory.django.DjangoModelFactory): + type = 'category' + slug = 'first-category' + name = 'First Category' + parent = factory.SubFactory(RootForum) + + class Meta: + model = 'misago.Forum' + django_get_or_create = ('slug',) + + +class RedirectForum(factory.django.DjangoModelFactory): + type = 'redirect' + slug = 'project-homepage' + name = 'Project Homepage' + redirect = 'http://misago-project.org' + + class Meta: + model = 'misago.Forum' + django_get_or_create = ('slug',) + + class ForumFactory(factory.django.DjangoModelFactory): + type = 'forum' slug = 'first-forum' + name = 'First Forum' + parent = factory.SubFactory(CategoryForum) class Meta: model = 'misago.Forum' diff --git a/misago/factories/forumrolemodel.py b/misago/factories/forumrolemodel.py new file mode 100644 index 0000000000..8b3133541c --- /dev/null +++ b/misago/factories/forumrolemodel.py @@ -0,0 +1,18 @@ +import factory +import factory.django + + +class FullAccessForumRole(factory.django.DjangoModelFactory): + name = 'Full Access' + + class Meta: + model = 'misago.ForumRole' + django_get_or_create = ('name',) + + +class ReadOnlyForumRole(factory.django.DjangoModelFactory): + name = "Read only" + + class Meta: + model = 'misago.ForumRole' + django_get_or_create = ('name',) diff --git a/misago/factories/rolemodel.py b/misago/factories/rolemodel.py new file mode 100644 index 0000000000..a2826cb33b --- /dev/null +++ b/misago/factories/rolemodel.py @@ -0,0 +1,18 @@ +import factory +import factory.django + + +class AdministratorRole(factory.django.DjangoModelFactory): + name = "Administrator" + + class Meta: + model = 'misago.Role' + django_get_or_create = ('name',) + + +class GuestRole(factory.django.DjangoModelFactory): + name = "Guest" + + class Meta: + model = 'misago.Role' + django_get_or_create = ('name',) diff --git a/misago/settings/testing.py b/misago/settings/testing.py index 41e6ac97f3..99d2faa110 100644 --- a/misago/settings/testing.py +++ b/misago/settings/testing.py @@ -1 +1,17 @@ from misago.settings.development import * + +SECRET_KEY = 'SECRET4TESTS' + +CACHES['default'] = { + 'BACKEND': 'django.core.cache.backends.dummy.DummyCache' +} + +SKIP_SOUTH_TESTS = True + +MEDIA_URL = "http://media.domain.com/" + +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.simple_backend.SimpleEngine', + }, +} diff --git a/misago/tests/apps/test_index.py b/misago/tests/apps/test_index.py index 07b816ce4f..3c8dac2472 100644 --- a/misago/tests/apps/test_index.py +++ b/misago/tests/apps/test_index.py @@ -1,11 +1,11 @@ -from django.core.management import call_command - from django_webtest import WebTest +from misago.utils.testing import load_fixtures + class IndexPageTests(WebTest): def test_index(self): - call_command('syncfixtures', quiet=1) + load_fixtures() resp = self.app.get('/') self.assertEqual(resp.status_int, 200) diff --git a/misago/tests/apps/threads/test_list.py b/misago/tests/apps/threads/test_list.py index 11fb39cdc9..df3e3373e0 100644 --- a/misago/tests/apps/threads/test_list.py +++ b/misago/tests/apps/threads/test_list.py @@ -1,15 +1,13 @@ -from django.core.management import call_command - from django_webtest import WebTest +from misago.utils.testing import load_fixtures from misago.factories.forummodel import ForumFactory class ForumListTests(WebTest): def test_forum_list_annonymous(self): - call_command('syncfixtures', quiet=1) - + load_fixtures() forum = ForumFactory() resp = self.app.get('/forum/%s-%d/' % (forum.slug, forum.pk)) diff --git a/misago/utils/testing.py b/misago/utils/testing.py new file mode 100644 index 0000000000..66802bf75e --- /dev/null +++ b/misago/utils/testing.py @@ -0,0 +1,44 @@ +from django.core.management import call_command + +from misago.models import Forum, ForumRole, Role + + +def fix_hardcoded_role(forums, forum_roles, role_name): + role = Role.objects.get(name=role_name) + perms = dict(role.permissions) + perms['forums'] = { + forums[forum_id]: forum_roles[forum_role_id] + for forum_id, forum_role_id in perms['forums'].items() + } + role.permissions = perms + role.save() + + +def fix_hardoced_fixtures(): + forums = { + 1: Forum.objects.get(slug='private').pk, + 2: Forum.objects.get(slug='reports').pk, + 3: Forum.objects.get(slug='root').pk, + 4: Forum.objects.get(slug='first-category').pk, + 5: Forum.objects.get(slug='first-forum').pk, + 6: Forum.objects.get(slug='project-homepage').pk, + } + + forum_roles = { + 1: ForumRole.objects.get(name='Full Access').pk, + 2: ForumRole.objects.get(name='Standard Access and Upload').pk, + 3: ForumRole.objects.get(name='Standard Access').pk, + 4: ForumRole.objects.get(name='Read and Download').pk, + 5: ForumRole.objects.get(name='Threads list only').pk, + 6: ForumRole.objects.get(name='Read only').pk, + } + + fix_hardcoded_role(forums, forum_roles, "Administrator") + fix_hardcoded_role(forums, forum_roles, "Moderator") + fix_hardcoded_role(forums, forum_roles, "Registered") + fix_hardcoded_role(forums, forum_roles, "Guest") + + +def load_fixtures(): + call_command('syncfixtures', quiet=1) + fix_hardoced_fixtures()