예제 #1
0
def test_match_any_correct_populates(site, django_user_model, mocker):
    user = django_user_model.objects.create(username='******')
    other_user = django_user_model.objects.create(username='******')
    other_page = site.root_page.get_last_child()

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, match_any=True)
    rule_1 = VisitCountRule(counted_page=site.root_page)
    rule_2 = VisitCountRule(counted_page=other_page)
    form = form_with_data(segment, rule_1, rule_2)
    mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user',
                 side_effect=[True, False, True, False])
    instance = form.save()

    assert user in instance.static_users.all()
    assert other_user in instance.static_users.all()
예제 #2
0
def test_session_not_added_to_static_segment_after_full(
        site, client, django_user_model):
    user = django_user_model.objects.create(username='******')
    other_user = django_user_model.objects.create(username='******')
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()

    assert not instance.static_users.all()

    session = client.session
    client.force_login(user)
    client.get(site.root_page.url)

    assert instance.static_users.count() == 1

    client.cookies.clear()
    second_session = client.session
    client.force_login(other_user)
    client.get(site.root_page.url)

    assert session.session_key != second_session.session_key
    assert instance.static_users.count() == 1
    assert user in instance.static_users.all()
    assert other_user not in instance.static_users.all()
예제 #3
0
def test_randomisation_percentage_max_100(site, client, mocker,
                                          django_user_model):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    segment.randomisation_percent = 101
    rule = VisitCountRule()

    form = form_with_data(segment, rule)
    assert not form.is_valid()
예제 #4
0
def test_count_matching_users_handles_match_all(site, client, mocker,
                                                django_user_model):
    django_user_model.objects.create(username='******')
    django_user_model.objects.create(username='******')

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    first_rule = VisitCountRule(counted_page=site.root_page)
    other_page = site.root_page.get_last_child()
    second_rule = VisitCountRule(counted_page=other_page)
    form = form_with_data(segment, first_rule, second_rule)

    mock_test_user = mocker.patch(
        'wagtail_personalisation.rules.VisitCountRule.test_user',
        side_effect=[True, True, False, True])

    assert form.count_matching_users([first_rule, second_rule], False) is 1
    mock_test_user.call_count == 4
예제 #5
0
def test_user_added_to_static_segment_at_creation(site, user, mocker):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user',
                 return_value=True)
    instance = form.save()

    assert user in instance.static_users.all()
예제 #6
0
def test_randomisation_percentage_added_to_segment_at_creation(
        site, client, mocker, django_user_model):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    segment.randomisation_percent = 80
    rule = VisitCountRule()

    form = form_with_data(segment, rule)
    instance = form.save()

    assert instance.randomisation_percent == 80
def test_anonymous_user_not_added_to_static_segment_at_creation(site, client):
    session = client.session
    session.save()
    client.get(site.root_page.url)

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()

    assert not instance.static_users.all()
예제 #8
0
def test_user_not_added_to_full_static_segment_at_creation(
        site, django_user_model, mocker):
    django_user_model.objects.create(username='******')
    django_user_model.objects.create(username='******')
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user',
                 side_effect=[True, True])
    instance = form.save()

    assert len(instance.static_users.all()) == 1
예제 #9
0
def test_count_users_matching_static_rules(site, client, mocker,
                                           django_user_model):
    django_user_model.objects.create(username='******')
    django_user_model.objects.create(username='******')

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user',
                 return_value=True)

    assert form.count_matching_users([rule], True) is 2
예제 #10
0
def test_session_added_to_static_segment_after_creation(site, client, user):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()

    session = client.session
    session.save()
    client.force_login(user)
    client.get(site.root_page.url)

    assert user in instance.static_users.all()
def test_match_any_correct_populates(site, client, django_user_model):
    user = django_user_model.objects.create(username='******')
    session = client.session
    client.force_login(user)
    client.get(site.root_page.url)

    other_user = django_user_model.objects.create(username='******')
    client.cookies.clear()
    second_session = client.session
    other_page = site.root_page.get_last_child()
    client.force_login(other_user)
    client.get(other_page.url)

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, match_any=True)
    rule_1 = VisitCountRule(counted_page=site.root_page)
    rule_2 = VisitCountRule(counted_page=other_page)
    form = form_with_data(segment, rule_1, rule_2)
    instance = form.save()

    assert session.session_key != second_session.session_key
    assert user in instance.static_users.all()
    assert other_user in instance.static_users.all()
예제 #12
0
def test_added_to_static_segment_at_creation_if_random_below_percent(
        site, mocker, user):
    mocker.patch('random.randint', return_value=39)
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC,
                                   randomisation_percent=40)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user',
                 return_value=True)
    instance = form.save()

    assert user in instance.static_users.all()
    assert user not in instance.excluded_users.all()
예제 #13
0
def test_not_offered_dynamic_segment_if_random_is_above_percentage(
        site, client, mocker):
    segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC,
                                   randomisation_percent=40)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()

    mocker.patch('random.randint', return_value=41)
    session = client.session
    session.save()
    client.get(site.root_page.url)

    assert len(client.session['segments']) == 0
    assert instance.id == client.session['excluded_segments'][0]['id']
