Пример #1
0
 def test_valid_solar(self):
     # Sunrise.
     field = "Mo,Th 09:00-12:00,13:00-sunrise"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     # Sunset.
     field = "Mo,Th 09:00-12:00,13:00-sunset"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
Пример #2
0
 def test_valid_solar_offset(self):
     # Sunrise.
     field = "Mo,Th 09:00-12:00,13:00-(sunrise+02:00)"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     # Sunset.
     field = "Mo,Th 09:00-12:00,13:00-(sunset-02:00)"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
Пример #3
0
 def test_invalid_days(self):
     field = "Mo,Wx 09:00-12:00,13:00-19:00"
     with self.assertRaises(humanized_opening_hours.DoesNotExistError) as context:
         hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     field = "Pl-Mo 09:00-12:00,13:00-19:00"
     with self.assertRaises(humanized_opening_hours.DoesNotExistError) as context:
         hoh = humanized_opening_hours.HumanizedOpeningHours(field)
Пример #4
0
 def test_holidays(self):
     field = "Mo-Sa,SH 09:00-19:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     self.assertEqual(hoh.field, field)
     
     field = "Mo-Sa 09:00-19:00 ; PH off"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     self.assertEqual(hoh.field, field)
Пример #5
0
 def test_invalid_solar(self):
     # Sunrise.
     field = "Mo,Th 09:00-12:00,13:00-(sunrise)"
     with self.assertRaises(humanized_opening_hours.ParseError) as context:
         hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     # Sunset.
     field = "Mo,Th 09:00-12:00,13:00-(sunset)"
     with self.assertRaises(humanized_opening_hours.ParseError) as context:
         hoh = humanized_opening_hours.HumanizedOpeningHours(field)
Пример #6
0
 def test_invalid_solar_offset(self):
     field = "Mo,Th 09:00-12:00,13:00-(sunrise=02:00)"
     with self.assertRaises(humanized_opening_hours.ParseError) as context:
         hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     field = "Mo,Th 09:00-12:00,13:00-(sunrise02:00)"
     with self.assertRaises(humanized_opening_hours.ParseError) as context:
         hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     field = "Mo,Th 09:00-12:00,13:00-(sunrise+02:00"
     with self.assertRaises(humanized_opening_hours.ParseError) as context:
         hoh = humanized_opening_hours.HumanizedOpeningHours(field)
Пример #7
0
 def test_2(self):
     field = "Mo,Th 09:00-12:00,13:00-19:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     # Is open?
     dt = datetime.datetime(2016, 2, 1, 15, 30, tzinfo=pytz.timezone("UTC"))
     self.assertTrue(hoh.is_open(dt))
     dt = datetime.datetime(2016, 2, 1, 19, 30, tzinfo=pytz.timezone("UTC"))
     self.assertFalse(hoh.is_open(dt))
     dt = datetime.datetime(2016, 2, 1, 12, 10, tzinfo=pytz.timezone("UTC"))
     self.assertFalse(hoh.is_open(dt))
     # Periods per day.
     self.assertEqual(len(hoh._opening_periods[0].periods), 2)
     self.assertEqual(len(hoh._opening_periods[1].periods), 0)
     self.assertEqual(len(hoh._opening_periods[3].periods), 2)
     # Next change.
     self.assertEqual(
         hoh.next_change(dt),
         datetime.datetime(2016, 2, 1, 13, 0, tzinfo=pytz.timezone("UTC"))
     )
     # Time before next change.
     self.assertEqual(
         hoh.time_before_next_change(dt),
         datetime.timedelta(minutes=50)
     )
     # Rendering.
     hohr = humanized_opening_hours.HOHRenderer(hoh)
     self.assertEqual(
         hohr.render_moment(hoh[0].periods[0].m2),
         "12:00"
     )
     self.assertEqual(
         hohr.render_period(hoh[0].periods[0]),
         "09:00 - 12:00"
     )
