Exemple #1
0
class TestFao(TestCase):
    def setUp(self):
        self.data = Fao()

    def test_list_countries(self):
        countries = self.data.list_countries()
        self.assertEqual(countries[0], "Afghanistan")
        self.assertEqual(countries[42], "Cyprus")
        self.assertEqual(countries[-1], "Zimbabwe")
        self.assertEqual(countries[137], "Senegal")
        self.assertEqual(len(countries), 174)

    def test_list_products_countries(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.list_products_countries([countries[0]])[countries[0]][0],
            "Wheat and products")
        self.assertEqual(
            self.data.list_products_countries([countries[-1]
                                               ])[countries[-1]][0],
            "Wheat and products")

    def test_min_production(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.min_production([countries[0]],
                                     [2010, 2013])[countries[0]][0][-1], 0)

    def test_max_production(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.max_production([countries[0]],
                                     [2010, 2013])[countries[0]][-1], 5495)

    def test_average_production(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.average_production([countries[0], countries[42]],
                                         [1961, 1965], "Wheat and products"), {
                                             countries[0]: 1889.8,
                                             countries[42]: 67.4
                                         })
Exemple #2
0
    def conclusion_gdp_growth_prod(self, countries_list, year_range_2, production_type):
        '''
        Returns a list with the countries and their average gdp, growth and production in years_list
         '''

        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        growth = []
        prod = []
        diff_growth = []
        diff_prod = []
        f = Fao()
        diff=""
        year_range_1 = [year_range_2[0]-5, year_range_2[0]-1]

        for country in countries_list:

            for fao_country in f.list_countries():

                if self.similar(country, fao_country) == 1:
                    growth.append(list(self.average_growth([country], year_range_1).values())[0])
                    prod.append(
                        list(f.average_production([country], year_range_1).values())[0])

                    growth.append(list(self.average_growth([country], year_range_2).values())[0])
                    prod.append(
                        list(f.average_production([country], year_range_2).values())[0])
                    break
        for i in range(len(growth)):
            if growth[i] == None:
                growth[i] = 0
            if prod[i] == None:
                prod[i] = 0

        for i in range(0, len(growth), 2):
            diff_growth.append(growth[i + 1] - growth[i])
            diff_prod.append(prod[i + 1] - prod[i])

        for i in range(len(diff_prod)):
            diff += str(countries_list[i]) + ": 'average growth difference' : " + str(
                diff_growth[i]) + ", 'average production difference' : " + str(
                diff_prod[i]) + "\n"
        return diff

        session.close()