diff --git a/datasette/views/table.py b/datasette/views/table.py index 99075abe..d80c9431 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -508,7 +508,7 @@ class TableView(RowTableShared): ) facet_results[column] = [] for row in facet_rows: - selected = other_args.get(column) == row["value"] + selected = str(other_args.get(column)) == str(row["value"]) if selected: toggle_path = path_with_removed_args( request, {column: row["value"]} diff --git a/tests/fixtures.py b/tests/fixtures.py index 2f2c32ff..99d686e4 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -269,25 +269,27 @@ INSERT INTO [select] VALUES ('group', 'having', 'and'); CREATE TABLE facetable ( pk integer primary key, + planet_id integer, state text, city text, neighborhood text ); -INSERT INTO facetable (state, city, neighborhood) VALUES - ('CA', 'San Francisco', 'Mission'), - ('CA', 'San Francisco', 'Dogpatch'), - ('CA', 'San Francisco', 'SOMA'), - ('CA', 'San Francisco', 'Tenderloin'), - ('CA', 'San Francisco', 'Bernal Heights'), - ('CA', 'San Francisco', 'Hayes Valley'), - ('CA', 'Los Angeles', 'Hollywood'), - ('CA', 'Los Angeles', 'Downtown'), - ('CA', 'Los Angeles', 'Los Feliz'), - ('CA', 'Los Angeles', 'Koreatown'), - ('MI', 'Detroit', 'Downtown'), - ('MI', 'Detroit', 'Greektown'), - ('MI', 'Detroit', 'Corktown'), - ('MI', 'Detroit', 'Mexicantown') +INSERT INTO facetable (planet_id, state, city, neighborhood) VALUES + (1, 'CA', 'San Francisco', 'Mission'), + (1, 'CA', 'San Francisco', 'Dogpatch'), + (1, 'CA', 'San Francisco', 'SOMA'), + (1, 'CA', 'San Francisco', 'Tenderloin'), + (1, 'CA', 'San Francisco', 'Bernal Heights'), + (1, 'CA', 'San Francisco', 'Hayes Valley'), + (1, 'CA', 'Los Angeles', 'Hollywood'), + (1, 'CA', 'Los Angeles', 'Downtown'), + (1, 'CA', 'Los Angeles', 'Los Feliz'), + (1, 'CA', 'Los Angeles', 'Koreatown'), + (1, 'MI', 'Detroit', 'Downtown'), + (1, 'MI', 'Detroit', 'Greektown'), + (1, 'MI', 'Detroit', 'Corktown'), + (1, 'MI', 'Detroit', 'Mexicantown'), + (2, 'MC', 'Memnonia', 'Arcadia Planitia') ; INSERT INTO simple_primary_key VALUES (1, 'hello'); diff --git a/tests/test_api.py b/tests/test_api.py index da8c177c..45b4b43c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -105,9 +105,9 @@ def test_database_page(app_client): 'fts_table': None, 'primary_keys': ['pk'], }, { - 'columns': ['pk', 'state', 'city', 'neighborhood'], + 'columns': ['pk', 'planet_id', 'state', 'city', 'neighborhood'], 'name': 'facetable', - 'count': 14, + 'count': 15, 'foreign_keys': {'incoming': [], 'outgoing': []}, 'fts_table': None, 'hidden': False, @@ -906,6 +906,12 @@ def test_page_size_matching_max_returned_rows(app_client_returend_rows_matches_p "selected": False, "toggle_url": "_facet=state&_facet=city&state=MI", }, + { + "value": "MC", + "count": 1, + "selected": False, + "toggle_url": "_facet=state&_facet=city&state=MC", + }, ], "city": [ { @@ -926,6 +932,12 @@ def test_page_size_matching_max_returned_rows(app_client_returend_rows_matches_p "selected": False, "toggle_url": "_facet=state&_facet=city&city=Los+Angeles", }, + { + "value": "Memnonia", + "count": 1, + "selected": False, + "toggle_url": "_facet=state&_facet=city&city=Memnonia", + }, ], }, ), ( @@ -948,6 +960,37 @@ def test_page_size_matching_max_returned_rows(app_client_returend_rows_matches_p }, ], }, + ), ( + "/test_tables/facetable.json?_facet=planet_id", + { + "planet_id": [ + { + "value": 1, + "count": 14, + "selected": False, + "toggle_url": "_facet=planet_id&planet_id=1", + }, + { + "value": 2, + "count": 1, + "selected": False, + "toggle_url": "_facet=planet_id&planet_id=2", + }, + ], + }, + ), ( + # planet_id is an integer field: + "/test_tables/facetable.json?_facet=planet_id&planet_id=1", + { + "planet_id": [ + { + "value": 1, + "count": 14, + "selected": True, + "toggle_url": "_facet=planet_id", + } + ], + }, ) ]) def test_facets(app_client, path, expected_facet_results):