Ejemplo n.º 1
0
def vital_unit_validation(field_name, resource, system_url):

    path = field_name.split('.')
    path.pop(0)

    systems_to_validate = utils.traverse(resource, path + ["system"])
    codes_to_validate = utils.traverse(resource, path + ["code"])
    resource_components = utils.traverse(resource, ["component"])
    values_to_validate = [resource] + (resource_components or [])

    if not isinstance(systems_to_validate, list):
        systems_to_validate = [systems_to_validate]

    if not isinstance(codes_to_validate, list):
        codes_to_validate = [codes_to_validate]

    if any(system != system_url for system in systems_to_validate):
        return {"resource": resource, "status": "Wrong System"}

    if any(not in_value_set({"code": code}, system_url) for code in codes_to_validate):
        return {"resource": resource, "status": "Invalid Code"}

    for value in values_to_validate:
        codes_with_unit_requirements = [c["code"] for c in value["code"]["coding"]
                                        if c["code"] in vitals_code_lookup]

        for code_with_unit_requirement in codes_with_unit_requirements:

            required_code_list = vitals_code_lookup[code_with_unit_requirement]

            if not value["valueQuantity"]["code"] in required_code_list:
                return {"resource": resource, "status": "Mismatched vital unit and vital type"}

    return None
Ejemplo n.º 2
0
def step_impl(context, system_url, field_name):
    if not StepDecider(context).should_run_test():
        return

    path = field_name.split('.')
    filter_type = path.pop(0)
    resources = get_resources(context.response.json(), filter_type)

    bad_resource_results = []

    for res in resources:
        if ArgonautObservationDecider(res).should_validate():
            found = utils.traverse(res, path)

            if found:
                vital_validation_status = vital_unit_validation(field_name, res, system_url)

                if vital_validation_status:
                    bad_resource_results.append(vital_validation_status)

    assert len(bad_resource_results) == 0, utils.bad_response_assert_with_resource(
        response=context.response,
        message=ERROR_UCUM_CODES,
        resource=bad_resource_results,
        field_name=field_name)
Ejemplo n.º 3
0
def step_impl(context, field_name, value):

    if not StepDecider(context).should_run_test():
        return

    path = field_name.split('.')
    filter_type = path.pop(0)
    resources = get_resources(context.response.json(), filter_type)

    for res in resources:
        if ArgonautObservationDecider(res).should_validate():
            found = utils.traverse(res, path)

            assert found, utils.bad_response_assert_with_resource(
                response=context.response,
                message=ERROR_FIELD_NOT_PRESENT,
                resource=res,
                field=field_name,
                json=json.dumps(res, indent=2))

            assert value in found, utils.bad_response_assert_with_resource(
                response=context.response,
                message=ERROR_WRONG_FIXED,
                resource=res,
                values=found,
                value=value)
Ejemplo n.º 4
0
def step_impl(context, resource_type, field_name):

    if not StepDecider(context).should_run_test():
        return

    path = field_name.split('.')
    filter_type = path.pop(0)
    resources = get_resources(context.response.json(), filter_type)

    for res in resources:
        try:
            reference = utils.traverse(res, path).get('reference')

            # Validate the reference for FHIR compliance formatting.
            # http://hl7.org/fhir/references.html
            reference_regex = r'((http|https)://([A-Za-z0-9\\\.\:\%\$]\/)*)?(' + \
                resource_type + r')\/[A-Za-z0-9\-\.]{1,64}(\/_history\/[A-Za-z0-9\-\.]{1,64})?'
            compiled_regex = re.compile(reference_regex)
            regex_search_results = compiled_regex.search(reference)

        except AttributeError:
            reference = ''

        assert regex_search_results, \
            utils.bad_response_assert(context.response,
                                      ERROR_REFERENCE_MATCH,
                                      reference=reference,
                                      resource_type=resource_type)
Ejemplo n.º 5
0
def step_impl(context, field_name, sub_field):

    if not StepDecider(context).should_run_test():
        return

    path = field_name.split('.')
    filter_type = path.pop(0)
    sub_path = sub_field.split('.')
    sub_type = sub_path.pop(0)
    resources = get_resources(context.response.json(), filter_type)

    for res in resources:
        found = utils.traverse(res, path)
        for item in found:
            match = utils.traverse(item, sub_path)
            assert match is not None, \
                utils.bad_response_assert_with_resource(response=context.response,
                                                        message=ERROR_REQUIRED,
                                                        resource=res,
                                                        name=sub_type)
