/test/programytest/services/test_duckduckgo.py

https://github.com/keiffster/program-y · Python · 112 lines · 76 code · 36 blank · 0 comment · 3 complexity · 544b4432873f37c66056dd910a229f99 MD5 · raw file

  1. import unittest
  2. from programy.services.duckduckgo import DuckDuckGoAPI
  3. from programy.services.duckduckgo import DuckDuckGoService
  4. from programy.services.requestsapi import RequestsAPI
  5. from programy.services.service import BrainServiceConfiguration
  6. from programytest.client import TestClient
  7. class MockResponse(object):
  8. def __init__(self, status_code, text):
  9. self.status_code = status_code
  10. self.text = text
  11. class MockRequestsAPI(object):
  12. def __init__(self):
  13. self._status_code = None
  14. self._response = None
  15. def get(self, url, params):
  16. self._url = url
  17. return MockResponse(self._status_code, self._response)
  18. class MockDuckDuckGoAPI(object):
  19. def __init__(self, response=None, throw_exception=False):
  20. self._response = response
  21. self._throw_exception = throw_exception
  22. def ask_question(self, url, question):
  23. self._question = question
  24. self._url = url
  25. if self._throw_exception is True:
  26. raise Exception()
  27. else:
  28. return self._response
  29. class TestDuckDuckGoAPI(unittest.TestCase):
  30. def test_init_no_requestapi(self):
  31. api = DuckDuckGoAPI()
  32. self.assertIsNotNone(api)
  33. self.assertIsInstance(api._requests_api, RequestsAPI)
  34. def test_init_with_requestapi(self):
  35. api = DuckDuckGoAPI(MockRequestsAPI())
  36. self.assertIsNotNone(api)
  37. self.assertIsInstance(api._requests_api, MockRequestsAPI)
  38. def test_ask_question(self):
  39. request_api = MockRequestsAPI()
  40. api = DuckDuckGoAPI(request_api)
  41. self.assertIsNotNone(api)
  42. self.assertIsInstance(api._requests_api, MockRequestsAPI)
  43. request_api._status_code = 200
  44. request_api._response = b'{"RelatedTopics": [{"Text": "A feline, 4 legged thing"}]}'
  45. response = api.ask_question("http:/test.url.com/ask", "cat")
  46. self.assertEqual("A feline, 4 legged thing", response)
  47. class DuckDuckGoServiceTests(unittest.TestCase):
  48. def setUp(self):
  49. client = TestClient()
  50. client.add_license_keys_store()
  51. self._client_context = client.create_client_context("testid")
  52. def test_init_with_no_request_api(self):
  53. config = BrainServiceConfiguration("pannous")
  54. config._url = "http://test.pandora.url"
  55. service = DuckDuckGoService(config=config)
  56. self.assertIsNotNone(service)
  57. self.assertIsInstance(service._api, DuckDuckGoAPI)
  58. def test_init_with_no_url(self):
  59. config = BrainServiceConfiguration("pannous")
  60. config._url = None
  61. with self.assertRaises(Exception):
  62. service = DuckDuckGoService(config=config)
  63. def test_ask_question(self):
  64. config = BrainServiceConfiguration("pannous")
  65. config._url = "http://test.pandora.url"
  66. service = DuckDuckGoService(config=config, api=MockDuckDuckGoAPI(response="Test DuckDuckGo response"))
  67. self.assertIsNotNone(service)
  68. response = service.ask_question(self._client_context, "what is a cat")
  69. self.assertEqual("Test DuckDuckGo response", response)
  70. def test_ask_question_general_exception(self):
  71. config = BrainServiceConfiguration("pannous")
  72. config._url = "http://test.pandora.url"
  73. service = DuckDuckGoService(config=config, api=MockDuckDuckGoAPI(response=None, throw_exception=True))
  74. self.assertIsNotNone(service)
  75. response = service.ask_question(self._client_context, "what is a cat")
  76. self.assertEqual("", response)