Example #1
0
    def test_get_headlines(self):
        client = TestClient()
        client.add_license_keys_store()

        mock_api = MockNewsApiApi()

        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"
                }
            ]
        }
        """))

        newsapi = NewsAPI(client.license_keys, mock_api)
        self.assertIsNotNone(newsapi)

        headlines = newsapi.get_headlines(NewsAPI.UK_NEWS)
        self.assertIsNotNone(headlines)

        headlines = newsapi.get_headlines("Other")
        self.assertIsNotNone(headlines)
        self.assertEquals([], headlines)
Example #2
0
    def test_ask_question_post_with_authorization(self):
        client = TestClient()
        client.add_license_keys_store()
        self._client_context = client.create_client_context("testid")

        self._client_context.brain._openchatbots._openchatbots['CHATBOT1'] = OpenChatBot("openchat1",
                                                                                         "http://localhost:5959/api/rest/v2.0/ask",
                                                                                         ["POST"],
                                                                                         "Basic",
                                                                                         "1234567890")

        mock_data = {"response": {
                            "text": "Hi there from chatbot1",
                        },
                        "status": {"code": 200, "text": "success"}
                     }
        mock_response = json.dumps(mock_data)

        service_config = BrainServiceConfiguration("openchatbot")
        service_config._method = 'POST'

        service = OpenChatRESTService(service_config, api=MockRestAPI(200, mock_response))

        response = service.ask_question(self._client_context, "chatbot1 Hello")
        self.assertIsNotNone(response)
        self.assertEqual("Hi there from chatbot1", response)
Example #3
0
    def test_abc_news_au(self):
        client = TestClient()
        client.add_license_keys_store()

        mock_api = MockNewsApiApi()

        newsapi = NewsAPI(client.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"
                }
            ]
        }
        """))

        response = NewsAPI.abc_news_au("testservice", 10, False, False)
        self.assertIsNotNone(response)
Example #4
0
    def test_get_news_feed_articles_none_200_response(self):
        client = TestClient()
        client.add_license_keys_store()

        mock_api = MockNewsApiApi()

        newsapi = NewsAPI(client.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))
Example #5
0
    def test_ask_question_get(self):
        client = TestClient()
        client.add_license_keys_store()
        self._client_context = client.create_client_context("testid")

        self._client_context.brain._openchatbots._openchatbots[
            'CHATBOT1'] = OpenChatBot(
                "openchat1", "http://localhost:5959/api/rest/v2.0/ask", "GET")

        mock_data = {
            "response": {
                "text": "Hi there from chatbot1",
            },
            "status": {
                "code": 200,
                "text": "success"
            }
        }
        mock_response = json.dumps(mock_data)

        service = OpenChatMetaBotRESTService(
            BrainServiceConfiguration("openchatbot"),
            api=MockRestAPI(200, mock_response))

        response = service.ask_question(self._client_context, "chatbot1 Hello")
        self.assertIsNotNone(response)
        self.assertEquals(
            '{"response": {"text": "Hi there from chatbot1"}, "status": {"code": 200, "text": "success"}}',
            response)
    def setUp(self):
        client = TestClient()
        client.add_license_keys_store()
        self.context = client.create_client_context("testid")

        bot = unittest.mock.Mock()
        self.context.bot = bot
        self.context.brain = bot.brain
    def test_geonames_no_lat_long(self):
        client = TestClient()
        client.add_license_keys_store()

        geonames = MockGeoNamesApi(response={"postalCodes": [{}]})
        self.assertIsNotNone(geonames)

        with self.assertRaises(Exception):
            _ = geonames.get_latlong_for_postcode('KY39UR')
Example #8
0
    def test_geonames(self):
        client = TestClient()
        client.add_license_keys_store()

        geonames = GeoNamesApi(client.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.assertEqual(latlng.latitude, 56.07206267570594)
        self.assertEqual(latlng.longitude, -3.175233048730664)
    def test_geonames(self):
        client = TestClient()
        client.add_license_keys_store()

        geonames = MockGeoNamesApi(response={
            "postalCodes": [{
                "lat": 56.07206267570594,
                "lng": -3.175233048730664
            }]
        })
        self.assertIsNotNone(geonames)

        latlng = geonames.get_latlong_for_postcode('KY39UR')
        self.assertIsNotNone(latlng)
        self.assertEqual(latlng.latitude, 56.07206267570594)
        self.assertEqual(latlng.longitude, -3.175233048730664)
Example #10
0
    def test_spellchecker(self):
        client = TestClient()

        client.add_license_keys_store()
        client.add_spelling_store()

        checker = NorvigSpellingChecker()
        self.assertIsNotNone(checker)
        checker.initialise(client.storage_factory)

        self.assertEqual("THIS", checker.correct("THIS"))
        self.assertEqual("THIS", checker.correct("This"))
        self.assertEqual("THIS", checker.correct("this"))

        self.assertEqual("LOCATION", checker.correct("LOCETION"))
        self.assertEqual("LOCATION", checker.correct("Locetion"))
        self.assertEqual("LOCATION", checker.correct("locetion"))
Example #11
0
    def test_get_news_feed_articles_content_not_json(self):
        client = TestClient()
        client.add_license_keys_store()

        mock_api = MockNewsApiApi()

        newsapi = NewsAPI(client.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))
Example #12
0
    def test_get_news_feed_articles_max_articles_99(self):
        client = TestClient()
        client.add_license_keys_store()

        mock_api = MockNewsApiApi()

        newsapi = NewsAPI(client.license_keys, mock_api)
        self.assertIsNotNone(newsapi)

        mock_api.response = MockResponse(
            200, {"content-type": "application/json"},
            json.loads("""
        {
            "articles": [
                {
                "title":        "test title1",
                "description":  "test description1",
                "publishedAt":  "test publishedAt1",
                "author":       "test author1",
                "url":          "test url1",
                "urlToImage":   "test urlToImage1"
                },
                {
                "title":        "test title2",
                "description":  "test description2",
                "publishedAt":  "test publishedAt2",
                "author":       "test author2",
                "url":          "test url2",
                "urlToImage":   "test urlToImage2"
                }
            ]
        }
        """))
        mock_url = NewsAPI._format_url("testservice", "key")

        articles = newsapi._get_news_feed_articles(mock_url, 99, True, False)
        self.assertIsNotNone(articles)
        self.assertEqual(2, len(articles))
        self.assertEqual("test title1", articles[0].title)
        self.assertEqual("test description1", articles[0].description)
        self.assertEqual("test publishedAt1", articles[0].published_at)
        self.assertEqual("test author1", articles[0].author)
        self.assertEqual("test url1", articles[0].url)
        self.assertEqual("test urlToImage1", articles[0].url_to_image)
 def setUp(self):
     client = TestClient()
     client.add_license_keys_store()
     self._client_context = client.create_client_context("testid")