From 85159560d27d46b9e04adae9455bf17e0d6a8931 Mon Sep 17 00:00:00 2001 From: tcezard Date: Thu, 14 Feb 2019 12:05:20 +0000 Subject: [PATCH] Add creation of input-output-map entry --- pyclarity_lims/descriptors.py | 34 +++++++++++++++++++++++++++++----- tests/test_descriptors.py | 22 +++++++++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/pyclarity_lims/descriptors.py b/pyclarity_lims/descriptors.py index 46e80c22..91e2a4d5 100644 --- a/pyclarity_lims/descriptors.py +++ b/pyclarity_lims/descriptors.py @@ -664,16 +664,40 @@ def _get_dict(self, lims, node): result[key] = node.attrib[key] except KeyError: pass - for uri in ['uri', 'post-process-uri']: - try: - result[uri] = Artifact(lims, uri=node.attrib[uri]) - except KeyError: - pass + for uri in ['uri', 'post-process-uri']: + try: + result[uri] = Artifact(lims, uri=node.attrib[uri]) + except KeyError: + pass node = node.find('parent-process') if node is not None: result['parent-process'] = Process(lims, node.attrib['uri']) return result + def _set_dict(self, element, value_dict): + for key in ['limsid', 'output-type', 'output-generation-type']: + if key in value_dict: + element.attrib[key] = value_dict[key] + for key in ['uri', 'post-process-uri']: + if key in value_dict: + element.attrib[key] = value_dict[key].uri + if 'parent-process' in value_dict: + node = ElementTree.SubElement(element, 'parent-process') + node.attrib['uri'] = value_dict['parent-process'].uri + + def _create_new_node(self, value ): + if not isinstance(value, tuple): + raise TypeError('You need to provide a tuple not %s' % (type(value))) + if len(value) != 2: + raise TypeError('You need to provide a tuple with 2 values, found %s' % len(value)) + input_dict, output_dict = value + node = ElementTree.Element(self.tag) + input_element = ElementTree.SubElement(node, 'input') + output_element = ElementTree.SubElement(node, 'output') + self._set_dict(input_element, input_dict) + self._set_dict(output_element, output_dict) + return node + class OutputPlacementList(TagXmlList): """This is a list of output placements as found in the StepPlacement. The list contains tuples organised as follows: diff --git a/tests/test_descriptors.py b/tests/test_descriptors.py index 6873caf3..2523475c 100644 --- a/tests/test_descriptors.py +++ b/tests/test_descriptors.py @@ -11,7 +11,7 @@ StringDictionaryDescriptor, IntegerDescriptor, BooleanDescriptor, UdfDictionary, EntityDescriptor, \ InputOutputMapList, EntityListDescriptor, PlacementDictionary, EntityList, SubTagDictionary, ExternalidList,\ XmlElementAttributeDict, XmlAttributeList, XmlReagentLabelList, XmlPooledInputDict, XmlAction, QueuedArtifactList -from pyclarity_lims.entities import Artifact, ProtocolStep, Container +from pyclarity_lims.entities import Artifact, ProtocolStep, Container, Process from pyclarity_lims.lims import Lims from tests import elements_equal @@ -663,6 +663,26 @@ def test___get__(self): assert sorted(res[0][0].keys()) == sorted(expected_keys_input) assert sorted(res[0][1].keys()) == sorted(expected_keys_ouput) + def test_create(self): + et = ElementTree.fromstring(""" + + """) + lims = Mock(cache={}) + instance = Mock(root=et, lims=lims) + res = self.IO_map.__get__(instance, None) + input_dict = {'uri': Artifact(lims, uri='input_uri'), 'limsid': 'a1', 'parent-process': Process(lims, uri='p_uri')} + output_dict = {'uri': Artifact(lims, uri='output_uri'), 'limsid': 'a2', 'output-type': 'PerInput'} + res.append((input_dict, output_dict)) + assert len(et) == 1 + node = et.find('input-output-map') + assert len(node) == 2 # input and output + assert node.find('input').attrib['uri'] == 'input_uri' + assert node.find('input').attrib['limsid'] == 'a1' + assert node.find('input').find('parent-process').attrib['uri'] == 'p_uri' + assert node.find('output').attrib['uri'] == 'output_uri' + assert node.find('output').attrib['limsid'] == 'a2' + assert node.find('output').attrib['output-type'] == 'PerInput' + class TestExternalidList(TestCase): def setUp(self):