Veracity_AI/tests/utils/test_database.py

62 lines
2.2 KiB
Python

import pytest
import sys
import os
# Add the src directory to the Python path
src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'src'))
sys.path.insert(0, src_dir)
from utils.database.database import FakeNewsChecker
@pytest.fixture(scope="module")
def checker():
checker = FakeNewsChecker()
yield checker
checker.create_connection().execute('DELETE FROM url_info')
checker.create_connection().close()
def test_create_table(checker):
conn = checker.create_connection()
# Inspect the actual table structure
result = conn.execute('PRAGMA table_info(url_info)').fetchall()
actual_columns = [(col[1], col[2], col[2], col[3], col[4], col[5]) for col in result]
# Compare the actual columns to the expected
expected_columns = [
('id', 'INTEGER', 'INTEGER', 1, None, 1),
('url', 'VARCHAR', 'VARCHAR', 1, None, 0),
('anbieter', 'VARCHAR', 'VARCHAR', 1, None, 0),
('is_fake_news', 'BOOLEAN', 'BOOLEAN', 1, None, 0),
]
assert actual_columns == expected_columns
# Clean up the test data
conn.execute('DELETE FROM url_info')
conn.commit()
conn.close()
def test_get_next_id(checker):
assert checker.get_next_id() == 1
def test_insert_data(checker):
checker.insert_data('https://example.com/news/123', 'Example News', False)
data = checker.fetch_data()
assert len(data) == 1
assert data[0] == (1, 'https://example.com/news/123', 'Example News', False)
checker.create_connection().execute('DELETE FROM url_info')
checker.create_connection().commit()
def test_fetch_data(checker):
checker.insert_data('https://example.com/news/123', 'Example News', False)
checker.insert_data('https://fakenews.com/article/456', 'Fake News', True)
data = checker.fetch_data()
assert len(data) == 2
assert data[0] == (1, 'https://example.com/news/123', 'Example News', False)
assert data[1] == (2, 'https://fakenews.com/article/456', 'Fake News', True)
checker.create_connection().execute('DELETE FROM url_info')
checker.create_connection().commit()
if __name__ == "__main__":
pytest.main([__file__])