-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flask.py
executable file
·77 lines (65 loc) · 2.22 KB
/
test_flask.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import json
import pytest
from lxml import html
from io import StringIO
from unittest import mock
from hello import app
@pytest.fixture
def client():
client = app.test_client()
return client
@pytest.fixture
def db_data():
cart = {
'notebook': 5,
'TV': 10,
'smartphone': 25,
'flash': 155
}
return StringIO(json.dumps(cart))
def test_index(client):
response = client.get('/')
response = response.data.decode('utf-8')
assert 'Hello, my dear customer.' in response
def test_post_items_update(client, db_data):
with mock.patch('hello.open') as mocked:
mocked.return_value = db_data
response = client.post(
'/items',
data={
'notebook_name': 'notebook',
'notebook_quantity': 5,
'TV_name': 'TV',
'TV_quantity': 10
}
)
response = response.data.decode('utf-8')
assert '<input type="text" value="notebook" name="notebook_name">' in response
assert '<input type="text" value="5" name="notebook_quantity">' \
in response
assert '<input type="text" value="TV" name="TV_name">' in response
assert '<input type="text" value="10" name="TV_quantity">' \
in response
def test_post_items_add(client, db_data):
with mock.patch('hello.open') as mocked:
mocked.return_value = db_data
response = client.post(
'/items',
data={'add': ''}
)
response = response.data.decode('utf-8')
response = html.fromstring(response)
assert len(response.cssselect('input[name="__name"]')) == 1
assert len(response.cssselect('input[name="__quantity"]')) == 1
def test_post_items_remove(client, db_data):
with mock.patch('hello.open') as mocked:
mocked.return_value = db_data
response = client.post(
'/items', data={
'flash_name': 'flash',
'flash_quantity': 155,
'flash_delete': 'on'
}
)
response = response.data.decode('utf-8')
assert 'flash' not in response