Skip to content

Commit

Permalink
Merge pull request #42 from EdinburghGenomics/import_from_genologics
Browse files Browse the repository at this point in the history
Import from genologics
  • Loading branch information
Timothee Cezard committed Aug 17, 2018
2 parents 1a9f7d1 + 0ceac8c commit 6e6eebb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
24 changes: 20 additions & 4 deletions pyclarity_lims/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Per Kraulis, Science for Life Laboratory, Stockholm, Sweden.
Copyright (C) 2012 Per Kraulis
"""
from decimal import Decimal

from pyclarity_lims.constants import nsmap

Expand Down Expand Up @@ -36,8 +37,8 @@ def __init__(self, nesting):
else:
self.rootkeys = []

def rootnode(self, instance):
_rootnode = instance.root
def rootnode_from_root(self, root):
_rootnode = root
for rootkey in self.rootkeys:
childnode = _rootnode.find(rootkey)
if childnode is None:
Expand All @@ -46,6 +47,9 @@ def rootnode(self, instance):
_rootnode = childnode
return _rootnode

def rootnode(self, instance):
return self.rootnode_from_root(instance.root)


class XmlMutable(XmlElement):
"""Class that receive an instance so it can be mutated in place"""
Expand Down Expand Up @@ -172,7 +176,7 @@ def _setitem(self, key, value):
if not self._is_string(value):
raise TypeError('Text UDF requires str or unicode value')
elif vtype == 'numeric':
if not isinstance(value, (int, float)):
if not isinstance(value, (int, float, Decimal)):
raise TypeError('Numeric UDF requires int or float value')
value = str(value)
elif vtype == 'boolean':
Expand Down Expand Up @@ -200,7 +204,7 @@ def _setitem(self, key, value):
elif isinstance(value, bool):
vtype = 'Boolean'
value = value and 'true' or 'false'
elif isinstance(value, (int, float)):
elif isinstance(value, (int, float, Decimal)):
vtype = 'Numeric'
value = str(value)
elif isinstance(value, datetime.date):
Expand Down Expand Up @@ -765,6 +769,16 @@ def _parse_element(self, element, lims, **kwargs):
queue_date = datetime.datetime.strptime(qt, date_format)
list.append(self, (input_art, queue_date, location))

def _update_elems(self):
root = self.instance.root
self._elems = []
while root:
self._elems += self.rootnode_from_root(root).findall(self.tag)
if root.find('next-page') is not None:
root = self.instance.lims.get(root.find('next-page').attrib.get('uri'))
else:
root = None


# Descriptors: This section contains the objects that can be used in entities
class BaseDescriptor(XmlElement):
Expand Down Expand Up @@ -884,6 +898,8 @@ def __set__(self, instance, value):
node = ElementTree.Element(self.tag)
self.rootnode(instance).append(node)
node.attrib['uri'] = value.uri
# if value._TAG in ['project', 'sample', 'artifact', 'container']:
# node.attrib['limsid'] = value.id


class DimensionDescriptor(TagDescriptor):
Expand Down
58 changes: 56 additions & 2 deletions tests/test_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ def test_set(self):

class TestQueuedArtifactList(TestCase):
def setUp(self):
et = ElementTree.fromstring('''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
queue_txt = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test-entry>
<artifacts>
<artifact uri="{url}/artifacts/a1">
Expand All @@ -805,10 +805,52 @@ def setUp(self):
</location>
</artifact>
</artifacts>
</test-entry>'''.format(url='http://testgenologics.com:4040/api/v2'))
</test-entry>'''


self.et_page1 = ElementTree.fromstring('''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test-entry>
<artifacts>
<artifact uri="{url}/artifacts/a1">
<queue-time>2011-12-25T01:10:10.050+00:00</queue-time>
<location>
<container uri="{url}/containers/c1"/>
<value>A:1</value>
</location>
</artifact>
</artifacts>
<next-page uri="{url}/queues/q1?page2=500"/>
</test-entry>'''.format(url='http://testgenologics.com:4040/api/v2'))
self.et_page2 = ElementTree.fromstring('''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test-entry>
<artifacts>
<artifact uri="{url}/artifacts/a2">
<queue-time>2011-12-25T01:10:10.200+01:00</queue-time>
<location>
<container uri="{url}/containers/c1"/>
<value>A:2</value>
</location>
</artifact>
</artifacts>
<next-page uri="{url}/queues/q1?page3=500"/>
</test-entry>'''.format(url='http://testgenologics.com:4040/api/v2'))
self.et_page3 = ElementTree.fromstring('''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test-entry>
<artifacts>
<artifact uri="{url}/artifacts/a3">
<queue-time>2011-12-25T01:10:10.050-01:00</queue-time>
<location>
<container uri="{url}/containers/c1"/>
<value>A:3</value>
</location>
</artifact>
</artifacts>
</test-entry>'''.format(url='http://testgenologics.com:4040/api/v2'))
et = ElementTree.fromstring(queue_txt.format(url='http://testgenologics.com:4040/api/v2'))

self.lims = Lims('http://testgenologics.com:4040', username='test', password='password')
self.instance1 = Mock(root=et, lims=self.lims)
self.instance2 = Mock(root=self.et_page1, lims=self.lims)

def get_queue_art(self, art_id, pos, microsec, time_delta):
if version_info[0] == 2:
Expand Down Expand Up @@ -838,3 +880,15 @@ def test_set(self):
qart = self.get_queue_art('a1', 'A:4', 50000, datetime.timedelta(0, 0))
with pytest.raises(NotImplementedError):
queued_artifacts.append(qart)

def test_parse_multipage(self):
self.lims.get = Mock(side_effect=[self.et_page2, self.et_page3])
queued_artifacts = QueuedArtifactList(self.instance2)
qart = self.get_queue_art('a1', 'A:1', 50000, datetime.timedelta(0, 0))
assert queued_artifacts[0] == qart
qart = self.get_queue_art('a2', 'A:2', 200000, datetime.timedelta(0, 3600))
assert queued_artifacts[1] == qart
qart = self.get_queue_art('a3', 'A:3', 50000, datetime.timedelta(0, -3600))
assert queued_artifacts[2] == qart


5 changes: 4 additions & 1 deletion tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,17 @@ def test_create_entity(self):
container=Container(self.lims, uri='container'),
position='1:1',
name='s1',
udf={'test1': 'test1value'}
)
data = '''<?xml version=\'1.0\' encoding=\'utf-8\'?>
<smp:samplecreation xmlns:smp="http://genologics.com/ri/sample">
<smp:samplecreation xmlns:smp="http://genologics.com/ri/sample" xmlns:udf="http://genologics.com/ri/userdefined">
<name>s1</name>
<project uri="project" />
<location>
<container uri="container" />
<value>1:1</value>
</location>
<udf:field name="test1" type="String">test1value</udf:field>
</smp:samplecreation>'''
print(patch_post.call_args_list[0][1]['data'])
assert elements_equal(ElementTree.fromstring(patch_post.call_args_list[0][1]['data']), ElementTree.fromstring(data))

0 comments on commit 6e6eebb

Please sign in to comment.