-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
97 lines (83 loc) · 3.79 KB
/
test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import unittest
import requests
import os
import json
LISTEN_URI = os.getenv('LISTEN_URI')
SERVER_IP = os.getenv('SERVER_IP')
SERVER_PORT = os.getenv('SERVER_PORT')
URL = 'http://' + SERVER_IP + ':' + SERVER_PORT + '/kv'
class TestLuaServer(unittest.TestCase):
def test_post_success(self):
data = [
{'key': 'one', 'value': json.dumps('1')}
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.post(URL, json=data[0]).status_code, 200)
self.assertEqual(requests.delete(URL + '/' + data[0]['key']).status_code, 200)
def test_post_already_exist(self):
data = [
{'key': 'one', 'value': json.dumps('1')}
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.post(URL, json=data[0]).status_code, 200)
self.assertEqual(requests.post(URL, json=data[0]).status_code, 409)
self.assertEqual(requests.delete(URL + '/' + data[0]['key']).status_code, 200)
def test_post_invalid_data(self):
data = [
{'key': 3, 'value': json.dumps(0)},
{'keys': '4', 'value': json.dumps(['a', 'b'])},
{'key': '5', 'val': json.dumps([])},
{'key': '6', 'value': 's'}
]
self.assertEqual(requests.post(URL, json=data[0]).status_code, 400)
self.assertEqual(requests.post(URL, json=data[1]).status_code, 400)
self.assertEqual(requests.post(URL, json=data[2]).status_code, 400)
self.assertEqual(requests.post(URL, json=data[3]).status_code, 400)
def test_get_success(self):
data = [
{'key': 'equation', 'value': json.dumps({'12+34': 46})},
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.post(URL, json=data[0]).status_code, 200)
self.assertEqual(requests.get(URL + '/' + data[0]['key']).status_code, 200)
self.assertEqual(requests.get(URL + '/' + data[0]['key']).text, data[0]['value'])
self.assertEqual(requests.delete(URL + '/' + data[0]['key']).status_code, 200)
def test_get_not_found(self):
data = [
{'key': 'equation', 'value': json.dumps({'12+34': 46})},
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.get(URL + '/' + data[0]['key']).status_code, 404)
def test_put_success(self):
data = [
{'key': 'equation', 'value': json.dumps({'12+34': 46})},
{'value': json.dumps({'a': 'b'})},
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.post(URL, json=data[0]).status_code, 200)
self.assertEqual(requests.put(URL + '/' + data[0]['key'], json=data[1]).status_code, 200)
self.assertEqual(requests.delete(URL + '/' + data[0]['key']).status_code, 200)
def test_put_invalid_data(self):
data = [
{'key': 'equation', 'value': json.dumps({'12+34': 46})},
{'value': 1},
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.post(URL, json=data[0]).status_code, 200)
self.assertEqual(requests.put(URL + '/' + data[0]['key'], json=data[1]).status_code, 400)
self.assertEqual(requests.delete(URL + '/' + data[0]['key']).status_code, 200)
def test_put_not_found(self):
data = [
{'key': 'notFoundKey'},
{'value': '1'},
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.put(URL + '/' + data[0]['key'], json=data[1]).status_code, 404)
def test_delete_not_found(self):
data = [
{'key': 'equation'},
]
requests.delete(URL + '/' + data[0]['key'])
self.assertEqual(requests.delete(URL + '/' + data[0]['key']).status_code, 404)
if __name__ == '__main__':
unittest.main()