-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_request.py
89 lines (63 loc) · 2.85 KB
/
demo_request.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
import json
import random
from pathlib import Path
import requests
from core_api.constants import CORE_PATH
def _create_random_weights():
random_weight = round(random.uniform(0.1, 0.7), ndigits=1)
print(f'Random weight is {random_weight}')
return {
'1': random_weight,
'2': round(1 - random_weight, ndigits=1)
}
def construct_payload_from_gold_pure_description(weights):
gold_asset_path = CORE_PATH / 'async_tasks' / 'decision_maker' / 'scripts' / 'bin' / 'description_multilevel.json'
with gold_asset_path.open(encoding='utf-8') as f:
task_description_multilevel = json.load(f)
task_description_multilevel['expertWeightsRule'] = weights
return {
'task_description': task_description_multilevel
}
def construct_payload_from_test_file(weights, gold_asset_path):
with gold_asset_path.open(encoding='utf-8') as f:
payload = json.load(f)
payload['task_description']['expertWeightsRule'] = weights
return payload
def make_request_to_service(url, payload):
headers = {
"content-type": "application/json"
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
return response.json()
def check_response(response_json, expected_weights):
print(f'Received weights: {response_json["taskResult"]["expertWeightsRule"]}')
err_msg = 'Weights are not the same. Does decision maker really consider a not hardcoded example?'
assert response_json["taskResult"]["expertWeightsRule"] == expected_weights, err_msg
assert response_json["taskResult"]["alternativesOrdered"], 'Decision Maker cannot return empty list of ordered ' \
'alternatives'
def main():
# host_to_call = 'http://localhost:5000'
host_to_call = 'https://ldss-core-api-app.herokuapp.com'
api_endpoint = '/api/v1/make-decision'
url_to_call = f'{host_to_call}{api_endpoint}'
print(f'Calling {url_to_call}')
print('Checking from pure description...')
weights = _create_random_weights()
print(f'New weights: {weights}')
payload = construct_payload_from_gold_pure_description(weights=weights)
result = make_request_to_service(url_to_call, payload)
check_response(result, weights)
print('Checks passed')
print('Checking from test payload...')
gold_asset_path = Path(r'C:\Users\demidovs\Downloads\testJSON1.json')
if not gold_asset_path.exists():
print('[WARNING] If you want to try with your own test file, provide it first')
return
weights = _create_random_weights()
print(f'New weights: {weights}')
payload = construct_payload_from_test_file(weights=weights, gold_asset_path=gold_asset_path)
result = make_request_to_service(url_to_call, payload)
check_response(result, weights)
print('Checks passed')
if __name__ == '__main__':
main()