-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_db_service.py
83 lines (65 loc) · 2.47 KB
/
test_db_service.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Testing db_service
"""
import sqlite3
import pytest
from freezegun import freeze_time
from database.db_service import DataBaseService
@pytest.fixture
def make_connection():
"""
The create_connection function creates a connection to the database.
Args:
None
:return: A connection object
:doc-author: Trelent
"""
with sqlite3.connect(':memory:') as connection:
return connection
@freeze_time('2024-01-08 00:00:00')
def test_get_borrowed_books_for_today_reminder_purpose(make_connection):
"""
The test_get_borrowed_books_for_today_reminder_purpose
function tests the get_borrowed_books_by_today function.
It checks if it returns a list of borrowers who borrowed books today.
:param create_connection: Create a connection to the database
:param monkeypatch: Mock the current time
:return: A list of borrowers
:doc-author: Trelent
"""
db = DataBaseService(make_connection)
db.create_database()
query = 'INSERT INTO borrowed_books VALUES(?,?,?,?,?,?)'
sample_data = [
(6, 'John Smith', '[email protected]',
'Lalka', 'Bolesław Prus', '2024-12-27 13:30:30'),
(7, 'Luiza Acante', '[email protected]',
'Engine Manual for Smartasses', 'Mario Bro', '2023-12-27 13:30:30')
]
for data in sample_data:
db.execute_query(query, data)
list_of_borrowers = db.get_borrowed_books_by_today()
assert len(list_of_borrowers) == 1
@freeze_time('2024-01-08 00:00:00')
def test_get_borrowed_books_for_today_amount_of_records(make_connection):
"""
The test_get_borrowed_books_for_today_amount_of_records
function tests the get_borrowed_passed function from DataBaseService class.
It checks if the amount of records returned by this function is correct.
:param create_connection: Create a connection to the database
:return: 1 because the second record has a date in the future
:doc-author: Trelent
"""
db = DataBaseService(make_connection)
db.create_database()
query = 'INSERT INTO borrowed_books VALUES(?,?,?,?,?,?)'
sample_data = [
(6, '[email protected]', 'John Smith', 'Lalka',
'Bolesław Prus', '2024-12-27 13:30:30'),
(7, '[email protected]', 'Luiza Acante', 'Engine Manual for Smartasses',
'Mario Bro', '2023-12-27 13:30:30')
]
for data in sample_data:
db.execute_query(query, data)
list_of_borrowers = db.get_borrowed_passed()
assert len(list_of_borrowers) == 1