Beispiel #1
0
    def setUp(self, random_climatics_patched):
        self.year = datetime.utcnow().year
        self.payload = {
            'São Paulo': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Cold',
                    '{}-01-02'.format(self.year): 'Partly Cloudy',
                    '{}-01-03'.format(self.year): 'Cold',
                    '{}-01-04'.format(self.year): 'Cold',
                    '{}-01-05'.format(self.year): 'Hot',
                }
            },
            'Rio de Janeiro': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Partly Cloudy',
                    '{}-01-02'.format(self.year): 'Cold',
                    '{}-01-03'.format(self.year): 'Hot',
                    '{}-01-04'.format(self.year): 'Fair',
                    '{}-01-05'.format(self.year): 'Cold'
                }
            },
            'Porto Alegre': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Partly Cloudy',
                    '{}-01-02'.format(self.year): 'Cold',
                    '{}-01-03'.format(self.year): 'Hot',
                    '{}-01-04'.format(self.year): 'Fair',
                    '{}-01-05'.format(self.year): 'Fair'
                }
            },
            'Pernambuco': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Hot',
                    '{}-01-02'.format(self.year): 'Hot',
                    '{}-01-03'.format(self.year): 'Hot',
                    '{}-01-04'.format(self.year): 'Hot',
                    '{}-01-05'.format(self.year): 'Hot'
                }
            }
        }
        random_climatics_patched.return_value = self.payload

        self.random_climatics = RandomClimaticsConditions(self.year)
        self.random_climatics.save_dicit_with_random_climatics_conditions_in_a_file(
        )
        self.qty_days = 2
        self.city_id = 1
        self.wheather = WeatherController(self.year, self.city_id,
                                          self.qty_days,
                                          ['Cold', 'Partly Cloudy'])

        super(TestWeatherController, self).setUp()
Beispiel #2
0
def home():
    if request.method == 'POST':
        data = dict(request.form)
        city_id = data.get('city_id')[0] if data.get('city_id') else 0
        total_days = data.get('days')[0] if data.get('days')[0] else 0
        conditions = data.get('conditions') if data.get('conditions') else []
        year = datetime.utcnow().year

        try:
            weather_controller = WeatherController(year, city_id, total_days,
                                                   conditions)
            best_period = weather_controller.humanize_best_period()

        except exceptions.IdNotValid as e:
            return render_template(
                'home.html',
                list_city=constants.CITIES.items(),
                list_conditions=constants.CLIMATICS_CONDITIONS,
                error=e.message)

        except exceptions.QtyDaysNotValid as e:
            return render_template(
                'home.html',
                list_city=constants.CITIES.items(),
                list_conditions=constants.CLIMATICS_CONDITIONS,
                error=e.message)

        except exceptions.ConditionsNotValid as e:
            return render_template(
                'home.html',
                list_city=constants.CITIES.items(),
                list_conditions=constants.CLIMATICS_CONDITIONS,
                error=e.message)

        return render_template(
            'home.html',
            list_city=constants.CITIES.items(),
            list_conditions=constants.CLIMATICS_CONDITIONS,
            best_period=best_period,
        )

    return render_template('home.html',
                           list_city=constants.CITIES.items(),
                           list_conditions=constants.CLIMATICS_CONDITIONS,
                           best_period=[])
