Skip to content

Commit

Permalink
Merge d51f63f into 14dae08
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhamgenomics committed Jun 23, 2017
2 parents 14dae08 + d51f63f commit 9ec3211
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
15 changes: 9 additions & 6 deletions pyclarity_lims/lims.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,21 @@ def get(self, uri, params=dict()):
else:
return self.parse_response(r)

def get_file_contents(self, id=None, uri=None):
def get_file_contents(self, id=None, uri=None, encoding=None, crlf=False):
"""Returns the contents of the file of <ID> or <uri>"""
if id:
segments = ['api', self.VERSION, 'files', id, 'download']
url = self.get_uri('files', id, 'download')
elif uri:
segments = [uri, 'download']
url = uri.rstrip('/') + '/download'
else:
raise ValueError("id or uri required")
url = urljoin(self.baseuri, '/'.join(segments))
raise ValueError('id or uri required')

r = self.request_session.get(url, auth=(self.username, self.password), timeout=TIMEOUT)
self.validate_response(r)
return r.text
if encoding:
r.encoding = encoding

return r.text.replace('\r\n', '\n') if crlf else r.text

def upload_new_file(self, entity, file_to_upload):
"""Upload a file and attach it to the provided entity."""
Expand Down
19 changes: 12 additions & 7 deletions tests/test_lims.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import xml
from unittest import TestCase

from requests.exceptions import HTTPError
Expand All @@ -19,6 +18,7 @@
from unittest.mock import patch, Mock
import builtins


class TestLims(TestCase):
url = 'http://testgenologics.com:4040'
username = 'test'
Expand All @@ -36,12 +36,10 @@ class TestLims(TestCase):
<exc:exception xmlns:exc="http://pyclarity_lims.com/ri/exception">
</exc:exception>"""


def test_get_uri(self):
lims = Lims(self.url, username=self.username, password=self.password)
assert lims.get_uri('artifacts',sample_name='test_sample') == '{url}/api/v2/artifacts?sample_name=test_sample'.format(url=self.url)


def test_parse_response(self):
lims = Lims(self.url, username=self.username, password=self.password)
r = Mock(content = self.sample_xml, status_code=200)
Expand All @@ -56,7 +54,6 @@ def test_parse_response(self):
r = Mock(content = self.error_no_msg_xml, status_code=400)
self.assertRaises(HTTPError, lims.parse_response, r)


@patch('requests.Session.get',return_value=Mock(content = sample_xml, status_code=200))
def test_get(self, mocked_instance):
lims = Lims(self.url, username=self.username, password=self.password)
Expand Down Expand Up @@ -88,7 +85,6 @@ def test_post(self):
self.assertRaises(HTTPError, lims.post, uri=uri, data=self.sample_xml)
assert mocked_put.call_count == 1


@patch('os.path.isfile', return_value=True)
@patch.object(builtins, 'open')
def test_upload_new_file(self, mocked_open, mocked_isfile):
Expand Down Expand Up @@ -124,8 +120,6 @@ def test_route_artifact(self, mocked_post):
lims.route_artifacts(artifact_list=[artifact], workflow_uri=self.url+'/api/v2/configuration/workflows/1')
assert mocked_post.call_count == 1



def test_tostring(self):
lims = Lims(self.url, username=self.username, password=self.password)
from xml.etree import ElementTree as ET
Expand All @@ -139,5 +133,16 @@ def test_tostring(self):
string = lims.tostring(etree)
assert string == expected_string

def test_get_file_contents(self):
lims = Lims(self.url, username=self.username, password=self.password)
lims.validate_response = Mock()
lims.request_session = Mock(get=Mock(return_value=Mock(encoding=None, text='some data\r\n')))
exp_url = self.url + '/api/v2/files/an_id/download'

assert lims.get_file_contents(uri=self.url + '/api/v2/files/an_id') == 'some data\r\n'
assert lims.request_session.get.return_value.encoding is None
lims.request_session.get.assert_called_with(exp_url, auth=(self.username, self.password), timeout=16)

assert lims.get_file_contents(id='an_id', encoding='utf-16', crlf=True) == 'some data\n'
assert lims.request_session.get.return_value.encoding == 'utf-16'
lims.request_session.get.assert_called_with(exp_url, auth=(self.username, self.password), timeout=16)

0 comments on commit 9ec3211

Please sign in to comment.