2017-02-27 11:14:39 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
from unittest import TestCase
|
|
|
|
from senpy.api import parse_params, API_PARAMS, NIF_PARAMS, WEB_PARAMS
|
|
|
|
from senpy.models import Error
|
|
|
|
|
|
|
|
|
|
|
|
class APITest(TestCase):
|
|
|
|
|
|
|
|
def test_api_params(self):
|
|
|
|
"""The API should not define any required parameters without a default"""
|
2017-06-21 17:58:18 +00:00
|
|
|
parse_params({}, API_PARAMS)
|
2017-02-27 11:14:39 +00:00
|
|
|
|
|
|
|
def test_web_params(self):
|
|
|
|
"""The WEB should not define any required parameters without a default"""
|
2017-06-21 17:58:18 +00:00
|
|
|
parse_params({}, WEB_PARAMS)
|
2017-02-27 11:14:39 +00:00
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
a = {}
|
2018-01-03 08:39:30 +00:00
|
|
|
self.assertRaises(Error, parse_params, a)
|
|
|
|
self.assertRaises(Error, parse_params, a, NIF_PARAMS)
|
2017-02-27 11:14:39 +00:00
|
|
|
a = {'input': 'hello'}
|
2017-06-21 17:58:18 +00:00
|
|
|
p = parse_params(a, NIF_PARAMS)
|
2017-02-27 11:14:39 +00:00
|
|
|
assert 'input' in p
|
|
|
|
b = {'i': 'hello'}
|
2017-06-21 17:58:18 +00:00
|
|
|
p = parse_params(b, NIF_PARAMS)
|
2017-02-27 11:14:39 +00:00
|
|
|
assert 'input' in p
|
|
|
|
|
|
|
|
def test_plugin(self):
|
|
|
|
query = {}
|
|
|
|
plug_params = {
|
|
|
|
'hello': {
|
|
|
|
'aliases': ['hello', 'hiya'],
|
|
|
|
'required': True
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 08:39:30 +00:00
|
|
|
self.assertRaises(Error, parse_params, plug_params)
|
2017-02-27 11:14:39 +00:00
|
|
|
query['hello'] = 'world'
|
2017-06-21 17:58:18 +00:00
|
|
|
p = parse_params(query, plug_params)
|
2017-02-27 11:14:39 +00:00
|
|
|
assert 'hello' in p
|
|
|
|
assert p['hello'] == 'world'
|
|
|
|
del query['hello']
|
|
|
|
|
|
|
|
query['hiya'] = 'dlrow'
|
2017-06-21 17:58:18 +00:00
|
|
|
p = parse_params(query, plug_params)
|
2017-02-27 11:14:39 +00:00
|
|
|
assert 'hello' in p
|
|
|
|
assert p['hello'] == 'dlrow'
|
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
spec = {
|
|
|
|
'hello': {
|
|
|
|
'required': True,
|
|
|
|
'default': 1
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 17:58:18 +00:00
|
|
|
p = parse_params({}, spec)
|
2017-02-27 11:14:39 +00:00
|
|
|
assert 'hello' in p
|
|
|
|
assert p['hello'] == 1
|
2017-08-27 16:43:40 +00:00
|
|
|
|
|
|
|
def test_call(self):
|
|
|
|
call = {
|
|
|
|
'input': "Aloha my friend",
|
|
|
|
'algo': "Dummy"
|
|
|
|
}
|
|
|
|
p = parse_params(call, API_PARAMS, NIF_PARAMS)
|
|
|
|
assert 'algorithm' in p
|
|
|
|
assert "Dummy" in p['algorithm']
|
|
|
|
assert 'input' in p
|
|
|
|
assert p['input'] == 'Aloha my friend'
|