Debian Codegraph @

tests/test_data.py

raw
1 import pytest
2 from unittest.mock import patch
3 from flask import Flask
4 from a2d.routes.data import data_routes
5
6 @pytest.fixture
7 def client():
8 app = create_app()
9 app.register_blueprint(data_routes)
10
11 with app.test_client() as client:
12 with app.app_context():
13 yield client
14
15 def create_app():
16 app = Flask(__name__)
17 app.config['TESTING'] = True
18 app.secret_key = 'your_secret_key' # Example secret key for session
19 return app
20
21 def test_fetch_logs(client):
22 with client.session_transaction() as sess:
23 sess['user_id'] = 1 # Example user ID
24
25 # Mocking the database query and configparser read
26 with patch('os.path.exists', side_effect=lambda x: True), \
27 patch('sqlite3.connect') as mock_connect, \
28 patch('configparser.ConfigParser') as MockConfigParser:
29
30 mock_cursor = mock_connect.return_value.cursor.return_value
31 mock_cursor.fetchall.side_effect = [
32 [(1, 'msg1', 'content1', '2023-01-01 10:00:00')],
33 [(2, 'msg2', 'content2', '2023-01-01 11:00:00')]
34 ]
35
36 # Simulate a GET request
37 rv = client.get('/fetch-logs')
38 assert rv.status_code == 302
39
40 def test_fetch_logs_no_login(client):
41 # User not logged in
42 rv = client.get('/fetch-logs')
43 assert rv.status_code == 302 # Expect redirect to login page or another location