kopia lustrzana https://github.com/simonw/datasette
Added unit test for foreign key links in HTML
Needed to add a further unit test for #207pull/205/head^2
rodzic
1cc5161089
commit
d72201e883
|
@ -87,10 +87,16 @@ METADATA = {
|
|||
|
||||
TABLES = '''
|
||||
CREATE TABLE simple_primary_key (
|
||||
pk varchar(30) primary key,
|
||||
id varchar(30) primary key,
|
||||
content text
|
||||
);
|
||||
|
||||
CREATE TABLE primary_key_multiple_columns (
|
||||
id varchar(30) primary key,
|
||||
content text,
|
||||
content2 text
|
||||
);
|
||||
|
||||
CREATE TABLE compound_primary_key (
|
||||
pk1 varchar(30),
|
||||
pk2 varchar(30),
|
||||
|
@ -108,6 +114,14 @@ CREATE TABLE compound_three_primary_keys (
|
|||
PRIMARY KEY (pk1, pk2, pk3)
|
||||
);
|
||||
|
||||
CREATE TABLE foreign_key_references (
|
||||
pk varchar(30) primary key,
|
||||
foreign_key_with_label varchar(30),
|
||||
foreign_key_with_no_label varchar(30),
|
||||
FOREIGN KEY (foreign_key_with_label) REFERENCES simple_primary_key(id),
|
||||
FOREIGN KEY (foreign_key_with_no_label) REFERENCES primary_key_multiple_columns(id)
|
||||
);
|
||||
|
||||
CREATE TABLE sortable (
|
||||
pk1 varchar(30),
|
||||
pk2 varchar(30),
|
||||
|
@ -166,6 +180,10 @@ INSERT INTO simple_primary_key VALUES (1, 'hello');
|
|||
INSERT INTO simple_primary_key VALUES (2, 'world');
|
||||
INSERT INTO simple_primary_key VALUES (3, '');
|
||||
|
||||
INSERT INTO primary_key_multiple_columns VALUES (1, 'hey', 'world');
|
||||
|
||||
INSERT INTO foreign_key_references VALUES (1, 1, 1);
|
||||
|
||||
INSERT INTO complex_foreign_keys VALUES (1, 1, 2, 1);
|
||||
|
||||
INSERT INTO [table/with/slashes.csv] VALUES (3, 'hey');
|
||||
|
|
|
@ -14,7 +14,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'] == 10
|
||||
assert d['tables_count'] == 12
|
||||
|
||||
|
||||
def test_database_page(app_client):
|
||||
|
@ -76,6 +76,25 @@ def test_database_page(app_client):
|
|||
'foreign_keys': {'incoming': [], 'outgoing': []},
|
||||
'label_column': None,
|
||||
'primary_keys': ['pk1', 'pk2', 'pk3'],
|
||||
}, {
|
||||
'columns': ['pk', 'foreign_key_with_label', 'foreign_key_with_no_label'],
|
||||
'name': 'foreign_key_references',
|
||||
'count': 1,
|
||||
'hidden': False,
|
||||
'foreign_keys': {
|
||||
'incoming': [],
|
||||
'outgoing': [{
|
||||
'column': 'foreign_key_with_no_label',
|
||||
'other_column': 'id',
|
||||
'other_table': 'primary_key_multiple_columns'
|
||||
}, {
|
||||
'column': 'foreign_key_with_label',
|
||||
'other_column': 'id',
|
||||
'other_table': 'simple_primary_key',
|
||||
}],
|
||||
},
|
||||
'label_column': None,
|
||||
'primary_keys': ['pk'],
|
||||
}, {
|
||||
'columns': ['content', 'a', 'b', 'c'],
|
||||
'name': 'no_primary_key',
|
||||
|
@ -84,6 +103,21 @@ def test_database_page(app_client):
|
|||
'foreign_keys': {'incoming': [], 'outgoing': []},
|
||||
'label_column': None,
|
||||
'primary_keys': [],
|
||||
}, {
|
||||
'columns': ['id', 'content', 'content2'],
|
||||
'name': 'primary_key_multiple_columns',
|
||||
'count': 1,
|
||||
'foreign_keys': {
|
||||
'incoming': [{
|
||||
'column': 'id',
|
||||
'other_column': 'foreign_key_with_no_label',
|
||||
'other_table': 'foreign_key_references'
|
||||
}],
|
||||
'outgoing': []
|
||||
},
|
||||
'hidden': False,
|
||||
'label_column': None,
|
||||
'primary_keys': ['id']
|
||||
}, {
|
||||
'columns': ['group', 'having', 'and'],
|
||||
'name': 'select',
|
||||
|
@ -93,12 +127,16 @@ def test_database_page(app_client):
|
|||
'label_column': None,
|
||||
'primary_keys': [],
|
||||
}, {
|
||||
'columns': ['pk', 'content'],
|
||||
'columns': ['id', 'content'],
|
||||
'name': 'simple_primary_key',
|
||||
'count': 3,
|
||||
'hidden': False,
|
||||
'foreign_keys': {
|
||||
'incoming': [{
|
||||
'column': 'id',
|
||||
'other_column': 'foreign_key_with_label',
|
||||
'other_table': 'foreign_key_references'
|
||||
}, {
|
||||
'column': 'id',
|
||||
'other_column': 'f3',
|
||||
'other_table': 'complex_foreign_keys'
|
||||
|
@ -113,8 +151,8 @@ def test_database_page(app_client):
|
|||
}],
|
||||
'outgoing': [],
|
||||
},
|
||||
'label_column': None,
|
||||
'primary_keys': ['pk'],
|
||||
'label_column': 'content',
|
||||
'primary_keys': ['id'],
|
||||
}, {
|
||||
'columns': [
|
||||
'pk1', 'pk2', 'content', 'sortable', 'sortable_with_nulls',
|
||||
|
@ -194,16 +232,16 @@ def test_table_json(app_client):
|
|||
response = app_client.get('/test_tables/simple_primary_key.json?_shape=objects', gather_request=False)
|
||||
assert response.status == 200
|
||||
data = response.json
|
||||
assert data['query']['sql'] == 'select * from simple_primary_key order by pk limit 51'
|
||||
assert data['query']['sql'] == 'select * from simple_primary_key order by id limit 51'
|
||||
assert data['query']['params'] == {}
|
||||
assert data['rows'] == [{
|
||||
'pk': '1',
|
||||
'id': '1',
|
||||
'content': 'hello',
|
||||
}, {
|
||||
'pk': '2',
|
||||
'id': '2',
|
||||
'content': 'world',
|
||||
}, {
|
||||
'pk': '3',
|
||||
'id': '3',
|
||||
'content': '',
|
||||
}]
|
||||
|
||||
|
@ -252,13 +290,13 @@ def test_table_shape_objects(app_client):
|
|||
gather_request=False
|
||||
)
|
||||
assert [{
|
||||
'pk': '1',
|
||||
'id': '1',
|
||||
'content': 'hello',
|
||||
}, {
|
||||
'pk': '2',
|
||||
'id': '2',
|
||||
'content': 'world',
|
||||
}, {
|
||||
'pk': '3',
|
||||
'id': '3',
|
||||
'content': '',
|
||||
}] == response.json['rows']
|
||||
|
||||
|
@ -270,15 +308,15 @@ def test_table_shape_object(app_client):
|
|||
)
|
||||
assert {
|
||||
'1': {
|
||||
'pk': '1',
|
||||
'id': '1',
|
||||
'content': 'hello',
|
||||
},
|
||||
'2': {
|
||||
'pk': '2',
|
||||
'id': '2',
|
||||
'content': 'world',
|
||||
},
|
||||
'3': {
|
||||
'pk': '3',
|
||||
'id': '3',
|
||||
'content': '',
|
||||
}
|
||||
} == response.json['rows']
|
||||
|
@ -512,13 +550,18 @@ def test_view(app_client):
|
|||
def test_row(app_client):
|
||||
response = app_client.get('/test_tables/simple_primary_key/1.json?_shape=objects', gather_request=False)
|
||||
assert response.status == 200
|
||||
assert [{'pk': '1', 'content': 'hello'}] == response.json['rows']
|
||||
assert [{'id': '1', 'content': 'hello'}] == response.json['rows']
|
||||
|
||||
|
||||
def test_row_foreign_key_tables(app_client):
|
||||
response = app_client.get('/test_tables/simple_primary_key/1.json?_extras=foreign_key_tables', gather_request=False)
|
||||
assert response.status == 200
|
||||
assert [{
|
||||
'column': 'id',
|
||||
'count': 1,
|
||||
'other_column': 'foreign_key_with_label',
|
||||
'other_table': 'foreign_key_references'
|
||||
}, {
|
||||
'column': 'id',
|
||||
'count': 1,
|
||||
'other_column': 'f3',
|
||||
|
|
|
@ -172,7 +172,7 @@ def test_table_html_simple_primary_key(app_client):
|
|||
table = Soup(response.body, 'html.parser').find('table')
|
||||
ths = table.findAll('th')
|
||||
assert 'Link' == ths[0].string.strip()
|
||||
for expected_col, th in zip(('pk', 'content'), ths[1:]):
|
||||
for expected_col, th in zip(('id', 'content'), ths[1:]):
|
||||
a = th.find('a')
|
||||
assert expected_col == a.string
|
||||
assert a['href'].endswith('/simple_primary_key?_sort={}'.format(
|
||||
|
@ -200,7 +200,7 @@ 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'
|
||||
'id', 'content'
|
||||
] == [th.string.strip() for th in table.select('thead th')]
|
||||
assert [
|
||||
[
|
||||
|
@ -276,6 +276,20 @@ def test_table_html_compound_primary_key(app_client):
|
|||
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
||||
|
||||
|
||||
def test_table_html_foreign_key_links(app_client):
|
||||
response = app_client.get('/test_tables/foreign_key_references', gather_request=False)
|
||||
table = Soup(response.body, 'html.parser').find('table')
|
||||
expected = [
|
||||
[
|
||||
'<td><a href="/test_tables/foreign_key_references/1">1</a></td>',
|
||||
'<td>1</td>',
|
||||
'<td><a href="/test_tables/simple_primary_key/1">hello</a>\xa0<em>1</em></td>',
|
||||
'<td>1</td>'
|
||||
]
|
||||
]
|
||||
assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]
|
||||
|
||||
|
||||
def test_row_html_compound_primary_key(app_client):
|
||||
response = app_client.get('/test_tables/compound_primary_key/a,b', gather_request=False)
|
||||
table = Soup(response.body, 'html.parser').find('table')
|
||||
|
|
Ładowanie…
Reference in New Issue