forked from learn-co-curriculum/python-p3-freebie-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
181 lines (142 loc) · 7.19 KB
/
Copy pathdebug.py
File metadata and controls
181 lines (142 loc) · 7.19 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Company, Dev, Freebie
import os
def test_relationships_and_methods():
"""Test all the relationships and methods with the seed data"""
# Fix the database path - use absolute path to freebies.db in current directory
db_path = os.path.join(os.path.dirname(__file__), 'freebies.db')
engine = create_engine(f'sqlite:///{db_path}')
Session = sessionmaker(bind=engine)
session = Session()
print("=" * 60)
print("TESTING SQLALCHEMY RELATIONSHIPS AND METHODS")
print("=" * 60)
try:
# Check if we have data first
company_count = session.query(Company).count()
dev_count = session.query(Dev).count()
freebie_count = session.query(Freebie).count()
if company_count == 0 or dev_count == 0 or freebie_count == 0:
print(" No data found in database!")
print("Please run: python seed.py first")
session.close()
return
print(f"Database contains: {company_count} companies, {dev_count} devs, {freebie_count} freebies")
# Get test data
raila = session.query(Dev).filter_by(name="Raila").first()
ruto = session.query(Dev).filter_by(name="Ruto").first()
rigachi = session.query(Dev).filter_by(name="Rigachi").first()
odm = session.query(Company).filter_by(name="ODM").first()
uda = session.query(Company).filter_by(name="UDA").first()
dcp = session.query(Company).filter_by(name="DCP").first()
if not all([raila, ruto, rigachi, odm, uda, dcp]):
print(" Expected data not found! Please run: python seed.py")
session.close()
return
print("\n1. TESTING BASIC RELATIONSHIPS")
print("-" * 40)
# Test Freebie relationships
print("Testing Freebie.dev and Freebie.company:")
freebie = session.query(Freebie).filter_by(item_name="ODM T-shirts").first()
if freebie:
print(f" Freebie '{freebie.item_name}' belongs to dev: {freebie.dev.name}")
print(f" Freebie '{freebie.item_name}' belongs to company: {freebie.company.name}")
# Test Company.freebies
print(f"\nTesting Company.freebies:")
print(f" ODM freebies: {[f.item_name for f in odm.freebies]}")
print(f" UDA freebies: {[f.item_name for f in uda.freebies]}")
print(f" DCP freebies: {[f.item_name for f in dcp.freebies]}")
# Test Dev.freebies
print(f"\nTesting Dev.freebies:")
print(f" Raila's freebies: {[f.item_name for f in raila.freebies]}")
print(f" Ruto's freebies: {[f.item_name for f in ruto.freebies]}")
print(f" Rigachi's freebies: {[f.item_name for f in rigachi.freebies]}")
# Test Company.devs (many-to-many through freebies)
print(f"\nTesting Company.devs:")
print(f" ODM devs: {[d.name for d in odm.devs]}")
print(f" UDA devs: {[d.name for d in uda.devs]}")
print(f" DCP devs: {[d.name for d in dcp.devs]}")
# Test Dev.companies (many-to-many through freebies)
print(f"\nTesting Dev.companies:")
print(f" Raila's companies: {[c.name for c in raila.companies]}")
print(f" Ruto's companies: {[c.name for c in ruto.companies]}")
print(f" Rigachi's companies: {[c.name for c in rigachi.companies]}")
print("\n2. TESTING FREEBIE METHODS")
print("-" * 40)
# Test Freebie.print_details()
print("Testing Freebie.print_details():")
for freebie in session.query(Freebie).limit(3):
print(f" {freebie.print_details()}")
print("\n3. TESTING COMPANY METHODS")
print("-" * 40)
# Test Company.oldest_company()
print("Testing Company.oldest_company():")
oldest = Company.oldest_company()
if oldest:
print(f" Oldest company: {oldest.name} (founded {oldest.founding_year})")
print("\n4. TESTING DEV METHODS")
print("-" * 40)
# Test Dev.received_one()
print("Testing Dev.received_one():")
print(f" Raila received 'ODM T-shirts': {raila.received_one('ODM T-shirts')}")
print(f" Raila received 'Laptop': {raila.received_one('Laptop')}")
print(f" Ruto received 'Wheelbarrow': {ruto.received_one('Wheelbarrow')}")
print(f" Ruto received 'ODM T-shirts': {ruto.received_one('ODM T-shirts')}")
print("\n5. TESTING AGGREGATE DATA")
print("-" * 40)
# Show summary statistics
total_companies = session.query(Company).count()
total_devs = session.query(Dev).count()
total_freebies = session.query(Freebie).count()
print(f"Total companies: {total_companies}")
print(f"Total devs: {total_devs}")
print(f"Total freebies: {total_freebies}")
# Show value statistics
total_value = sum(f.value for f in session.query(Freebie).all())
print(f"Total value of all freebies: KSh {total_value:,}")
# Show most valuable freebie
most_valuable = session.query(Freebie).order_by(Freebie.value.desc()).first()
if most_valuable:
print(f"Most valuable freebie: {most_valuable.item_name} (KSh {most_valuable.value:,})")
print("\n" + "=" * 60)
print("ALL TESTS COMPLETED SUCCESSFULLY!")
print("=" * 60)
except Exception as e:
print(f" Error during testing: {e}")
finally:
session.close()
if __name__ == '__main__':
# Check if database exists
db_path = os.path.join(os.path.dirname(__file__), 'freebies.db')
if not os.path.exists(db_path):
print(" Database file 'freebies.db' not found!")
print("Please run the following commands first:")
print("1. alembic upgrade head")
print("2. python seed.py")
exit(1)
# Run the tests first
test_relationships_and_methods()
# Then start the interactive session
print("\nStarting interactive debug session...")
print("Available objects: Company, Dev, Freebie")
print("Sample usage:")
print(" raila = session.query(Dev).filter_by(name='Raila').first()")
print(" print(raila.freebies)")
print("\n" + "-" * 60)
engine = create_engine(f'sqlite:///{db_path}')
Session = sessionmaker(bind=engine)
session = Session()
# Make some common objects available in the debug session
try:
raila = session.query(Dev).filter_by(name="Raila").first()
ruto = session.query(Dev).filter_by(name="Ruto").first()
rigachi = session.query(Dev).filter_by(name="Rigachi").first()
odm = session.query(Company).filter_by(name="ODM").first()
uda = session.query(Company).filter_by(name="UDA").first()
dcp = session.query(Company).filter_by(name="DCP").first()
print("Pre-loaded objects: raila, ruto, rigachi, odm, uda, dcp, session")
except Exception as e:
print(f"Warning: Could not pre-load objects: {e}")
import ipdb; ipdb.set_trace()