-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep1.py
50 lines (37 loc) · 1.14 KB
/
step1.py
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
import csv
import arrow
import flask
from flask import request
import flask_webtest
import webtest
app = flask.Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
csv_file = request.files['csv_file']
age_sum = 0
csv_lines = csv_file.read().decode('utf-8').splitlines()
for row in csv.reader(csv_lines):
date_delta = arrow.now() - arrow.get(row[1])
age_sum += date_delta.days // 365
return 'Age sum: {}'.format(age_sum)
else:
return flask.render_template('page.html')
class TestStep:
@classmethod
def setup_class(cls):
app.testing = True
def test_get(self):
ta = flask_webtest.TestApp(app)
resp = ta.get('/')
assert 'Upload CSV file' in resp.text
def test_post(self):
ta = flask_webtest.TestApp(app)
resp = ta.get('/')
resp.form['csv_file'] = webtest.Upload(
'foo.csv',
b'foo, 2000-01-01\nbar, 1980-01-01',
content_type='text/csv'
)
resp = resp.form.submit()
assert resp.text == 'Age sum: 56'