-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_database.py
98 lines (76 loc) · 3.5 KB
/
test_database.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from datetime import date
from inspect import getmembers, ismethod
from typing import Any, Callable, Dict, List, Optional, Tuple
from uuid import uuid4
from unit_test_framework import Asserter, TestCase, TestSuite
from app.model.database import DatabaseConnection, travelCRUD
class MockCursor:
def __init__(self):
self.executed_queries:List[Any] = []
self.fetch_results = []
self.rowcount=0
def execute(self,query:str,params:Tuple[Any,...]=()):
self.executed_queries.append((query,params))
self.rowcount = 1
def executemany(self,query:str,params:Tuple[Any,...]=()):
self.executed_queries.append((query,params))
self.rowcount = len(params) if params else 0
def fetchall(self) ->Optional[List[Any]]:
return self.fetch_results
def fetchone(self) -> Optional[Any]:
return self.fetch_results[0] if self.fetch_results else None
def close(self):
pass
class MockDB:
def __init__(self):
self.mock_cursor = MockCursor()
def cursor(self):
return self.mock_cursor
class MockDatabaseConnection(DatabaseConnection):
def __init__(self,**kwargs):
self.cursor_instance = MockCursor()
self.autocommit = False
def __enter__(self) -> Callable[...,Any]:
return self
def __call__(self,**kwargs):
return self
def cursor(self):
return self.cursor_instance
def __exit__(self,exc_type,exc_val,exc_tb):
pass
class TestTravelCrud(TestCase):
def initialize(self):
# get DB
self.db_params: Dict[str,Any] = {'host':'localhost','port':5432}
self.table_name = 'test_bookings'
#
self.crud = travelCRUD(MockDatabaseConnection,self.db_params,self.table_name)
def finalize(self):
self.crud.executed_queries_history = []
self.crud.fetch_results_history = []
def test_create_schema(self):
self.crud.create_schema()
executed_query = self.crud.executed_queries_history[-1][0]
Asserter.assert_true("CREATE TABLE IF NOT EXISTS" in executed_query,"CREATE TABLE query not executed")
Asserter.assert_true(self.table_name in executed_query, "Table name not in CREATE TABLE query")
def test_insert_data_from_list(self):
test_data:List[Tuple[Any,...]] = [(
str(uuid4()), str(uuid4()), "John Doe", "[email protected]", "1234567890",
date(2023, 1, 1), date(2023, 2, 1), date(2023, 2, 15), "Paris", "New York",
"FL123", "Hotel Paris", "Double", 1000.00, "Paid", "Credit Card",
"Best Travel", "None", "LP12345")
]
self.crud.insert_data_from_list(test_data)
executed_query,params = self.crud.executed_queries_history[-1]
Asserter.assert_true("INSERT INTO" in executed_query, "INSERT query not executed.")
Asserter.assert_equal(params,test_data,"Inserted data does not match test data")
if __name__ == '__main__':
test_suite = TestSuite()
test_methods = [method for method in dir(TestTravelCrud) if method.startswith('test_')]
for method in test_methods:
test_suite.add_test(TestTravelCrud(method))
class_methods_to_test = [item for item in getmembers(travelCRUD(MockDatabaseConnection,{},''),predicate=ismethod) if not item[0].startswith('_')]
print(f"Need to test {len(class_methods_to_test)} different methods for travelCRUD-class.\n--------")
test_suite.do_tests()
print("--------")
print("nice")