-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
147 lines (115 loc) · 3.49 KB
/
conftest.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import shutil
import tempfile
from unittest.mock import Mock, patch
import pytest
from flask import g
from app import create_app
from app.models.category import Category
from app.models.product import Product
from app.models.supplier import Supplier
from app.services.category import CategoryService
from app.services.product import ProductService
from app.services.supplier import SupplierService
@pytest.fixture
def app():
# Create a temporary file to store the test database
db_fd, db_path = tempfile.mkstemp()
os.close(db_fd) # Close the file descriptor as we'll use the path with shutil
# Create the app first to get the main database path
app = create_app()
main_db = os.path.join(app.root_path, "northwind.db")
# Copy the main database to our test location
shutil.copy2(main_db, db_path)
# Update the app config to use the test database
app.config.update({"TESTING": True, "DATABASE": db_path})
yield app
# Clean up the temporary file
os.unlink(db_path)
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def app_context(app):
with app.app_context():
yield
@pytest.fixture
def mock_cursor():
cursor = Mock()
return cursor
@pytest.fixture
def mock_db(app, mock_cursor):
with app.app_context():
mock_conn = Mock()
mock_conn.cursor.return_value = mock_cursor
# Patch the database connection
with patch("app.utils.database.get_db", return_value=mock_conn):
g.db = mock_conn
yield mock_cursor
@pytest.fixture
def mock_category_service(mock_db):
service = CategoryService()
# Ensure the cursor is set after initialization
service.cursor = mock_db
return service
@pytest.fixture
def mock_product_service(mock_db):
service = ProductService()
# Ensure the cursor is set after initialization
service.cursor = mock_db
return service
@pytest.fixture
def mock_supplier_service(mock_db):
service = SupplierService()
# Ensure the cursor is set after initialization
service.cursor = mock_db
return service
# DO NOT CHANGE THIS FIXTURE AS ITS VALUE IS USED IN MANY TESTS
@pytest.fixture
def mock_product():
return Product(
ProductID=1,
ProductName="Test Product",
CategoryID=1,
SupplierID=1,
QuantityPerUnit="10 boxes",
UnitPrice=10.0,
UnitsInStock=100,
UnitsOnOrder=0,
ReorderLevel=0,
Discontinued="1",
)
# DO NOT CHANGE THIS FIXTURE AS ITS VALUE IS USED IN MANY TESTS
@pytest.fixture
def mock_category():
return Category(
CategoryID=1,
CategoryName="Test Category",
Description="Test Category Description",
Picture=b"Test Category Picture",
)
# DO NOT CHANGE THIS FIXTURE AS ITS VALUE IS USED IN MANY TESTS
@pytest.fixture
def mock_supplier():
return Supplier(
SupplierID=1,
CompanyName="Test Company",
ContactName="Test Contact",
ContactTitle="Test Contact Title",
Address="Test Address",
City="Test City",
Region="Test Region",
PostalCode="Test Postal Code",
Country="Test Country",
Phone="Test Phone",
Fax="Test Fax",
HomePage="Test Home Page",
)
@pytest.fixture(autouse=True)
def cleanup_db(app):
"""Clean up the database after each test."""
yield
with app.app_context():
if hasattr(g, "db"):
g.db.execute("DELETE FROM Shopping_Cart")
g.db.commit()