From 7a7e4b2ed8c76c6d002a9d707dbc840f6a2abf7f Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 9 Dec 2017 15:32:54 -0800 Subject: [PATCH] Started unit tests for row/table HTML pages Refs #167 Thanks to the new tests, spotted and fixed a bug where pages that were supposed to have 100 things on them were actually displaying 101. --- datasette/app.py | 1 + setup.py | 1 + tests/test_app.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/datasette/app.py b/datasette/app.py index 3067b60f..38d8542e 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -672,6 +672,7 @@ class TableView(RowTableShared): next_url = urllib.parse.urljoin(request.url, path_with_added_args(request, { '_next': next_value, })) + rows = rows[:self.page_size] # Number of filtered rows in whole set: filtered_table_rows = None diff --git a/setup.py b/setup.py index 2830833e..1b187a70 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,7 @@ setup( tests_require=[ 'pytest==3.2.1', 'aiohttp==2.3.2', + 'beautifulsoup4==4.6.0', ], classifiers=[ 'Development Status :: 3 - Alpha', diff --git a/tests/test_app.py b/tests/test_app.py index a2a6d9cc..b4004652 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,3 +1,4 @@ +from bs4 import BeautifulSoup as Soup from datasette.app import Datasette import os import pytest @@ -89,7 +90,7 @@ def test_database_page(app_client): }, { 'columns': ['pk1', 'pk2', 'content'], 'name': 'compound_primary_key', - 'count': 0, + 'count': 1, 'hidden': False, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'label_column': None, @@ -441,6 +442,75 @@ def test_css_classes_on_body(app_client, path, expected_classes): assert classes == expected_classes +def test_table_html_simple_primary_key(app_client): + response = app_client.get('/test_tables/simple_primary_key', gather_request=False) + table = Soup(response.body, 'html.parser').find('table') + assert [ + 'Link', 'pk', 'content' + ] == [th.string for th in table.select('thead th')] + assert [ + [ + '1', + '1', + 'hello' + ], [ + '2', + '2', + 'world' + ], [ + '3', + '3', + '' + ] + ] == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')] + + +def test_row_html_simple_primary_key(app_client): + response = app_client.get('/test_tables/simple_primary_key/1', gather_request=False) + table = Soup(response.body, 'html.parser').find('table') + assert [ + 'pk', 'content' + ] == [th.string for th in table.select('thead th')] + assert [ + [ + '1', + 'hello' + ] + ] == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')] + + +def test_table_html_no_primary_key(app_client): + response = app_client.get('/test_tables/no_primary_key', gather_request=False) + table = Soup(response.body, 'html.parser').find('table') + assert [ + 'rowid', 'content' + ] == [th.string for th in table.select('thead th')] + expected = [ + [ + '{}'.format(i, i), + '{}'.format(i), + ] for i in range(1, 51) + ] + assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')] + + +def test_table_html_compound_primary_key(app_client): + response = app_client.get('/test_tables/compound_primary_key', gather_request=False) + table = Soup(response.body, 'html.parser').find('table') + assert [ + 'Link', 'pk1', 'pk2', 'content' + ] == [th.string for th in table.select('thead th')] + expected = [ + [ + 'a,b', + 'a', + 'b', + 'c', + ] + ] + assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')] + + TABLES = ''' CREATE TABLE simple_primary_key ( pk varchar(30) primary key, @@ -454,6 +524,8 @@ CREATE TABLE compound_primary_key ( PRIMARY KEY (pk1, pk2) ); +INSERT INTO compound_primary_key VALUES ('a', 'b', 'c'); + CREATE TABLE no_primary_key ( content text );