Skip to content

Commit 18c8345

Browse files
author
James Robinson
authored
Simplify titles of secondary metadata HTML tables. (#71)
1 parent 0283b4a commit 18c8345

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

noteable_magics/sql/meta_commands.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def run(self, invoked_as: str, args: List[str]) -> Tuple[DataFrame, bool]:
449449
display_view_name = displayable_relation_name(schema, relation_name) # noqa: F841
450450
html_buf = []
451451
html_buf.append('<br />')
452-
html_buf.append(f'<h2>View <code>{display_view_name}</code> Definition</h2>')
452+
html_buf.append('<h2>View Definition</h2>')
453453
html_buf.append('<br />')
454454
html_buf.append(f'<pre>{view_definition}</pre>')
455455

@@ -504,9 +504,7 @@ def constraints_dataframe(
504504
}
505505
)
506506

507-
title = f'Table <code>{displayable_relation_name(schema, table_name)}</code> Check Constraints'
508-
509-
return set_dataframe_metadata(df, title=title)
507+
return set_dataframe_metadata(df, title='Check Constraints')
510508

511509

512510
def foreignkeys_dataframe(
@@ -542,9 +540,7 @@ def foreignkeys_dataframe(
542540
}
543541
)
544542

545-
title = f'Table <code>{displayable_relation_name(schema, table_name)}</code> Foreign Keys'
546-
547-
return set_dataframe_metadata(df, title=title)
543+
return set_dataframe_metadata(df, title='Foreign Keys')
548544

549545

550546
def index_dataframe(
@@ -584,9 +580,7 @@ def index_dataframe(
584580

585581
df = DataFrame({'Index': index_names, 'Columns': column_lists, 'Unique': uniques})
586582

587-
title = f'Table <code>{displayable_relation_name(schema, table_name)}</code> Indices'
588-
589-
return set_dataframe_metadata(df, title=title)
583+
return set_dataframe_metadata(df, title='Indexes')
590584

591585

592586
def set_dataframe_metadata(df: DataFrame, title=None) -> DataFrame:

tests/test_sql_magic_meta_commands.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,7 @@ def test_table_without_schema(
324324
# 2) HTML describing the indices
325325
index_df_html = mock_display.call_args_list[1].args[0]
326326
assert isinstance(index_df_html, HTML)
327-
assert (
328-
'<h2>Table <code>int_table</code> Indices</h2>' in index_df_html.data
329-
) # title was projected.
327+
assert '<h2>Indexes</h2>' in index_df_html.data # title was projected.
330328

331329
# Convert the HTML spelling of the indices back into a DF to test the output.
332330
df_from_index_html = pd.read_html(index_df_html.data)[0]
@@ -380,9 +378,7 @@ def test_against_view(
380378
html_obj = mock_display.call_args_list[1].args[0]
381379
assert isinstance(html_obj, HTML)
382380
html_contents: str = html_obj.data
383-
assert html_contents.startswith(
384-
'<br />\n<h2>View <code>str_int_view</code> Definition</h2>'
385-
)
381+
assert html_contents.startswith('<br />\n<h2>View Definition</h2>')
386382
# Some dialects include a 'CREATE VIEW' statement, others just start with 'select\n', and will vary by case.
387383
matcher = re.compile(
388384
'.*<pre>.*select.*s.str_id, s.int_col.*</pre>$',
@@ -420,9 +416,7 @@ def test_against_schema_qualified_view(self, sql_magic, ipython_namespace, mock_
420416
html_obj = mock_display.call_args_list[1].args[0]
421417
assert isinstance(html_obj, HTML)
422418
html_contents: str = html_obj.data
423-
assert html_contents.startswith(
424-
'<br />\n<h2>View <code>public.str_int_view</code> Definition</h2>'
425-
), html_contents
419+
assert html_contents.startswith('<br />\n<h2>View Definition</h2>'), html_contents
426420

427421
@pytest.mark.parametrize(
428422
'handle,schema', [('@cockroach', ''), ('@cockroach', 'public'), ('@sqlite', '')]
@@ -445,9 +439,7 @@ def test_foreign_keys(self, sql_magic, ipython_namespace, mock_display, handle,
445439
assert isinstance(fk_html, HTML)
446440
html_contents: str = fk_html.data
447441

448-
assert html_contents.startswith(
449-
f'<br />\n<h2>Table <code>{qualified_references_int_table}</code> Foreign Keys</h2>'
450-
), html_contents
442+
assert html_contents.startswith('<br />\n<h2>Foreign Keys</h2>'), html_contents
451443

452444
# Convert the HTML table back to dataframe to complete test.
453445
fk_df = pd.read_html(html_contents)[0]
@@ -502,9 +494,7 @@ def test_against_table_without_a_primary_key(self, sql_magic, ipython_namespace,
502494
# Test test_constraints() will exercise this further. Only mention it here
503495
# because will be returned and should not be talking about primary key / indices.
504496
constraint_html = mock_display.call_args_list[1].args[0].data
505-
assert constraint_html.startswith(
506-
'<br />\n<h2>Table <code>str_table</code> Check Constraints</h2>'
507-
)
497+
assert constraint_html.startswith('<br />\n<h2>Check Constraints</h2>')
508498

509499
# All CRDB tables have a primary key, so conditionally expect it to be described.
510500
@pytest.mark.parametrize(
@@ -522,9 +512,7 @@ def test_constraints(
522512

523513
# The constraints HTML blob will be the final one always.
524514
constraint_html = mock_display.call_args_list[-1].args[0].data
525-
assert constraint_html.startswith(
526-
'<br />\n<h2>Table <code>str_table</code> Check Constraints</h2>'
527-
)
515+
assert constraint_html.startswith('<br />\n<h2>Check Constraints</h2>')
528516

529517
# Convert back to dataframe
530518
constraint_df = pd.read_html(constraint_html)[0]

0 commit comments

Comments
 (0)