def test_json_to_outputs__json_from_file(): """ Given - valid json file: {"aaa":100,"bbb":"foo"} - prefix: XDR.Incident - command: xdr-get-incidents When - passed to json_to_outputs Then - ensure outputs generated in the following format arguments: [] name: xdr-get-incidents outputs: - contextPath: XDR.Incident.aaa description: '' type: Number - contextPath: XDR.Incident.bbb description: '' type: String """ yaml_output = parse_json(data='{"aaa":100,"bbb":"foo"}', command_name='xdr-get-incidents', prefix='XDR.Incident') assert yaml_output == '''arguments: []
def test_json_to_outputs__invalid_json(): """ Given - invalid json file {"aaa":100 When - passed to json_to_outputs Then - ensure the function raises clear error that indicates that json is invalid """ try: parse_json(data='{"aaa":100', command_name='xdr-get-incidents', prefix='XDR.Incident') assert False except Exception as ex: assert str(ex) == 'Invalid input JSON'
def dict_from_outputs_str(command: str, outputs: str, verbose=False): """ Create a pythonic dict from the yml outputs string. Args: command: the command to parse. outputs: the json outputs to parse into a dict. verbose: whether to run in verbose mode or not. """ dict_output = parse_json(outputs, command, "", verbose, return_object=True) return dict_output
def test_json_to_outputs__a_list_of_dict(): """ Given - A list of dictionaries When - Passed to json_to_outputs Then - ensure the returned type is correct """ yaml_output = parse_json(data='[{"a": "b", "c": "d"}, {"a": 1}]', command_name='jira-ticket', prefix='Jira.Ticket', descriptions={"a": DUMMY_FIELD_DESCRIPTION}) assert yaml_output == f'''arguments: []
def test_json_to_outputs__invalid_description_dictionary( description_dictionary, expected_a_description): """ Given - A list of dictionaries When - Passed to json_to_outputs Then - ensure the returned type is correct """ yaml_output = parse_json(data='[{"a": "b", "c": "d"}, {"a": 1}]', command_name='jira-ticket', prefix='Jira.Ticket', descriptions=description_dictionary) assert yaml_output == f'''arguments: []
def test_json_to_outputs__detect_date(time_created): """ Given - valid json {"create_at": "2019-10-10T00:00:00"} When - passed to json_to_outputs Then - ensure the type of create_at is Date """ yaml_output = parse_json( data=json.dumps({'created_at': time_created}), command_name='jira-ticket', prefix='Jira.Ticket', descriptions={'created_at': 'time when the ticket was created.'}) assert yaml_output == '''arguments: []
def test_json_to_outputs_return_object(): """ Given - valid json file: {"aaa":100,"bbb":"foo"} - prefix: XDR.Incident - command: xdr-get-incidents When - passed to json_to_outputs with return_object=True Then - ensure outputs generated aer a pythonic object and not yaml arguments: [] name: xdr-get-incidents outputs: - contextPath: XDR.Incident.aaa description: '' type: Number - contextPath: XDR.Incident.bbb description: '' type: String """ yaml_output = parse_json( data='{"aaa":100,"bbb":"foo"}', command_name='xdr-get-incidents', prefix='XDR.Incident', return_object=True, ) assert yaml_output == { 'arguments': [], 'name': 'xdr-get-incidents', 'outputs': [{ 'contextPath': 'XDR.Incident.aaa', 'description': '', 'type': 'Number' }, { 'contextPath': 'XDR.Incident.bbb', 'description': '', 'type': 'String' }] }