Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests for most functions (96% coverage)
  • Loading branch information
nkansal96 committed Jun 27, 2018
commit e3ccf0bbf0bf2a832eae5e68e2445a6621038d82
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ test:
python3 setup.py test

clean:
find . -name "*.pyc" -delete
find . -name "__pycache__" -delete
find . -name "*.pyc" -exec rm -rf {} \+
find . -name "__pycache__" -exec rm -rf {} \+
rm -rf .pytest_cache htmlcov .coverage

clean-all: clean
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ from auroraapi.dialog import Dialog

def udf(context):
# get data for a particular step
data = context.get_step_data("step_id")
data = context.get_step("step_id")
# set some custom data
context.set_user_data("id", "some data value")
context.set_data("id", "some data value")
# you can get the data later
assert context.get_data("id") == "some data value"
# return True to take the upward branch in the dialog builder
return True

Expand All @@ -275,9 +277,11 @@ from auroraapi.dialog import Dialog
def handle_update(context):
# this function is called whenever the current step is changed or
# whenever the data in the context is updated
# you can get the current dialog step like this
step = context.get_current_step()
print(step, context)
#
# you can get the current and previous dialog steps like this
curr = context.get_current_step()
prev = context.get_current_step()
print(curr, prev, context)

dialog = Dialog("DIALOG_ID", on_context_update=handle_update)
dialog.run()
Expand Down
35 changes: 35 additions & 0 deletions tests/api/backend/test___init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
from auroraapi.globals import Config
from auroraapi.api.backend import CallParams, Credentials, Backend

class TestCredentials(object):
def test_create(self):
c = Credentials("app_id", "app_token", "device_id")
assert c.app_id == "app_id"
assert c.app_token == "app_token"
assert c.device_id == "device_id"

def test_headers(self):
c = Credentials("app_id", "app_token", "device_id")
assert len(c.headers) == 3
assert c.headers["X-Application-ID"] == "app_id"
assert c.headers["X-Application-Token"] == "app_token"
assert c.headers["X-Device-ID"] == "device_id"

def test_from_config(self):
config = Config("app_id", "app_token", "device_id")
c = Credentials.from_config(config)
assert c.app_id == "app_id"
assert c.app_token == "app_token"
assert c.device_id == "device_id"

class TestBackend(object):
def test_create(self):
b = Backend("base_url", timeout=10000)
assert b.base_url == "base_url"
assert b.timeout == 10000

def test_call(self):
with pytest.raises(NotImplementedError):
b = Backend("base_url")
b.call(CallParams())
85 changes: 85 additions & 0 deletions tests/api/backend/test_aurora.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import pytest
from auroraapi.api.backend import CallParams, Credentials, Backend
from auroraapi.api.backend.aurora import AuroraBackend
from auroraapi.errors import APIException

class TestAuroraBackend(object):
pass

# class MockResponse(object):
# def __init__(self):
# self.status_code = 200
# self.headers = {}
# self.text = ""

# def json(self):
# return json.loads(self.text)


# class TestAPIUtils(object):
# def setup(self):
# _config.app_id = "appid"
# _config.app_token = "apptoken"

# def teardown(self):
# _config.app_id = None
# _config.app_token = None

# def test_get_headers(self):
# h = get_headers()
# assert h["X-Application-ID"] == _config.app_id
# assert h["X-Application-Token"] == _config.app_token
# assert h["X-Device-ID"] == _config.device_id

# def test_handle_error_no_error(self):
# handle_error(MockResponse())

# def test_handle_error_json(self):
# r = MockResponse()
# r.status_code = 400
# r.headers = { "content-type": "application/json" }
# r.text = json.dumps({
# "id": "id",
# "code": "MissingApplicationIDHeader",
# "type": "BadRequest",
# "status": 400,
# "message": "message"
# })

# with pytest.raises(APIException) as e:
# handle_error(r)
# assert e.id == "id"
# assert e.code == "MissingApplicationIDHeader"
# assert e.type == "BadRequest"
# assert e.status == 400
# assert e.message == "message"

# def test_handle_error_413(self):
# r = MockResponse()
# r.status_code = 413
# r.headers["content-type"] = "text/html"
# r.text = "Request entity too large"

# with pytest.raises(APIException) as e:
# handle_error(r)
# assert e.id == None
# assert e.status == 413
# assert e.type == "RequestEntityTooLarge"
# assert e.code == "RequestEntityTooLarge"
# assert e.message == "Request entity too large"

# def test_handle_error_other(self):
# r = MockResponse()
# r.status_code = 503
# r.headers["content-type"] = "text/html"
# r.text = "Service unavailable"


