Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Fractalis
fractalis
Commits
bea8f971
Commit
bea8f971
authored
Jul 18, 2017
by
Sascha Herzinger
Browse files
added a bunch of tests
parent
22aacca7
Pipeline
#2235
passed with stage
in 3 minutes and 24 seconds
Changes
16
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
fractalis/data/etlhandler.py
View file @
bea8f971
...
...
@@ -30,23 +30,33 @@ class ETLHandler(metaclass=abc.ABCMeta):
"""
pass
def
_get_token_for_credentials
(
self
,
server
:
str
,
user
:
str
,
passwd
:
str
)
->
str
:
def
_get_token_for_credentials
(
self
,
server
:
str
,
auth
:
dict
)
->
str
:
""" Authenticate with the server and return a token.
:param server: The server to authenticate with.
:param
user: The user id.
:
param passwd: The password
.
:param
auth: dict containing credentials to auth with API
:
return The token returned by the API
.
"""
raise
NotImplementedError
()
def
__init__
(
self
,
server
,
auth
):
if
not
isinstance
(
server
,
str
)
or
len
(
server
)
<
10
:
error
=
(
"{} is not a valid server url."
.
format
(
server
))
logger
.
error
(
error
)
raise
ValueError
(
error
)
if
not
isinstance
(
auth
,
dict
):
error
=
"'auth' must be a valid dictionary."
logger
.
error
(
error
)
raise
ValueError
(
error
)
self
.
_server
=
server
# if no token is given we have to get one
try
:
self
.
_token
=
auth
[
'token'
]
if
not
isinstance
(
self
.
_token
,
str
)
or
len
(
self
.
_token
)
==
0
:
raise
KeyError
except
KeyError
:
self
.
_token
=
self
.
_get_token_for_credentials
(
server
,
auth
[
'user'
],
auth
[
'passwd'
])
logger
.
info
(
'No token has been provided. '
'Attempting to authenticate with the API.'
)
self
.
_token
=
self
.
_get_token_for_credentials
(
server
,
auth
)
@
staticmethod
@
abc
.
abstractmethod
...
...
fractalis/data/etls/ada/handler_ada.py
View file @
bea8f971
"""This module provides AdaHandler, an implementation of ETLHandler for ADA."""
import
logging
import
requests
from
fractalis.data.etlhandler
import
ETLHandler
logger
=
logging
.
getLogger
(
__name__
)
class
AdaHandler
(
ETLHandler
):
"""This ETLHandler provides integration with ADA.
...
...
@@ -25,8 +30,17 @@ class AdaHandler(ETLHandler):
return
'{} ({})'
.
format
(
descriptor
[
'dictionary'
][
'label'
],
descriptor
[
'data_set'
])
def
_get_token_for_credentials
(
self
,
server
:
str
,
user
:
str
,
passwd
:
str
)
->
str
:
def
_get_token_for_credentials
(
self
,
server
:
str
,
auth
:
dict
)
->
str
:
try
:
user
=
auth
[
'user'
]
passwd
=
auth
[
'passwd'
]
if
len
(
user
)
==
0
or
len
(
passwd
)
==
0
:
raise
KeyError
except
KeyError
:
error
=
"The authentication object must contain the non-empty "
\
"fields 'user' and 'passwd'."
logger
.
error
(
error
)
raise
ValueError
(
error
)
r
=
requests
.
post
(
url
=
'{}/login'
.
format
(
server
),
headers
=
{
'Accept'
:
'application/json'
},
data
=
{
'id'
:
user
,
'password'
:
passwd
})
...
...
fractalis/data/etls/transmart/handler_transmart.py
View file @
bea8f971
"""This module provides TransmartHandler, an implementation of ETLHandler for
tranSMART."""
import
logging
import
requests
from
fractalis.data.etlhandler
import
ETLHandler
logger
=
logging
.
getLogger
(
__name__
)
class
TransmartHandler
(
ETLHandler
):
"""This ETLHandler provides integration with tranSMART.
...
...
@@ -26,16 +31,37 @@ class TransmartHandler(ETLHandler):
def
make_label
(
descriptor
:
dict
)
->
str
:
return
descriptor
[
'path'
]
def
_get_token_for_credentials
(
self
,
server
:
str
,
user
:
str
,
passwd
:
str
)
->
str
:
r
=
requests
.
post
(
url
=
'{}/oauth/token?grant_type=password'
'&client_id=glowingbear-js'
'&client_secret='
'&username={}'
'&password={}'
.
format
(
server
,
user
,
passwd
),
def
_get_token_for_credentials
(
self
,
server
:
str
,
auth
:
dict
)
->
str
:
try
:
user
=
auth
[
'user'
]
passwd
=
auth
[
'passwd'
]
if
len
(
user
)
==
0
or
len
(
passwd
)
==
0
:
raise
KeyError
except
KeyError
:
error
=
"The authentication object must contain the non-empty "
\
"fields 'user' and 'passwd'."
logger
.
error
(
error
)
raise
ValueError
(
error
)
r
=
requests
.
post
(
url
=
server
+
'/oauth/token'
,
params
=
{
'grant_type'
:
'password'
,
'client_id'
:
'glowingbear-js'
,
'client_secret'
:
''
,
'username'
:
user
,
'password'
:
passwd
},
headers
=
{
'Accept'
:
'application/json'
})
auth_error
=
''
if
r
.
status_code
!=
200
:
raise
ValueError
(
"Could not authenticate. Reason: [{}]: {}"
.
format
(
r
.
status_code
,
r
.
text
))
response
=
r
.
json
()
return
response
[
'access_token'
]
auth_error
=
"Could not authenticate. "
\
"Reason: [{}]: {}"
.
format
(
r
.
status_code
,
r
.
text
)
logger
.
error
(
auth_error
)
raise
ValueError
(
auth_error
)
try
:
response
=
r
.
json
()
return
response
[
'access_token'
]
except
ValueError
:
auth_error
=
"Could not authenticate. "
\
"Got unexpected response: '{}'"
.
format
(
r
.
text
)
logger
.
error
(
auth_error
)
raise
ValueError
(
auth_error
)
\ No newline at end of file
setup.py
View file @
bea8f971
...
...
@@ -28,5 +28,6 @@ setup(
'pytest==3.0.3'
,
'pytest-cov'
,
'pytest-mock'
,
'responses'
]
)
tests/ada_etls/__init__.py
0 → 100644
View file @
bea8f971
tests/ada_etls/test_etl_boolean.py
0 → 100644
View file @
bea8f971
tests/ada_etls/test_etl_date.py
0 → 100644
View file @
bea8f971
tests/ada_etls/test_etl_double.py
0 → 100644
View file @
bea8f971
tests/ada_etls/test_etl_enum.py
0 → 100644
View file @
bea8f971
tests/ada_etls/test_etl_integer.py
0 → 100644
View file @
bea8f971
tests/ada_etls/test_etl_string.py
0 → 100644
View file @
bea8f971
tests/ada_etls/test_handler_ada.py
0 → 100644
View file @
bea8f971
"""This module provides tests for the ada etl handler."""
import
pytest
from
fractalis.data.etls.ada.handler_ada
import
AdaHandler
# noinspection PyMissingOrEmptyDocstring,PyMissingTypeHints
class
TestAdaHandler
:
@
pytest
.
fixture
(
scope
=
'function'
,
params
=
[
{
'server'
:
'http://foo.bar'
,
'auth'
:
''
},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'abc'
:
'abc'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'token'
:
''
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'token'
:
object
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
'foo'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'passwd'
:
'foo'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
''
,
'passwd'
:
''
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
'foo'
,
'passwd'
:
''
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
''
,
'passwd'
:
'foo'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
''
,
'passwd'
:
''
,
'foo'
:
'bar'
}},
{
'server'
:
''
,
'auth'
:
{
'token'
:
'foo'
}},
{
'server'
:
object
,
'auth'
:
{
'token'
:
'foo'
}}
])
def
bad_init_args
(
self
,
request
):
return
request
.
param
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
tests/transmart_etls/__init__.py
0 → 100644
View file @
bea8f971
tests/transmart_etls/test_etl_categorical.py
0 → 100644
View file @
bea8f971
tests/transmart_etls/test_etl_numerical.py
0 → 100644
View file @
bea8f971
tests/transmart_etls/test_handler_transmart.py
0 → 100644
View file @
bea8f971
"""This module provides tests for the transmart etl handler."""
import
pytest
import
responses
import
requests
from
fractalis.data.etls.transmart.handler_transmart
import
TransmartHandler
# noinspection PyMissingOrEmptyDocstring,PyMissingTypeHints
class
TestTransmartHandler
:
@
pytest
.
fixture
(
scope
=
'function'
,
params
=
[
{
'server'
:
'http://foo.bar'
,
'auth'
:
''
},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'abc'
:
'abc'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'token'
:
''
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'token'
:
object
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
'foo'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'passwd'
:
'foo'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
''
,
'passwd'
:
''
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
'foo'
,
'passwd'
:
''
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
''
,
'passwd'
:
'foo'
}},
{
'server'
:
'http://foo.bar'
,
'auth'
:
{
'user'
:
''
,
'passwd'
:
''
,
'foo'
:
'bar'
}},
{
'server'
:
''
,
'auth'
:
{
'token'
:
'foo'
}},
{
'server'
:
object
,
'auth'
:
{
'token'
:
'foo'
}}
])
def
bad_init_args
(
self
,
request
):
return
request
.
param
def
test_throws_if_bad_init_args
(
self
,
bad_init_args
):
with
pytest
.
raises
(
ValueError
):
TransmartHandler
(
**
bad_init_args
)
def
test_returns_token_for_credentials
(
self
):
with
responses
.
RequestsMock
()
as
response
:
response
.
add
(
response
.
POST
,
'http://foo.bar/oauth/token'
,
body
=
'{"access_token":"foo-token","token_type":"bearer","expires_in":43185,"scope":"read write"}'
,
status
=
200
,
content_type
=
'application/json'
)
tmh
=
TransmartHandler
(
server
=
'http://foo.bar'
,
auth
=
{
'user'
:
'foo'
,
'passwd'
:
'bar'
})
assert
tmh
.
_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/oauth/token'
,
body
=
'123{//}'
,
status
=
200
,
content_type
=
'application/json'
)
with
pytest
.
raises
(
ValueError
)
as
e
:
TransmartHandler
(
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/oauth/token'
,
body
=
'something'
,
status
=
400
,
content_type
=
'application/json'
)
with
pytest
.
raises
(
ValueError
)
as
e
:
TransmartHandler
(
server
=
'http://foo.bar'
,
auth
=
{
'user'
:
'foo'
,
'passwd'
:
'bar'
})
assert
'[400]'
in
e
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment