8

Add clean method to ensure mixed static segments are valid

This commit is contained in:
Todd Dembrey
2017-10-20 10:57:19 +01:00
parent f339879907
commit cf41be4b76
2 changed files with 52 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import datetime
import pytest
from django.core.exceptions import ValidationError
from tests.factories.segment import SegmentFactory
from wagtail_personalisation.models import Segment
from wagtail_personalisation.rules import TimeRule, VisitCountRule
@@ -116,3 +117,39 @@ def test_does_not_calculate_the_segment_again(rf, site, client, mocker):
mock_test_rule = mocker.patch('wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules')
client.get(site.root_page.url)
assert mock_test_rule.call_count == 0
@pytest.mark.django_db
def test_non_static_rules_have_a_count():
segment = SegmentFactory(type=Segment.TYPE_STATIC, count=0)
TimeRule.objects.create(
start_time=datetime.time(0, 0, 0),
end_time=datetime.time(23, 59, 59),
segment=segment,
)
with pytest.raises(ValidationError):
segment.clean()
@pytest.mark.django_db
def test_static_segment_with_static_rules_needs_no_count(site):
segment = SegmentFactory(type=Segment.TYPE_STATIC, count=0)
VisitCountRule.objects.create(counted_page=site.root_page, segment=segment)
try:
segment.clean()
except ValidationError:
pytest.fail('Should not raise ValidationError.')
@pytest.mark.django_db
def test_dynamic_segment_with_non_static_rules_have_a_count():
segment = SegmentFactory(type=Segment.TYPE_DYNAMIC, count=0)
TimeRule.objects.create(
start_time=datetime.time(0, 0, 0),
end_time=datetime.time(23, 59, 59),
segment=segment,
)
try:
segment.clean()
except ValidationError:
pytest.fail('Should not raise ValidationError.')