# with pytest.raises(APIException) as e:
# handle_error(r)
# assert e.id == None
# assert e.status == 413
# assert e.type == None
# assert e.code == None
# assert e.message == r.text
# assert str(e) == "[{}] {}".format(r.status_code, r.text)
44 changes: 44 additions & 0 deletions tests/api/backend/test_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest
from auroraapi.api.backend import CallParams, Credentials, Backend
from auroraapi.errors import APIException
from tests.mocks.backend import MockBackend

class TestMockBackend(object):
def test_create(self):
b = MockBackend()
assert b.responses == []

def test_call_success(self):
b = MockBackend()
b.set_expected_response(200, { "data": "value" })

r = b.call(CallParams())
assert len(r) == 1
assert r["data"] == "value"

def test_call_failure_text(self):
b = MockBackend()
b.set_expected_response(400, "error")

with pytest.raises(APIException) as e:
r = b.call(CallParams())
assert e.status == 400
assert e.code == None
assert e.message == "error"

def test_call_failure_json(self):
b = MockBackend()
b.set_expected_response(400, { "code": "ErrorCode", "message": "error" })

with pytest.raises(APIException) as e:
r = b.call(CallParams())
assert e.status == 400
assert e.code == "ErrorCode"
assert e.message == "error"

def test_call_multiple(self):
b = MockBackend()
b.set_expected_responses((200, "first"), (200, "second"))

assert b.call(CallParams()) == "first"
assert b.call(CallParams()) == "second"
Binary file added tests/assets/empty.wav
Binary file not shown.
Binary file added tests/assets/new-empty
Binary file not shown.
106 changes: 106 additions & 0 deletions tests/dialog/step/test_listen_step.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import pytest, mock
from auroraapi.dialog.context import DialogContext
from auroraapi.dialog.graph import GraphEdge
from auroraapi.dialog.step.listen import ListenStep
from auroraapi.globals import _config
from auroraapi.interpret import Interpret
from auroraapi.text import Text
from tests.mocks import *

class ContextWrapper(object):
def __init__(self, val):
self.val = val
def context_dict(self):
return self.val

LISTEN_TEXT = {
"id": "listen_id",
"type": "listen",
"data": {
"model": "general",
"interpret": False,
"stepName": "listen_name",
"length": "",
"silenceLen": "",
},
}

LISTEN_TEXT_CUSTOM = {
"id": "listen_id",
"type": "listen",
"data": {
"model": "general",
"interpret": False,
"stepName": "listen_name",
"length": "3.5",
"silenceLen": "1",
},
}

LISTEN_INTERPRET = {
"id": "listen_id",
"type": "listen",
"data": {
"model": "general",
"interpret": True,
"stepName": "listen_name",
"length": "",
"silenceLen": "",
},
}

class TestListenStep(object):
def setup(self):
self.orig_backend = _config.backend
_config.backend = MockBackend()

def teardown(self):
_config.backend = self.orig_backend

def test_create_default_text(self):
s = ListenStep(LISTEN_TEXT)
assert s.model == LISTEN_TEXT["data"]["model"]
assert s.step_name == LISTEN_TEXT["data"]["stepName"]
assert s.interpret == LISTEN_TEXT["data"]["interpret"]
assert s.listen_settings["length"] == 0
assert s.listen_settings["silence_len"] == 0.5

def test_create_custom_text(self):
s = ListenStep(LISTEN_TEXT_CUSTOM)
assert s.listen_settings["length"] == 3.5
assert s.listen_settings["silence_len"] == 1

def test_create_default_interpret(self):
s = ListenStep(LISTEN_INTERPRET)
assert s.interpret
assert s.listen_settings["length"] == 0
assert s.listen_settings["silence_len"] == 0.5

def test_execute_text(self):
_config.backend.set_expected_response(200, { "transcript": "hello" })
c = DialogContext()
s = ListenStep(LISTEN_TEXT)
with mock.patch('auroraapi.audio._pyaudio_record', new=mock_pyaudio_record):
assert s.execute(c, GraphEdge()) == None
assert isinstance(c.get_step("listen_name"), Text)
assert c.get_step("listen_name").text == "hello"

def test_execute_interpret(self):
_config.backend.set_expected_responses(
(200, {
"transcript": "hello"
}),
(200, {
"text": "hello",
"intent": "greeting",
"entities": {},
}),
)
c = DialogContext()
s = ListenStep(LISTEN_INTERPRET)
with mock.patch('auroraapi.audio._pyaudio_record', new=mock_pyaudio_record):
assert s.execute(c, GraphEdge()) == None
assert isinstance(c.get_step("listen_name"), Interpret)
assert c.get_step("listen_name").text == "hello"
assert c.get_step("listen_name").intent == "greeting"
assert c.get_step("listen_name").entities == {}
Loading