Skip to content

Commit

Permalink
Fix failing tests and hardcoded fixtures
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sirex committed Aug 5, 2015
1 parent 6d0df1f commit b2073ce
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 7 deletions.
35 changes: 35 additions & 0 deletions misago/factories/forummodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 18 additions & 0 deletions misago/factories/forumrolemodel.py
Original file line number Diff line number Diff line change
@@ -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',)
18 changes: 18 additions & 0 deletions misago/factories/rolemodel.py
Original file line number Diff line number Diff line change
@@ -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',)
16 changes: 16 additions & 0 deletions misago/settings/testing.py
Original file line number Diff line number Diff line change
@@ -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',
},
}
6 changes: 3 additions & 3 deletions misago/tests/apps/test_index.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 2 additions & 4 deletions misago/tests/apps/threads/test_list.py
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
44 changes: 44 additions & 0 deletions misago/utils/testing.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit b2073ce

Please sign in to comment.