def test_list(self):
        """Lists of items should be represented properly"""
        params = {
            "name": "My thing",
            "items": [StringConstant("one"),
                      StringConstant("two")]
        }
        made_params = make_parameters(**params)
        expected = '''name: "My thing"\n        items: [one, two]'''
        assert expected == made_params

        params = {"name": "My thing", "items": ["val1", "val2"]}
        made_params = make_parameters(**params)
        expected = '''name: "My thing"\n        items: ["val1", "val2"]'''
        assert expected == made_params
    def test_make_parameters(self):
        """A regular set of parameters"""

        params = {"name": "My thing", "source": "The internet"}
        made_params = make_parameters(**params)
        expected = '''name: "My thing"\n        source: "The internet"'''
        assert expected == made_params
Exemple #3
0
def format_mutation(mutationname: str, args: Dict[str, Any]):
    """Create a mutation to send to the Contributor Environment.
    Arguments:
        mutationname: the name of the mutation to generate
        args: a dictionary of field: value pairs to add to the mutation
    Returns:
        A formatted mutation
    """

    formatted_mutation = MUTATION_TEMPLATE.format(
        mutationname=mutationname, parameters=make_parameters(**args))
    return MUTATION.format(mutation=formatted_mutation)
Exemple #4
0
def mutation_update(args, mutation_string: str):
    """Returns a mutation for updating an object
    Arguments:
        args: a dictionary of arguments for the template. The fucntion calling this function is responsible for validating the arguments.
    Returns:
        The string for the mutation for updating the object.
    Raises:
        Assertion error if the input language is not one of the supported languages.
    """

    create_mutation = mutation_string.format(parameters=make_parameters(
        **args))
    return MUTATION.format(mutation=create_mutation)
Exemple #5
0
def create_alias_mutation(mutationalias: str, mutationname: str,
                          args: Dict[str, Any]):
    """Create a mutation alias to send to the Contributor Environment.
    Arguments:
        mutationalias: the alias of the mutation to generate
        mutationname: the name of the mutation to generate
        args: a dictionary of field: value pairs to add to the mutation
    Returns:
        A mutation string
    """
    return MUTATION_ALIAS_TEMPLATE.format(mutationalias=mutationalias,
                                          mutationname=mutationname,
                                          parameters=make_parameters(**args))
Exemple #6
0
def mutation_delete(identifier: str, mutation_string: str):
    """Returns a mutation for deleting an object
    Arguments:
        identifier: The unique identifier of the object.
    Returns:
        The string for the mutation for creating the object.
    Raises:
        Assertion error if the input language is not one of the supported languages.
    """

    args = {"identifier": identifier}

    delete_mutation = mutation_string.format(parameters=make_parameters(
        **args))
    return MUTATION.format(mutation=delete_mutation)
Exemple #7
0
def format_itemlist_query(queryname: str, args: Dict[str, Any]):
    """Create a query to send to the Contributor Environment.
    Arguments:
        queryname: the name of the query to generate
        args: a dictionary of field: value pairs to add to the query.
        return_items_list: A list of items for the query to return.
    Returns:
        A formatted query
    """

    parameters = ""
    if args:
        parameters = make_parameters(**args)
    formatted_query = QUERY_ITEMLIST_TEMPLATE.format(queryname=queryname,
                                                     parameters=parameters)
    return QUERY.format(query=formatted_query)
