Skip to content

Commit

Permalink
Passing building of url through Lims.get_uri, adding tests for id and…
Browse files Browse the repository at this point in the history
… uri.
  • Loading branch information
mwhamgenomics committed Jun 23, 2017
1 parent e43d179 commit d51f63f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
9 changes: 4 additions & 5 deletions pyclarity_lims/lims.py
Expand Up @@ -92,15 +92,14 @@ def get(self, uri, params=dict()):
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)

if encoding:
r.encoding = encoding

Expand Down
10 changes: 5 additions & 5 deletions tests/test_lims.py
Expand Up @@ -133,16 +133,16 @@ def test_tostring(self):
string = lims.tostring(etree)
assert string == expected_string

@patch('pyclarity_lims.lims.urljoin', return_value='a_url')
def test_get_file_contents(self, mocked_urljoin):
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(id='an_id') == 'some data\r\n'
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'

mocked_urljoin.assert_called_with('http://testgenologics.com:4040/', 'api/v2/files/an_id/download')
lims.request_session.get.assert_called_with(exp_url, auth=(self.username, self.password), timeout=16)

0 comments on commit d51f63f

Please sign in to comment.