Skip to content

Commit

Permalink
🔧 Make sure dynamic customer categories possible (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronkurz authored Mar 24, 2022
1 parent 78eaeb6 commit 9583e87
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 151 deletions.
47 changes: 26 additions & 21 deletions api-tests/batch_policy_proposal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def after_all():
utils.remove_everything_from_db()


def test_first_one_automatically_created():
@pytest.mark.parametrize("customer_categories", [["gov", "public"], ["corporate", "sme"]])
def test_first_one_automatically_created(customer_categories):
""" Test whether the first, naive bapol proposal is created for a new proposal """
utils.post_processes_a_b("helicopter_license", "./resources/bpmn/helicopter/helicopter_vA.bpmn",
"./resources/bpmn/helicopter/helicopter_vB.bpmn",
customer_categories=["public", "gov"], default_version='a',
customer_categories=customer_categories,
default_version='a',
path_history="./resources/bpmn/helicopter/helicopter_vA_100.json")
assert utils.get_process_count() == 1
params = {
Expand All @@ -35,6 +37,14 @@ def test_first_one_automatically_created():
assert response.status_code == requests.codes.ok
assert True is response.json().get('newProposalExists')

# make sure that customer categories are correct
response_cust_cats = []
for exec_strat in response.json().get('proposal').get('executionStrategy'):
response_cust_cats.append(exec_strat.get('customerCategory'))
customer_categories.sort()
response_cust_cats.sort()
assert response_cust_cats == customer_categories

# make sure the id is of the process just posted
bapol_prop_process_id = response.json().get('proposal').get('processId')
response_process_meta = requests.get(BASE_URL + "/process/active/meta")
Expand All @@ -45,27 +55,14 @@ def test_first_one_automatically_created():
assert None is response.json().get('proposal').get('baPolId')


def test_new_proposal_after_batch():
@pytest.mark.parametrize("customer_categories", [["gov", "public"], ["corporate", "sme"]])
def test_new_proposal_after_batch(customer_categories):
utils.post_processes_a_b("helicopter_license", "./resources/bpmn/helicopter/helicopter_vA.bpmn",
"./resources/bpmn/helicopter/helicopter_vB.bpmn",
customer_categories=["public", "gov"], default_version='a',
customer_categories=customer_categories, default_version='a',
path_history="./resources/bpmn/helicopter/helicopter_vA_100.json")
assert utils.get_bapol_proposal_count_active_process() == 1
utils.post_bapol_currently_active_process({
"batchSize": 5,
"executionStrategy": [
{
"customerCategory": "public",
"explorationProbabilityA": 0.3,
"explorationProbabilityB": 0.7
},
{
"customerCategory": "gov",
"explorationProbabilityA": 0.7,
"explorationProbabilityB": 0.3
}
]
})
utils.post_bapol_currently_active_process(utils.example_batch_policy_size(5, customer_categories))
assert utils.get_bapol_count() == 1
cs.start_client_simulation(5, 1)
assert utils.get_bapol_proposal_count_active_process() == 2
Expand All @@ -74,6 +71,14 @@ def test_new_proposal_after_batch():
assert response.status_code == requests.codes.ok
assert True is response.json().get('newProposalExists')

# make sure that customer categories are correct
response_cust_cats = []
for exec_strat in response.json().get('proposal').get('executionStrategy'):
response_cust_cats.append(exec_strat.get('customerCategory'))
customer_categories.sort()
response_cust_cats.sort()
assert response_cust_cats == customer_categories


def test_requests_in_between_batches():
bapol_5_size = {
Expand Down Expand Up @@ -122,7 +127,7 @@ def test_requests_in_between_batches():


def test_after_manual_decision_no_proposal():
bapol_5_size = utils.example_batch_policy_size(5)
bapol_5_size = utils.example_batch_policy_size(5, ["gov", "public"])
utils.post_processes_a_b("fast", "./resources/bpmn/fast_a_better/fast_a_better_vA.bpmn",
"./resources/bpmn/fast_a_better/fast_a_better_vB.bpmn",
customer_categories=["public", "gov"], default_version='a',
Expand All @@ -146,7 +151,7 @@ def test_bapol_prop_goes_away_cool_off():
customer_categories=["public", "gov"], default_version='a',
path_history="./resources/bpmn/fast_a_better/fast_a_better_vA_100.json")
# finish a batch
utils.post_bapol_currently_active_process(utils.example_batch_policy_size(5))
utils.post_bapol_currently_active_process(utils.example_batch_policy_size(5, ["gov", "public"]))
cs.start_client_simulation(5, 1)
# one open proposal
assert utils.new_open_proposal_exists_active_process()
Expand Down
25 changes: 14 additions & 11 deletions api-tests/batch_policy_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
import requests

import utils
from config import BASE_URL

Expand Down Expand Up @@ -83,12 +82,13 @@ def test_set_bapol_failing_json():
assert response.status_code == 400


def test_set_bapol_failing_customer_category():
@pytest.mark.parametrize("customer_categories", [["gov", "public"], ["corporate", "sme"]])
def test_set_bapol_failing_customer_category(customer_categories):
# given
utils.post_processes_a_b("fast",
"./resources/bpmn/fast_a_better/fast_a_better_vA.bpmn",
"./resources/bpmn/fast_a_better/fast_a_better_vB.bpmn",
customer_categories=["public", "gov"], default_version='a',
customer_categories=customer_categories, default_version='a',
path_history="./resources/bpmn/fast_a_better/fast_a_better_vA_100.json")
bapol = {
"batchSize": 200,
Expand All @@ -99,7 +99,7 @@ def test_set_bapol_failing_customer_category():
"explorationProbabilityB": 0.7
},
{
"customer_Category": "enterprise",
"customer_Category": "corporate",
"explorationProbabilityA": 0.7,
"explorationProbabilityB": 0.3
}
Expand All @@ -113,23 +113,25 @@ def test_set_bapol_failing_customer_category():
assert response.status_code == 400


def test_get_latest():
@pytest.mark.parametrize("customer_categories", [["gov", "public"], ["corporate", "sme"]])
def test_get_latest(customer_categories):
""" Test if retrieval of the latest batch policy works """
# given
utils.post_processes_a_b("helicopter_license", "./resources/bpmn/helicopter/helicopter_vA.bpmn",
"./resources/bpmn/helicopter/helicopter_vB.bpmn",
customer_categories=["public", "gov"], default_version='a',
customer_categories=customer_categories, default_version='a',
path_history="./resources/bpmn/helicopter/helicopter_vA_100.json")
utils.post_bapol_currently_active_process({
utils.post_bapol_currently_active_process(
{
"batchSize": 200,
"executionStrategy": [
{
"customerCategory": "public",
"customerCategory": customer_categories[0],
"explorationProbabilityA": 0.3,
"explorationProbabilityB": 0.7
},
{
"customerCategory": "gov",
"customerCategory": customer_categories[1],
"explorationProbabilityA": 0.7,
"explorationProbabilityB": 0.3
}
Expand All @@ -145,9 +147,10 @@ def test_get_latest():
assert response_json.get("processId") == utils.get_currently_active_process_id()
for i in range(2):
exec_strat = response_json.get("executionStrategy")[i]
if exec_strat.get("customerCategory") == "public":
assert exec_strat.get("customerCategory") in customer_categories
if exec_strat.get("customerCategory") == customer_categories[0]:
assert exec_strat.get("explorationProbabilityA") == 0.3
assert exec_strat.get("explorationProbabilityB") == 0.7
if exec_strat.get("customerCategory") == "gov":
if exec_strat.get("customerCategory") == customer_categories[1]:
assert exec_strat.get("explorationProbabilityA") == 0.7
assert exec_strat.get("explorationProbabilityB") == 0.3
16 changes: 5 additions & 11 deletions api-tests/client_simulator_api_tests.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
""" Use this to simulate client requests in the API tests"""
import random
from time import sleep

import requests
from numpy.random import normal

from config import BASE_URL
from utils import get_currently_active_process_id
from utils import get_currently_active_process_id, get_currently_active_process_meta

CUSTOMER_CATEGORIES = ["public", "gov"]
NORMAL_DIST_STD_DEV = 0.1


def _get_random_customer_category():
return CUSTOMER_CATEGORIES[random.randint(0, len(CUSTOMER_CATEGORIES) - 1)]


def send_request_for_new_processes_instance(process_id):
def send_request_for_new_processes_instance(process_id, customer_categories):
params = {
"process-id": process_id,
"customer-category": _get_random_customer_category()
"customer-category": customer_categories[random.randint(0, len(customer_categories) - 1)]
}
response = requests.get(BASE_URL + "/instance-router/start-instance", params=params)
assert response.status_code == requests.codes.ok
return response.json().get("camunda_instance_id")


def start_client_simulation(amount_of_requests_to_send: int, avg_break_sec: float):
customer_categories = get_currently_active_process_meta().get('customer_categories').split('-')
currently_active_p_id = get_currently_active_process_id()
for i in range(amount_of_requests_to_send):
send_request_for_new_processes_instance(currently_active_p_id)
send_request_for_new_processes_instance(currently_active_p_id, customer_categories)
normal_sample = normal(avg_break_sec, NORMAL_DIST_STD_DEV)
sleep_value = normal_sample if normal_sample >= 0 else 0
sleep(sleep_value)
Loading

0 comments on commit 9583e87

Please sign in to comment.