Skip to content

Commit

Permalink
Initial Playwright test, refs #43
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 13, 2024
1 parent 5308a54 commit a6e1c6c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
)
62 changes: 62 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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))
29 changes: 29 additions & 0 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a6e1c6c

Please sign in to comment.