Correctly display facets with value of 0 - fixes #318

Also added comprehensive unit test for facet display HTML.
pull/322/head^2
Simon Willison 2018-06-20 21:30:13 -07:00
rodzic fdfbbbb9ee
commit aeeb50f61b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 17E2DEA2588B7F52
5 zmienionych plików z 122 dodań i 34 usunięć

Wyświetl plik

@ -117,7 +117,7 @@
<ul> <ul>
{% for facet_value in facet_info.results %} {% for facet_value in facet_info.results %}
{% if not facet_value.selected %} {% if not facet_value.selected %}
<li><a href="{{ facet_value.toggle_url }}">{{ facet_value.label or "_" }}</a> {{ "{:,}".format(facet_value.count) }}</li> <li><a href="{{ facet_value.toggle_url }}">{{ (facet_value.label if facet_value.label is not none else "_") }}</a> {{ "{:,}".format(facet_value.count) }}</li>
{% else %} {% else %}
<li>{{ facet_value.label }} &middot; {{ "{:,}".format(facet_value.count) }} <a href="{{ facet_value.toggle_url }}" class="cross">&#x2716;</a></li> <li>{{ facet_value.label }} &middot; {{ "{:,}".format(facet_value.count) }} <a href="{{ facet_value.toggle_url }}" class="cross">&#x2716;</a></li>
{% endif %} {% endif %}

Wyświetl plik

@ -332,27 +332,30 @@ INSERT INTO facet_cities (id, name) VALUES
CREATE TABLE facetable ( CREATE TABLE facetable (
pk integer primary key, pk integer primary key,
planet_int integer, planet_int integer,
on_earth integer,
state text, state text,
city_id integer, city_id integer,
neighborhood text, neighborhood text,
FOREIGN KEY ("city_id") REFERENCES [facet_cities](id) FOREIGN KEY ("city_id") REFERENCES [facet_cities](id)
); );
INSERT INTO facetable (planet_int, state, city_id, neighborhood) VALUES INSERT INTO facetable
(1, 'CA', 1, 'Mission'), (planet_int, on_earth, state, city_id, neighborhood)
(1, 'CA', 1, 'Dogpatch'), VALUES
(1, 'CA', 1, 'SOMA'), (1, 1, 'CA', 1, 'Mission'),
(1, 'CA', 1, 'Tenderloin'), (1, 1, 'CA', 1, 'Dogpatch'),
(1, 'CA', 1, 'Bernal Heights'), (1, 1, 'CA', 1, 'SOMA'),
(1, 'CA', 1, 'Hayes Valley'), (1, 1, 'CA', 1, 'Tenderloin'),
(1, 'CA', 2, 'Hollywood'), (1, 1, 'CA', 1, 'Bernal Heights'),
(1, 'CA', 2, 'Downtown'), (1, 1, 'CA', 1, 'Hayes Valley'),
(1, 'CA', 2, 'Los Feliz'), (1, 1, 'CA', 2, 'Hollywood'),
(1, 'CA', 2, 'Koreatown'), (1, 1, 'CA', 2, 'Downtown'),
(1, 'MI', 3, 'Downtown'), (1, 1, 'CA', 2, 'Los Feliz'),
(1, 'MI', 3, 'Greektown'), (1, 1, 'CA', 2, 'Koreatown'),
(1, 'MI', 3, 'Corktown'), (1, 1, 'MI', 3, 'Downtown'),
(1, 'MI', 3, 'Mexicantown'), (1, 1, 'MI', 3, 'Greektown'),
(2, 'MC', 4, 'Arcadia Planitia') (1, 1, 'MI', 3, 'Corktown'),
(1, 1, 'MI', 3, 'Mexicantown'),
(2, 0, 'MC', 4, 'Arcadia Planitia')
; ;
INSERT INTO simple_primary_key VALUES (1, 'hello'); INSERT INTO simple_primary_key VALUES (1, 'hello');

Wyświetl plik

