Skip to content

Commit

Permalink
Update get_truncated_message function and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tl3119 committed Jul 3, 2023
1 parent 75c2043 commit 214fbf7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/sql/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ def _nonbreaking_spaces(match_obj):
_cell_with_spaces_pattern = re.compile(r"(<td>)( {2,})")


def _get_truncated_message(html=False) -> str:
if html:
return (
'%s\n<span style="font-style:italic;text-align:center;">'
"Truncated to displaylimit of %d</span>"
"<br>"
'<span style="font-style:italic;text-align:center;">'
"If you want to see more, please visit "
'<a href="https://jupysql.ploomber.io/en/latest/api/configuration.html#displaylimit">displaylimit</a>' # noqa: E501
" configuration</span>"
)
else:
return (
"\n"
"Truncated to display limit of 10\n"
"If you want to see more, please visit the "
"displaylimit configuration at "
"https://jupysql.ploomber.io/en/latest/api/configuration.html#displaylimit"
)


class ResultSet(ColumnGuesserMixin):
"""
Results of a SQL query.
Expand Down Expand Up @@ -140,37 +161,12 @@ def _repr_html_(self):
result = html.unescape(result)
result = _cell_with_spaces_pattern.sub(_nonbreaking_spaces, result)
if self.truncated:
HTML = (
'%s\n<span style="font-style:italic;text-align:center;">'
"Truncated to displaylimit of %d</span>"
"<br>"
'<span style="font-style:italic;text-align:center;">'
"If you want to see more, please visit "
'<a href="https://jupysql.ploomber.io/en/latest/api/configuration.html#displaylimit">displaylimit</a>' # noqa: E501
" configuration</span>"
)
HTML = _get_truncated_message(html=True)
result = HTML % (result, self.pretty.row_count)
return result
else:
return None

def _repr_text(self):
if self.pretty:
self.pretty.add_rows(self)
result = str(self.pretty)
if self.truncated:
result += (
"\nTruncated to display limit of {}\n"
"If you want to see more, please visit the "
"displaylimit configuration at "
"https://jupysql.ploomber.io/en/latest/api/configuration.html#displaylimit".format( # noqa: E501
self.pretty.row_count
)
)
return result
else:
return None

def __len__(self):
return len(self._results)

Expand All @@ -186,6 +182,13 @@ def __str__(self, *arg, **kwarg):
return str(self.pretty or "")

def __repr__(self) -> str:
if self.pretty:
self.pretty.add_rows(self)
result = str(self.pretty)
if self.truncated:
message = _get_truncated_message()
result = f"{result} {message}"
return result
return str(self)

def __eq__(self, another: object) -> bool:
Expand Down
20 changes: 20 additions & 0 deletions src/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
select_df_type,
set_autocommit,
display_affected_rowcount,
_get_truncated_message,
)


Expand Down Expand Up @@ -181,3 +182,22 @@ def test_commit_is_called(
run(mock_conns, "\\", mock_config)

mock__commit.assert_called()


def test_get_truncated_message():
expected_text_message = "\nTruncated to display limit of 10\nIf you want to see more, please visit the displaylimit configuration at https://jupysql.ploomber.io/en/latest/api/configuration.html#displaylimit" # noqa: E501
expected_html_message = (
'%s\n<span style="font-style:italic;text-align:center;">'
"Truncated to displaylimit of %d</span>"
"<br>"
'<span style="font-style:italic;text-align:center;">'
"If you want to see more, please visit "
'<a href="https://jupysql.ploomber.io/en/latest/api/configuration.html#displaylimit">displaylimit</a>' # noqa: E501
" configuration</span>"
)

text_message = _get_truncated_message(html=False)
assert text_message == expected_text_message

html_message = _get_truncated_message(html=True)
assert html_message == expected_html_message

0 comments on commit 214fbf7

Please sign in to comment.