diff --git a/setup.py b/setup.py index 03fab46..7716db3 100644 --- a/setup.py +++ b/setup.py @@ -39,5 +39,8 @@ def get_long_description(): ] }, install_requires=["datasette>=0.54", "datasette-leaflet>=0.2.2"], - extras_require={"test": ["pytest", "pytest-asyncio", "httpx", "sqlite-utils"]}, + extras_require={ + "test": ["pytest", "pytest-asyncio", "httpx", "sqlite-utils", "nest-asyncio"], + "playwright": ["pytest-playwright"], + }, ) diff --git a/tests/conftest.py b/tests/conftest.py index c1c3c47..f17c9f2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,67 @@ import datasette +import pytest +import sqlite3 +from subprocess import Popen, PIPE +import sys +import time +import httpx def pytest_report_header(): return "Datasette: {}".format(datasette.__version__) + + +@pytest.fixture(scope="session") +def ds_server(tmp_path_factory): + tmpdir = tmp_path_factory.mktemp("tmp") + db_path = str(tmpdir / "data.db") + db = sqlite3.connect(db_path) + for latitude, longitude in ( + ("latitude", "longitude"), + ("lat", "lon"), + ("lat", "lng"), + ("lat", "long"), + ("foo_latitude", "foo_longitude"), + ): + with db: + db.execute( + f""" + create table {latitude}_{longitude} ( + id integer primary key, + {latitude} float, + {longitude} float + ) + """ + ) + db.execute( + f""" + insert into {latitude}_{longitude} ({latitude}, {longitude}) + values (37.0167, -122.0024), (37.3184, -121.9511) + """ + ) + process = Popen( + [ + sys.executable, + "-m", + "datasette", + "--port", + "8126", + str(db_path), + ], + stdout=PIPE, + ) + wait_until_responds("http://localhost:8126/") + yield "http://localhost:8126" + process.terminate() + process.wait() + + +def wait_until_responds(url, timeout=5.0): + start = time.time() + while time.time() - start < timeout: + try: + httpx.get(url) + return + except httpx.ConnectError: + time.sleep(0.1) + raise AssertionError("Timed out waiting for {} to respond".format(url)) diff --git a/tests/test_playwright.py b/tests/test_playwright.py new file mode 100644 index 0000000..689807a --- /dev/null +++ b/tests/test_playwright.py @@ -0,0 +1,29 @@ +import pytest + +try: + from playwright import sync_api +except ImportError: + sync_api = None +import pytest +import nest_asyncio + +nest_asyncio.apply() + +pytestmark = pytest.mark.skipif(sync_api is None, reason="playwright not installed") + + +@pytest.mark.parametrize( + "table", + ( + "foo_latitude_foo_longitude", + "lat_lng", + "lat_lon", + "lat_long", + "latitude_longitude", + ), +) +def test_blah(ds_server, table, page): + page.goto(ds_server + "/data/" + table) + # There should be two leaflet-marker-icons + page.wait_for_selector(".leaflet-marker-icon", timeout=15000) + assert len(page.query_selector_all(".leaflet-marker-icon")) == 2