111 lines
5.0 KiB
Python
111 lines
5.0 KiB
Python
"""Tests du bornage automatique des champs numériques (2.2)."""
|
|
import json
|
|
import re
|
|
|
|
|
|
def _create_screen(client, slug, name="Accueil"):
|
|
resp = client.post(f"/game/{slug}/screens/new", data={"name": name}, follow_redirects=False)
|
|
return int(re.search(r"/screens/(\d+)/edit", resp.headers["Location"]).group(1))
|
|
|
|
|
|
def _create_numeric_object(client, slug, field_name="reputation", min_v="0", max_v="100", start="50"):
|
|
resp = client.post(f"/game/{slug}/objects/new", data={
|
|
"object_name": "Partie", "field_name[]": [field_name], "field_type[]": ["nombre_entier"],
|
|
"field_relation[]": [""], "field_required[]": ["0"],
|
|
"field_min[]": [min_v], "field_max[]": [max_v],
|
|
}, follow_redirects=False)
|
|
def_id = int(resp.headers["Location"].rstrip("/").split("/")[-1])
|
|
resp = client.post(f"/game/{slug}/objects/{def_id}/data/new", data={field_name: start})
|
|
return def_id
|
|
|
|
|
|
def _get_row_value(client, slug, def_id, field_name):
|
|
import db
|
|
definition = db.get_definition(slug, def_id)
|
|
rows = db.list_rows(slug, definition)
|
|
return rows[0].get(db.slugify(field_name).replace("-", "_"))
|
|
|
|
|
|
def test_field_bounds_are_stored_at_creation(client, game):
|
|
def_id = _create_numeric_object(client, game, "reputation", "0", "100", "50")
|
|
import db
|
|
definition = db.get_definition(game, def_id)
|
|
field = definition["fields"][0]
|
|
assert field["min_value"] == 0
|
|
assert field["max_value"] == 100
|
|
|
|
|
|
def test_incrementer_clamps_to_max(client, game):
|
|
"""Régression 2.2 : une action "Augmenter de..." ne doit jamais faire
|
|
dépasser la borne maxi réglée sur le champ, même si le montant ajouté
|
|
dépasse largement ce qu'il reste avant la limite."""
|
|
def_id = _create_numeric_object(client, game, "reputation", "0", "100", "95")
|
|
row_value = _get_row_value(client, game, def_id, "reputation")
|
|
|
|
screen_id = _create_screen(client, game)
|
|
resp = client.post(f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({
|
|
"node_type": "action", "action_type": "modifier_donnee",
|
|
"target_definition_id": def_id, "target_row_id": 1, "target_field": "reputation",
|
|
"data_operation": "incrementer", "data_value": "50",
|
|
}), content_type="application/json")
|
|
node = resp.get_json()
|
|
|
|
resp = client.post(f"/game/{game}/flow/nodes/{node['id']}/run-data")
|
|
assert resp.get_json() == {"ok": True}
|
|
|
|
new_value = _get_row_value(client, game, def_id, "reputation")
|
|
assert new_value == 100, f"la valeur aurait dû être bornée à 100, vaut {new_value}"
|
|
|
|
|
|
def test_decrementer_clamps_to_min(client, game):
|
|
"""Régression 2.2 : symétrique pour "Diminuer de..." et la borne mini."""
|
|
def_id = _create_numeric_object(client, game, "reputation", "0", "100", "5")
|
|
|
|
screen_id = _create_screen(client, game)
|
|
resp = client.post(f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({
|
|
"node_type": "action", "action_type": "modifier_donnee",
|
|
"target_definition_id": def_id, "target_row_id": 1, "target_field": "reputation",
|
|
"data_operation": "decrementer", "data_value": "50",
|
|
}), content_type="application/json")
|
|
node = resp.get_json()
|
|
|
|
client.post(f"/game/{game}/flow/nodes/{node['id']}/run-data")
|
|
new_value = _get_row_value(client, game, def_id, "reputation")
|
|
assert new_value == 0, f"la valeur aurait dû être bornée à 0, vaut {new_value}"
|
|
|
|
|
|
def test_definir_nombre_also_clamped(client, game):
|
|
"""Régression 2.2 : "Donner cette valeur (nombre)" doit aussi être
|
|
bornée, pas seulement les incréments/décréments."""
|
|
def_id = _create_numeric_object(client, game, "reputation", "0", "100", "50")
|
|
|
|
screen_id = _create_screen(client, game)
|
|
resp = client.post(f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({
|
|
"node_type": "action", "action_type": "modifier_donnee",
|
|
"target_definition_id": def_id, "target_row_id": 1, "target_field": "reputation",
|
|
"data_operation": "definir_nombre", "data_value": "9999",
|
|
}), content_type="application/json")
|
|
node = resp.get_json()
|
|
|
|
client.post(f"/game/{game}/flow/nodes/{node['id']}/run-data")
|
|
new_value = _get_row_value(client, game, def_id, "reputation")
|
|
assert new_value == 100
|
|
|
|
|
|
def test_no_bounds_set_means_unlimited(client, game):
|
|
"""Sans bornes réglées (champ laissé vide), le comportement doit rester
|
|
celui d'avant 2.2 : aucune limite appliquée."""
|
|
def_id = _create_numeric_object(client, game, "reputation", "", "", "50")
|
|
|
|
screen_id = _create_screen(client, game)
|
|
resp = client.post(f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({
|
|
"node_type": "action", "action_type": "modifier_donnee",
|
|
"target_definition_id": def_id, "target_row_id": 1, "target_field": "reputation",
|
|
"data_operation": "incrementer", "data_value": "500",
|
|
}), content_type="application/json")
|
|
node = resp.get_json()
|
|
|
|
client.post(f"/game/{game}/flow/nodes/{node['id']}/run-data")
|
|
new_value = _get_row_value(client, game, def_id, "reputation")
|
|
assert new_value == 550
|