Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Python-Chatbot/tests.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
162 lines (111 sloc)
5.57 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################## | |
# Benjamin Grant | |
import unittest | |
import asyncio | |
class TestQuoteFunctionality(unittest.TestCase): | |
@classmethod | |
def setUpClass(self): | |
import quote | |
# Code by dano at https://stackoverflow.com/questions/32456881/getting-values-from-functions-that-run-as-asyncio-tasks to get return values from coroutines. Only function names modified | |
loop = asyncio.get_event_loop() | |
tasks = [asyncio.ensure_future(quote.generateQuote())] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
# End code by dano | |
self.quote = tasks[0].result() | |
#Test if quote is empty | |
def test_length(self): | |
self.assertNotEqual(len(self.quote), 0) | |
#Test if HTML entities or unicode remain | |
def test_html(self): | |
self.assertNotIn("&#", self.quote) | |
self.assertNotIn("\\u", self.quote) | |
class TestCurrencyFunctionality(unittest.TestCase): | |
@classmethod | |
def setUpClass(self): | |
import exchange | |
# Code by dano at https://stackoverflow.com/questions/32456881/getting-values-from-functions-that-run-as-asyncio-tasks to get return values from coroutines. Only function names modified | |
loop = asyncio.get_event_loop() | |
tasks = [asyncio.ensure_future(exchange.convertCurrency("USD", "GBP", 320874239)), | |
asyncio.ensure_future(exchange.convertCurrency("EUR", "USD", 120837)), | |
asyncio.ensure_future(exchange.convertCurrency("GBP", "JPY", 0)), | |
asyncio.ensure_future(exchange.convertCurrency("GBP", "XDR", -5))] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
# End code by dano | |
self.gbpusd = tasks[0].result() | |
self.usdeur = tasks[1].result() | |
self.jpygbp = tasks[2].result() | |
self.neg = tasks[3].result() | |
#Test if any convert to < 0 | |
def test_zero(self): | |
assert self.gbpusd >= 0, "Less than zero" | |
assert self.usdeur >= 0, "Less than zero" | |
assert self.jpygbp >= 0, "Less than zero" | |
#Test how negative input is handled (*hint*, it isn't) //TODO | |
def test_negative(self): | |
self.assertEqual(-5, self.neg) | |
class TestNameStorage(unittest.TestCase): | |
@classmethod | |
def setUpClass(self): | |
import namestore | |
# Code by dano at https://stackoverflow.com/questions/32456881/getting-values-from-functions-that-run-as-asyncio-tasks to get return values from coroutines. Only function names modified | |
loop = asyncio.get_event_loop() | |
tasks = [asyncio.ensure_future(namestore.set_name(12345, "Mike")), | |
asyncio.ensure_future(namestore.set_name(54321, "There is a space")), | |
asyncio.ensure_future(namestore.set_name(67890, "cApItAlIsEd")), | |
asyncio.ensure_future(namestore.set_name(9876, "&&&&&")), | |
asyncio.ensure_future(namestore.set_name(3456, "21314")), | |
asyncio.ensure_future(namestore.set_name(6543, "ééééééééé")), | |
asyncio.ensure_future(namestore.set_name_pickle(2391, "Pickle"))] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
tasks = [asyncio.ensure_future(namestore.get_name(12345)), | |
asyncio.ensure_future(namestore.get_name(67890)), | |
asyncio.ensure_future(namestore.get_name(54321)), | |
asyncio.ensure_future(namestore.get_name(9876)), | |
asyncio.ensure_future(namestore.get_name(3456)), | |
asyncio.ensure_future(namestore.get_name(6543)), | |
asyncio.ensure_future(namestore.get_name_pickle(2391))] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
# End code by dano | |
self.mike = tasks[0].result() | |
self.cap = tasks[1].result() | |
self.space = tasks[2].result() | |
self.amp = tasks[3].result() | |
self.num = tasks[4].result() | |
self.fgn = tasks[5].result() | |
self.pickle = tasks[6].result() | |
#Test if pickle version of namestore works | |
def test_pickle(self): | |
self.assertEqual(self.pickle, "Pickle") | |
#Test if process of dumping and retrieval inteferes with true values | |
def test_names(self): | |
self.assertEqual(self.mike, "Mike") | |
self.assertEqual(self.amp, "&&&&&") | |
self.assertEqual(self.num, "21314") | |
self.assertEqual(self.fgn, "Ééééééééé") | |
self.assertEqual(self.cap, "Capitalised") | |
self.assertEqual(self.space, "There is a space") | |
class TestSocialInteraction(unittest.TestCase): | |
@classmethod | |
def setUpClass(self): | |
import conversate | |
# Code by dano at https://stackoverflow.com/questions/32456881/getting-values-from-functions-that-run-as-asyncio-tasks to get return values from coroutines. Only function names modified | |
loop = asyncio.get_event_loop() | |
tasks = [asyncio.ensure_future(conversate.learn("TEST", "cool")), | |
asyncio.ensure_future(conversate.learn("TEST", "as")), | |
asyncio.ensure_future(conversate.learn("TEST", "if"))] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
tasks = [asyncio.ensure_future(conversate.get_response("TEST"))] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
self.response = tasks[0].result() | |
tasks = [asyncio.ensure_future(conversate.unlearn(category="TEST"))] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
tasks = [asyncio.ensure_future(conversate.get_response("TEST"))] | |
done, _ = loop.run_until_complete(asyncio.wait(tasks)) | |
# End code by dano | |
self.broken_response = tasks[0].result() | |
def test_learning(self): | |
self.assertIn(self.response, ["cool", "as", "if"]) | |
self.assertEqual(self.broken_response, None) | |
if __name__ == "__main__": | |
unittest.main() | |
################################################## |