예제 #14
0
def test_anonymous_user_not_added_to_static_segment_at_creation(
        site, client, mocker):
    session = client.session
    session.save()
    client.get(site.root_page.url)

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    mock_test_rule = mocker.patch(
        'wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules')
    instance = form.save()

    assert not instance.static_users.all()
    assert mock_test_rule.call_count == 0
예제 #15
0
def test_mixed_static_dynamic_session_doesnt_generate_at_creation(
        site, mocker):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1)
    static_rule = VisitCountRule(counted_page=site.root_page)
    non_static_rule = TimeRule(
        start_time=datetime.time(0, 0, 0),
        end_time=datetime.time(23, 59, 59),
    )
    form = form_with_data(segment, static_rule, non_static_rule)

    mock_test_rule = mocker.patch(
        'wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules')
    instance = form.save()

    assert not instance.static_users.all()
    assert mock_test_rule.call_count == 0
예제 #16
0
def test_always_in_segment_if_percentage_is_100(site, client, mocker, user):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC,
                                   count=1,
                                   randomisation_percent=100)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()

    session = client.session
    session.save()
    client.force_login(user)
    client.get(site.root_page.url)

    assert instance.id == client.session['segments'][0]['id']
    assert user in instance.static_users.all()
    assert user not in instance.excluded_users.all()
예제 #17
0
def test_does_not_calculate_the_segment_again(site, client, mocker, user):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=2)
    rule = VisitCountRule(counted_page=site.root_page, segment=segment)
    form = form_with_data(segment, rule)
    mocker.patch('wagtail_personalisation.rules.VisitCountRule.test_user',
                 return_value=True)
    instance = form.save()

    assert user in instance.static_users.all()

    mock_test_rule = mocker.patch(
        'wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules')
    session = client.session
    session.save()
    client.force_login(user)
    client.get(site.root_page.url)
    assert mock_test_rule.call_count == 0
def test_mixed_static_dynamic_session_doesnt_generate_at_creation(
        site, client, user):
    session = client.session
    session.save()
    client.force_login(user)
    client.get(site.root_page.url)

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=1)
    static_rule = VisitCountRule(counted_page=site.root_page)
    non_static_rule = TimeRule(
        start_time=datetime.time(0, 0, 0),
        end_time=datetime.time(23, 59, 59),
    )
    form = form_with_data(segment, static_rule, non_static_rule)
    instance = form.save()

    assert not instance.static_users.all()
예제 #19
0
def test_matched_user_count_added_to_segment_at_creation(
        site, mocker, django_user_model):
    django_user_model.objects.create(username='******')
    django_user_model.objects.create(username='******')

    segment = SegmentFactory.build(type=Segment.TYPE_STATIC)
    rule = VisitCountRule()

    form = form_with_data(segment, rule)
    form.instance.type = Segment.TYPE_STATIC
    mock_test_user = mocker.patch(
        'wagtail_personalisation.rules.VisitCountRule.test_user',
        return_value=True)
    instance = form.save()

    assert mock_test_user.call_count == 2
    instance.matched_users_count = 2
예제 #20
0
def test_not_in_static_segment_if_random_is_above_percentage(
        site, client, mocker, user):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC,
                                   count=1,
                                   randomisation_percent=40)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()

    mocker.patch('random.randint', return_value=41)
    session = client.session
    session.save()
    client.force_login(user)
    client.get(site.root_page.url)

    assert len(client.session['segments']) == 0
    assert user not in instance.static_users.all()
    assert user in instance.excluded_users.all()
예제 #21
0
def test_rules_check_skipped_if_dynamic_segment_in_excluded(
        site, client, mocker, user):
    segment = SegmentFactory.build(type=Segment.TYPE_DYNAMIC,
                                   randomisation_percent=100)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()
    instance.persistent = True
    instance.save()

    session = client.session
    session['excluded_segments'] = [{'id': instance.pk}]
    session.save()

    mock_test_rule = mocker.patch(
        'wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules')

    client.force_login(user)
    client.get(site.root_page.url)

    assert mock_test_rule.call_count == 0
    assert len(client.session['segments']) == 0
예제 #22
0
def test_rules_check_skipped_if_user_in_excluded(site, client, mocker, user):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC,
                                   count=1,
                                   randomisation_percent=100)
    rule = VisitCountRule(counted_page=site.root_page)
    form = form_with_data(segment, rule)
    instance = form.save()
    instance.excluded_users.add(user)
    instance.save

    mock_test_rule = mocker.patch(
        'wagtail_personalisation.adapters.SessionSegmentsAdapter._test_rules')

    session = client.session
    session.save()
    client.force_login(user)
    client.get(site.root_page.url)

    assert mock_test_rule.call_count == 0
    assert len(client.session['segments']) == 0
    assert user not in instance.static_users.all()
    assert user in instance.excluded_users.all()
예제 #23
0
def test_static_segment_with_static_rules_needs_no_count(site):
    segment = SegmentFactory.build(type=Segment.TYPE_STATIC, count=0)
    rule = VisitCountRule(counted_page=site.root_page, segment=segment)
    form = form_with_data(segment, rule)
    assert form.is_valid()
def test_call_test_user_on_invalid_rule_fails(site, user, mocker):
    rule = VisitCountRule()
    assert not (rule.test_user(None, user))