Skip to content

Commit

Permalink
mplement parsing of output for Templatereplacer doubler test
Browse files Browse the repository at this point in the history
This means that the parsing of the output file can be moved from
the daemon test to the actual parser itself
  • Loading branch information
sphuber committed Nov 7, 2017
1 parent c395eed commit 53c3479
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
55 changes: 36 additions & 19 deletions .travis-data/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,42 @@ def jobs_have_finished(pks):
print "{}/{} finished".format(num_finished, len(finished_list))
return not (False in finished_list)

def print_logshow(pk):
print "Output of 'verdi calculation logshow {}':".format(pk)
try:
print subprocess.check_output(
["verdi", "calculation", "logshow", "{}".format(pk)],
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as e2:
print "Note: the command failed, message: {}".format(e2.message)

def validate_calculations(expected_results):
valid = True
actual_value = None
for pk, expected_value in expected_results.iteritems():
actual_dict = {}
for pk, expected_dict in expected_results.iteritems():
calc = load_node(pk)
if not calc.has_finished_ok():
print 'Calculation<{}> status was not FINISHED'.format(pk)
print_logshow(pk)
return False

try:
actual_value = int(calc.out.retrieved.folder.open('path/output.txt').read())
except (AttributeError, IOError, ValueError) as e:
print "* UNABLE TO RETRIEVE VALUE for calc pk={}: I expected {}, I got {}: {}".format(
pk, expected_value, type(e), e)

print "Output of 'verdi calculation logshow {}':".format(pk)
try:
print subprocess.check_output(
["verdi", "calculation", "logshow", "{}".format(pk)],
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as e2:
print "Note: the command failed, message: {}".format(e2.message)
valid = False
actual_dict = calc.out.output_parameters.get_dict()
except (KeyError, AttributeError) as exception:
print 'Could not retrieve output_parameters node for Calculation<{}>'.format(pk)
print_logshow(pk)
return False

if actual_value != expected_value:
try:
actual_dict['retrieved_temporary_files'] = dict(actual_dict['retrieved_temporary_files'])
except KeyError:
# If the retrieval fails we simply pass as the following check of the actual value will fail anyway
pass

if actual_dict != expected_dict:
print "* UNEXPECTED VALUE {} for calc pk={}: I expected {}".format(
actual_value, pk, expected_value)
actual_dict, pk, expected_dict)
valid = False

return valid
Expand Down Expand Up @@ -119,7 +131,12 @@ def main():
calc.store_all()
print "[{}] created calculation {}, pk={}".format(
counter, calc.uuid, calc.dbnode.pk)
expected_results_calculations[calc.pk] = inputval*2
expected_results_calculations[calc.pk] = {
'value': inputval * 2,
'retrieved_temporary_files': {
'triple_value.tmp': str(inputval * 3)
}
}
calc.submit()
print "[{}] calculation submitted.".format(counter)

Expand Down
29 changes: 28 additions & 1 deletion aiida/parsers/simpleplugins/templatereplacer/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from aiida.orm import CalculationFactory
from aiida.parsers.parser import Parser
from aiida.orm.data.parameter import ParameterData

TemplatereplacerCalculation = CalculationFactory('simpleplugins.templatereplacer')

Expand All @@ -27,6 +28,26 @@ def parse_with_retrieved(self, retrieved):
:param retrieved: a dictionary of retrieved nodes
"""
output_nodes = []

try:
output_file = self._calc.inp.template.get_dict()['output_file_name']
except KeyError:
self.logger.error("the output file name 'output_file_name' was not specified in the 'template' input node")
return False, ()

retrieved_folder = retrieved[self._calc._get_linkname_retrieved()]
try:
parsed_value = int(retrieved_folder.get_file_content(output_file).strip())
except (AttributeError, IOError, ValueError) as e:
self.logger.error("* UNABLE TO RETRIEVE VALUE for calc pk={}: I got {}: {}".format(self._calc.pk, type(e), e))
return False, ()

output_dict = {
'value': parsed_value,
'retrieved_temporary_files': []
}

try:
retrieve_temporary_files = self._calc.inp.template.get_dict()['retrieve_temporary_files']
except KeyError:
Expand All @@ -46,4 +67,10 @@ def parse_with_retrieved(self, retrieved):
self.logger.error('the file {} was not found in the temporary retrieved folder'.format(retrieved_file))
return False, ()

return True, ()
# We always strip the content of the file from whitespace to simplify testing for expected output
output_dict['retrieved_temporary_files'].append((retrieved_file, temporary_folder.get_file_content(retrieved_file).strip()))

output_parameters = ParameterData(dict=output_dict)
output_nodes.append((self.get_linkname_outparams(), output_parameters))

return True, output_nodes

0 comments on commit 53c3479

Please sign in to comment.