Skip to content

Commit

Permalink
MM-61976: fix breaking table (#1674)
Browse files Browse the repository at this point in the history
* MM-61976: fix breaking table

* Fix linter issue

* Revert unintended change
  • Loading branch information
ifoukarakis authored Nov 26, 2024
1 parent 9da24ee commit d5ecac9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/utils/db/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ def test_data(sqlalchemy_memory_engine):
df.to_sql('books', conn)

return df


@pytest.fixture
def test_data_with_newline(sqlalchemy_memory_engine):
with sqlalchemy_memory_engine.connect() as conn, conn.begin():
df = pd.DataFrame({'id': [1, 2], 'title': ['The Great Gatsby', 'The Lord of the Rings:\nThe Two Towers']})
df.to_sql('books', conn)

return df
37 changes: 37 additions & 0 deletions tests/utils/db/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@ def test_post_query_results(sqlalchemy_memory_engine, test_data, responses):
}


def test_post_query_results_with_new_line(sqlalchemy_memory_engine, test_data_with_newline, responses):
# GIVEN: a mattermost server waiting for a request

response = Response(
method="POST",
url='https://mattermost.a-test-server.com',
status=200,
content_type='application/json',
)
responses.add(response)

# GIVEN: a database with some data
with sqlalchemy_memory_engine.connect() as conn, conn.begin():
# WHEN: request to post query results to Mattermost
post_query_results(
conn,
'select id, title from books order by id',
['ISBN', 'Title'],
'https://mattermost.a-test-server.com',
'book-club',
'No books found',
)

# THEN: a request is made to the Mattermost server
assert json.loads(responses.calls[0].request.body) == {
'text': dedent(
'''
| ISBN | Title |
|--------|----------------------------------------|
| 1 | The Great Gatsby |
| 2 | The Lord of the Rings:\\nThe Two Towers |
'''
).strip(),
'channel': 'book-club',
}


def test_post_query_results_with_empty_results(sqlalchemy_memory_engine, test_data, responses):
# GIVEN: a mattermost server waiting for a request

Expand Down
3 changes: 3 additions & 0 deletions utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def post_df_to_mattermost(url: str, channel: str, df: pd.DataFrame, headers: Lis
json={"text": empty_data_message, "channel": channel},
)
else:
for col in df.select_dtypes(include=['object']).columns:
df[col] = df[col].apply(lambda val: val.replace("\n", "\\n") if val and type(val) == str else val)

msg = tabulate(df, headers=headers, tablefmt='github', showindex='never')
response = requests.post(
url,
Expand Down

0 comments on commit d5ecac9

Please sign in to comment.