Ejemplo n.º 6
0
def step_impl(context, name, field_name):

    if not StepDecider(context).should_run_test():
        return

    path = field_name.split('.')
    filter_type = path.pop(0)
    resources = get_resources(context.response.json(), filter_type)

    for res in resources:
        found = utils.traverse(res, path)
        assert len(found) > 0, utils.bad_response_assert(context.response,
                                                         ERROR_REQUIRED,
                                                         name=name)
Ejemplo n.º 7
0
def step_impl(context, name, field_one_name, field_two_name):

    if not StepDecider(context).should_run_test():
        return

    path_one = field_one_name.split('.')
    path_two = field_two_name.split('.')

    filter_type = path_one.pop(0)
    assert filter_type == path_two.pop(0)

    resources = get_resources(context.response.json(), filter_type)

    for res in resources:
        found_one = utils.traverse(res, path_one)
        found_two = utils.traverse(res, path_two)

        assert (found_one is not None) or (found_two is not None), \
            utils.bad_response_assert_with_resource(response=context.response,
                                                    message=ERROR_REQUIRED,
                                                    resource=res,
                                                    name=name
                                                    )
Ejemplo n.º 8
0
def step_impl(context, field_name, value_set_url_one, value_set_url_two):

    if not StepDecider(context).should_run_test():
        return

    path = field_name.split('.')
    filter_type = path.pop(0)
    resources = get_resources(context.response.json(), filter_type)
    system_names = '{0} or {1}'.format(value_set_url_one, value_set_url_two)

    for res in resources:
        found = utils.traverse(res, path)
        if isinstance(found, str):
            found = [found]
        elif isinstance(found, dict):
            assert 'coding' in found, \
                utils.bad_response_assert_with_resource(response=context.response,
                                                        message=ERROR_CODING_MISSING,
                                                        resource=res,
                                                        field_name=field_name,
                                                        json=json.dumps(found, indent=2))
            found = [
                coding.get('code') for coding in found.get('coding')
                if in_value_set(coding, value_set_url_one)
                or in_value_set(coding, value_set_url_two)
            ]

        assert found, \
            utils.bad_response_assert_with_resource(response=context.response,
                                                    message=ERROR_MISSING_SYSTEM_CODING,
                                                    resource=res,
                                                    field_name=field_name,
                                                    system=system_names)

        for code in found:
            try:
                valid = systems.validate_code(code, value_set_url_one) or \
                        systems.validate_code(code, value_set_url_two)
            except systems.SystemNotRecognized:
                valid = False

            assert valid, utils.bad_response_assert_with_resource(
                response=context.response,
                message=ERROR_INVALID_BINDING,
                resource=res,
                code=code,
                system=system_names,
                json=json.dumps(res, indent=2))
Ejemplo n.º 9
0
def found_at_least_one(resources, path, value):
    """
    Return a boolean indicating if we found a resource with
    the specified path declared, having the specified value.
    :param resources: A list of dictionaries.
    :param path: A list representing the steps in the path, top element is NOT the resource type.
    :param value: The value you want the last element in the path to have.
    :return: Boolean
    """
    for res in resources:
        found_path = utils.traverse(res, path)

        if found_path and value in found_path:
            return True

    return False
Ejemplo n.º 10
0
def step_impl(context, field_name, value_set_url_one, value_set_url_two):

    if not StepDecider(context).should_run_test():
        return

    path = field_name.split('.')
    filter_type = path.pop(0)
    resources = get_resources(context.response.json(), filter_type)
    system_names = '{0} or {1}'.format(value_set_url_one, value_set_url_two)

    for res in resources:
        found = utils.traverse(res, path)
        if isinstance(found, str):
            found = [found]
        elif isinstance(found, dict):
            assert 'coding' in found, \
                utils.bad_response_assert_with_resource(response=context.response,
                                                        message=ERROR_CODING_MISSING,
                                                        resource=res,
                                                        field_name=field_name,
                                                        json=json.dumps(found, indent=2))
            found = [coding.get('code') for coding in found.get('coding')
                     if in_value_set(coding, value_set_url_one) or
                     in_value_set(coding, value_set_url_two)]

        assert found, \
            utils.bad_response_assert_with_resource(response=context.response,
                                                    message=ERROR_MISSING_SYSTEM_CODING,
                                                    resource=res,
                                                    field_name=field_name,
                                                    system=system_names)

        for code in found:
            try:
                valid = systems.validate_code(code, value_set_url_one) or \
                        systems.validate_code(code, value_set_url_two)
            except systems.SystemNotRecognized:
                valid = False

            assert valid, utils.bad_response_assert_with_resource(response=context.response,
                                                                  message=ERROR_INVALID_BINDING,
                                                                  resource=res,
                                                                  code=code,
                                                                  system=system_names,
                                                                  json=json.dumps(res, indent=2))