Use wagtail-factories for testing
This commit is contained in:
committed by
Boris Besemer
parent
08f88181f4
commit
8e754fef07
4
Makefile
4
Makefile
@ -10,7 +10,9 @@ clean:
|
|||||||
find . -name '*.egg-info' -delete
|
find . -name '*.egg-info' -delete
|
||||||
|
|
||||||
requirements:
|
requirements:
|
||||||
pip install --upgrade -e .
|
pip install --upgrade -e .[test]
|
||||||
|
|
||||||
|
install: develop
|
||||||
|
|
||||||
develop: clean requirements
|
develop: clean requirements
|
||||||
|
|
||||||
|
1
setup.py
1
setup.py
@ -13,6 +13,7 @@ tests_require = [
|
|||||||
'pytest-sugar==0.7.1',
|
'pytest-sugar==0.7.1',
|
||||||
'freezegun==0.3.8',
|
'freezegun==0.3.8',
|
||||||
'factory_boy==2.8.1',
|
'factory_boy==2.8.1',
|
||||||
|
'wagtail_factories==0.2.0',
|
||||||
]
|
]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
|
@ -2,49 +2,17 @@ from __future__ import absolute_import, unicode_literals
|
|||||||
|
|
||||||
import factory
|
import factory
|
||||||
from wagtail.wagtailcore.models import Page
|
from wagtail.wagtailcore.models import Page
|
||||||
|
from wagtail_factories import PageFactory
|
||||||
|
|
||||||
from personalisation.models import PersonalisablePage
|
from personalisation.models import PersonalisablePage
|
||||||
from tests.sandbox.pages.models import HomePage
|
from tests.sandbox.pages.models import HomePage
|
||||||
|
|
||||||
|
|
||||||
class PersonalisablePageFactory(factory.DjangoModelFactory):
|
class PersonalisablePageFactory(PageFactory):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = PersonalisablePage
|
model = PersonalisablePage
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _create(cls, *args, **kwargs):
|
|
||||||
obj = super(PersonalisablePageFactory, cls)._build(*args, **kwargs)
|
|
||||||
if not obj.title:
|
|
||||||
obj.title = "Page-Test"
|
|
||||||
return obj
|
|
||||||
|
|
||||||
|
class HomePageFactory(PageFactory):
|
||||||
class HomePageFactory(factory.DjangoModelFactory):
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = HomePage
|
model = HomePage
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def build(cls, *args, **kwargs):
|
|
||||||
obj = super(HomePageFactory, cls)._build(*args, **kwargs)
|
|
||||||
|
|
||||||
for part in ('subtitle', 'body'):
|
|
||||||
if not getattr(obj, part):
|
|
||||||
setattr(obj, part, "{}".format(part))
|
|
||||||
return obj
|
|
||||||
|
|
||||||
|
|
||||||
class SiteRootFactory(factory.DjangoModelFactory):
|
|
||||||
title = 'site-root'
|
|
||||||
depth = 2
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Page
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _create(cls, *args, **kwargs):
|
|
||||||
try:
|
|
||||||
root = Page.objects.get(depth=0)
|
|
||||||
except Page.DoesNotExist:
|
|
||||||
root = Page.add_root(title='root')
|
|
||||||
|
|
||||||
return root.add_child(title=kwargs['title'])
|
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
from __future__ import absolute_import, unicode_literals
|
|
||||||
|
|
||||||
import factory
|
|
||||||
from wagtail.wagtailcore.models import Site
|
|
||||||
|
|
||||||
from tests.factories.page import SiteRootFactory
|
|
||||||
|
|
||||||
|
|
||||||
class SiteFactory(factory.DjangoModelFactory):
|
|
||||||
hostname = 'localhost'
|
|
||||||
port = 8000
|
|
||||||
site_name = 'Testing Site'
|
|
||||||
root_page = factory.SubFactory(SiteRootFactory)
|
|
||||||
is_default_site = True
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Site
|
|
@ -6,11 +6,11 @@ import pytest
|
|||||||
from django.test.client import Client
|
from django.test.client import Client
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
from wagtail.wagtailcore.models import Page
|
from wagtail.wagtailcore.models import Page
|
||||||
|
from wagtail_factories import SiteFactory
|
||||||
|
|
||||||
from tests.factories.segment import (
|
from tests.factories.segment import (
|
||||||
QueryRuleFactory, ReferralRuleFactory, SegmentFactory, TimeRuleFactory,
|
QueryRuleFactory, ReferralRuleFactory, SegmentFactory, TimeRuleFactory,
|
||||||
DayRuleFactory, VisitCountRuleFactory, DeviceRuleFactory)
|
DayRuleFactory, VisitCountRuleFactory, DeviceRuleFactory)
|
||||||
from tests.factories.site import SiteFactory
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@ -20,7 +20,7 @@ class TestUserSegmenting(object):
|
|||||||
"""
|
"""
|
||||||
Sets up a site root to test segmenting
|
Sets up a site root to test segmenting
|
||||||
"""
|
"""
|
||||||
self.site = SiteFactory()
|
self.site = SiteFactory(is_default_site=True)
|
||||||
|
|
||||||
def test_no_segments(self, client):
|
def test_no_segments(self, client):
|
||||||
request = client.get('/')
|
request = client.get('/')
|
||||||
@ -164,7 +164,7 @@ class TestUserSegmenting(object):
|
|||||||
segment = SegmentFactory(name='Visit Count')
|
segment = SegmentFactory(name='Visit Count')
|
||||||
|
|
||||||
visit_count_rule = VisitCountRuleFactory(
|
visit_count_rule = VisitCountRuleFactory(
|
||||||
counted_page=Page.objects.filter(slug="root").first(),
|
counted_page=self.site.root_page,
|
||||||
segment=segment
|
segment=segment
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -269,7 +269,7 @@ class TestUserSegmenting(object):
|
|||||||
class TestUserVisitCount(object):
|
class TestUserVisitCount(object):
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
self.site = SiteFactory()
|
self.site = SiteFactory(is_default_site=True)
|
||||||
|
|
||||||
# TODO: Set up a bunch of pages for testing the visit count
|
# TODO: Set up a bunch of pages for testing the visit count
|
||||||
|
|
||||||
|
@ -6,9 +6,9 @@ import pytest
|
|||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
from wagtail.wagtailcore.models import Site
|
from wagtail.wagtailcore.models import Site
|
||||||
|
from wagtail_factories import SiteFactory
|
||||||
|
|
||||||
from personalisation import models
|
from personalisation import models
|
||||||
from tests.factories.site import SiteFactory
|
|
||||||
|
|
||||||
"""Time Rule tests"""
|
"""Time Rule tests"""
|
||||||
@freeze_time("10:00:00")
|
@freeze_time("10:00:00")
|
||||||
|
Reference in New Issue
Block a user