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
84 lines (69 loc) · 3.65 KB
/
debug.py
File metadata and controls
84 lines (69 loc) · 3.65 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
#!/usr/bin/env python3
from db import Session
from models import Company, Dev, Freebie
session = Session()
import ipdb;
# TEST. Run these in ipdb, these # => indicate expected results.
# Relationship Attributes and Methods
# Freebie
# # Access the first freebie instance in the database
# freebie = session.query(Freebie).first()
# freebie
# # => Freebie(id=1, item_name=others, value=209, company_id=1, dev_id=8)
# Get the Dev who received the freebie
# freebie.dev # == returns the Dev instance for this Freebie.
# # => Dev(id=8, name=Jennifer Hernandez)
# freebie.dev.name
# # => 'Jennifer Hernandez'
# freebie.company # == returns the Company instance for this Freebie.
# # => Company(id=1, name=Carter-Brandt, founding-year=1925)
# Company
# # Access the first company instance in the database
# company = session.query(Company).first()
# company
# # => Company(id=1, name=Carter-Brandt, founding-year=1925)
# company.freebies # == returns a collection of all the freebies for the Company.
# # => [Freebie(id=1, item_name=others, value=209, company_id=1, dev_id=8), Freebie(id=2, item_name=have, value=346, company_id=1, dev_id=12), Freebie(id=3, item_name=same, value=377, company_id=1, dev_id=2)]
# company.devs # == returns a collection of all the devs who collected freebies from the company.
# # => [Dev(id=8, name=Jennifer Hernandez), Dev(id=12, name=Corey Wu), Dev(id=2, name=Willie Goodman)]
# Dev
# # Access the first dev instance in the database
# dev = session.query(Dev).first()
# dev
# # => Dev(id=1, name=Chelsea Long)
# dev.freebies # == returns a collection of all the freebies that the Dev has collected.
# # => [Freebie(id=15, item_name=light, value=151, company_id=5, dev_id=1), Freebie(id=28, item_name=apply, value=87, company_id=8, dev_id=1), Freebie(id=40, item_name=watch, value=189, company_id=11, dev_id=1)]
# dev.companies # == returns a collection of all the companies that the Dev has collected freebies from.
# # => [Company(id=5, name=George LLC, founding-year=1914), Company(id=8, name=Thomas, Anderson and Larson, founding-year=2003), Company(id=11, name=Hall Ltd, founding-year=1938)]
# Aggregate Methods
# Freebie
# print(freebie.print_details()) # using our freebie instance. If you get an error: check print(freebie.__dict__), run freebie = session.query(Freebie).first() first or quit and restart python debug.py
# # => Jennifer Hernandez owns a others from Carter-Brandt
# Company
# Testing give_freebie(dev, item_name, value) method in Company class.
company = session.query(Company).first()
dev = session.query(Dev).first()
freebie = company.give_freebie(dev, item_name="T-shirt", value=70, session=session)
print(freebie.print_details())
# # => Chelsea Long owns a T-shirt from Carter-Brandt
# company.oldest_company()
# # => Company(id=2, name=David-Boyd, founding-year=1886)
# Dev
# dev = session.query(Dev).first() get first dev
# print(dev.received_one('gas'))
# # => False
# print(dev.received_one('light'))
# # => True
# Testing give_away(dev, freebie) method in Dev class.
dev1 = session.query(Dev).filter(Dev.name == "Jennifer Hernandez").first()
dev2 = session.query(Dev).filter(Dev.name == "Anita Romero").first() # id 3 in devs table
freebie = session.query(Freebie).first() # freebie to give away. According to the freebies table, the first conform to dev_id 8, "Jennifer Hernandez"
# Ensure the freebie belongs to dev1 before trying to give it away
if freebie.dev == dev1:
updated_freebie = dev1.give_away(dev2, freebie)
print(f"Freebie '{updated_freebie.item_name}' has been transferred to {dev2.name}.")
else:
print(f"Freebie '{freebie.item_name}' does not belong to {dev1.name}.")
# session.commit()
session.close() # close
ipdb.set_trace()