Check-in rules: Make logic results understandable (#2050)

Co-authored-by: Richard Schreiber <schreiber@rami.io>
This commit is contained in:
Raphael Michel
2021-05-07 09:45:18 +02:00
committed by GitHub
parent fb8ddc9cb6
commit b5fdba796b
7 changed files with 522 additions and 62 deletions

View File

@@ -413,6 +413,7 @@ def test_rules_product(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Ticket type not allowed' in str(excinfo.value)
clist.rules = {
"inList": [
@@ -449,6 +450,7 @@ def test_rules_variation(item, position, clist):
perform_checkin(position, clist, {})
assert not OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
assert excinfo.value.code == 'rules'
assert 'Ticket type not allowed' in str(excinfo.value)
clist.rules = {
"inList": [
@@ -481,6 +483,7 @@ def test_rules_scan_number(position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Maximum number of entries' in str(excinfo.value)
@pytest.mark.django_db
@@ -500,12 +503,14 @@ def test_rules_scan_today(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Maximum number of entries today' in str(excinfo.value)
with freeze_time("2020-01-01 22:50:00"):
assert not OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Maximum number of entries today' in str(excinfo.value)
with freeze_time("2020-01-01 23:10:00"):
assert OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
@@ -516,6 +521,7 @@ def test_rules_scan_today(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Maximum number of entries today' in str(excinfo.value)
@pytest.mark.django_db
@@ -547,6 +553,7 @@ def test_rules_scan_days(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Maximum number of days with an entry exceeded.' in str(excinfo.value)
@pytest.mark.django_db
@@ -562,6 +569,7 @@ def test_rules_time_isafter_tolerance(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed after 11:50' in str(excinfo.value)
with freeze_time("2020-01-01 10:51:00"):
assert OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
@@ -582,6 +590,7 @@ def test_rules_time_isafter_no_tolerance(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed after 12:00' in str(excinfo.value)
with freeze_time("2020-01-01 11:01:00"):
assert OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
@@ -601,6 +610,7 @@ def test_rules_time_isbefore_with_tolerance(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed before 12:10' in str(excinfo.value)
with freeze_time("2020-01-01 11:09:00"):
assert OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
@@ -618,6 +628,7 @@ def test_rules_time_isafter_custom_time(event, position, clist):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed after 23:00' in str(excinfo.value)
with freeze_time("2020-01-01 22:05:00"):
assert OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
@@ -639,12 +650,108 @@ def test_rules_isafter_subevent(position, clist, event):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed after 12:00' in str(excinfo.value)
with freeze_time("2020-02-01 11:01:00"):
assert OrderPosition.objects.filter(SQLLogic(clist).apply(clist.rules), pk=position.pk).exists()
perform_checkin(position, clist, {})
@pytest.mark.django_db
def test_rules_reasoning_prefer_close_date(event, position, clist):
# Ticket is valid starting at a custom time
event.settings.timezone = 'Europe/Berlin'
clist.rules = {
"or": [
{
"and": [
{"isAfter": [{"var": "now"}, {"buildTime": ["custom", "2020-01-01T10:00:00.000Z"]}, None]},
{"isBefore": [{"var": "now"}, {"buildTime": ["custom", "2020-01-01T18:00:00.000Z"]}, None]},
]
},
{
"and": [
{"isAfter": [{"var": "now"}, {"buildTime": ["custom", "2020-01-02T10:00:00.000Z"]}, None]},
{"isBefore": [{"var": "now"}, {"buildTime": ["custom", "2020-01-02T18:00:00.000Z"]}, None]},
]
},
]
}
clist.save()
with freeze_time("2020-01-01 09:00:00Z"):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed after 11:00' in str(excinfo.value)
with freeze_time("2020-01-01 20:00:00Z"):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed before 19:00' in str(excinfo.value)
with freeze_time("2020-01-02 09:00:00Z"):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed after 11:00' in str(excinfo.value)
with freeze_time("2020-01-03 18:00:00Z"):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed before 2020-01-02 19:00' in str(excinfo.value)
@pytest.mark.django_db
def test_rules_reasoning_prefer_date_over_product(event, position, clist):
i2 = event.items.create(name="Ticket", default_price=3, admission=True)
clist.rules = {
"or": [
{
"inList": [
{"var": "product"}, {
"objectList": [
{"lookup": ["product", str(i2.pk), "Ticket"]},
]
}
]
},
{
"and": [
{"isAfter": [{"var": "now"}, {"buildTime": ["custom", "2020-01-02T10:00:00.000Z"]}, None]},
{"isBefore": [{"var": "now"}, {"buildTime": ["custom", "2020-01-02T18:00:00.000Z"]}, None]},
]
}
]
}
clist.save()
with freeze_time("2020-01-02 20:00:00Z"):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Only allowed before 19:00' in str(excinfo.value)
@pytest.mark.django_db
def test_rules_reasoning_prefer_number_over_date(event, position, clist):
clist.rules = {
"and": [
{"isAfter": [{"var": "now"}, {"buildTime": ["custom", "2020-01-02T10:00:00.000Z"]}, None]},
{"isBefore": [{"var": "now"}, {"buildTime": ["custom", "2020-01-02T18:00:00.000Z"]}, None]},
{">": [{"var": "entries_today"}, 3]}
]
}
clist.save()
with freeze_time("2020-01-01 20:00:00Z"):
with pytest.raises(CheckInError) as excinfo:
perform_checkin(position, clist, {})
assert excinfo.value.code == 'rules'
assert 'Minimum number of entries today exceeded' in str(excinfo.value)
@pytest.mark.django_db(transaction=True)
def test_position_queries(django_assert_num_queries, position, clist):
with django_assert_num_queries(11) as captured:

View File

@@ -0,0 +1,80 @@
#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io GmbH and contributors
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
# Public License as published by the Free Software Foundation in version 3 of the License.
#
# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are
# applicable granting you additional permissions and placing additional restrictions on your usage of this software.
# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive
# this file, see <https://pretix.eu/about/en/license>.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
import pytest
from pretix.helpers.jsonlogic_boolalg import convert_to_dnf
params = [
(
{"and": [{"var": "a"}, {"eq": [{"var": "a"}, 3]}]},
{"and": [{"var": "a"}, {"eq": [{"var": "a"}, 3]}]},
),
(
{"or": [{"var": "a"}, {"eq": [{"var": "a"}, 3]}]},
{"or": [{"var": "a"}, {"eq": [{"var": "a"}, 3]}]},
),
(
{"and": [{"or": ["a", "b"]}, 3]},
{"or": [{"and": [3, "a"]}, {"and": [3, "b"]}]},
),
(
{"and": [{"or": ["a", "b"]}, {"or": ["c", "d"]}]},
{"or": [{"and": ["a", "c"]}, {"and": ["a", "d"]}, {"and": ["b", "c"]}, {"and": ["b", "d"]}]},
),
(
{"and": [{"or": ["a", {"and": ["e", "f"]}]}, {"or": ["c", "d"]}]},
{"or": [{"and": ["a", "c"]}, {"and": ["a", "d"]}, {"and": ["e", "f", "c"]}, {"and": ["e", "f", "d"]}]},
),
(
{"and": [{"or": ["a", {"and": ["e", {"or": ["f", "g"]}]}]}, {"or": ["c", "d"]}]},
{"or": [{"and": ["a", "c"]}, {"and": ["a", "d"]}, {"and": ["c", "e", "f"]}, {"and": ["c", "e", "g"]},
{"and": ["d", "e", "f"]}, {"and": ["d", "e", "g"]}]},
),
(
{"and": [{"or": ["a", {"and": ["e", {"or": ["f", {"and": ["g", "h"]}]}]}]}, {"or": ["c", "d"]}]},
{"or": [{"and": ["a", "c"]}, {"and": ["a", "d"]}, {"and": ["c", "e", "f"]}, {"and": ["c", "e", "g", "h"]},
{"and": ["d", "e", "f"]}, {"and": ["d", "e", "g", "h"]}]},
),
]
def compare_ignoring_order(data1, data2):
if isinstance(data1, list) and isinstance(data2, list):
try:
assert set(data1) == set(data2)
except:
print(data1, data2)
assert len(data1) == len(data2) and all(data1.count(i) == data2.count(i) for i in data1)
elif isinstance(data1, dict) and isinstance(data2, dict):
assert set(data1.keys()) == set(data2.keys())
compare_ignoring_order(list(data1.values()), list(data2.values()))
else:
assert data1 == data2
@pytest.mark.parametrize("logic,expected", params)
def test_convert_to_dnf(logic, expected):
print("orig", logic)
print("resu", convert_to_dnf(logic))
print("expe", expected)
assert convert_to_dnf(logic) == expected