-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_main.py
94 lines (79 loc) · 3.05 KB
/
test_main.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
from fastapi.testclient import TestClient
from fastapi import status
from main import app
client = TestClient(app)
data = {
"key": "1",
"value": "padmaja"
}
data2 = {
"key": " ",
"value": "padmaja"
}
def test_create_value():
response = client.put("/cache/1", json=data)
assert response.status_code == status.HTTP_201_CREATED
assert response.json() == data
key = " "
assert client.put(f"/cache/{key}", json=data2).status_code == status.HTTP_404_NOT_FOUND
assert client.put(f"/cache/{key}", json=data2).json() == {"detail": "Invalid key"}
def test_get_value():
response = client.get("/cache/1")
assert response.status_code == status.HTTP_200_OK
assert response.json() == data
assert client.get("/cache/2").status_code == status.HTTP_404_NOT_FOUND
assert client.get("/cache/2").json() == {"detail": "Value with key 2 not found"}
def test_view_keys():
response = client.get("/cache/all/keys")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"keys": [data["key"]]}
def test_get_entire_cache():
response = client.get("/cache")
assert response.status_code == status.HTTP_200_OK
assert response.json() == {'data': {data["key"]: data["value"]}}
def test_delete():
response = client.delete("/cache/1")
assert response.status_code == status.HTTP_204_NO_CONTENT
assert client.get("/cache/1").status_code == 404
def test_set_bulk_value():
key1, value1 = "key1", "value1"
key2, value2 = "key2", "value2"
key3, value3 = "key3", "value3"
key4, value4 = " ", "value4"
request_body = {
"items": [
{"key": key1, "value": value1},
{"key": key2, "value": value2},
{"key": key3, "value": value3},
]
}
request_body2 = {
"items": [
{"key": key1, "value": value1},
{"key": key2, "value": value2},
{"key": key4, "value": value4},
]
}
response = client.put("/bulkcache", json=request_body)
assert response.status_code == 201
assert response.json() == {'message': 'Set successfully'}
assert client.put(f"/bulkcache", json=request_body2).status_code == status.HTTP_404_NOT_FOUND
def test_get_cache_values():
key1, value1 = "key1", "value1"
key2, value2 = "key2", "value2"
key3, value3 = "key3", "value3"
request_body = {"keys": [key1, key2, key3]}
response = client.get("/bulkcache", params={"keys": ["key1", "key2", "key3"]})
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"values": {key1: value1, key2: value2, key3: value3}}
def test_delete_bulk():
response = client.delete("/bulkcache", params={"keys": ["key1", "key2", "key3"]})
assert response.status_code == status.HTTP_204_NO_CONTENT
data1 = {
"maxsize": 256,
"ttl": 300
}
def test_configure_cache():
response = client.put("/configure", json=data1)
assert response.status_code == status.HTTP_202_ACCEPTED
assert response.json() == {"data": f"new maxsize set to {data1['maxsize']} and ttl set to {data1['ttl']}"}