Skip to content

Commit

Permalink
Fixed sort orders for timeline view, closes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 17, 2020
1 parent 60be561 commit 7c5f162
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
14 changes: 7 additions & 7 deletions dogsheep_beta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
limit 100
"""
FILTER_COLS = ("type", "category", "is_public")
SORT_ORDERS = {
"oldest": "search_index.timestamp",
"newest": "search_index.timestamp desc",
}


async def beta(request, datasette):
Expand All @@ -53,11 +57,13 @@ async def beta(request, datasette):
dogsheep_beta_config_file = config["config_file"]
rules = parse_metadata(open(dogsheep_beta_config_file).read())
q = request.args.get("q") or ""
sorted_by = "relevance"
sorted_by = "relevance" if q else "newest"
if request.args.get("sort") in SORT_ORDERS:
sorted_by = request.args["sort"]
other_sort_orders = []
for sort_order in ("relevance", "newest", "oldest"):
if not q and sort_order == "relevance":
continue
if sort_order != sorted_by:
other_sort_orders.append(
{
Expand Down Expand Up @@ -97,12 +103,6 @@ async def beta(request, datasette):
)


SORT_ORDERS = {
"oldest": "search_index.timestamp",
"newest": "search_index.timestamp desc",
}


async def search(datasette, database_name, request):
from datasette.utils import sqlite3, escape_fts

Expand Down
37 changes: 37 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,43 @@ async def test_search_order(ds, sort, expected):
assert ">{}</a>".format(sort_order) in response.text


ALL_EXPECTED = [
"github.db/commits:5becbf70d64951e2910314ef5227d19b11c25b0c9586934941366da8997e57cb",
"emails.db/emails:2",
"github.db/commits:a5b39c5049b28997528bb0eca52730ab6febabeaba54cfcba0ab5d70e7207523",
"emails.db/emails:1",
]


@pytest.mark.asyncio
@pytest.mark.parametrize(
"sort,expected",
(
("", ALL_EXPECTED),
("newest", ALL_EXPECTED),
("oldest", list(reversed(ALL_EXPECTED))),
),
)
async def test_search_order_for_timeline(ds, sort, expected):
async with httpx.AsyncClient(app=ds.app()) as client:
url = "http://localhost/-/beta"
if sort:
url += "?sort=" + sort
response = await client.get(url)
assert response.status_code == 200
soup = Soup(response.text, "html5lib")
results = [el["data-table-key"] for el in soup.select("[data-table-key]")]
assert results == expected
# Check that sort links exist and are correct
sort_label = sort or "newest"
assert "<strong>{}</strong>".format(sort_label) in response.text
assert ">relevance</a>" not in response.text
assert (
">{}</a>".format("oldest" if sort_label == "newest" else "newest")
in response.text
)


@pytest.mark.asyncio
async def test_fixture(ds):
async with httpx.AsyncClient(app=ds.app()) as client:
Expand Down

0 comments on commit 7c5f162

Please sign in to comment.