Beispiel #3
0
class TestWeatherController(unittest.TestCase):
    @mock.patch.object(RandomClimaticsConditions,
                       'generate_dicit_with_random_climatics_conditions')
    def setUp(self, random_climatics_patched):
        self.year = datetime.utcnow().year
        self.payload = {
            'São Paulo': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Cold',
                    '{}-01-02'.format(self.year): 'Partly Cloudy',
                    '{}-01-03'.format(self.year): 'Cold',
                    '{}-01-04'.format(self.year): 'Cold',
                    '{}-01-05'.format(self.year): 'Hot',
                }
            },
            'Rio de Janeiro': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Partly Cloudy',
                    '{}-01-02'.format(self.year): 'Cold',
                    '{}-01-03'.format(self.year): 'Hot',
                    '{}-01-04'.format(self.year): 'Fair',
                    '{}-01-05'.format(self.year): 'Cold'
                }
            },
            'Porto Alegre': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Partly Cloudy',
                    '{}-01-02'.format(self.year): 'Cold',
                    '{}-01-03'.format(self.year): 'Hot',
                    '{}-01-04'.format(self.year): 'Fair',
                    '{}-01-05'.format(self.year): 'Fair'
                }
            },
            'Pernambuco': {
                self.year: {
                    '{}-01-01'.format(self.year): 'Hot',
                    '{}-01-02'.format(self.year): 'Hot',
                    '{}-01-03'.format(self.year): 'Hot',
                    '{}-01-04'.format(self.year): 'Hot',
                    '{}-01-05'.format(self.year): 'Hot'
                }
            }
        }
        random_climatics_patched.return_value = self.payload

        self.random_climatics = RandomClimaticsConditions(self.year)
        self.random_climatics.save_dicit_with_random_climatics_conditions_in_a_file(
        )
        self.qty_days = 2
        self.city_id = 1
        self.wheather = WeatherController(self.year, self.city_id,
                                          self.qty_days,
                                          ['Cold', 'Partly Cloudy'])

        super(TestWeatherController, self).setUp()

    @raises(ValueError)
    def test_create_wheather_controller_with_conditions_is_not_a_list(self):
        WeatherController(self.year, 1, 2, {})

    @raises(exceptions.ConditionsNotValid)
    def test_create_wheather_controller_with_conditions_is_not_a_empty_list(
            self):
        WeatherController(self.year, 1, 2, [])

    @raises(exceptions.QtyDaysNotValid)
    def test_create_wheather_controller_with_qty_days_is_zero(self):
        qty_days = 0
        WeatherController(self.year, 1, qty_days, ['Cold', 'Partly Cloudy'])

    @raises(exceptions.IdNotValid)
    def test_create_wheather_controller_withc_city_id_not_valid(self):
        city_id = 20
        WeatherController(self.year, city_id, 5, ['Cold', 'Partly Cloudy'])

    def test_get_best_period(self):
        best_period = self.wheather.get_best_period()
        expected_return = [
            '{}-01-01'.format(self.year), '{}-01-02'.format(self.year),
            '{}-01-03'.format(self.year), '{}-01-04'.format(self.year)
        ]
        self.assertEquals(best_period, expected_return)

    def test_humanize_best_period(self):
        best_period = self.wheather.get_best_period()
        initial_date = datetime.strptime(best_period[0],
                                         '%Y-%m-%d').strftime('%d/%m/%Y')
        end_date = datetime.strptime(best_period[-1],
                                     '%Y-%m-%d').strftime('%d/%m/%Y')
        city = constants.CITIES[self.city_id]
        days = self.qty_days
        expected_return = u'Para a cidade {}, com {} dias livres, o melhor '\
                          'período para as suas férias é de {} até {}.'.format(city, days, initial_date, end_date)
        humanize_best_period = self.wheather.humanize_best_period()

        self.assertEquals(expected_return, humanize_best_period)
Beispiel #4
0
 def test_create_wheather_controller_withc_city_id_not_valid(self):
     city_id = 20
     WeatherController(self.year, city_id, 5, ['Cold', 'Partly Cloudy'])
Beispiel #5
0
 def test_create_wheather_controller_with_qty_days_is_zero(self):
     qty_days = 0
     WeatherController(self.year, 1, qty_days, ['Cold', 'Partly Cloudy'])
Beispiel #6
0
 def test_create_wheather_controller_with_conditions_is_not_a_empty_list(
         self):
     WeatherController(self.year, 1, 2, [])