Пример #8
0
 def test_3(self):
     field = "Mo-Sa 09:00-19:00 ; Su 09:00-12:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     # Is open?
     dt = datetime.datetime(2017, 1, 2, 15, 30, tzinfo=pytz.timezone("UTC"))
     self.assertTrue(hoh.is_open(dt))
     # Periods per day.
     self.assertEqual(len(hoh._opening_periods[0].periods), 1)
     self.assertEqual(len(hoh._opening_periods[1].periods), 1)
     self.assertEqual(len(hoh._opening_periods[6].periods), 1)
     # Next change.
     self.assertEqual(
         hoh.next_change(dt),
         datetime.datetime(2017, 1, 2, 19, 0, tzinfo=pytz.timezone("UTC"))
     )
     # Time before next change.
     dt = datetime.datetime(2017, 1, 8, 15, 30, tzinfo=pytz.timezone("UTC"))
     self.assertEqual(
         hoh.time_before_next_change(dt),
         datetime.timedelta(days=1, hours=3, minutes=30)
     )
     # Rendering.
     hohr = humanized_opening_hours.HOHRenderer(hoh)
     self.assertEqual(
         hohr.render_moment(hoh[6].periods[0].m1),
         "09:00"
     )
     self.assertEqual(
         hohr.render_period(hoh[6].periods[0]),
         "09:00 - 12:00"
     )
Пример #9
0
 def test_holidays(self):
     field = "Mo-We 09:00-19:00 ; SH off ; PH 09:00-12:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     hohr = humanized_opening_hours.HOHRenderer(hoh)
     self.assertEqual(
         hohr.holidays(),
         {
             "main": "Open on public holidays. Closed on school holidays.",
             "PH": (True, ["09:00 - 12:00"]),
             "SH": (False, []),
         }
     )
     return
Пример #10
0
 def test_periods_per_day_solar_universal(self):
     field = "Mo-We sunrise-19:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     hohr = humanized_opening_hours.HOHRenderer(hoh)
     field_dict = {
         0: ("Monday", ["sunrise - 19:00"]),
         1: ("Tuesday", ["sunrise - 19:00"]),
         2: ("Wednesday", ["sunrise - 19:00"]),
         3: ('Thursday', []),
         4: ('Friday', []),
         5: ('Saturday', []),
         6: ('Sunday', []),
     }
     self.assertDictEqual(hohr.periods_per_day(), field_dict)
Пример #11
0
 def __init__(self, uuid, geojson, search_preset, user_coordinates,
              timezone_name):
     self.uuid = uuid
     if geojson['geometry']['type'] == "Point":
         self.osm_meta = ("node", geojson["id"])
         self.coordinates = (geojson["geometry"]["coordinates"][1],
                             geojson["geometry"]["coordinates"][0])
     else:  # Should be "LineString".
         self.osm_meta = ("way", geojson["id"])
         self.coordinates = (geojson["geometry"]["coordinates"][0][1],
                             geojson["geometry"]["coordinates"][0][0])
     self.user_coords = user_coordinates
     self.search_preset = search_preset
     self.properties = geojson["properties"]
     self.default_address = self.get_default_address()
     self.string_address = ''
     self.distance = round(
         distance.vincenty((user_coordinates[0], user_coordinates[1]),
                           self.coordinates).m)
     self.bearing = get_bearing(user_coordinates, self.coordinates)
     self.direction = deg2dir(self.bearing)
     oh_field = self.properties.get("opening_hours")
     self.opening_hours = None
     lang = get_language().split('-')[0]
     if lang not in ["fr", "en"]:
         lang = "en"
     if oh_field:
         try:
             self.opening_hours = humanized_opening_hours.HumanizedOpeningHours(
                 oh_field, lang, tz=pytz.timezone(timezone_name))
         except humanized_opening_hours.HOHError:
             # TODO : Warn user ?
             debug_logger.error(
                 "Opening hours - HOHError ; OSM_ID: '{id}' ; opening_hours: '{oh}'"
                 .format(id=self.osm_meta[1], oh=oh_field))
             self.opening_hours = None
         except Exception as e:
             # TODO : Warn user ?
             debug_logger.error(
                 "Opening hours - Error ; Exception: '{exception}' ; OSM_ID: '{id}' ; opening_hours: '{oh}'"
                 .format(exception=str(e), id=self.osm_meta[1],
                         oh=oh_field))
             self.opening_hours = None
     self.tags = self.get_tags()
     self.renderable_tags = [t[0] for t in self.tags]
     return
