Example #1
0
def get_system_and_city(allow_any_city=True):
    system = get_param('system') or get_arg(1)
    city = get_param('city') or get_arg(2)

    if system in ALL_SYSTEMS:
        try:
            city_data = systems.get_city_by_name(system, city)
            if allow_any_city or city_data['electric'] == 'some':
                return city_data
        except KeyError:
            # city name not valid, fall through to default
            pass

    # if city or system were incorrect, return default
    return systems.get_city_by_name(DEFAULT_SYSTEM, DEFAULT_CITY)
Example #2
0
def get_system_and_city(allow_any_city=True):
    system = get_param('system') or get_arg(1)
    city = get_param('city') or get_arg(2)

    if system in ALL_SYSTEMS:
        try:
            city_data = systems.get_city_by_name(system, city)
            if allow_any_city or city_data['electric'] == 'some':
                return city_data
        except KeyError:
            # city name not valid, fall through to default
            pass

    # if city or system were incorrect, return default
    return systems.get_city_by_name(DEFAULT_SYSTEM, DEFAULT_CITY)
Example #3
0
    def test_output_not_identical_for_different_cities(self):
        """
        For selected systems, request information for two different cities
        supported and make sure they're not the same.

        Checks for silly errors like always returning data for the same city,
        no matter which city is requested.
        """
        for city in self.test_different_cities:
            first_city = systems.get_city_by_name(city[0], city[1][0])
            first_text, session = download.download_one_city(first_city)

            second_city = systems.get_city_by_name(city[0], city[1][1])
            second_text, session = download.download_one_city(second_city,
                                                              session=session)
            session.close()

            self.assertNotEqual(first_city, second_city)
Example #4
0
    def test_car2go_get_text(self):
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            text, session = download.download_one_city(city_data)
            session.close()

            # could throw exception if JSON is malformed, test if it does
            info = json.loads(text)

            # assert there is something
            self.assertGreater(len(info), 0)
Example #5
0
    def test_download(self):
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            t, failures = download.save(city[0], city[1], should_archive=True)

            self.assertEqual(len(failures), 0)

            file_absolute = files.get_file_path(city_data, t)
            file_current = files.get_current_file_path(city_data)

            self.assertTrue(os.path.exists(file_absolute))
            self.assertTrue(os.path.exists(file_current))
Example #6
0
    def test_cache(self):
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            # warm up the cache
            _, _ = download.save(city[0], city[1], should_archive=False)

            text, cache = download.get_current(city_data, max_cache_age=30)

            # check we've gotten a cached file
            self.assertGreater(cache, 0)

            info = json.loads(text)  # check the json can be parsed

            self.assertGreater(len(info), 0)  # check there is something
Example #7
0
    def test_get_api_output_as_json(self):
        """
        Test that all systems specified successfully return a JSON object.

        NOTE: this currently assumes all systems output JSON.
        This assumption is baked fairly deeply into the project,
        in web_helper.get_electric_cars and
        in analysis.normalize.Electric2goDataArchive
        """
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            text, session = download.download_one_city(city_data)
            session.close()

            self._assert_api_output_is_valid_json(text)
Example #8
0
    def test_download(self):
        """
        Test that downloading the data results in physical file being created
        for all systems specified.
        """
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            t, failures = download.save(city[0], city[1], should_archive=True)

            self.assertEqual(len(failures), 0)

            file_absolute = files.get_file_path(city_data, t)
            file_current = files.get_current_file_path(city_data)

            self.assertTrue(os.path.exists(file_absolute))
            self.assertTrue(os.path.exists(file_current))
Example #9
0
    def test_cache(self):
        """
        Tests that repeated requests to get data for a given system
        and city result in cached data being returned.
        """
        for city in self.test_cities:
            city_data = systems.get_city_by_name(city[0], city[1])

            # warm up the cache
            _, _ = download.save(city[0], city[1], should_archive=False)

            text, cache = download.get_current(city_data, max_cache_age=30)

            # check we've gotten a cached file
            self.assertGreater(cache, 0)

            self._assert_api_output_is_valid_json(text)