Skip to content

Commit b5053ef

Browse files
committed
stater code for python testing project.
1 parent 30f9e0c commit b5053ef

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

apps/py/.idea/py.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/py/ch09_testing/guitar_app.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import lib
2+
3+
4+
def main():
5+
print("Welcome to the guitar app.")
6+
while True:
7+
print()
8+
style = input("What style of guitar do you want to see? ")
9+
if not style:
10+
print("cya")
11+
break
12+
13+
guitars = lib.all_guitars(style)
14+
print(f"We found {len(guitars)} guitars.")
15+
for idx, g in enumerate(guitars, start=1):
16+
print(f'{idx}. {g.name} for ${g.price:,.0f} ({g.style})')
17+
18+
19+
if __name__ == '__main__':
20+
main()

apps/py/ch09_testing/lib.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import List, Optional
2+
from colorama import Fore
3+
4+
5+
class Guitar:
6+
def __init__(self, name: str = None, price: float = 0.0,
7+
img: str = None, style: str = None):
8+
self.style = style
9+
self.img = img
10+
self.price = price
11+
self.name = name
12+
13+
14+
def all_guitars(style: Optional[str]) -> List[Guitar]:
15+
if not style or not style.strip():
16+
raise ValueError("'style' cannot be empty.")
17+
18+
style = style.lower()
19+
20+
log(f"Guitars for {style}")
21+
guitars = get_guitars_from_db()
22+
if style == 'all':
23+
return guitars
24+
25+
return [
26+
g
27+
for g in guitars
28+
if g.style == style
29+
]
30+
31+
32+
def log(msg: str):
33+
# raise Exception("NO LOG!")
34+
print(Fore.YELLOW + "LOGGING THIS TO A FILE, SHOULD NOT SEE IN TEST: " + Fore.WHITE + f"{msg}.")
35+
36+
37+
# noinspection DuplicatedCode
38+
def get_guitars_from_db():
39+
# raise Exception("NO DB!")
40+
print(Fore.YELLOW + "GETTING GUITARS FROM DB! Should not see in test." + Fore.WHITE)
41+
# This guitar data simulates what we would actually get back from the database.
42+
guitars = [
43+
Guitar('AX Black', 499, '/static/img/guitars/ax-black.jpg', style='electric'),
44+
Guitar('Jet Black Electric', 599, '/static/img/guitars/jet-black-electric.jpg', style='electric'),
45+
Guitar('Weezer Classic', 1499, '/static/img/guitars/weezer-classic.jpg', style='electric'),
46+
Guitar('Acoustic Black', 1299, '/static/img/guitars/black-acoustic.jpg', style='acoustic'),
47+
Guitar('Mellow Yellow', 799, '/static/img/guitars/mellow-yellow.jpg', style='electric'),
48+
Guitar('White Vibes', 699, '/static/img/guitars/white-vibes.jpg', style='electric'),
49+
Guitar('Brush Riffs', 599, '/static/img/guitars/brushed-black-electric.jpg', style='electric'),
50+
Guitar("Nature's Song", 799, '/static/img/guitars/natures-song.jpg', style='electric'),
51+
Guitar('Electric Wood Grain', 399, '/static/img/guitars/woodgrain-electric.jpg', style='electric'),
52+
]
53+
guitars.sort(key=lambda g: g.price, reverse=True)
54+
return guitars

0 commit comments

Comments
 (0)