"""A row of Manage holdings saving itself, in a real browser. Fields save as they change. A refused one used to say only "not saved"; the row now says why, and keeps saying it until the next save. """ from __future__ import annotations import json import re import subprocess import pytest from test_trade_dialog_in_the_browser import CHROME, HTML, _standalone import factories as fac from test_routes import bind_to_only_portfolio, make_login pytestmark = pytest.mark.visual RULE = "A currency is a three-letter code, like AUD or USD." SERVER = """ """ % RULE DRIVER = """ """ def test_a_refused_field_says_why(client, session_factory, tmp_path): make_login(client, session_factory) with session_factory() as s: bind_to_only_portfolio(s) fac.hold(s, fac.make_instrument(s, "ACME")) s.commit() page = client.get("/holdings", headers=HTML) target = tmp_path / "holdings.html" target.write_text(_standalone(page.text, DRIVER).replace("", "" + SERVER, 1), encoding="utf-8") dom = subprocess.run( [CHROME, "--headless", "--disable-gpu", "--dump-dom", "--no-sandbox", "--allow-file-access-from-files", "--window-size=1400,1000", "--virtual-time-budget=10000", target.as_uri()], capture_output=True, text=True, timeout=90).stdout found = re.search(r'
(.*?)
', dom, re.S) assert found, "the driver did not finish" out = json.loads(found.group(1).replace(""", '"')) assert out == {"refused": RULE, "later": RULE, "saved": "saved"} # Every POST the page makes, and a confirm that answers as the walk says. ASKING = """ """ ASKED = """ """ def test_a_new_currency_that_reaches_other_portfolios_asks_first(client, session_factory, tmp_path): """Declined, nothing is saved and the old currency is back in the field. Accepted, it saves; and once saved, a later edit of the row asks nothing.""" from test_holdings_edit import _setup _setup(client, session_factory, admin=True) page = client.get("/holdings", headers=HTML) target = tmp_path / "holdings.html" target.write_text(_standalone(page.text, ASKED).replace("", "" + ASKING, 1), encoding="utf-8") dom = subprocess.run( [CHROME, "--headless", "--disable-gpu", "--dump-dom", "--no-sandbox", "--allow-file-access-from-files", "--window-size=1400,1000", "--virtual-time-budget=10000", target.as_uri()], capture_output=True, text=True, timeout=90).stdout found = re.search(r'
(.*?)
', dom, re.S) assert found, "the driver did not finish" out = json.loads(found.group(1).replace(""", '"')) assert out == {"declined": {"posts": [], "value": "AUD"}, "accepted": ["USD"], "asked": 2}