Visualize company sentiment (#422)

Updates the tab and widget "Stimmung"
This commit is contained in:
KM-R
2023-11-22 22:32:38 +01:00
committed by GitHub
parent 65c41bb20c
commit 4e83c83374
9 changed files with 777 additions and 53 deletions

View File

@@ -4,6 +4,7 @@ import pandas as pd
from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.ui import data_elements
from aki_prj23_transparenzregister.utils.enum_types import SentimentLabel
def test_import() -> None:
@@ -86,3 +87,27 @@ def test_get_finance_data(full_db: Session) -> None:
}
)
pd.testing.assert_frame_equal(finance_df, test_data)
def test_get_news_of_one_company0(news_db: Session) -> None:
"""Tests what happens if no sentiment can be found."""
selected_company_id = 100
assert data_elements.get_news_of_one_company(selected_company_id, news_db).empty
def test_get_news_of_one_company1(news_db: Session) -> None:
"""Tests if the sentiments can be collected for a company specified."""
selected_company_id = 2
sentiment_df = data_elements.get_news_of_one_company(selected_company_id, news_db)
assert sentiment_df is not None
assert sentiment_df.to_dict(orient="records") == [
{
"overall_sentiment_certainty": 0.95,
"overall_sentiment_label": SentimentLabel.POSITIVE,
"source_domain": "example-news.com",
"source_url": "http://example-news.com/ai-revolution",
"times_named": 2,
"timestamp": pd.Timestamp("2023-11-01 15:30:00"),
"title": "AI Revolution in Tech",
},
]

View File

@@ -0,0 +1,32 @@
"""Tests for sentiment ui elements."""
from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.ui import data_elements, sentiment_elements
def test_sentiment_layout(news_db: Session) -> None:
"""Checks if the sentiment tab layout of the company page can be created."""
selected_company_id = 1
sentiment_elements.sentiment_layout(selected_company_id, news_db)
def test_sentiment_trend_figure(news_db: Session) -> None:
"""Checks if the sentiment trend graph can be created."""
n = 10
company_id = 1
articles = data_elements.get_news_of_one_company(company_id, news_db)
articles["overall_sentiment_label"] = articles["overall_sentiment_label"].apply(
lambda _: _.value
)
articles = articles.sort_values(by=["timestamp"], ascending=True)
articles["rolling_mean"] = articles["overall_sentiment_label"].rolling(n).mean()
sentiment_elements.sentiment_trend_figure(articles, n)
def test_sentiment_gauge_figure() -> None:
"""Checks if the sentiment gauge graph can be created."""
sentiment_score = 0.2
sentiment_elements.sentiment_gauge_figure(sentiment_score)