import json def _verify_single(schedule): assert isinstance(schedule.get("id"), str), "schedule ID is not a string" assert isinstance(schedule.get("name"), str), "schedule name is not a string" assert isinstance(schedule.get("periods"), list), "schedule periods is not a list" for period in schedule.get("periods"): assert isinstance(period, dict), "schedule periods contain a periods which is not a dict" assert isinstance(period.get("start"), str), "schedule periods contain a periods with start not being a string" assert isinstance(period.get("end"), str), "schedule periods contain a periods with end not being a string" assert isinstance(schedule.get("tags"), list), "schedule tags is not a list" for tag in schedule.get("tags"): assert isinstance(tag, str), "schedule tags contain a tag which is not a string" def single(response): _verify_single(response.json()) def multiple(response): assert isinstance(response.json(), list), "response is not a list" for schedule in response.json(): _verify_single(schedule) def check_name(response, name): response_name = response.json().get("name") assert response_name == name, f"schedule name check failed (expected: '{name}'; actual: '{response_name}')" def check_id(response, id): assert response.json().get("id") == id, "schedule id check failed" def check_periods(response, periods): periods_json = json.loads(periods.replace("'", "\"")) assert len(periods_json) == len(response.json().get("periods")), "periods in response and request have different lengths" for request_period in periods_json: found_in_response = False for response_period in response.json().get("periods"): if response_period.get("start") != request_period.get("start"): continue if response_period.get("end") != request_period.get("end"): continue found_in_response = True if not found_in_response: print(request_period) assert False, "a period from the request was missing from the response" def check_tag(response, tag): for response_tag in response.json().get("tags"): if response_tag == tag: return assert False, "tag not found in schedule," def compare_off(response): assert response.json().get("id") == "off", "schedule off did not return id off" assert len(response.json().get("periods")) == 0, "schedule off has periods" def compare_on(response): assert response.json().get("id") == "on", "schedule on did not return id on" assert len(response.json().get("periods")) == 1, "schedule on has unexpected amount of periods" assert response.json().get("periods")[0].get("start") == "00:00", "Schedule on has unexpected start" assert response.json().get("periods")[0].get("end") == "00:00", "Schedule on has unexpected start" def find(response, id=None, name=None, periods=None, tag=None): if periods != None: periods_json = json.loads(periods.replace("'", "\"")) for schedule in response.json(): if id != None and id != schedule.get("id"): print(schedule.get("id")) continue if name != None and name != schedule.get("name"): print(schedule.get("name")) continue if periods != None: if len(periods_json) != len(schedule.get("periods")): continue for request_period in periods_json: found_in_response = False for response_period in schedule.get("periods"): if response_period.get("start") != request_period.get("start"): continue if response_period.get("end") != request_period.get("end"): continue found_in_response = True if not found_in_response: continue if tag != None: found_in_response = False for response_tag in schedule.get("tags"): if response_tag == tag: found_in_response = True if not found_in_response: continue return assert False, "schedule not found in list"