-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acd9555
commit e4afd3b
Showing
3 changed files
with
30 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +0,0 @@ | ||
from datetime import datetime | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from data_analyzer.analyzer import Analyzer | ||
from data_analyzer.utils.models import Analysis, News | ||
|
||
APPLE_NEWS_1 = News( | ||
id=1, | ||
category="equity", | ||
symbol="AAPL", | ||
src="example", | ||
src_url="https://www.example.com", | ||
img_src_url="https://example.com/img.png", | ||
headline="Apple launches new iPhone 12", | ||
summary="Apple launches new iPhone 12", | ||
publish_time="2020-05-17T10:00:00.000Z", | ||
sentiment=1, | ||
need_attention=False, | ||
reason=None, | ||
) | ||
|
||
APPLE_NEWS_2 = News( | ||
id=2, | ||
category="equity", | ||
symbol="AAPL", | ||
src="example", | ||
src_url="https://www.example.com", | ||
img_src_url="https://example.com/img.png", | ||
headline="Apple launches new iPad 5", | ||
summary="Apple launches new iPad 5", | ||
publish_time="2020-05-17T10:00:00.000Z", | ||
sentiment=1, | ||
need_attention=True, | ||
reason=None, | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def analyzer(): | ||
with patch("psycopg.connect") as mock_connect: | ||
mock_connect.return_value.cursor.return_value.fetchone.return_value = [ | ||
1, | ||
"equity", | ||
"AAPL", | ||
"example", | ||
"https://www.example.com", | ||
"https://example.com/img.png", | ||
"Apple launches new iPhone 12", | ||
"Apple launches new iPhone 12", | ||
"2020-05-17T10:00:00.000Z", | ||
1, | ||
False, | ||
None, | ||
] | ||
yield Analyzer() | ||
|
||
|
||
class TestAnalyzer: | ||
@pytest.mark.parametrize( | ||
"symbol, news, expected", | ||
[ | ||
( | ||
"AAPL", | ||
[ | ||
APPLE_NEWS_1, | ||
APPLE_NEWS_2, | ||
], | ||
Analysis( | ||
category="equity", | ||
symbol="AAPL", | ||
date=datetime.now().date().isoformat(), | ||
average_sentiment=1, | ||
total_news=2, | ||
positive_news=2, | ||
negative_news=0, | ||
neutral_news=0, | ||
need_attention=True, | ||
), | ||
), | ||
], | ||
) | ||
def test__analyze_symbol(self, symbol, news, expected): | ||
analyzer = Analyzer() | ||
actual = analyzer.analyze_symbol(symbol, news) | ||
assert actual.__dict__ == expected.__dict__ | ||
|
||
def test__fetch_news(self, analyzer): | ||
expected_news = APPLE_NEWS_1 | ||
result = analyzer.fetch_news(1) | ||
assert result.__dict__ == expected_news.__dict__ | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
WITH news_stats AS ( | ||
SELECT | ||
COALESCE(AVG(sentiment) FILTER ( WHERE sentiment != 0 ), 0) AS avg_sentiment, | ||
COUNT(*) AS total_news_count, | ||
COUNT(*) FILTER (WHERE sentiment > 0) AS positive_news_count, | ||
COUNT(*) FILTER (WHERE sentiment < 0) AS negative_news_count, | ||
COUNT(*) FILTER (WHERE sentiment = 0) AS neutral_news_count, | ||
BOOL_OR(need_attention) AS any_need_attention | ||
FROM | ||
news_news | ||
WHERE | ||
DATE(publish_time) = '2023-12-17' | ||
AND symbol = 'NFLX' | ||
) | ||
UPDATE | ||
news_analysis | ||
SET | ||
average_sentiment = news_stats.avg_sentiment, | ||
total_news = news_stats.total_news_count, | ||
positive_news = news_stats.positive_news_count, | ||
negative_news = news_stats.negative_news_count, | ||
neutral_news = news_stats.neutral_news_count, | ||
need_attention = news_stats.any_need_attention | ||
FROM | ||
news_stats | ||
WHERE | ||
news_analysis.symbol = 'NFLX' | ||
AND news_analysis.date = '2023-12-17'; |