Add clean method to ensure mixed static segments are valid
This commit is contained in:
@@ -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.')
|
||||
|
Reference in New Issue
Block a user