Skip to content

Commit 48fdf7c

Browse files
author
Plamen Milenkov
committed
Add Unit tests to the omdb parser
1 parent 11c6fe1 commit 48fdf7c

File tree

3 files changed

+97
-12
lines changed

3 files changed

+97
-12
lines changed

days/40-42-json-data/log_book.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import logbook
2+
3+
logbook.Logger('App')
4+
5+
def init_logger(filename: str = None):
6+
level = logbook.TRACE
7+
8+
if filename:
9+
logbook.TimedRotatingFileHandler(filename, level=level).push_application()
10+
else:
11+
logbook.StreamHandler(sys.stdout, level=level).push_application()
12+
13+
msg = 'Logging initialized, level: {}, mode: {}'.format(
14+
level, "stdout mode" if not filename else 'file mode: ' + filename)
15+
logger = logbook.Logger('Startup')
16+
logger.notice(msg)
17+

days/40-42-json-data/omdb_parse.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,63 @@
11
import json
22
from pprint import pprint
33
import requests
4+
import logbook
5+
from log_book import init_logger
46

5-
user_input = input('Movie name: ')
7+
logger = logbook.Logger(__file__)
68

7-
request = requests.get('http://www.omdbapi.com/?apikey=3e6f7dd7&t={}'.format(user_input))
8-
json_result = json.loads(request.text)
99

10-
print("{}'s Awards: {}".format(user_input, json_result['Awards']))
10+
def main():
11+
init_logger('movie-app.log')
12+
logbook.info("Starting the omdb search app...")
1113

14+
logbook.debug("Getting user's input...")
15+
movie_name = get_user_input()
16+
17+
logbook.debug("Loading '{0}' data...".format(movie_name))
18+
movie_data = load_movie_data(movie_name)
19+
20+
21+
logbook.debug("Displaying Awards...".format(movie_name))
22+
display_awards(movie_data, movie_name)
23+
24+
logbook.debug("Displaying Ratings...".format(movie_name))
25+
displays_ratings(movie_data)
26+
27+
def get_user_input():
28+
user_input = input('Movie name: ')
29+
return user_input
30+
31+
def load_movie_data(movie_name):
32+
request = requests.get('http://www.omdbapi.com/?apikey=3e6f7dd7&t={}'.format(movie_name))
33+
json_result = json.loads(request.text)
34+
return json_result
35+
36+
def display_awards(movie_data, movie_name):
37+
print("{}'s Awards: {}".format(movie_name, movie_data['Awards']))
38+
39+
def displays_ratings(movie_data):
40+
for rating in movie_data['Ratings']:
41+
display_rating(rating)
42+
43+
def display_rating(rating):
44+
value = rating['Value']
45+
value = parse_value(value)
46+
print(f"{rating['Source']} - {value}")
1247

1348
def parse_value(value: str):
49+
logbook.debug("Parsing value '{0}'...".format(value))
1450
if value.endswith('%'):
1551
value = value.replace('%','')
1652
return '{}/10'.format(int(value) / 10)
1753
elif value.endswith('100'):
1854
(value, _) = value.split('/')
1955
return '{}/10'.format(int(value) / 10)
20-
return value
21-
56+
else:
57+
(value, _) = value.split('/')
58+
return '{}/10'.format(float(value) * 1.0)
2259

23-
def display_rating(rating):
24-
value = rating['Value']
25-
value = parse_value(value)
26-
print(f"{rating['Source']} - {value}")
2760

2861

29-
for rating in json_result['Ratings']:
30-
display_rating(rating)
62+
if __name__ == '__main__':
63+
main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
from omdb_parse import load_movie_data, parse_value
4+
5+
def test_load_movie_data():
6+
movie_data_it = load_movie_data('It')
7+
assert movie_data_it != None
8+
9+
assert 'Title' in movie_data_it
10+
assert 'It' == movie_data_it['Title']
11+
assert 'Year' in movie_data_it
12+
assert 'Awards' in movie_data_it
13+
14+
def test_load_movie_data_empty_name():
15+
empty_name = load_movie_data('')
16+
assert empty_name != None
17+
assert 'Response' in empty_name
18+
assert 'Error' in empty_name
19+
20+
@pytest.mark.parametrize("arg,ret", [
21+
('0%','0.0/10'),
22+
('1%','0.1/10'),
23+
('100%','10.0/10'),
24+
('67%','6.7/10'),
25+
('0/100','0.0/10'),
26+
('1/100','0.1/10'),
27+
('100/100','10.0/10'),
28+
('67/100','6.7/10'),
29+
('0/10','0.0/10'),
30+
('1/10','1.0/10'),
31+
('10/10','10.0/10'),
32+
('6.7/10','6.7/10'),
33+
])
34+
def test_parse_value(arg, ret):
35+
assert ret == parse_value(arg)

0 commit comments

Comments
 (0)