@ -117,7 +117,7 @@ def test_database_page(app_client):
'label_column': 'name', 'label_column': 'name',
'primary_keys': ['id'], 'primary_keys': ['id'],
}, { }, {
'columns': ['pk', 'planet_int', 'state', 'city_id', 'neighborhood'], 'columns': ['pk', 'planet_int', 'on_earth', 'state', 'city_id', 'neighborhood'],
'name': 'facetable', 'name': 'facetable',
'count': 15, 'count': 15,
'foreign_keys': { 'foreign_keys': {
@ -1107,6 +1107,7 @@ def test_expand_labels(app_client):
"2": { "2": {
"pk": 2, "pk": 2,
"planet_int": 1, "planet_int": 1,
"on_earth": 1,
"state": "CA", "state": "CA",
"city_id": { "city_id": {
"value": 1, "value": 1,
@ -1117,6 +1118,7 @@ def test_expand_labels(app_client):
"13": { "13": {
"pk": 13, "pk": 13,
"planet_int": 1, "planet_int": 1,
"on_earth": 1,
"state": "MI", "state": "MI",
"city_id": { "city_id": {
"value": 3, "value": 3,

Wyświetl plik

@ -12,22 +12,22 @@ world
'''.replace('\n', '\r\n') '''.replace('\n', '\r\n')
EXPECTED_TABLE_WITH_LABELS_CSV = ''' EXPECTED_TABLE_WITH_LABELS_CSV = '''
pk,planet_int,state,city_id,city_id_label,neighborhood pk,planet_int,on_earth,state,city_id,city_id_label,neighborhood
1,1,CA,1,San Francisco,Mission 1,1,1,CA,1,San Francisco,Mission
2,1,CA,1,San Francisco,Dogpatch 2,1,1,CA,1,San Francisco,Dogpatch
3,1,CA,1,San Francisco,SOMA 3,1,1,CA,1,San Francisco,SOMA
4,1,CA,1,San Francisco,Tenderloin 4,1,1,CA,1,San Francisco,Tenderloin
5,1,CA,1,San Francisco,Bernal Heights 5,1,1,CA,1,San Francisco,Bernal Heights
6,1,CA,1,San Francisco,Hayes Valley 6,1,1,CA,1,San Francisco,Hayes Valley
7,1,CA,2,Los Angeles,Hollywood 7,1,1,CA,2,Los Angeles,Hollywood
8,1,CA,2,Los Angeles,Downtown 8,1,1,CA,2,Los Angeles,Downtown
9,1,CA,2,Los Angeles,Los Feliz 9,1,1,CA,2,Los Angeles,Los Feliz
10,1,CA,2,Los Angeles,Koreatown 10,1,1,CA,2,Los Angeles,Koreatown
11,1,MI,3,Detroit,Downtown 11,1,1,MI,3,Detroit,Downtown
12,1,MI,3,Detroit,Greektown 12,1,1,MI,3,Detroit,Greektown
13,1,MI,3,Detroit,Corktown 13,1,1,MI,3,Detroit,Corktown
14,1,MI,3,Detroit,Mexicantown 14,1,1,MI,3,Detroit,Mexicantown
15,2,MC,4,Memnonia,Arcadia Planitia 15,2,0,MC,4,Memnonia,Arcadia Planitia
'''.lstrip().replace('\n', '\r\n') '''.lstrip().replace('\n', '\r\n')
def test_table_csv(app_client): def test_table_csv(app_client):

Wyświetl plik

@ -211,6 +211,89 @@ def test_sort_links(app_client):
] == attrs_and_link_attrs ] == attrs_and_link_attrs
def test_facet_display(app_client):
response = app_client.get(
"/fixtures/facetable?_facet=planet_int&_facet=city_id&_facet=on_earth"
)
assert response.status == 200
soup = Soup(response.body, "html.parser")
divs = soup.find(
"div", {"class": "facet-results"}
).findAll("div")
actual = []
for div in divs:
actual.append(
{
"name": div.find("strong").text,
"items": [
{
"name": a.text,
"qs": a["href"].split("?")[-1],
"count": int(str(a.parent).split("</a>")[1].split("<")[0]),
}
for a in div.find("ul").findAll("a")
],
}
)
assert [
{
"name": "city_id",
"items": [
{
"name": "San Francisco",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=1",
"count": 6,
},
{
"name": "Los Angeles",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=2",
"count": 4,
},
{
"name": "Detroit",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=3",
"count": 4,
},
{
"name": "Memnonia",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&city_id=4",
"count": 1,
},
],
},
{
"name": "planet_int",
"items": [
{
"name": "1",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&planet_int=1",
"count": 14,
},
{
"name": "2",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&planet_int=2",
"count": 1,
},
],
},
{
"name": "on_earth",
"items": [
{
"name": "1",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&on_earth=1",
"count": 14,
},
{
"name": "0",
"qs": "_facet=planet_int&_facet=city_id&_facet=on_earth&on_earth=0",
"count": 1,
},
],
},
] == actual
def test_facets_persist_through_filter_form(app_client): def test_facets_persist_through_filter_form(app_client):
response = app_client.get( response = app_client.get(
'/fixtures/facetable?_facet=planet_int&_facet=city_id' '/fixtures/facetable?_facet=planet_int&_facet=city_id'