Exemple #8
0
def format_query(queryname: str, args: Dict[str, Any],
                 return_items: Union[list, str]):
    """Create a query to send to the Contributor Environment.
    Arguments:
        queryname: the name of the query to generate
        args: a dictionary of field: value pairs to add to the query.
        return_items: A list of items for the query to return, or a formatted string of graphql.
    Returns:
        A formatted query
    """

    parameters = ""
    if args:
        parameters = "({})".format(make_parameters(**args))
    if isinstance(return_items, list):
        return_items = make_select_query(return_items)
    formatted_query = QUERY_TEMPLATE.format(queryname=queryname,
                                            parameters=parameters,
                                            return_items=return_items)
    return QUERY.format(query=formatted_query)
    def test_Neo4jDate(self):
        """Neo4jDate values should output a date format with dateparts year, month and day encapsulated in {}"""
        params = {"date": _Neo4jDate(date(2020, 1, 15))}
        made_params = make_parameters(**params)
        expected = '''date: { year: 2020 month: 1 day: 15 }'''
        assert expected, made_params == "Date format did not equal expected result"

        params = {"date": _Neo4jDate([2020, 1, 15])}
        made_params = make_parameters(**params)
        expected = '''date: { year: 2020 month: 1 day: 15 }'''
        assert expected == made_params, "Year, month, day dateparts did not equal expected result"

        params = {"date": _Neo4jDate([2020, 1])}
        made_params = make_parameters(**params)
        expected = '''date: { year: 2020 month: 1 }'''
        assert expected == made_params, "Year, month dateparts did not equal expected result"

        params = {"date": _Neo4jDate([2020])}
        made_params = make_parameters(**params)
        expected = '''date: { year: 2020 }'''
        assert expected == made_params, "Year datepart did not equal expected result"

        params = {"date": _Neo4jDate(2020)}
        made_params = make_parameters(**params)
        expected = '''date: { year: 2020 }'''
        assert expected == made_params, "Year integer did not equal expected result"

        params = {"date": _Neo4jDate([2020, 1, 15, 13, 30])}
        made_params = make_parameters(**params)
        expected = '''date: { year: 2020 month: 1 day: 15 }'''
        assert expected == made_params, "Year, month, day and more values did not output a date"

        # Date formatted as a string
        params = {"date": _Neo4jDate("2020-01-15")}
        made_params = make_parameters(**params)
        expected = '''date: { year: 2020 month: 1 day: 15 }'''
        assert expected == made_params, "Year, month, day and more values did not output a date"
async def request_controlaction(req_config_file='req_config1.ini'):
    """
    Request a control action based on the configurations in the req_config_file
    Arguments:
        req_config_file: The ini file with the configuration for the request to be sent.
    Raises:
        ValueNotFound exception if one of the required values for the property or property value specifications is not set.
    """
    config = configparser.ConfigParser()
    config.read(req_config_file)

    entrypoint_id = config['EntryPoint']['ce_id']
    controlaction_id = config['ControlAction']['ce_id']
    num_props = int(config['ControlAction']['numprops'])
    num_pvs = int(config['ControlAction']['numpvs'])
    props = []
    pvss = []
    for i in range(num_props):
        prop_dict = {}
        prop = config['Property{}'.format(i + 1)]
        prop_dict['potentialActionPropertyIdentifier'] = prop['ce_id']
        if prop['value'] == '':
            raise ValueNotFound('potentialActionPropertyIdentifier{}'.format(1 + 1))
        prop_dict['nodeIdentifier'] = prop['value']
        prop_dict['nodeType'] = StringConstant(prop['rangeincludes'])
        # TODO: Right now, assumes that only one value is given.
        prop_params = make_parameters(**prop_dict)
        props.append("{{{}}}".format(prop_params))

    for i in range(num_pvs):
        pvs_dict = {}
        pvs = config['PropertyValueSpecification{}'.format(i + 1)]
        pvs_dict['potentialActionPropertyValueSpecificationIdentifier'] = pvs['ce_id']
        if pvs['value'] == '' and pvs.getboolean('valuerequired'):
            raise ValueNotFound('potentialActionPropertyValueSpecificationIdentifier{}'.format(1 + 1))
        pvs_dict['value'] = pvs['value']
        pvs_dict['valuePattern'] = StringConstant(pvs['valuepattern'])
        pvs_params = make_parameters(**pvs_dict)
        pvss.append("{{{}}}".format(pvs_params))
    param_dict = {"entryPointIdentifier": entrypoint_id, "potentialActionIdentifier": controlaction_id, \
                  "propertyObject": props, "propertyValueObject": pvss}
    params = make_parameters(**param_dict)

    params = params.replace("\\n", "\n").replace("\\", "").replace("\"\"", "\"").replace("\"{", "{").replace("}\"", "}")

    query = q1.format(params=params)

    resp_1 = await submit_query(query)

    output_id = resp_1['data']['RequestControlAction']['identifier']

    # await subscribe_controlaction(control_id)

    status_query = q2.format(control_id=output_id)

    act_status = 'accepted'

    while act_status == 'accepted' or act_status == 'running':
        await asyncio.sleep(1)

        resp_2 = await submit_query(status_query)

        act_status = resp_2['data']['ControlAction'][0]['actionStatus']

        print("Action Status: {}".format(act_status))
 def test_boolean(self):
     """Boolean values should be represented as json bools (lowercase true/false)"""
     params = {"name": "My thing", "valueRequired": True}
     made_params = make_parameters(**params)
     expected = '''name: "My thing"\n        valueRequired: true'''
     assert expected == made_params
 def test_string_constant(self):
     """String constants are represented without quotes around them"""
     params = {"name": "My thing", "language": StringConstant("en")}
     made_params = make_parameters(**params)
     expected = '''name: "My thing"\n        language: en'''
     assert expected == made_params