class ServiceHandlerTests(unittest.TestCase): def test_init(self): handler = ServiceHandler() self.assertIsNotNone(handler) self.assertEqual({}, handler.services) mock_service = Mock() handler.add_service("test1", mock_service) self.assertTrue("test1" in handler.services) self.assertFalse("test2" in handler.services) handler.empty() self.assertFalse("test1" in handler.services) self.assertFalse("test2" in handler.services) @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_load_services(self): client = ServiceHandlerTestClient() self.assertIsNotNone(client) client_context = client.create_client_context("test1") self.assertIsNotNone(client_context.brain._services) self.assertTrue("wikipedia" in client_context.brain._services.services) self.assertTrue("gnews" in client_context.brain._services.services) result = client.ask_question(client_context, "GNEWS SEARCH CHATBOTS") self.assertIsNotNone(result) self.assertTrue(result.startswith("<ul><li>"))
class MicrosoftNewsServiceTests(ServiceTestCase): def test_init(self): service = MicrosoftNewsService( ServiceConfiguration.from_data( "rest", "microsoft.news", "news", url="https://chatilly.cognitiveservices.azure.com/bing/v7.0")) self.assertIsNotNone(service) def patch_requests_news_success_reponse(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = news_success_reponse return mock_response def _do_news(self): service = MicrosoftNewsService( ServiceConfiguration.from_data( "rest", "microsoft.news", "news", url="https://chatilly.cognitiveservices.azure.com/bing/v7.0")) self.assertIsNotNone(service) client = MicrosoftNewsServiceTestClient() service.initialise(client) response = service.news("chatbots") self.assertResponse(response, 'news', "microsoft.news", "news") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_news_integration(self): self._do_news() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_news_success_reponse) def test_news_unit(self): self._do_news() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_news_success_reponse) def test_news_aiml(self): client = MicrosoftNewsServiceTestClient() conf_file = MicrosoftNewsService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "microsoft.news", "MICROSOFT NEWS CHATBOTS") self.assertIsNotNone(response) self.assertTrue( response.startswith( "<ul>\n<li>How Europe deals with terror offenders when they are freed from jail" ))
class WorldTradingDataStocksServiceTests(ServiceTestCase): def test_init(self): service = WorldTradingDataStocksService( ServiceConfiguration.from_data("rest", "worldtradingdata", "stockmarket")) self.assertIsNotNone(service) def patch_requests_stocks_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.text = stocks_success_response return mock_response def _do_stocks(self): service = WorldTradingDataStocksService( ServiceConfiguration.from_data("rest", "worldtradingdata", "stockmarket")) self.assertIsNotNone(service) client = WorldTradingDataServiceTestClient() service.initialise(client) response = service.stocks("AAPL") self.assertResponse(response, 'stocks', "worldtradingdata", "stockmarket") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_stocks_integration(self): self._do_stocks() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_stocks_success) def test_stocks_unit(self): self._do_stocks() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_stocks_success) def test_handler_load(self): client = WorldTradingDataServiceTestClient() conf_file = WorldTradingDataStocksService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "worldtradingdata", "WORLDTRADINGDATA SYMBOLS AMZN") self.assertIsNotNone(response) self.assertEqual( "Apple Inc. [ AAPL ] is currently priced at 318.85 USD.", response)
class YelpEventsSearchServiceTests(ServiceTestCase): def test_init(self): service = YelpEventsSearchService( ServiceConfiguration.from_data("rest", "yelp.events.search", "search")) self.assertIsNotNone(service) def patch_requests_search_success_reponse(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = search_success_reponse return mock_response def _do_search(self): service = YelpEventsSearchService( ServiceConfiguration.from_data("rest", "yelp.events.search", "search")) self.assertIsNotNone(service) client = YelpEventsSearchServiceTestClient() service.initialise(client) response = service.search("kinghorn") self.assertResponse(response, 'search', "yelp.events.search", "search") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_search_integration(self): self._do_search() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_search_success_reponse) def test_search_unit(self): self._do_search() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_search_success_reponse) def test_search_aiml(self): client = YelpEventsSearchServiceTestClient() conf_file = YelpEventsSearchService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "yelp.events.search", "YELP EVENTS SEARCH LOCATION KINGHORN") self.assertIsNotNone(response) self.assertTrue( response.startswith( "<ul>\n<li>10th Annual Hilton Head Island Seafood Festival"))
class GoogleDirectionsServiceTests(ServiceTestCase): def test_init(self): service = GoogleDirectionsService( ServiceConfiguration.from_data("rest", "google", "directions")) self.assertIsNotNone(service) def patch_requests_directions_success_reponse(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = directions_success_reponse return mock_response def _do_directions(self): service = GoogleDirectionsService( ServiceConfiguration.from_data("rest", "google", "directions")) self.assertIsNotNone(service) client = GoogleDirectionsServiceTestClient() service.initialise(client) response = service.get_directions("London", "Brighton") self.assertResponse(response, 'get_directions', "google", "directions") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_directions_integrastion(self): self._do_directions() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_directions_success_reponse) def test_directions_unit(self): self._do_directions() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_directions_success_reponse) def test_handler_load(self): client = GoogleDirectionsServiceTestClient() conf_file = GoogleDirectionsService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "google.directions", "GOOGLE DIRECTIONS ORIGIN KINGHORN DESTINATION EDINBURGH") self.assertIsNotNone(response) self.assertTrue( response.startswith( "<ol><li>Head <b>north</b> toward <b>A4</b></li>"))
class GoogleDistanceServiceTests(ServiceTestCase): def test_init(self): service = GoogleDistanceService( ServiceConfiguration.from_data("rest", "google", "distance")) self.assertIsNotNone(service) def patch_requests_distance_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = distance_success_response return mock_response def _do_distance(self): service = GoogleDistanceService( ServiceConfiguration.from_data("rest", "google", "distance")) self.assertIsNotNone(service) client = GoogleDistanceServiceTestClient() service.initialise(client) response = service.get_distance("London", "Brighton") self.assertResponse(response, 'get_distance', "google", "distance") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_distance_integration(self): self._do_distance() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_distance_success_response) def test_distance_unit(self): self._do_distance() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_distance_success_response) def test_handler_load(self): client = GoogleDistanceServiceTestClient() conf_file = GoogleDistanceService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "google.distance", "GOOGLE DISTANCE ORIGIN KINGHORN DESTINATION EDINBURGH") self.assertIsNotNone(response) self.assertEquals("About 64.6 mi.", response)
class MetOffice24HourForecastServiceTests(ServiceTestCase): def test_init(self): service = MetOfficeService(ServiceConfiguration.from_data("library", "metoffice", "weather")) self.assertIsNotNone(service) client = MetOfficeTestClient() service.initialise(client) def patch_metoffer_24_hour_forecast_success(self, lat, lng): return forecast_3hourly_payload def _do_24_hour_forecast(self): service = MetOfficeService(ServiceConfiguration.from_data('rest', "metoffice", "weather")) self.assertIsNotNone(service) client = MetOfficeTestClient() service.initialise(client) response = service.forecast(56.0712, -3.1743, metoffer.THREE_HOURLY) payload = self.assertResponse(response, 'forecast', 'metoffice', 'weather') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_24_hour_forecast_integration(self): self._do_24_hour_forecast() @patch("programy.services.library.metoffice.metoffice.MetOffice.twentyfour_hour_forecast", patch_metoffer_24_hour_forecast_success) def test_24_hour_forecast_unit(self): self._do_24_hour_forecast() def patch_get_current_date(self): return "2017-04-03Z" @patch("programy.services.library.metoffice.service.MetOfficeHoursForecastQuery._get_current_date", patch_get_current_date) @patch("programy.services.library.metoffice.metoffice.MetOffice.twentyfour_hour_forecast", patch_metoffer_24_hour_forecast_success) def test_handler_load_24_hour_forecast(self): client = MetOfficeTestClient() conf_file = MetOfficeService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "metoffice", "METOFFICE FORECAST LAT SIGN POS DEC 56 FRAC 0719912 LNG SIGN NEG DEC 3 FRAC 1750909 HOURS 3") self.assertIsNotNone(response) self.assertEqual("It will be a Sunny day , with a temperature of 4 'C.", response)
class GoogleGeoCodeServiceTests(ServiceTestCase): def test_init(self): service = GoogleGeoCodeService(ServiceConfiguration.from_data("rest", "google", "geocode")) self.assertIsNotNone(service) def patch_requests_postcode_valid_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = postcode_valid_response return mock_response def _do_postcode(self): service = GoogleGeoCodeService(ServiceConfiguration.from_data("rest", "google", "geocode")) self.assertIsNotNone(service) client = GoogleGeoCodeServiceTestClient() service.initialise(client) response = service.latlng_for_postcode("KY3 9UR") self.assertResponse(response, 'latlng_for_postcode', "google", "geocode") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_postcode_integration(self): self._do_postcode() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_postcode_valid_response) def test_postcode_unit(self): self._do_postcode() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_postcode_valid_response) def test_handler_load(self): client = GoogleGeoCodeServiceTestClient() conf_file = GoogleGeoCodeService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "google.geocode", "GOOGLE LATLNG POSTCODE KY39UR") self.assertIsNotNone(response) self.assertEquals("LAT SIGN POS DEC 56 FRAC 0719912 LNG SIGN NEG DEC 3 FRAC 1750909.", response)
class OMDBServiceTests(ServiceTestCase): def test_init(self): service = OMDBService(ServiceConfiguration.from_data("rest", "omdb", "film")) self.assertIsNotNone(service) def patch_requests_title_search_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = title_search_success_response return mock_response def _do_title_search(self): service = OMDBService(ServiceConfiguration.from_data("rest", "omdb", "film")) self.assertIsNotNone(service) client = OMDBServiceTestClient() service.initialise(client) response = service.title_search("Aliens") self.assertResponse(response, 'title_search', "omdb", "film") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_title_search_integration(self): self._do_title_search() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_title_search_success) def test_title_search_unit(self): self._do_title_search() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_title_search_success) def test_handler_title_search(self): client = OMDBServiceTestClient() conf_file = OMDBService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "omdb", "OMDB TITLE SEARCH ALIENS") self.assertIsNotNone(response) self.assertTrue(response.startswith("Aliens was released in 18 Jul 1986 and directed by James Cameron and starring Sigourney Weaver"))
class WolframAlphaServiceTests(ServiceTestCase): def test_init(self): service = WolframAlphaService( ServiceConfiguration.from_data("rest", "wolframalpha", "search")) self.assertIsNotNone(service) def patch_requests_simple_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.text = simple_success_response return mock_response def patch_requests_short_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.text = short_success_response return mock_response def _do_simple(self): service = WolframAlphaService( ServiceConfiguration.from_data("rest", "wolframalpha", "search")) self.assertIsNotNone(service) client = WolframAlphaServiceTestClient() service.initialise(client) response = service.simple("CHATBOTS") self.assertResponse(response, 'simple', "wolframalpha", "search") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_simple_integration(self): self._do_simple() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_simple_success) def test_simple_unit(self): self._do_simple() def _do_short(self): service = WolframAlphaService( ServiceConfiguration.from_data("rest", "wolframalpha", "search")) self.assertIsNotNone(service) client = WolframAlphaServiceTestClient() service.initialise(client) response = service.short("How far is Los Angeles from New York") self.assertResponse(response, 'short', "wolframalpha", "search") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_short_integration(self): self._do_short() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_short_success) def test_short_unit(self): self._do_short() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_simple_success) def test_handler_load_simple(self): client = WolframAlphaServiceTestClient() conf_file = WolframAlphaService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "wolframalpha", "WOLFRAMALPHA SIMPLE EDINBURGH UK") self.assertIsNotNone(response) self.assertEqual("Edinburgh, Edinburgh.", response) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_short_success) def test_handler_load_short(self): client = WolframAlphaServiceTestClient() conf_file = WolframAlphaService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "wolframalpha", "WOLFRAMALPHA SHORT How far is Los Angeles from New York") self.assertIsNotNone(response) self.assertEqual("About 2464 miles.", response)
class PandoraServiceTests(ServiceTestCase): def test_init(self): service = PandoraService( ServiceConfiguration.from_data("rest", "pandora", "chatbot")) self.assertIsNotNone(service) def patch_requests_ask_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = ask_success_response return mock_response def patch_requests_ask_failure_server_error(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 503 mock_response.json.return_value = None return mock_response def patch_ask_failure_service_error(self, question): return ask_service_failure def _do_ask(self): service = PandoraService( ServiceConfiguration.from_data("rest", "pandora", "chatbot")) self.assertIsNotNone(service) client = PandoraServiceTestClient() service.initialise(client) response = service.ask("Hello") self.assertResponse(response, 'ask', "pandora", "chatbot") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_ask_integration(self): self._do_ask() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_ask_success) def test_ask_unit(self): self._do_ask() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_ask_success) def test_handler_load_ask_success(self): client = PandoraServiceTestClient() conf_file = PandoraService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "pandora", "PANDORA ASK HELLO") self.assertIsNotNone(response) self.assertEqual("Hi there!", response) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_ask_failure_server_error) def test_handler_load_ask_server_failure(self): client = PandoraServiceTestClient() conf_file = PandoraService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "pandora", "PANDORA ASK HELLO") self.assertIsNotNone(response) self.assertEqual("Pandorabots failed to return a valid response.", response) @patch("programy.services.rest.pandora.service.PandoraService.ask", patch_ask_failure_service_error) def test_handler_load_ask_service_failure(self): client = PandoraServiceTestClient() conf_file = PandoraService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "pandora", "PANDORA ASK HELLO") self.assertIsNotNone(response) self.assertEqual("Pandorabots failed to return a valid response.", response)
class MetOfficeObservationServiceTests(ServiceTestCase): def test_init(self): service = MetOfficeService( ServiceConfiguration.from_data("library", "metoffice", "weather")) self.assertIsNotNone(service) client = MetOfficeTestClient() service.initialise(client) def patch_metoffer_current_observation_success(self, lat, lng): return observation_payload def _do_observation(self): service = MetOfficeService( ServiceConfiguration.from_data("library", "metoffice", "weather")) self.assertIsNotNone(service) client = MetOfficeTestClient() service.initialise(client) response = service.observation(56.0712, -3.1743) payload = self.assertResponse(response, 'observation', 'metoffice', 'weather') self.assertTrue('observation' in payload) self.assertTrue('SiteRep' in payload['observation']) self.assertTrue('DV' in payload['observation']['SiteRep']) self.assertTrue('Location' in payload['observation']['SiteRep']['DV']) self.assertTrue( 'Period' in payload['observation']['SiteRep']['DV']['Location']) @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_observation_integration(self): self._do_observation() @patch( "programy.services.library.metoffice.metoffice.MetOffice.current_observation", patch_metoffer_current_observation_success) def test_observation_unit(self): self._do_observation() @patch( "programy.services.library.metoffice.metoffice.MetOffice.current_observation", patch_metoffer_current_observation_success) def test_obversation_by_aiml(self): client = MetOfficeTestClient() conf_file = MetOfficeService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "metoffice", "METOFFICE OBSERVATION LAT SIGN POS DEC 56 FRAC 0719912 LNG SIGN NEG DEC 3 FRAC 1750909" ) self.assertIsNotNone(response) self.assertEqual( "It is currently Partly cloudy (day) , with a temperature of 12 . 3 'C.", response) def patch_requests_postcode_success_reponse(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = postcode_success_response return mock_response @patch( "programy.services.library.metoffice.metoffice.MetOffice.current_observation", patch_metoffer_current_observation_success) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_postcode_success_reponse) def test_weather_postcode(self): client = MetOfficeTestClient() client_context = client.create_client_context("testuser") self._load_conf_file(client_context, MetOfficeService.get_default_conf_file()) self.assertTrue( "metoffice" in client_context.brain.service_handler.services) self._load_conf_file(client_context, GeoNamesService.get_default_conf_file()) self.assertTrue( "geonames" in client_context.brain.service_handler.services) response = client_context.bot.ask_question( client_context, "METOFFICE WEATHER POSTCODE KY39UR") self.assertIsNotNone(response) self.assertEqual( "It is currently Partly cloudy (day) , with a temperature of 12 . 3 'C.", response)
class GeoNamesServiceTests(ServiceTestCase): def test_init(self): service = GeoNamesService(ServiceConfiguration.from_data("rest", "geonames", "geocode")) self.assertIsNotNone(service) def patch_requests_postcode_success_reponse(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = postcode_success_response return mock_response def patch_requests_placename_success_reponse(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = placename_success_response return mock_response def _do_postcode(self): service = GeoNamesService(ServiceConfiguration.from_data("rest", "geonames", "geocode")) self.assertIsNotNone(service) client = GeoNamesServiceTestClient() service.initialise(client) response = service.latlng_for_postcode("KY3 9UR") self.assertResponse(response, 'latlng_for_postcode', "geonames", "geocode") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_postcode_integration(self): self._do_postcode() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_postcode_success_reponse) def test_postcode_unit(self): self._do_postcode() def _do_placename(self): service = GeoNamesService(ServiceConfiguration.from_data("rest", "geonames", "geocode")) self.assertIsNotNone(service) client = GeoNamesServiceTestClient() service.initialise(client) response = service.latlng_for_placename("KY3 9UR") self.assertResponse(response, 'latlng_for_placename', "geonames", "geocode") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_placename_integration(self): self._do_placename() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_placename_success_reponse) def test_placename_unit(self): self._do_placename() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_postcode_success_reponse) def test_postcode_aiml(self): client = GeoNamesServiceTestClient() conf_file = GeoNamesService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "geonames", "GEONAMES LATLNG POSTCODE KY39UR") self.assertIsNotNone(response) self.assertEqual("LAT SIGN POS DEC 56 FRAC 07206630948395 LNG SIGN NEG DEC 3 FRAC 175233081708717.", response) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_placename_success_reponse) def test_placename_aiml(self): client = GeoNamesServiceTestClient() conf_file = GeoNamesService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "geonames", "GEONAMES LATLNG PLACENAME KINGHORN") self.assertIsNotNone(response) self.assertEqual("LAT SIGN POS DEC 56 FRAC 07206630948395 LNG SIGN NEG DEC 3 FRAC 175233081708717.", response) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_placename_success_reponse) def test_placename_aiml_with_country(self): client = GeoNamesServiceTestClient() conf_file = GeoNamesService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "geonames", "GEONAMES LATLNG PLACENAME KINGHORN COUNTRY UK") self.assertIsNotNone(response) self.assertEqual("LAT SIGN POS DEC 56 FRAC 07206630948395 LNG SIGN NEG DEC 3 FRAC 175233081708717.", response) @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_placename_aiml_different_country(self): client = GeoNamesServiceTestClient() conf_file = GeoNamesService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "geonames", "GEONAMES LATLNG PLACENAME WASHINGTON COUNTRY US") self.assertIsNotNone(response) self.assertEqual("LAT SIGN POS DEC 38 FRAC 545851 LNG SIGN NEG DEC 91 FRAC 019346.", response) response = self._do_handler_load(client, conf_file, "geonames", "GEONAMES LATLNG PLACENAME WASHINGTON COUNTRY UK") self.assertIsNotNone(response) self.assertEqual("LAT SIGN POS DEC 54 FRAC 885019095463335 LNG SIGN NEG DEC 1 FRAC 5427184694082379.", response)
class AccuWeatherServiceTests(ServiceTestCase): def test_init(self): service = AccuWeatherService(ServiceConfiguration.from_data("rest", "omdb", "film")) self.assertIsNotNone(service) def patch_requests_postcodesearch_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = postcodesearch_success_response return mock_response def patch_requests_textsearch_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = textsearch_success_response return mock_response def patch_requests_conditions_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = conditions_success_response return mock_response def _do_postcodesearch(self): service = AccuWeatherService(ServiceConfiguration.from_data("rest", "accuweather", "weather")) self.assertIsNotNone(service) client = AccuWeatherServiceTestClient() service.initialise(client) response = service.postcodesearch("KY3 9UR") self.assertResponse(response, 'postcodesearch', 'accuweather', 'weather') key = AccuWeatherService.get_location_key(response) self.assertIsNotNone(key) self.assertEquals("47724_PC", key) @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_postcodesearch_integration(self): self._do_postcodesearch() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_postcodesearch_success) def test_postcodesearch_unit(self): self._do_postcodesearch() def _do_textsearch(self): service = AccuWeatherService(ServiceConfiguration.from_data("rest", "accuweather", "weather")) self.assertIsNotNone(service) client = AccuWeatherServiceTestClient() service.initialise(client) response = service.textsearch("Kinghorn") self.assertResponse(response, 'textsearch', 'accuweather', 'weather') key = AccuWeatherService.get_location_key(response) self.assertIsNotNone(key) self.assertEquals("3385695", key) @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_textsearch_integration(self): self._do_textsearch() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_textsearch_success) def test_textsearch_unit(self): self._do_textsearch() def _do_conditions(self): service = AccuWeatherService(ServiceConfiguration.from_data("rest", "accuweather", "weather")) self.assertIsNotNone(service) client = AccuWeatherServiceTestClient() service.initialise(client) response = service.conditions("3385695") self.assertResponse(response, 'conditions', 'accuweather', 'weather') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_conditions_integration(self): self._do_conditions() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_conditions_success) def test_conditions_unit(self): self._do_conditions() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_textsearch_success) def test_handler_load_textsearch(self): client = AccuWeatherServiceTestClient() conf_file = AccuWeatherService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "accuweather", "ACCUWEATHER TEXTSEARCH LOCATION KINGHORN") self.assertIsNotNone(response) self.assertEqual("ACCUWEATHER RESULT KEY 3385695.", response) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_conditions_success) def test_conditions_location_aiml(self): client = AccuWeatherServiceTestClient() conf_file = AccuWeatherService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "accuweather", "ACCUWEATHER CONDITIONS LOCATION KINGHORN") self.assertIsNotNone(response) self.assertEqual("It is currently -0.7 C and Cloudy.", response) @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_weather_location_aiml(self): client = AccuWeatherServiceTestClient() conf_file = AccuWeatherService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "accuweather", "ACCUWEATHER WEATHER LOCATION KINGHORN") self.assertIsNotNone(response) self.assertTrue(response.startswith("It is currently"))
class NewsAPIServiceTests(ServiceTestCase): def test_init(self): service = NewsAPIService(ServiceConfiguration.from_data("rest", "newsapi", "news")) self.assertIsNotNone(service) def patch_requests_everything_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = everything_success_response return mock_response def patch_requests_headlines_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = headlines_success_response return mock_response def patch_requests_sources_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = sources_success_response return mock_response def _do_everything(self): service = NewsAPIService(ServiceConfiguration.from_data("rest", "newsapi", "news")) self.assertIsNotNone(service) client = NewsAPIServiceTestClient() service.initialise(client) response = service.get_everything("chatbot") self.assertResponse(response, 'get_everything', "newsapi", "news") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_everything_integration(self): self._do_everything() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_everything_success_response) def test_everything_unit(self): self._do_everything() def _do_headlines(self): service = NewsAPIService(ServiceConfiguration.from_data("rest", "newsapi", "news")) self.assertIsNotNone(service) client = NewsAPIServiceTestClient() service.initialise(client) response = service.get_headlines("uk") self.assertResponse(response, 'get_headlines', "newsapi", "news") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_headlines_integration(self): self._do_headlines() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_headlines_success_response) def test_headlines_unit(self): self._do_headlines() def _do_sources(self): service = NewsAPIService(ServiceConfiguration.from_data("rest", "newsapi", "news")) self.assertIsNotNone(service) client = NewsAPIServiceTestClient() service.initialise(client) response = service.get_sources() self.assertResponse(response, 'get_sources', "newsapi", "news") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_sources_integration(self): self._do_sources() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_sources_success_response) def test_sources_unit(self): self._do_sources() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_everything_success_response) def test_handler_load_everything(self): client = NewsAPIServiceTestClient() conf_file = NewsAPIService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "newsapi", "NEWSAPI EVERYTHING CHATBOTS") self.assertIsNotNone(response) self.assertTrue(response.startswith("<ul><li>How artificial intelligence and machine learning produced robots we can talk to</li>"))
class GNewsServiceTests(ServiceTestCase): def test_init(self): service = GNewsService(ServiceConfiguration.from_data("rest", "gnews", "news",)) self.assertIsNotNone(service) def patch_requests_get_search_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = search_success_response return mock_response def patch_requests_get_top_news_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = top_news_success_response return mock_response def patch_requests_get_topics_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = topics_success_response return mock_response def do_search(self): service = GNewsService(ServiceConfiguration.from_data("rest", "gnews", "news")) client = GNewsServiceTestClient() service.initialise(client) self.assertIsNotNone(service) response = service.search("CHATBOTS") self.assertResponse(response, 'search', "gnews", "news") @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_get_search_success) def test_search_unit(self): self.do_search() @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_search_integration(self): self.do_search() def do_top_news(self): service = GNewsService(ServiceConfiguration.from_data("rest", "gnews", "news")) client = GNewsServiceTestClient() service.initialise(client) self.assertIsNotNone(service) response = service.top_news() self.assertResponse(response, 'top_news', "gnews", "news") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_top_news_integration(self): self.do_top_news() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_get_top_news_success) def test_top_news_unit(self): self.do_top_news() def do_topics(self): service = GNewsService(ServiceConfiguration.from_data("rest", "gnews", "news")) client = GNewsServiceTestClient() service.initialise(client) self.assertIsNotNone(service) response = service.topics("technology") self.assertResponse(response, 'topics', "gnews", "news") @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_topics_integration(self): self.do_topics() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_get_topics_success) def test_topics_unit(self): self.do_topics() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_get_search_success) def test_search_aiml(self): client = GNewsServiceTestClient() conf_file = GNewsService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "gnews", "GNEWS SEARCH CHATBOTS") self.assertIsNotNone(response) self.assertTrue(response.startswith("<ul><li>Recovering Lost Sales with Facebook Messenger Marketing</li>")) def test_topics_aiml(self): client = GNewsServiceTestClient() conf_file = GNewsService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "gnews", "GNEWS TOPICS") self.assertIsNotNone(response) self.assertEqual(response, "<ul><li>world</li><li>nation</li><li>business</li><li>technology</li><li>entertainment</li><li>sports</li><li>science</li><li>health</li></ul>.") @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_get_topics_success) def test_topic_aiml(self): client = GNewsServiceTestClient() conf_file = GNewsService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "gnews", "GNEWS TOPIC TECHNOLOGY") self.assertIsNotNone(response) self.assertEqual(response, """<ul><li>A triple folding phone? Hands-on with TCL's working DragonHinge prototype</li> <li>Windows 10 April 2020 Update</li> <li>Miyamoto Says The Success Of The Switch Was All Thanks To The "Good Timing" Of Its Release</li> <li>How to livestream the Oppo Find X2 and Oppo Smartwatch launch [Video]</li> <li>Xbox Series X and PS5 graphics hardware will finally kiss goodbye to weird-looking hair</li> <li>Santa Clara County Asks Apple, Google and Others to Cancel Large In-Person Meetings and Conferences</li> <li>Porsche Explains Why The New 911 Turbo S Is Way More Powerful</li> <li>Twitch Streamer Suspended After Accidentally Firing Real Gun At His Monitor</li> <li>Pokemon Mystery Dungeon: Rescue Team DX Review</li> <li>Apple Issues New Warning Affecting Millions Of iPhone Users</li></ul>.""") @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_get_top_news_success) def test_topnews_aiml(self): client = GNewsServiceTestClient() conf_file = GNewsService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "gnews", "GNEWS TOPNEWS EN UK") self.assertIsNotNone(response) self.assertEqual(response, "<ul><li>Coronavirus: Britons quarantined on cruise ship off San Francisco</li><li>Ceasefire in Syria after Russia and Turkey strike deal</li><li>Vatican City reports its first case of coronavirus, days after Pope tested negative</li><li>Bomb squad called and two held after suspicious device found in car in Luton</li><li>Boris Johnson's government has already spent £4.4bn on Brexit preparations, new figures reveal</li><li>Mum goes days without food in icy cold home and struggles sending son to £1 playgroup</li><li>Meghan Markle sends Twitter into meltdown as viewers think she ‘pushed’ Harry out the way</li><li>Supermarket rejects minister's food supplies claim</li><li>Coronavirus news</li><li>Amber Rudd hits out at 'rude' Oxford students after talk cancelled</li></ul>.")
class DuckDuckGoServiceTests(ServiceTestCase): def test_init(self): service = DuckDuckGoService( ServiceConfiguration.from_data("rest", "duckduckgo", "search")) self.assertIsNotNone(service) def patch_requests_instant_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = instant_success_response return mock_response def patch_requests_scrape_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = scrape_success_response return mock_response def _do_instant(self): service = DuckDuckGoService( ServiceConfiguration.from_data("rest", "duckduckgo", "search")) self.assertIsNotNone(service) client = DuckDuckGoServiceTestClient() service.initialise(client) response = service.instant("CHATBOTS") self.assertResponse(response, 'instant', 'duckduckgo', 'search') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_do_instant_integration(self): self._do_instant() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_instant_success_response) def test_do_instant_unit(self): self._do_instant() def _do_scrape(self): service = DuckDuckGoService( ServiceConfiguration.from_data("rest", "duckduckgo", "search")) self.assertIsNotNone(service) client = DuckDuckGoServiceTestClient() service.initialise(client) response = service.scrape("CHATBOTS") self.assertResponse(response, 'scrape', 'duckduckgo', 'search') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_do_scraoe_integration(self): self._do_scrape() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_scrape_success_response) def test_do_scrape_unit(self): self._do_scrape() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_instant_success_response) def test_instant_aiml(self): client = DuckDuckGoServiceTestClient() conf_file = DuckDuckGoService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "duckduckgo", "DUCKDUCKGO INSTANT CHATBOTS") self.assertIsNotNone(response) self.assertTrue( response.startswith( "A chatbot is a piece of software that conducts a conversation via auditory or textual methods" )) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_scrape_success_response) def test_scrape_aiml(self): client = DuckDuckGoServiceTestClient() conf_file = DuckDuckGoService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "duckduckgo", "DUCKDUCKGO SCRAPE WHAT ARE CHATBOTS") self.assertTrue( response.startswith( "A chatbot is a piece of software that conducts a conversation via auditory or textual methods" ))
class CocktailDBServiceTests(ServiceTestCase): def test_init(self): service = CocktailDBService( ServiceConfiguration.from_data("rest", "cocktaildb", "drinks")) self.assertIsNotNone(service) def patch_requests_search_by_name_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = search_by_name_success_response return mock_response def patch_requests_search_by_ingredient_success(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = search_by_ingredient_success_response return mock_response def _do_search_by_name(self): service = CocktailDBService( ServiceConfiguration.from_data("rest", "cocktaildb", "drinks")) self.assertIsNotNone(service) client = CocktailDBServiceTestClient() service.initialise(client) response = service.search_by_name("Old fashioned") self.assertResponse(response, 'search_by_name', 'cocktaildb', 'drinks') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_search_by_name_integration(self): self._do_search_by_name() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_search_by_name_success) def test_search_by_name_unit(self): self._do_search_by_name() def _do_search_by_ingredient(self): service = CocktailDBService( ServiceConfiguration.from_data("rest", "cocktaildb", "drinks")) self.assertIsNotNone(service) client = CocktailDBServiceTestClient() service.initialise(client) response = service.search_by_ingredient("bourbon") self.assertResponse(response, 'search_by_ingredient', 'cocktaildb', 'drinks') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_search_by_ingredient_integration(self): self._do_search_by_ingredient() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_search_by_ingredient_success) def test_search_by_ingredient_unit(self): self._do_search_by_ingredient() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_search_by_name_success) def test_handler_load_name(self): client = CocktailDBServiceTestClient() conf_file = CocktailDBService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "cocktaildb", "COCKTAILDB SEARCH NAME OLD FASHIONED") self.assertIsNotNone(response) self.assertTrue(response.startswith("To make a Old Fashioned")) @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_search_by_ingredient_success) def test_handler_load_ingredient(self): client = CocktailDBServiceTestClient() conf_file = CocktailDBService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "cocktaildb", "COCKTAILDB SEARCH INGREDIENT BOURBON") self.assertIsNotNone(response) self.assertTrue( response.startswith( "Bourbon whiskey /bɜːrbən/ is a type of American whiskey:"))
class WikipediaServiceTests(ServiceTestCase): def test_init(self): service = WikipediaService( ServiceConfiguration.from_data("library", "wikipedia", "search")) self.assertIsNotNone(service) def patch_wikipedia_search_success(self, query, results=10, suggestion=False): return search_success_response def patch_wikipedia_summary_success(self, title, sentences=0, chars=0, auto_suggest=True, redirect=True): return summary_success_response def _do_search(self): service = WikipediaService( ServiceConfiguration.from_data("library", "wikipedia", "search")) self.assertIsNotNone(service) response = service.search("CHATBOTS") payload = self.assertResponse(response, 'search', 'wikipedia', 'search') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_search_integration(self): self._do_search() @patch("wikipedia.search", patch_wikipedia_search_success) def test_search_unit(self): self._do_search() def _do_summary(self): service = WikipediaService( ServiceConfiguration.from_data("library", "wikipedia", "search")) self.assertIsNotNone(service) response = service.summary("8 Out of 10 Cats Does Countdown") payload = self.assertResponse(response, 'summary', 'wikipedia', 'search') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_summary_integration(self): self._do_summary() @patch("wikipedia.summary", patch_wikipedia_summary_success) def test_summary_unit(self): self._do_summary() @patch("wikipedia.search", patch_wikipedia_search_success) def test_search_by_aiml(self): client = WikipediaTestClient() conf_file = WikipediaService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "wikipedia", "WIKIPEDIA SEARCH CHATBOTS") self.assertIsNotNone(response) self.assertTrue(response.startswith("<ul><li>Chatbot</li><li>")) @patch("wikipedia.summary", patch_wikipedia_summary_success) def test_summary_by_aiml(self): client = WikipediaTestClient() conf_file = WikipediaService.get_default_conf_file() response = self._do_handler_load(client, conf_file, "wikipedia", "WIKIPEDIA SUMMARY 8 Out of 10 Cats") self.assertIsNotNone(response) self.assertTrue( response.startswith( "8 Out of 10 Cats Does Countdown is a British comedy panel show." ))
class DarkSkyServiceTests(ServiceTestCase): def test_init(self): service = DarkSkyService( ServiceConfiguration.from_data("rest", "darksky", "weather")) self.assertIsNotNone(service) def patch_requests_forecast_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = forecast_success_response return mock_response def patch_requests_timemachine_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = timemachine_success_response return mock_response def _do_forecast(self): service = DarkSkyService( ServiceConfiguration.from_data("rest", "darksky", "weather")) self.assertIsNotNone(service) client = DarkSkyServiceTestClient() service.initialise(client) response = service.forecast(56.0712, -3.1743) self.assertResponse(response, 'forecast', 'darksky', 'weather') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_forecast_integration(self): self._do_forecast() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_forecast_success_response) def test_forecast_unit(self): self._do_forecast() def _do_timemachine(self): service = DarkSkyService( ServiceConfiguration.from_data("rest", "darksky", "weather")) self.assertIsNotNone(service) client = DarkSkyServiceTestClient() service.initialise(client) response = service.timemachine(56.0712, -3.1743, "255657600") self.assertResponse(response, 'timemachine', 'darksky', 'weather') @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_timemachine_integration(self): self._do_timemachine() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_timemachine_success_response) def test_timemachine_unit(self): self._do_timemachine() @patch("programy.services.rest.base.RESTService._requests_get", patch_requests_forecast_success_response) def test_forecast_aiml(self): client = DarkSkyServiceTestClient() conf_file = DarkSkyService.get_default_conf_file() response = self._do_handler_load( client, conf_file, "darksky", "DARKSKY FORECAST LAT SIGN POS DEC 56 FRAC 0712 LNG SIGN NEG DEC 3 FRAC 1743" ) self.assertIsNotNone(response) self.assertEqual("It is currently clear.", response) def patch_requests_postcode_success_response(self, url, headers, timeout): mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = postcode_success_response return mock_response @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_forecast_postcode_aiml(self): client = DarkSkyServiceTestClient() client_context = client.create_client_context("testuser") self._load_conf_file(client_context, DarkSkyService.get_default_conf_file()) self.assertTrue( "darksky" in client_context.brain.service_handler.services) self._load_conf_file(client_context, GeoNamesService.get_default_conf_file()) self.assertTrue( "geonames" in client_context.brain.service_handler.services) response = client_context.bot.ask_question( client_context, "DARKSKY FORECAST POSTCODE KY39UR") self.assertIsNotNone(response) self.assertTrue(response.startswith("It is currently")) @unittest.skipIf(integration_tests_active() is False, integration_tests_disabled) def test_timemachine_postcode_aiml(self): client = DarkSkyServiceTestClient() client_context = client.create_client_context("testuser") self._load_conf_file(client_context, DarkSkyService.get_default_conf_file()) self.assertTrue( "darksky" in client_context.brain.service_handler.services) self._load_conf_file(client_context, GeoNamesService.get_default_conf_file()) self.assertTrue( "geonames" in client_context.brain.service_handler.services) response = client_context.bot.ask_question( client_context, "DARKSKY TIMEMACHINE POSTCODE KY39UR TIME 255657600") self.assertIsNotNone(response) self.assertTrue(response.startswith("It was"))
def get_license_key_file(self): if integration_tests_active() is True: return os.path.dirname(__file__) + os.sep + ServiceTestClient.RELATIVE_PATH + os.sep + "license.keys" else: return os.path.dirname(__file__) + os.sep + ServiceTestClient.RELATIVE_PATH + os.sep + "test_licenses.keys"