Skip to content

Commit f57a5d0

Browse files
author
Matthew Shirtliffe
committed
time service with tests
1 parent eb68ab2 commit f57a5d0

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed

time_service/app.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from datetime import datetime
2+
3+
# export FLASK_APP=app.py
4+
# flask run
5+
6+
from flask import Flask
7+
app = Flask(__name__)
8+
9+
10+
def get_now():
11+
return datetime.utcnow()
12+
13+
@app.route('/')
14+
def get_time():
15+
return {'current_time':get_now()}

time_service/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import requests
2+
3+
4+
def get_data():
5+
url = 'http://localhost:5000/'
6+
response = requests.get(url)
7+
response.raise_for_status
8+
if response.ok:
9+
return response
10+
11+
12+
def print_current_time(time):
13+
print(f'The current time is {time}')
14+
15+
16+
if __name__ == "__main__":
17+
data = get_data()
18+
data = data.json()
19+
print_current_time(data['current_time'])

time_service/tests/__init__.py

Whitespace-only changes.

time_service/tests/system/__init__.py

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
from unittest import TestCase
3+
from unittest.mock import patch, call
4+
from app import app
5+
import client
6+
7+
import json
8+
from datetime import datetime
9+
10+
11+
class AppTest(TestCase):
12+
13+
14+
def setUp(self):
15+
app.testing = True
16+
self.app = app.test_client()
17+
18+
def test_home(self):
19+
with self.app as client, patch('app.get_now', return_value=datetime(2015, 8, 4, 12, 22, 44, 123456)) as mocked_datetime:
20+
response = client.get('/')
21+
self.assertEqual(response.status_code, 200)
22+
self.assertEqual(json.loads(response.get_data()), {'current_time': 'Tue, 04 Aug 2015 12:22:44 GMT'})
23+
24+
def test_get_data(self):
25+
with patch('requests.get') as mocked_request_get:
26+
mocked_request_get.return_value.status_code = 200
27+
response = client.get_data()
28+
self.assertEqual(response.status_code, 200)
29+
mocked_request_get.assert_called_with(f'http://localhost:5000/')
30+
31+
32+
def test_print_current_time(self):
33+
with patch('builtins.print') as mocked_print:
34+
client.print_current_time('Tue, 06 Aug 2019 12:03:47 GMT')
35+
mocked_print.assert_called_with('The current time is Tue, 06 Aug 2019 12:03:47 GMT')

time_service/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from unittest import TestCase
2+
import app
3+
4+
class FilteringRecordsTest(TestCase):
5+
pass

0 commit comments

Comments
 (0)