Пример #12
0
    def test_description(self):
        field = "Mo-We 09:00-19:00 ; SH off ; PH 09:00-12:00"
        description = """\
Monday: 09:00 - 19:00
Tuesday: 09:00 - 19:00
Wednesday: 09:00 - 19:00
Thursday: closed
Friday: closed
Saturday: closed
Sunday: closed

Public holidays: 09:00 - 12:00
Open on public holidays. Closed on school holidays."""
        hoh = humanized_opening_hours.HumanizedOpeningHours(field)
        hohr = humanized_opening_hours.HOHRenderer(hoh)
        self.assertEqual(
            hohr.description(),
            description
        )
        
        # With translation.
        field = "Mo,SH 09:00-19:00"
        hoh = humanized_opening_hours.HumanizedOpeningHours(field)
        hohr = humanized_opening_hours.HOHRenderer(hoh, lang="fr")
        description = """\
Lundi : 09:00 - 19:00
Mardi : fermé
Mercredi : fermé
Jeudi : fermé
Vendredi : fermé
Samedi : fermé
Dimanche : fermé

Vacances scolaires : 09:00 - 19:00
Ouvert durant les vacances scolaires."""
        self.assertEqual(
            hohr.description(),
            description
        )
        
        # Without holidays.
        field = "Mo,SH 09:00-19:00"
        hoh = humanized_opening_hours.HumanizedOpeningHours(field)
        hohr = humanized_opening_hours.HOHRenderer(hoh, lang="fr")
        description = """\
Lundi : 09:00 - 19:00
Mardi : fermé
Mercredi : fermé
Jeudi : fermé
Vendredi : fermé
Samedi : fermé
Dimanche : fermé"""
        self.assertEqual(
            hohr.description(holidays=False),
            description
        )
        
        # 24/7.
        field = "24/7"
        hoh = humanized_opening_hours.HumanizedOpeningHours(field)
        hohr = humanized_opening_hours.HOHRenderer(hoh)
        description = "Open 24 hours a day and 7 days a week."
        self.assertEqual(
            hohr.description(),
            description
        )
        return
Пример #13
0
 def test_closed_days(self):
     field = "Mo-We 09:00-19:00 ; Dec 25 off ; May 1 off"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     hohr = humanized_opening_hours.HOHRenderer(hoh)
     self.assertEqual(hohr.closed_days(), ["25 December", "1st May"])
     return
Пример #14
0
 def test_periods_per_day_solar_not_universal(self):
     field = "Mo-We sunrise-19:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     with self.assertRaises(humanized_opening_hours.NotParsedError) as context:
         hohr = humanized_opening_hours.HOHRenderer(hoh, universal=False)
Пример #15
0
 def test_invalid_2(self):
     field = "Mo,th 9:00-1200,13:00-19:00"
     sanitized_field = "Mo,Th 09:00-12:00,13:00-19:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     self.assertEqual(hoh.field, sanitized_field)
Пример #16
0
 def test_get_day(self):
     field = "Mo,Th 09:00-12:00,13:00-19:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     self.assertEqual(hoh.get_day(0), hoh[0])
     self.assertEqual(hoh.get_day(6), hoh[6])
     self.assertEqual(hoh.get_day("SH"), hoh["SH"])
Пример #17
0
 def test_invalid_1(self):
     field = "Mo-sa 0900-19:00"
     sanitized_field = "Mo-Sa 09:00-19:00"
     hoh = humanized_opening_hours.HumanizedOpeningHours(field)
     self.assertEqual(hoh.field, sanitized_field)