Skip to content

Commit

Permalink
a todo list with external data source and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Shirtliffe committed Aug 7, 2019
1 parent f57a5d0 commit e3e2868
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
pickleDB==0.9.2
pkg-resources==0.0.0
pycodestyle==2.5.0
requests==2.22.0
Expand Down
106 changes: 106 additions & 0 deletions todo_list/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import os
import requests
from string import Template
import urllib
import pickledb

def print_header():
""" Print header
"""
header_text = ''
TEXT = f' Todo List \n'
line = '-' * len(TEXT)
line += '\n'

header_text += line
header_text += TEXT
header_text += line
print(header_text)

def get_task():
name = input('Enter task: ')
return name

def store_task(task):
if task:
auto_dump = True
db = pickledb.load('tasks.db', auto_dump)
db.set(task, False)
# db.dump()

def print_tasks(tasks):
print('Tasks: ')
for task in tasks:
print(task)

def get_tasks():
auto_dump = True
db = pickledb.load('tasks.db', auto_dump)
return db.getall()

def delete_task(task):
auto_dump = True
db = pickledb.load('tasks.db', auto_dump)
if task and db.exists(task):
db.rem(task)
db.dump()

def get_movie_name():

while True:
name = input('Enter the name of a movie: ')
if len(name) > 0:
return urllib.parse.quote(name)
else:
print('A valid input required.')


def get_movie_data(movie_name):
api_key = os.environ['OMD_API_KEY']
url = f'https://www.omdbapi.com/?apikey={api_key}&t={movie_name}'
response = requests.get(url)
response.raise_for_status
if response.ok:
return response

def print_movie_data(data):

title = data['Title']
year = data['Year']
rated = data['Rated']
runtime = data['Runtime']
plot = data['Plot']

print(f'Title: {title}')
print(f'Year: {year}')
print(f'Rated: {rated}')
print(f'Runtime: {runtime}')
print(f'Plot: {plot}')
for rating in data.get('Ratings'):
if rating['Source'] == "Rotten Tomatoes":
rating = int(rating['Value'].replace('%', ''))
if rating >= 80:
print('You should watch this film right now!')


if __name__ == "__main__":
print_header()
task = get_task()
while task:
store_task(task)
task = get_task()

print()
tasks = get_tasks()
print_tasks(tasks)

print('\nDelete task')
task = get_task()
while task:
delete_task(task)
task = get_task()

print()
tasks = get_tasks()
print_tasks(tasks)

1 change: 1 addition & 0 deletions todo_list/tasks.db
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"go for a walk": false, "book appointment": false, "smoke weed": false, "egg food": false, "walk like a chimp": false, "Pick up some milk": false, "dsf": false}
Empty file added todo_list/tests/__init__.py
Empty file.
Empty file.
98 changes: 98 additions & 0 deletions todo_list/tests/system/app_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os
from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import patch, call
import app

import pickledb


from string import Template


class AppTest(TestCase):

def test_print_header(self):

expected = '------------\n Todo List \n------------\n'
with patch('builtins.print') as mocked_print:
app.print_header()
mocked_print.assert_called_with(expected)

def test_get_task(self):
expected = 'Enter task: '
with patch('builtins.input', return_value='Pick up some milk') as mocked_input:
with patch('builtins.print') as mocked_print:
task = app.get_task()
mocked_input.assert_called_with(expected)
self.assertEqual(task, 'Pick up some milk')

def test_store_task(self):
task = 'Pick up some milk'
with patch('pickledb.load') as mocked_load:
mock = MagicMock()
mocked_load.return_value = mock
app.store_task(task)
mocked_load.assert_called_with('tasks.db', True)
mock.set.assert_called_with('Pick up some milk', False)

def test_print_tasks(self):
expected_calls = [call('Tasks: '), call('go for a walk'), call('Pick up some milk')]
tasks = {"go for a walk": False, "Pick up some milk": False}
with patch('builtins.print') as mocked_print:
app.print_tasks(tasks.keys())
mocked_print.assert_has_calls(expected_calls)

def test_get_tasks(self):
with patch('pickledb.load') as mocked_load:
mock = MagicMock()
mocked_load.return_value = mock
tasks = app.get_tasks()
mocked_load.assert_called_with('tasks.db', True)
mock.getall.assert_called()

def test_delete_task(self):
task = 'Pick up some milk'
with patch('pickledb.load') as mocked_load:
mock = MagicMock()
mocked_load.return_value = mock
app.delete_task(task)
mocked_load.assert_called_with('tasks.db', True)
mock.exists.assert_called()
mock.rem.assert_called_with(task)


def get_movie_data(self):
movie_name = 'Guardians%20of%20the%20Galaxy'
with patch('requests.get') as mocked_request_get:
mocked_request_get.return_value.status_code = 200
response = app.get_movie_data(movie_name)
self.assertEqual(response.status_code, 200)
api_key = os.environ['OMD_API_KEY']
mocked_request_get.assert_called_with(f'https://www.omdbapi.com/?apikey={api_key}&t=Guardians%20of%20the%20Galaxy')


def print_movie_data(self):

expected = [
call('Title: Guardians of the Galaxy'),
call('Year: 2014'),
call('Rated: PG-13'),
call('Runtime: 121 min'),
call('Plot: A group of intergalactic criminals must pull together to stop a fanatical warrior with plans to purge the universe.'),
call('You should watch this film right now!')
]

with patch('builtins.print') as mocked_print:
data = {
'Title': 'Guardians of the Galaxy',
'Year': '2014',
'Rated': 'PG-13',
'Released': '01 Aug 2014',
'Runtime': '121 min',
'Plot': 'A group of intergalactic criminals must pull together to stop a fanatical warrior with plans to purge the universe.',
'Ratings':[{'Source':'Rotten Tomatoes','Value':'91%'}]
}

app.print_movie_data(data)
mocked_print.assert_has_calls(expected)
Empty file.
5 changes: 5 additions & 0 deletions todo_list/tests/unit/unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from unittest import TestCase
import app

class FilteringRecordsTest(TestCase):
pass

0 comments on commit e3e2868

Please sign in to comment.