diff --git a/setup.py b/setup.py index 52d12f7..f9f7c77 100644 --- a/setup.py +++ b/setup.py @@ -18,6 +18,7 @@ tests_require = [ 'pytest-cov==2.4.0', 'pytest-django==3.1.2', 'pytest-sugar==0.7.1', + 'pytest-mock==1.6.3', 'pytest==3.1.0', 'wagtail_factories==0.3.0', ] diff --git a/src/wagtail_personalisation/adapters.py b/src/wagtail_personalisation/adapters.py index 5bde235..16260ec 100644 --- a/src/wagtail_personalisation/adapters.py +++ b/src/wagtail_personalisation/adapters.py @@ -175,7 +175,7 @@ class SessionSegmentsAdapter(BaseSegmentsAdapter): # Run tests on all remaining enabled segments to verify applicability. additional_segments = [] for segment in enabled_segments: - if segment.is_static and self.request.session in segment.sessions.all(): + if segment.is_static and self.request.session.session_key in segment.sessions.values_list('session_key', flat=True): additional_segments.append(segment) elif not segment.is_static or not segment.is_full: segment_rules = [] diff --git a/tests/unit/test_static_dynamic_segments.py b/tests/unit/test_static_dynamic_segments.py index 53e30b1..7815d8e 100644 --- a/tests/unit/test_static_dynamic_segments.py +++ b/tests/unit/test_static_dynamic_segments.py @@ -73,3 +73,17 @@ def test_sessions_not_added_to_static_segment_if_rule_not_static(): segment=segment) assert not segment.sessions.all() + + +@pytest.mark.django_db +def test_does_not_calculate_the_segment_again(rf, site, client, mocker): + session = client.session + session.save() + client.get(site.root_page.url) + + segment = SegmentFactory(type=Segment.TYPE_STATIC, count=2) + VisitCountRule.objects.create(counted_page=site.root_page, segment=segment) + + mock_test_rule = mocker.patch('wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules') + client.get(site.root_page.url) + assert mock_test_rule.call_count == 0