Skip to content

Commit

Permalink
Fix plus tests for Datasette 1 error, refs #19
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 8, 2024
1 parent 3ecd8e6 commit 502ac1e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 6 additions & 3 deletions datasette_search_all/templates/search_all.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ <h1>Search all tables</h1>
var rows = data.rows;
var database = data.database;
var table = data.table;
var columns = data.columns;
var count = data.filtered_table_rows_count;
var columns = [];
if (data.rows.length) {
columns = Object.keys(data.rows[0]);
}
var count = data.filtered_table_rows_count || data.count;
var ths = safe(columns.map(c => autoescape`<th>${c}</th>`).join(""));
var tr_rows = safe(rows.map(row => autoescape`<tr>${safe(columns.map(column => autoescape`<td>${displayCell(row[column])}</td>`).join(""))}</tr>`).join(""));
var view_more = '';
Expand Down Expand Up @@ -159,7 +162,7 @@ <h2><a href="${search_url}">${Number(count).toLocaleString()} result${count == 1
var base_url = item.url;
var li = document.querySelector(`[data-searchable-url="${base_url}"]`);
li.classList.add('search-all-loading');
var json_url = `${item.url_json}?_shape=objects&_labels=on&_size=${NUM_RESULTS}&_search=${encodeURIComponent(q.value)}`;
var json_url = `${item.url_json}?_shape=objects&_labels=on&_extra=count&_extra=database&_extra=table&_size=${NUM_RESULTS}&_search=${encodeURIComponent(q.value)}`;
fetch(json_url).then(r => r.json()).then((data) => {
li.style.display = 'none';
displayResults(data, base_url);
Expand Down
22 changes: 21 additions & 1 deletion tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
sync_api = None
import pytest

pytestmark = pytest.mark.skipif(sync_api is None, reason="playwright not installed")


@pytest.mark.skipif(sync_api is None, reason="playwright not installed")
def test_ds_server(ds_server):
with sync_api.sync_playwright() as playwright:
browser = playwright.chromium.launch()
Expand All @@ -14,3 +15,22 @@ def test_ds_server(ds_server):
assert page.title() == "Datasette: data"
# It should have a search form
assert page.query_selector('form[action="/-/search"]')


def test_search(ds_server):
with sync_api.sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto(ds_server + "/-/search?q=cleo")
# Should show search results, after fetching them
assert page.locator("table tr th:nth-child(1)").inner_text() == "rowid"
assert page.locator("table tr th:nth-child(2)").inner_text() == "name"
assert page.locator("table tr th:nth-child(3)").inner_text() == "description"
assert page.locator("table tr:nth-child(2) td:nth-child(1)").inner_text() == "1"
assert (
page.locator("table tr:nth-child(2) td:nth-child(2)").inner_text() == "Cleo"
)
assert (
page.locator("table tr:nth-child(2) td:nth-child(3)").inner_text()
== "A medium sized dog"
)

0 comments on commit 502ac1e

Please sign in to comment.