Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-61976: fix breaking table #1674

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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