def test_get_news_feed_articles_none_200_response(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 401, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title", "description": "test description", "publishedAt": "test publishedAt", "author": "test author", "url": "test url", "urlToImage": "test urlToImage" } ] } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles))
def setUp(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + "/../../../../bots/y-bot/config/license.keys") self.lat = 56.0720397 self.lng = -3.1752001
class NewsAPIExtensionIntegrationTests(unittest.TestCase): def setUp(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file(os.path.dirname(__file__)+"/../../../../bots/y-bot/config/license.keys") def test_init(self): newsapi = NewsAPI(self.license_keys) self.assertIsNotNone(newsapi) articles = newsapi.get_headlines(NewsAPI.BBC_NEWS) self.assertIsNotNone(articles)
def test_geonames(self): license_keys = LicenseKeys() license_keys.load_license_key_file(os.path.dirname(__file__)+ os.sep + "test.keys") geonames = GeoNamesApi(license_keys) self.assertIsNotNone(geonames) GeoNamesApi.get_latlong_for_postcode_response_file = os.path.dirname(__file__)+ os.sep + "geonames_latlong.json" latlng = geonames.get_latlong_for_postcode('KY39UR') self.assertIsNotNone(latlng) self.assertEquals(latlng.latitude, 56.07206267570594) self.assertEquals(latlng.longitude, -3.175233048730664)
class GeoNamesTests(unittest.TestCase): def setUp(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + "/../../../../bots/y-bot/config/license.keys") def test_geonames(self): geonames = GeoNamesApi(self.license_keys) self.assertIsNotNone(geonames) latlng = geonames.get_latlong_for_postcode('KY39UR') self.assertIsNotNone(latlng) self.assertEquals(latlng.latitude, 56.07206267570594) self.assertEquals(latlng.longitude, -3.175233048730664)
def test_get_news_feed_articles_content_not_json(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse(200, {"content-type": "application/xml"}, None) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles))
def __init__(self, id, argument_parser=None): self._id = id self._arguments = self.parse_arguments(argument_parser=argument_parser) # print("self._arguments: {}".format(self._arguments)) self.initiate_logging(self.arguments) self._configuration = None self.load_configuration(self.arguments) self.parse_configuration() bot_configurations = self.configuration.client_configuration.configurations self._bot_factory = BotFactory(self, bot_configurations) self._license_keys = LicenseKeys() self.load_license_keys() self.get_license_keys() self._scheduler = None self.load_scheduler() self._renderer = self.load_renderer()
def test_geonames_with_license_keys(self): license_keys = LicenseKeys() license_keys.add_key('GEO_NAMES_COUNTRY', "DummyValue") license_keys.add_key('GEO_NAMES_ACCOUNTNAME', "DummyValue") geonames = GeoNamesApi(license_keys)
def test_geonames_no_country(self): license_keys = LicenseKeys() license_keys.add_key('GEO_NAMES_ACCOUNTNAME', "DummyValue") with self.assertRaises(Exception): geonames = GeoNamesApi(license_keys)
def test_geonames_no_license_keys(self): license_keys = LicenseKeys() with self.assertRaises(Exception): geonames = GeoNamesApi(license_keys)
def test_missing_keys(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "bad_test.keys") with self.assertRaises(Exception): newsapi = NewsAPI(self.license_keys)
class MetOfficeWeatherExtensionIntegrationTests(unittest.TestCase): def setUp(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + "/../../../../bots/y-bot/config/license.keys") self.lat = 56.0720397 self.lng = -3.1752001 def test_observation(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) observation = met_office.current_observation(self.lat, self.lng) self.assertIsNotNone(observation) report = observation.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day_now() report = observation.get_report_for_date(date) self.assertIsNotNone(report) datapoint = report.get_time_period_by_time("300") self.assertIsNotNone(datapoint) self.assertIsInstance(datapoint, ObservationDataPoint) def test_threehourly_forecast(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) forecast = met_office.three_hourly_forecast(self.lat, self.lng) self.assertIsNotNone(forecast) report = forecast.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day_now() report = forecast.get_report_for_date(date) self.assertIsNotNone(report) datapoint = report.get_time_period_by_time("900") self.assertIsNotNone(datapoint) self.assertIsInstance(datapoint, ThreeHourlyForecastDataPoint) def test_daily_forecast(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) forecast = met_office.daily_forecast(self.lat, self.lng) self.assertIsNotNone(forecast) report = forecast.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day_now() report = forecast.get_report_for_date(date) self.assertIsNotNone(report) day_datapoint = report.get_time_period_by_type('Day') self.assertIsNotNone(day_datapoint) self.assertIsInstance(day_datapoint, DailyForecastDayDataPoint) night_datapoint = report.get_time_period_by_type('Night') self.assertIsNotNone(night_datapoint) self.assertIsInstance(night_datapoint, DailyForecastNightDataPoint)
class NewsAPITests(unittest.TestCase): def test_missing_license_keys(self): with self.assertRaises(Exception): newsapi = NewsAPI(None) def test_missing_keys(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "bad_test.keys") with self.assertRaises(Exception): newsapi = NewsAPI(self.license_keys) def test_format_url(self): self.assertEqual( "https://newsapi.org/v1/articles?source=testservice&sortBy=top&apiKey=key", NewsAPI._format_url("testservice", "key")) self.assertEqual( "https://newsapi.org/v1/articles?source=testservice&sortBy=bottom&apiKey=key", NewsAPI._format_url("testservice", "key", sort_by="bottom")) def test_get_news_feed_articles(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 200, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title", "description": "test description", "publishedAt": "test publishedAt", "author": "test author", "url": "test url", "urlToImage": "test urlToImage" } ] } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(1, len(articles)) self.assertEqual("test title", articles[0].title) self.assertEqual("test description", articles[0].description) self.assertEqual("test publishedAt", articles[0].published_at) self.assertEqual("test author", articles[0].author) self.assertEqual("test url", articles[0].url) self.assertEqual("test urlToImage", articles[0].url_to_image) def test_get_news_feed_articles_none_200_response(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 401, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title", "description": "test description", "publishedAt": "test publishedAt", "author": "test author", "url": "test url", "urlToImage": "test urlToImage" } ] } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles)) def test_get_news_feed_articles_content_not_json(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse(200, {"content-type": "application/xml"}, None) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles)) def test_get_news_feed_articles_no_articles(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 200, {"content-type": "application/json"}, json.loads(""" { } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles)) def test_get_news_feed_articles_no_articles_included(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 200, {"content-type": "application/json"}, json.loads(""" { "articles": [ ] } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles))
def test_load_license_keys_data(self): keys = LicenseKeys() keys.load_license_key_data(""" KEY1=Key1Data KEY2=Key2Data # This is a comment KEY3=KEY3 Data With Spaces This is not a key """) self.assertTrue(keys.has_key('KEY1')) self.assertEquals("Key1Data", keys.get_key("KEY1")) self.assertTrue(keys.has_key('KEY2')) self.assertEquals("Key2Data", keys.get_key("KEY2")) self.assertTrue(keys.has_key('KEY3')) self.assertEquals("KEY3 Data With Spaces", keys.get_key("KEY3")) with self.assertRaises(Exception): keys.get_key("KEY5")
import os import metoffer from programr.utils.weather.metoffice import MetOffice if __name__ == '__main__': # Only to be used to create test data for unit aiml_tests from programr.utils.license.keys import LicenseKeys license_keys = LicenseKeys() license_keys.load_license_key_file( os.path.dirname(__file__) + '/../../../../bots/y-bot/config/license.keys') met_office = MetOffice(license_keys) lat = 56.0720397 lng = -3.1752001 log_to_file = False if log_to_file: met_office.nearest_location_observation_to_file( lat, lng, "observation.json") met_office.nearest_location_forecast_to_file(lat, lng, metoffer.DAILY, "forecast_daily.json") met_office.nearest_location_forecast_to_file( lat, lng, metoffer.THREE_HOURLY, "forecast_threehourly.json")
def setUp(self): self.bot = TestBot() self.bot.license_keys = LicenseKeys() self.bot.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys")
class BotClient(ResponseLogger): def __init__(self, id, argument_parser=None): self._id = id self._arguments = self.parse_arguments(argument_parser=argument_parser) # print("self._arguments: {}".format(self._arguments)) self.initiate_logging(self.arguments) self._configuration = None self.load_configuration(self.arguments) self.parse_configuration() bot_configurations = self.configuration.client_configuration.configurations self._bot_factory = BotFactory(self, bot_configurations) self._license_keys = LicenseKeys() self.load_license_keys() self.get_license_keys() self._scheduler = None self.load_scheduler() self._renderer = self.load_renderer() def ylogger_type(self): return "client" @property def configuration(self): return self._configuration @property def id(self): return self._id @property def arguments(self): return self._arguments @property def license_keys(self): return self._license_keys @property def scheduler(self): return self._scheduler @property def bot_factory(self): return self._bot_factory @property def renderer(self): return self._renderer @property def session_saving_mode(self): return self._session_saving_mode @property def client_context(self): session_pickle_dir = self.configuration.client_configuration.configurations[ 0].session.session_saving_dir self._session_saving_mode = self.configuration.client_configuration.configurations[ 0].session.session_saving_mode if self.session_saving_mode: try: client_context = self.load_client_context( self.configuration.client_configuration.default_userid, session_pickle_dir) except Exception as exp: YLogger.exception(self, "could not load the session pickles", exp) client_context = self.create_client_context( self._configuration.client_configuration.default_userid) else: client_context = self.create_client_context( self._configuration.client_configuration.default_userid) return client_context def get_description(self): raise NotImplementedError( "You must override this and return a client description") def add_client_arguments(self, parser=None): # Nothing to add return def parse_configuration(self): # Nothing to add return def parse_args(self, arguments, parsed_args): # Nothing to add return def parse_arguments(self, argument_parser): client_args = CommandLineClientArguments(self, parser=argument_parser) client_args.parse_args(self) return client_args def load_license_keys(self): if self.configuration is not None: if self.configuration.client_configuration.license_keys is not None: YLogger.debug( self, f"filename containing license keys: {self.configuration.client_configuration.license_keys}" ) self._license_keys.load_license_key_file( self.configuration.client_configuration.license_keys) else: YLogger.warning( self, "No client configuration setting for license_keys") else: YLogger.warning( self, "No configuration defined when loading license keys") def get_license_keys(self): return def initiate_logging(self, arguments): if arguments.logging is not None: with open(arguments.logging, 'r+', encoding="utf-8") as yml_data_file: logging_config = yaml.load(yml_data_file, Loader=yaml.FullLoader) logging.config.dictConfig(logging_config) YLogger.info(self, "Now logging under configuration") else: print( "Warning. No logging configuration file defined, using defaults..." ) def get_client_configuration(self): """ By overriding this class in you Configuration file, you can add new configurations and stil use the dynamic loader capabilities :return: Client configuration object """ raise NotImplementedError( "You must override this and return a subclassed client configuration" ) def load_configuration(self, arguments): if arguments.bot_root is None: if arguments.config_filename is not None: arguments.bot_root = os.path.dirname(arguments.config_filename) else: arguments.bot_root = "." if arguments.config_filename is not None: self._configuration = ConfigurationFactory.load_configuration_from_file( self.get_client_configuration(), arguments.config_filename, arguments.config_format, arguments.bot_root) else: print("No configuration file specified, using defaults only !") self._configuration = ProgramrConfiguration( self.get_client_configuration()) def load_scheduler(self): if self.configuration.client_configuration.scheduler is not None: self._scheduler = ProgramrScheduler( self, self.configuration.client_configuration.scheduler) self._scheduler.start() def create_client_context(self, userid, load_variables=True): client_context = ClientContext(self, userid) client_context.bot = self._bot_factory.select_bot() #TODO: here is where we need to load in variables client_context.brain = client_context.bot.brain if load_variables: client_context.brain._load_variables(client_context) return client_context def load_client_context(self, userid, session_pickle_dir): questions_save_file = os.path.join(session_pickle_dir, "questions.p") properties_save_file = os.path.join(session_pickle_dir, "properties.p") questions_pickle_file = open(questions_save_file, 'rb') properties_pickle_file = open(properties_save_file, 'rb') questions = pickle.load(questions_pickle_file) properties = pickle.load(properties_pickle_file) client_context = self.create_client_context(userid) client_context.brain.bot.set_conversation_question( client_context, questions) client_context.bot.conversations[userid].set_properties(properties) return client_context def load_renderer(self): try: if self.get_client_configuration().renderer is not None: clazz = ClassLoader.instantiate_class( self.get_client_configuration().renderer.renderer) return clazz(self) except Exception as e: YLogger.exception(None, "Failed to load config specified renderer", e) return self.get_default_renderer() def get_default_renderer(self): return TextRenderer(self) def get_question(self, client_context): raise NotImplementedError( "You must override this and implement the logic") def process_question(self, client_context, question): raise NotImplementedError( "You must override this and implement the logic to create a response to the question" ) def render_response(self, client_context, response): raise NotImplementedError( "You must override this and implement the logic to process the response by rendering using a RCS renderer" ) def process_response(self, client_context, response): raise NotImplementedError( "You must override this and implement the logic to display the response to the user" ) def run(self): raise NotImplementedError( "You must override this and implement the logic to run the client")
def test_add_keys(self): keys = LicenseKeys() self.assertFalse(keys.has_key("KEY1")) keys.add_key("KEY1", "VALUE1") self.assertTrue(keys.has_key("KEY1")) self.assertEqual("VALUE1", keys.get_key("KEY1")) keys.add_key("KEY1", "VALUE2") self.assertTrue(keys.has_key("KEY1")) self.assertEqual("VALUE2", keys.get_key("KEY1"))
def setUp(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + "/../../../../bots/y-bot/config/license.keys")
def test_load_license_keys_file(self): keys = LicenseKeys() keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "test.keys") self.assertTrue(keys.has_key('KEY1')) self.assertEquals("Key1Data", keys.get_key("KEY1")) self.assertTrue(keys.has_key('KEY2')) self.assertEquals("Key2Data", keys.get_key("KEY2")) self.assertTrue(keys.has_key('KEY3')) self.assertEquals("KEY3 Data With Spaces", keys.get_key("KEY3")) with self.assertRaises(Exception): keys.get_key("KEY5")
import os from programr.utils.text.text import TextUtils from programr.utils.geo.google import GoogleMaps from programr.utils.license.keys import LicenseKeys if __name__ == '__main__': license_keys = LicenseKeys() license_keys.load_license_key_file( os.path.dirname(__file__) + TextUtils.replace_path_seperator( '/../../../../bots/y-bot/config/license.keys')) # Only to be used to create test data for unit aiml_tests googlemaps = GoogleMaps(license_keys) # Running these tools drops test files into the geocode test folder googlemaps.store_get_latlong_for_location_to_file( "KY3 9UR", TextUtils.replace_path_seperator( "../../../test/utils/geo/google_latlong.json")) googlemaps.store_get_distance_between_addresses_as_file( "Edinburgh", "Kinghorn", TextUtils.replace_path_seperator( "../../../test/utils/geo/distance.json")) googlemaps.store_get_directions_between_addresses_as_file( "Edinburgh", "Kinghorn", TextUtils.replace_path_seperator( "../../../test/utils/geo/directions.json")) googlemaps.store_get_latlong_for_location_to_file(