コード例 #1
0
class TestBaseAPI(unittest.TestCase):
    def setUp(self):
        self.baseAPI = BaseAPI()

    @patch('requests.get')
    def test_get_country_info_success(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 200
        mock_resp.content = json.dumps([{'alpha2Code': 'EG', 'bb': 'hhh'}])

        mock_get.return_value = mock_resp

        self.assertEqual('EG',
                         self.baseAPI.get_country_info("egypt", "alpha2Code"))

    @patch('requests.get')
    def test_get_country_info_fail(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 201

        mock_get.return_value = mock_resp

        self.assertIsNone(self.baseAPI.get_country_info("egypt", "alpha2Code"))

    def test_get_country_info_fail_invalid_input_type(self):

        self.assertIsNone(self.baseAPI.get_country_info(5, "alpha2Code"))
        self.assertIsNone(self.baseAPI.get_country_info("egypt", 0.2))
        self.assertIsNone(self.baseAPI.get_country_info("egypt", 'sss'))

    @patch('requests.get')
    def test_call_api_success(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 200
        mock_get.return_value = mock_resp
        self.assertTrue(self.baseAPI.callAPI())

    @patch('requests.get')
    def test_call_api_fail(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 400
        mock_get.return_value = mock_resp
        self.assertFalse(self.baseAPI.callAPI())

    def test_cache_success(self):
        requests_cache.install_cache(cache_name='My_cache',
                                     backend='sqlite',
                                     expire_after=10)
        _, formCache1 = self.baseAPI.get_country_info("egypt", "alpha2Code")
        _, formCache2 = self.baseAPI.get_country_info("egypt", "alpha2Code")

        self.assertFalse(formCache1)
        self.assertTrue(formCache2)

    def test_cache_fail(self):
        _, formCache1 = self.baseAPI.get_country_info("egypt", "alpha2Code")
        _, formCache2 = self.baseAPI.get_country_info("egypt", "alpha2Code")

        self.assertFalse(formCache1)
        self.assertFalse(formCache2)
コード例 #2
0
    def test_RunIntegration1(self, mock_get_country_info):
        app = Flask(__name__)
        with app.app_context():
            mock_get_country_info.return_value = "EGY"
            baseAPI = BaseAPI()
            countryInfo = baseAPI.get_country_info(str("egypt"),
                                                   "alpha3Code")  # EGY
            if countryInfo is None:
                countryInfo = "Not Found Or Invalid Input"

            self.assertEqual(str("EGY"), str(countryInfo))
コード例 #3
0
class TestBaseAPI(unittest.TestCase):
    def setUp(self):
        self.baseAPI = BaseAPI()
        self.tester = app.test_client()
        self.tester.testing = True

    @patch('requests.get')
    def test_get_country_info_success(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 200
        mock_resp.content = json.dumps([{'alpha2Code': 'EG', 'bb': 'hhh'}])

        mock_get.return_value = mock_resp

        a, _ = self.baseAPI.get_country_info("egypt", "alpha2Code")
        self.assertEqual('EG', a)

    @patch('requests.get')
    def test_get_country_info_fail(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 201

        mock_get.return_value = mock_resp

        a, _ = self.baseAPI.get_country_info("egypt", "alpha2Code")
        self.assertIsNone(a)

    def test_get_country_info_fail_invalid_input_type(self):
        a, _ = self.baseAPI.get_country_info(5, "alpha2Code")
        self.assertIsNone(a)
        b, _ = self.baseAPI.get_country_info("egypt", 0.2)
        self.assertIsNone(b)
        c, _ = self.baseAPI.get_country_info("egypt", 'sss')
        self.assertIsNone(c)

    @patch('requests.get')
    def test_call_api_success(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 200
        mock_get.return_value = mock_resp
        self.assertTrue(self.baseAPI.callAPI())

    @patch('requests.get')
    def test_call_api_fail(self, mock_get):
        mock_resp = Mock()
        mock_resp.status_code = 400
        mock_get.return_value = mock_resp
        self.assertFalse(self.baseAPI.callAPI())

    def test_cache_success(self):
        appTest = Flask(__name__)
        with appTest.app_context():
            _, formCache1 = self.baseAPI.get_country_info(
                "egypt", "alpha2Code")
            _, formCache2 = self.baseAPI.get_country_info(
                "egypt", "alpha2Code")

            self.assertFalse(formCache1)
            self.assertTrue(formCache2)
コード例 #4
0
ファイル: app.py プロジェクト: Karememad21/REST_Countries_API
def outputPage(countryName, category):
    baseAPI = BaseAPI()
    status = baseAPI.callAPI()
    if status:
        categories = category.split(",")
        countryInfo = []
        for i in categories:
            temp, _ = baseAPI.get_country_info(str(countryName), i)
            countryInfo.append(temp)

        if countryInfo is None:
            countryInfo = "Not Found Or Invalid Input"

        return jsonify(countryInfo)
    else:
        return jsonify("Error")
コード例 #5
0
 def setUp(self):
     self.baseAPI = BaseAPI()
コード例 #6
0
 def setUp(self):
     self.baseAPI = BaseAPI()
     self.tester = app.test_client()
     self.tester.testing = True