Commit c65687e2 authored by Sascha Herzinger's avatar Sascha Herzinger
Browse files

Added some tests for Ada ETLs

parent 9480390a
Pipeline #2238 passed with stage
in 3 minutes and 10 seconds
......@@ -47,6 +47,8 @@ class ETLHandler(metaclass=abc.ABCMeta):
error = "'auth' must be a valid dictionary."
logger.error(error)
raise ValueError(error)
if server.endswith('/'):
server = server[:-1]
self._server = server
# if no token is given we have to get one
try:
......
......@@ -58,4 +58,4 @@ def name_to_label(data: List[dict], descriptor: dict) -> List[dict]:
value = row[descriptor['dictionary']['name']]
del row[descriptor['dictionary']['name']]
row[label] = value
return data
\ No newline at end of file
return data
"""This module provides tests for the string ETL for Ada."""
import json
import pytest
import responses
from fractalis.data.etls.ada.etl_string import StringETL
# noinspection PyMissingOrEmptyDocstring,PyMissingTypeHints
class TestStringETL:
etl = StringETL()
valid_descriptor = {
'dictionary': {
'name': 'foo',
'projection': 'foo',
'label': 'bar',
'fieldType': 'String',
'isArray': False
},
'data_set': 'baz'
}
def test_correct_handler(self):
assert self.etl.can_handle(handler='ada', descriptor={
'dictionary': {'fieldType': 'String'}
})
assert not self.etl.can_handle(handler='transmart', descriptor={
'dictionary': {'fieldType': 'String'}
})
assert not self.etl.can_handle(handler='ada', descriptor={
'dictionary': {'fieldType': 'foo'}
})
def test_extract_raises_readable_if_not_200(self):
with responses.RequestsMock() as response:
response.add(response.GET,
'http://foo.bar/studies/records/findCustom',
body='{}',
status=400,
content_type='application/json')
with pytest.raises(ValueError) as e:
self.etl.extract(server='http://foo.bar',
token='',
descriptor=self.valid_descriptor)
assert '[400]' in e
def test_extract_raises_readable_if_not_json(self):
with responses.RequestsMock() as response:
response.add(response.GET,
'http://foo.bar/studies/records/findCustom',
body='abc{//}',
status=200,
content_type='application/json')
with pytest.raises(TypeError) as e:
self.etl.extract(server='http://foo.bar',
token='',
descriptor=self.valid_descriptor)
assert 'unexpected data' in e
def test_returns_data_for_correct_input(self):
body = json.dumps([{'a': 1, '_id': {'$oid': '12345'}}])
with responses.RequestsMock() as response:
response.add(response.GET,
'http://foo.bar/studies/records/findCustom',
body=body,
status=200,
content_type='application/json')
raw_data = self.etl.extract(server='http://foo.bar',
token='',
descriptor=self.valid_descriptor)
assert isinstance(raw_data, list)
def test_returns_df_for_correct_data(self):
body = json.dumps([{'a': 1, '_id': {'$oid': '12345'}}])
with responses.RequestsMock() as response:
response.add(response.GET,
'http://foo.bar/studies/records/findCustom',
body=body,
status=200,
content_type='application/json')
raw_data = self.etl.extract(server='http://foo.bar',
token='',
descriptor=self.valid_descriptor)
df = self.etl.transform(raw_data=raw_data,
descriptor=self.valid_descriptor)
assert df.shape == (1, 2)
assert df.values.tolist() == [[1, '12345']]
assert list(df) == ['a', 'id']
"""This module provides tests for the ada etl handler."""
import pytest
import responses
from fractalis.data.etls.ada.handler_ada import AdaHandler
......@@ -29,4 +30,42 @@ class TestAdaHandler:
def test_throws_if_bad_init_args(self, bad_init_args):
with pytest.raises(ValueError):
AdaHandler(**bad_init_args)
\ No newline at end of file
AdaHandler(**bad_init_args)
@staticmethod
def request_callback(request):
headers = {
'Set-Cookie': 'PLAY2AUTH_SESS_ID="foo-token"'}
body = ''
return 200, headers, body
def test_returns_token_for_credentials(self):
with responses.RequestsMock() as response:
response.add_callback(response.POST, 'http://foo.bar/login',
callback=self.request_callback,
content_type='application/json')
adah = AdaHandler(server='http://foo.bar',
auth={'user': 'foo', 'passwd': 'bar'})
assert adah._token == 'foo-token'
def test_auth_raises_exception_for_non_json_return(self):
with responses.RequestsMock() as response:
response.add(response.POST, 'http://foo.bar/login',
body='123{//}',
status=200,
content_type='application/json')
with pytest.raises(ValueError) as e:
AdaHandler(server='http://foo.bar',
auth={'user': 'foo', 'passwd': 'bar'})
assert 'unexpected response' in e
def test_auth_raises_exception_for_non_200_return(self):
with responses.RequestsMock() as response:
response.add(response.POST, 'http://foo.bar/login',
body='something',
status=400,
content_type='application/json')
with pytest.raises(ValueError) as e:
AdaHandler(server='http://foo.bar',
auth={'user': 'foo', 'passwd': 'bar'})
assert '[400]' in e
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment