|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Unit tests for timezone.py""" |
| 3 | + |
| 4 | +# Based on original Java code: |
| 5 | +# java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberToTimeZonesMapperTest.java |
| 6 | +# Copyright (C) 2012 The Libphonenumber Authors |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | + |
| 20 | +import unittest |
| 21 | + |
| 22 | +from phonenumbers import PhoneNumber, FrozenPhoneNumber |
| 23 | +from phonenumbers import timezone |
| 24 | +from phonenumbers.timezone import time_zones_for_geographical_number |
| 25 | +from phonenumbers.timezone import time_zones_for_number |
| 26 | +from phonenumbers.timezone import _UNKNOWN_TIME_ZONE_LIST |
| 27 | + |
| 28 | +# Allow override library timezone metadata with the test metadata. |
| 29 | +REAL_TIMEZONE_DATA = timezone.TIMEZONE_DATA |
| 30 | +REAL_TIMEZONE_LONGEST_PREFIX = timezone.TIMEZONE_LONGEST_PREFIX |
| 31 | +from .testtzdata import TIMEZONE_DATA as TEST_TIMEZONE_DATA |
| 32 | +from .testtzdata import TIMEZONE_LONGEST_PREFIX as TEST_TIMEZONE_LONGEST_PREFIX |
| 33 | + |
| 34 | + |
| 35 | +def reinstate_real_tzdata(): |
| 36 | + """Reinstate real phone number timezone metadata""" |
| 37 | + timezone.TIMEZONE_DATA = REAL_TIMEZONE_DATA |
| 38 | + timezone.TIMEZONE_LONGEST_PREFIX = REAL_TIMEZONE_LONGEST_PREFIX |
| 39 | + |
| 40 | + |
| 41 | +def insert_test_tzdata(): |
| 42 | + """Insert test timezone metadata into library""" |
| 43 | + timezone.TIMEZONE_DATA = TEST_TIMEZONE_DATA |
| 44 | + timezone.TIMEZONE_LONGEST_PREFIX = TEST_TIMEZONE_LONGEST_PREFIX |
| 45 | + |
| 46 | + |
| 47 | +# Set up some test numbers to re-use. |
| 48 | +AU_NUMBER = FrozenPhoneNumber(country_code=61, national_number=236618300L) |
| 49 | +CA_NUMBER = FrozenPhoneNumber(country_code=1, national_number=6048406565L) |
| 50 | +KO_NUMBER = FrozenPhoneNumber(country_code=82, national_number=22123456L) |
| 51 | +KO_INVALID_NUMBER = FrozenPhoneNumber(country_code=82, national_number=1234L) |
| 52 | +US_NUMBER1 = FrozenPhoneNumber(country_code=1, national_number=6509600000L) |
| 53 | +US_NUMBER2 = FrozenPhoneNumber(country_code=1, national_number=2128120000L) |
| 54 | +US_NUMBER3 = FrozenPhoneNumber(country_code=1, national_number=6174240000L) |
| 55 | +US_INVALID_NUMBER = FrozenPhoneNumber(country_code=1, national_number=123456789L) |
| 56 | +NUMBER_WITH_INVALID_COUNTRY_CODE = FrozenPhoneNumber(country_code=999, national_number=2423651234L) |
| 57 | +INTERNATIONAL_TOLL_FREE = FrozenPhoneNumber(country_code=800, national_number=12345678L) |
| 58 | + |
| 59 | +# NANPA time zones. |
| 60 | +_CHICAGO_TZ = "America/Chicago" |
| 61 | +_LOS_ANGELES_TZ = "America/Los_Angeles" |
| 62 | +_NEW_YORK_TZ = "America/New_York" |
| 63 | +_WINNIPEG_TZ = "America/Winnipeg" |
| 64 | +_NANPA_TZ_LIST = (_NEW_YORK_TZ, _CHICAGO_TZ, _WINNIPEG_TZ, _LOS_ANGELES_TZ) |
| 65 | + |
| 66 | +# Non NANPA time zones. |
| 67 | +_SEOUL_TZ = "Asia/Seoul" |
| 68 | +_SYDNEY_TZ = "Australia/Sydney" |
| 69 | + |
| 70 | + |
| 71 | +class PhoneNumberToTimeZonesMapperTest(unittest.TestCase): |
| 72 | + """Unit tests for timezone.py""" |
| 73 | + |
| 74 | + def setUp(self): |
| 75 | + insert_test_tzdata() |
| 76 | + |
| 77 | + def tearDown(self): |
| 78 | + reinstate_real_tzdata() |
| 79 | + |
| 80 | + def testGetTimeZonesForNumber(self): |
| 81 | + # Test with invalid numbers even when their country code prefixes exist in the mapper. |
| 82 | + self.assertEqual(_UNKNOWN_TIME_ZONE_LIST, time_zones_for_number(US_INVALID_NUMBER)) |
| 83 | + self.assertEqual(_UNKNOWN_TIME_ZONE_LIST, time_zones_for_number(KO_INVALID_NUMBER)) |
| 84 | + # Test with valid prefixes. |
| 85 | + self.assertEqual((_SYDNEY_TZ,), time_zones_for_number(AU_NUMBER)) |
| 86 | + self.assertEqual((_SEOUL_TZ,), time_zones_for_number(KO_NUMBER)) |
| 87 | + self.assertEqual((_WINNIPEG_TZ,), time_zones_for_number(CA_NUMBER)) |
| 88 | + self.assertEqual((_LOS_ANGELES_TZ,), time_zones_for_number(US_NUMBER1)) |
| 89 | + self.assertEqual((_NEW_YORK_TZ,), time_zones_for_number(US_NUMBER2)) |
| 90 | + # Test with an invalid country code. |
| 91 | + self.assertEqual(_UNKNOWN_TIME_ZONE_LIST, time_zones_for_number(NUMBER_WITH_INVALID_COUNTRY_CODE)) |
| 92 | + # Test with a non geographical phone number. |
| 93 | + self.assertEqual(_UNKNOWN_TIME_ZONE_LIST, time_zones_for_number(INTERNATIONAL_TOLL_FREE)) |
| 94 | + # Python version extra test: check a number that can't geocoded; falls back to per-country |
| 95 | + kr_mobile_number = FrozenPhoneNumber(country_code=82, national_number=801234567L) |
| 96 | + self.assertEqual((_SEOUL_TZ,), time_zones_for_number(kr_mobile_number)) |
| 97 | + |
| 98 | + def testGetTimeZonesForValidNumber(self): |
| 99 | + # Test with invalid numbers even when their country code prefixes exist in the mapper. |
| 100 | + self.assertEqual(_NANPA_TZ_LIST, time_zones_for_geographical_number(US_INVALID_NUMBER)) |
| 101 | + self.assertEqual((_SEOUL_TZ,), time_zones_for_geographical_number(KO_INVALID_NUMBER)) |
| 102 | + # Test with valid prefixes. |
| 103 | + self.assertEqual((_SYDNEY_TZ,), time_zones_for_geographical_number(AU_NUMBER)) |
| 104 | + self.assertEqual((_SEOUL_TZ,), time_zones_for_geographical_number(KO_NUMBER)) |
| 105 | + self.assertEqual((_WINNIPEG_TZ,), time_zones_for_geographical_number(CA_NUMBER)) |
| 106 | + self.assertEqual((_LOS_ANGELES_TZ,), time_zones_for_geographical_number(US_NUMBER1)) |
| 107 | + self.assertEqual((_NEW_YORK_TZ,), time_zones_for_geographical_number(US_NUMBER2)) |
| 108 | + # Test with an invalid country code. |
| 109 | + self.assertEqual(_UNKNOWN_TIME_ZONE_LIST, time_zones_for_geographical_number(NUMBER_WITH_INVALID_COUNTRY_CODE)) |
| 110 | + # Test with a non geographical phone number. |
| 111 | + self.assertEqual(_UNKNOWN_TIME_ZONE_LIST, time_zones_for_geographical_number(INTERNATIONAL_TOLL_FREE)) |
| 112 | + |
| 113 | + def testGetTimeZonesForValidNumberSearchingAtCountryCodeLevel(self): |
| 114 | + # Test that the country level time zones are returned when the number passed in is valid but |
| 115 | + # not covered by any non-country level prefixes in the mapper. |
| 116 | + self.assertEqual(time_zones_for_number(US_NUMBER3), _NANPA_TZ_LIST) |
0 commit comments