Skip to content

Commit

Permalink
label_column now defined on the table-being-linked-to, fixes #234
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 22, 2018
1 parent f27cabb commit f3f4295
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ async def display_columns_and_rows(self, database, table, description, rows, lin
foreign_keys = table_info['foreign_keys']['outgoing']
for fk in foreign_keys:
label_column = (
# First look for metadata.json definition:
table_metadata.get('label_column')
# First look in metadata.json definition for this foreign key table:
self.table_metadata(database, fk['other_table']).get('label_column')
# Fall back to label_column from .inspect() detection:
or tables.get(fk['other_table'], {}).get('label_column')
)
Expand Down
11 changes: 9 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def generate_sortable_rows(num):
'frequency': 'Hz'
}
},
'custom_foreign_key_label': {
'primary_key_multiple_columns_explicit_label': {
'label_column': 'content2',
},
}
Expand Down Expand Up @@ -145,6 +145,12 @@ def extra_js_urls():
content2 text
);
CREATE TABLE primary_key_multiple_columns_explicit_label (
id varchar(30) primary key,
content text,
content2 text
);
CREATE TABLE compound_primary_key (
pk1 varchar(30),
pk2 varchar(30),
Expand Down Expand Up @@ -221,7 +227,7 @@ def extra_js_urls():
CREATE TABLE "custom_foreign_key_label" (
pk varchar(30) primary key,
foreign_key_with_custom_label text,
FOREIGN KEY ("foreign_key_with_custom_label") REFERENCES [primary_key_multiple_columns](id)
FOREIGN KEY ("foreign_key_with_custom_label") REFERENCES [primary_key_multiple_columns_explicit_label](id)
);
CREATE TABLE units (
Expand All @@ -246,6 +252,7 @@ def extra_js_urls():
INSERT INTO simple_primary_key VALUES (3, '');
INSERT INTO primary_key_multiple_columns VALUES (1, 'hey', 'world');
INSERT INTO primary_key_multiple_columns_explicit_label VALUES (1, 'hey', 'world2');
INSERT INTO foreign_key_references VALUES (1, 1, 1);
Expand Down
19 changes: 15 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_homepage(app_client):
assert response.json.keys() == {'test_tables': 0}.keys()
d = response.json['test_tables']
assert d['name'] == 'test_tables'
assert d['tables_count'] == 14
assert d['tables_count'] == 15


def test_database_page(app_client):
Expand Down Expand Up @@ -89,12 +89,12 @@ def test_database_page(app_client):
'outgoing': [{
'column': 'foreign_key_with_custom_label',
'other_column': 'id',
'other_table': 'primary_key_multiple_columns'
'other_table': 'primary_key_multiple_columns_explicit_label'
}],
},
'label_column': None,
'primary_keys': ['pk'],
}, {
}, {
'columns': ['pk', 'foreign_key_with_label', 'foreign_key_with_no_label'],
'name': 'foreign_key_references',
'count': 1,
Expand Down Expand Up @@ -130,7 +130,18 @@ def test_database_page(app_client):
'column': 'id',
'other_column': 'foreign_key_with_no_label',
'other_table': 'foreign_key_references'
}, {
}],
'outgoing': []
},
'hidden': False,
'label_column': None,
'primary_keys': ['id']
}, {
'columns': ['id', 'content', 'content2'],
'name': 'primary_key_multiple_columns_explicit_label',
'count': 1,
'foreign_keys': {
'incoming': [{
'column': 'id',
'other_column': 'foreign_key_with_custom_label',
'other_table': 'custom_foreign_key_label'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_table_html_foreign_key_custom_label_column(app_client):
expected = [
[
'<td class="col-pk"><a href="/test_tables/custom_foreign_key_label/1">1</a></td>',
'<td class="col-foreign_key_with_custom_label"><a href="/test_tables/primary_key_multiple_columns/1">world</a>\xa0<em>1</em></td>',
'<td class="col-foreign_key_with_custom_label"><a href="/test_tables/primary_key_multiple_columns_explicit_label/1">world2</a>\xa0<em>1</em></td>',
]
]
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
Expand Down

0 comments on commit f3f4295

Please sign in to comment.