forked from leonkuperman/llm_demo_news
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_news_fetcher.py
38 lines (29 loc) · 1.28 KB
/
test_news_fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import unittest
import asyncio
from db_setup import get_db_connection, init_db, get_last_id
from news_fetcher import fetch_and_store_articles
from logger_config import get_logger
logger = get_logger(__name__)
class TestNewsFetcher(unittest.TestCase):
@classmethod
def setUpClass(cls):
init_db() # Initialize the test database
logger.info("Test database initialized.")
@classmethod
def tearDownClass(cls):
with get_db_connection() as conn:
conn.execute("DROP TABLE IF EXISTS articles")
logger.info("Test database cleaned up.")
def test_fetch_and_store_articles(self):
async def run_test():
last_id_before = get_last_id()
await fetch_and_store_articles() # Fetch and store new articles
# Get the count of rows inserted
with get_db_connection() as conn:
cursor = conn.execute("SELECT COUNT(*) FROM articles WHERE finnhub_id > ?", (last_id_before,))
row_count = cursor.fetchone()[0]
logger.info(f"Rows inserted during test: {row_count}")
self.assertGreater(row_count, 0, "No new articles were inserted into the test database.")
asyncio.run(run_test())
if __name__ == "__main__":
unittest.main()