def testGetExpectedCost(self):
        premiumRateExample = shortnumberinfo._example_short_number_for_cost("FR", ShortNumberCost.PREMIUM_RATE)
        self.assertEqual(ShortNumberCost.PREMIUM_RATE, expected_cost_for_region(_parse(premiumRateExample, "FR"), "FR"))
        premiumRateNumber = PhoneNumber(country_code=33, national_number=int(premiumRateExample))
        self.assertEqual(ShortNumberCost.PREMIUM_RATE, expected_cost(premiumRateNumber))

        standardRateExample = shortnumberinfo._example_short_number_for_cost("FR", ShortNumberCost.STANDARD_RATE)
        self.assertEqual(ShortNumberCost.STANDARD_RATE, expected_cost_for_region(_parse(standardRateExample, "FR"), "FR"))
        standardRateNumber = PhoneNumber(country_code=33, national_number=int(standardRateExample))
        self.assertEqual(ShortNumberCost.STANDARD_RATE, expected_cost(standardRateNumber))

        tollFreeExample = shortnumberinfo._example_short_number_for_cost("FR", ShortNumberCost.TOLL_FREE)
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost_for_region(_parse(tollFreeExample, "FR"), "FR"))
        tollFreeNumber = PhoneNumber(country_code=33, national_number=int(tollFreeExample))
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost(tollFreeNumber))

        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost_for_region(_parse("12345", "FR"), "FR"))
        unknownCostNumber = PhoneNumber(country_code=33, national_number=12345)
        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost(unknownCostNumber))

        # Test that an invalid number may nevertheless have a cost other than UNKNOWN_COST.
        self.assertFalse(is_valid_short_number_for_region("116123", "FR"))
        invalidNumber = PhoneNumber(country_code=33, national_number=116123)
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost_for_region(_parse("116123", "FR"), "FR"))
        self.assertFalse(is_valid_short_number(invalidNumber))
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost(invalidNumber))

        # Test a nonexistent country code.
        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost_for_region(_parse("911", "US"), "ZZ"))
        unknownCostNumber.country_code = 123
        unknownCostNumber.national_number = 911
        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost(unknownCostNumber))
Ejemplo n.º 2
0
    def testIsCarrierSpecific(self):
        carrierSpecificNumber = PhoneNumber(country_code=1,
                                            national_number=33669)
        self.assertTrue(
            shortnumberinfo.is_carrier_specific(carrierSpecificNumber))
        self.assertTrue(
            shortnumberinfo.is_carrier_specific_for_region(
                _parse("33669", "US"), "US"))

        notCarrierSpecificNumber = PhoneNumber(country_code=1,
                                               national_number=911)
        self.assertFalse(
            shortnumberinfo.is_carrier_specific(notCarrierSpecificNumber))
        self.assertFalse(
            shortnumberinfo.is_carrier_specific_for_region(
                _parse("911", "US"), "US"))

        carrierSpecificNumberForSomeRegion = PhoneNumber(country_code=1,
                                                         national_number=211)
        self.assertTrue(
            shortnumberinfo.is_carrier_specific(
                carrierSpecificNumberForSomeRegion))
        self.assertTrue(
            shortnumberinfo.is_carrier_specific_for_region(
                carrierSpecificNumberForSomeRegion, "US"))
        self.assertFalse(
            shortnumberinfo.is_carrier_specific_for_region(
                carrierSpecificNumberForSomeRegion, "BB"))
        # Python version extra test: check invalid region code
        self.assertFalse(
            shortnumberinfo.is_carrier_specific_for_region(
                carrierSpecificNumberForSomeRegion, "XY"))
Ejemplo n.º 3
0
    def testIllegalArguments(self):
        """Tests the value type semantics for matches with a None number."""
        try:
            PhoneNumberMatch(-110, "1 800 234 45 67", PhoneNumber())
            self.fail("Expected failed constructor")
        except Exception:
            pass

        try:
            PhoneNumberMatch(10, "1 800 234 45 67", None)
            self.fail("Expected failed constructor")
        except Exception:
            pass

        try:
            PhoneNumberMatch(10, None, PhoneNumber())
            self.fail("Expected failed constructor")
        except Exception:
            pass

        try:
            PhoneNumberMatch(10, None, None)
            self.fail("Expected failed constructor")
        except Exception:
            pass
Ejemplo n.º 4
0
    def testIsValidShortNumber(self):
        self.assertTrue(
            is_valid_short_number(
                PhoneNumber(country_code=33, national_number=1010)))
        self.assertTrue(is_valid_short_number_for_region("1010", "FR"))
        self.assertFalse(
            is_valid_short_number(
                PhoneNumber(country_code=33, national_number=123456)))
        self.assertFalse(is_valid_short_number_for_region("123456", "FR"))

        # Note that GB and GG share the country calling code 44.
        self.assertTrue(
            is_valid_short_number(
                PhoneNumber(country_code=44, national_number=18001)))

        # Python version extra test: check invalid region code
        self.assertFalse(is_valid_short_number_for_region("123456", "XY"))
        self.assertFalse(
            is_valid_short_number(
                PhoneNumber(country_code=99, national_number=123)))
        # Python version extra test: not matching general desc
        self.assertFalse(is_valid_short_number_for_region("2123456", "US"))
        # Python version extra test: shared country code (44 => GB+GG) but not valid in either
        self.assertFalse(
            is_valid_short_number(
                PhoneNumber(country_code=44, national_number=58001)))
Ejemplo n.º 5
0
 def testInternals(self):
     # Python-specific test: coverage of internals
     from phonenumbers.phonenumbermatcher import _limit, _verify, _is_national_prefix_present_if_required
     from phonenumbers import CountryCodeSource
     self.assertEqual("{1,2}", _limit(1, 2))
     self.assertRaises(Exception, _limit, *(-1, 2))
     self.assertRaises(Exception, _limit, *(1, 0))
     self.assertRaises(Exception, _limit, *(2, 1))
     number = PhoneNumber(country_code=44, national_number=7912345678L)
     self.assertRaises(Exception, _verify, *(99, number, "12345678"))
     self.assertRaises(ValueError, PhoneNumberMatcher, *("text", "US"),
                       **{"leniency": None})
     self.assertRaises(ValueError, PhoneNumberMatcher, *("text", "US"),
                       **{"max_tries": -2})
     # Invalid country looks like national prefix is present (no way to tell)
     number2 = PhoneNumber(
         country_code=99,
         national_number=12345678L,
         country_code_source=CountryCodeSource.FROM_DEFAULT_COUNTRY)
     self.assertTrue(_is_national_prefix_present_if_required(number2))
     # National prefix rule has no lead digits
     number3 = PhoneNumber(
         country_code=61,
         national_number=1234567890L,
         country_code_source=CountryCodeSource.FROM_DEFAULT_COUNTRY)
     self.assertTrue(_is_national_prefix_present_if_required(number3))
Ejemplo n.º 6
0
 def testInternals(self):
     # Python-specific test: coverage of internals
     from phonenumbers.phonenumbermatcher import _limit, _verify, _is_national_prefix_present_if_required, _get_national_number_groups
     from phonenumbers import CountryCodeSource
     self.assertEqual("{1,2}", _limit(1, 2))
     self.assertRaises(Exception, _limit, *(-1, 2))
     self.assertRaises(Exception, _limit, *(1, 0))
     self.assertRaises(Exception, _limit, *(2, 1))
     number = PhoneNumber(country_code=44, national_number=7912345678L)
     self.assertRaises(Exception, _verify, *(99, number, "12345678"))
     self.assertRaises(ValueError, PhoneNumberMatcher, *("text", "US"),
                       **{"leniency": None})
     self.assertRaises(ValueError, PhoneNumberMatcher, *("text", "US"),
                       **{"max_tries": -2})
     # Invalid country looks like national prefix is present (no way to tell)
     number2 = PhoneNumber(
         country_code=99,
         national_number=12345678L,
         country_code_source=CountryCodeSource.FROM_DEFAULT_COUNTRY)
     self.assertTrue(_is_national_prefix_present_if_required(number2))
     # National prefix rule has no lead digits
     number3 = PhoneNumber(
         country_code=61,
         national_number=1234567890L,
         country_code_source=CountryCodeSource.FROM_DEFAULT_COUNTRY)
     self.assertTrue(_is_national_prefix_present_if_required(number3))
     # Coverage for _get_national_number_groups() with a formatting pattern provided
     us_number = PhoneNumber(country_code=1, national_number=6502530000L)
     num_format = NumberFormat(pattern="(\\d{3})(\\d{3})(\\d{4})",
                               format="\\1-\\2-\\3")
     self.assertEqual(["650", "253", "0000"],
                      _get_national_number_groups(us_number, num_format))
Ejemplo n.º 7
0
 def test_non_equal_with_different_object(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.preferred_domestic_carrier_code = ""
     self.assertNotEqual(numberA, None)
     self.assertNotEqual(numberA, "")
     self.assertNotEqual(numberA, 1)
Ejemplo n.º 8
0
 def test_non_equal_with_different_object(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.preferred_domestic_carrier_code = ""
     self.assertNotEqual(numberA, None)
     self.assertNotEqual(numberA, "")
     self.assertNotEqual(numberA, 1)
Ejemplo n.º 9
0
 def test_equal_with_preferred_dcc_set(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.preferred_domestic_carrier_code = ""
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = 6502530000L
     numberB.preferred_domestic_carrier_code = ""
     self.assertEquals(numberA, numberB)
Ejemplo n.º 10
0
 def test_equal_other_objects(self):
     # Python-specific extra tests for equality against other types
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.preferred_domestic_carrier_code = ""
     self.assertNotEqual(numberA, None)
     self.assertNotEqual(numberA, "")
     self.assertNotEqual(numberA, "+16502530000")
     self.assertNotEqual(numberA, 6502530000L)
 def test_equal_other_objects(self):
     # Python-specific extra tests for equality against other types
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.preferred_domestic_carrier_code = ""
     self.assertNotEqual(numberA, None)
     self.assertNotEqual(numberA, "")
     self.assertNotEqual(numberA, "+16502530000")
     self.assertNotEqual(numberA, 6502530000L)
Ejemplo n.º 12
0
 def test_non_equal_with_preferred_dcc_default(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = to_long(6502530000)
     numberA.preferred_domestic_carrier_code = ""
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = to_long(6502530000)
     self.assertNotEqual(numberA, numberB)
Ejemplo n.º 13
0
 def test_non_equal_with_italian_leading_zero_set(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.italian_leading_zero = True
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = 6502530000L
     self.assertNotEqual(numberA, numberB)
Ejemplo n.º 14
0
 def testFormatNumberForMobile(self):
     # Python version extra test.  Special cases for CO and BR in
     # format_number_for_mobile_dialing(), included here so that real metadata is used
     coNumberFixed = PhoneNumber(country_code=57, national_number=12345678L)
     coNumberMobile = PhoneNumber(country_code=57,
                                  national_number=3211234567L)
     peNumberFixed = PhoneNumber(country_code=51, national_number=11234567L)
     brNumberFixed = PhoneNumber(country_code=55,
                                 national_number=1123456789L)
     brNumberMobile = PhoneNumber(country_code=55,
                                  national_number=1161234567L,
                                  preferred_domestic_carrier_code="303")
     self.assertEqual(
         "0312345678",
         phonenumberutil.format_number_for_mobile_dialing(
             coNumberFixed, "CO", False))
     self.assertEqual(
         "03 1 2345678",
         phonenumberutil.format_number_for_mobile_dialing(
             coNumberFixed, "CO", True))
     self.assertEqual(
         "3211234567",
         phonenumberutil.format_number_for_mobile_dialing(
             coNumberMobile, "CO", False))
     self.assertEqual(
         "321 1234567",
         phonenumberutil.format_number_for_mobile_dialing(
             coNumberMobile, "CO", True))
     self.assertEqual(
         "011234567",
         phonenumberutil.format_number_for_mobile_dialing(
             peNumberFixed, "PE", False))
     self.assertEqual(
         "(01) 1234567",
         phonenumberutil.format_number_for_mobile_dialing(
             peNumberFixed, "PE", True))
     self.assertEqual(
         "",
         phonenumberutil.format_number_for_mobile_dialing(
             brNumberFixed, "BR", False))
     self.assertEqual(
         "",
         phonenumberutil.format_number_for_mobile_dialing(
             brNumberFixed, "BR", True))
     self.assertEqual(
         "03031161234567",
         phonenumberutil.format_number_for_mobile_dialing(
             brNumberMobile, "BR", False))
     self.assertEqual(
         "0 303 (11) 6123-4567",
         phonenumberutil.format_number_for_mobile_dialing(
             brNumberMobile, "BR", True))
    def testMatchesMultiplePhoneNumbersSeparatedByPhoneNumberPunctuation(self):
        text = "Call 650-253-4561 -- 455-234-3451"
        region = "US"
        number1 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region),
                              national_number=6502534561)
        match1 = PhoneNumberMatch(5, "650-253-4561", number1)
        number2 = PhoneNumber(country_code=phonenumberutil.country_code_for_region(region),
                              national_number=4552343451)
        match2 = PhoneNumberMatch(21, "455-234-3451", number2)

        matches = PhoneNumberMatcher(text, region)
        self.assertEqual(match1, matches.next())
        self.assertEqual(match2, matches.next())
Ejemplo n.º 16
0
 def testFrozenPhoneNumber(self):
     # Python version extra tests
     gb_mobile = PhoneNumber(country_code=44, national_number=7912345678)
     it_number = PhoneNumber(country_code=39,
                             national_number=236618300,
                             italian_leading_zero=True)
     frozen_gb_mobile1 = FrozenPhoneNumber(country_code=44,
                                           national_number=7912345678)
     frozen_it_number1 = FrozenPhoneNumber(country_code=39,
                                           national_number=236618300,
                                           italian_leading_zero=True)
     frozen_gb_mobile2 = FrozenPhoneNumber(gb_mobile)
     frozen_it_number2 = FrozenPhoneNumber(it_number)
     self.assertEqual(frozen_gb_mobile1, gb_mobile)
     self.assertEqual(frozen_gb_mobile2, gb_mobile)
     self.assertEqual(frozen_gb_mobile1, frozen_gb_mobile2)
     self.assertEqual(frozen_it_number1, it_number)
     self.assertEqual(frozen_it_number2, it_number)
     self.assertEqual(frozen_it_number1, frozen_it_number2)
     self.assertEqual(hash(frozen_it_number1), hash(frozen_it_number2))
     self.assertNotEqual(hash(frozen_it_number1), hash(frozen_gb_mobile1))
     phonedict = {frozen_it_number1: 1, frozen_gb_mobile1: 2}
     self.assertEqual(phonedict[frozen_it_number1], 1)
     try:
         frozen_gb_mobile1.country_code = 12
         self.fail("Should not be able to modify FrozenPhoneNubmer")
     except TypeError:
         pass
     try:
         frozen_gb_mobile2.raw_input = ""
         self.fail("Should not be able to modify FrozenPhoneNubmer")
     except TypeError:
         pass
     try:
         frozen_gb_mobile1.clear()
         self.fail("Should not be able to modify FrozenPhoneNubmer")
     except TypeError:
         pass
     try:
         frozen_gb_mobile1.merge_from(frozen_it_number1)
         self.fail("Should not be able to modify FrozenPhoneNubmer")
     except TypeError:
         pass
     try:
         del frozen_gb_mobile1.country_code
         self.fail("Should not be able to modify FrozenPhoneNubmer")
     except TypeError:
         pass
     # Coverage test
     frozen_gb_mobile1._mutable = True
     del frozen_gb_mobile1.country_code
Ejemplo n.º 17
0
 def testMergeFrom(self):
     # Python version extra test
     full_number = PhoneNumber(
         country_code=44,
         national_number=7912345678,
         extension=123,
         italian_leading_zero=True,
         number_of_leading_zeros=3,
         raw_input="+440007912345678 ext 123",
         country_code_source=CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN,
         preferred_domestic_carrier_code="7912")
     other = PhoneNumber()
     other.merge_from(full_number)
     self.assertEqual(other, full_number)
    def testGetExpectedCost(self):
        premiumRateExample = shortnumberinfo._example_short_number_for_cost("FR", ShortNumberCost.PREMIUM_RATE)
        self.assertEqual(ShortNumberCost.PREMIUM_RATE, expected_cost_for_region(_parse(premiumRateExample, "FR"), "FR"))
        premiumRateNumber = PhoneNumber(country_code=33, national_number=int(premiumRateExample))
        self.assertEqual(ShortNumberCost.PREMIUM_RATE, expected_cost(premiumRateNumber))

        standardRateExample = shortnumberinfo._example_short_number_for_cost("FR", ShortNumberCost.STANDARD_RATE)
        self.assertEqual(ShortNumberCost.STANDARD_RATE, expected_cost_for_region(_parse(standardRateExample, "FR"), "FR"))
        standardRateNumber = PhoneNumber(country_code=33, national_number=int(standardRateExample))
        self.assertEqual(ShortNumberCost.STANDARD_RATE, expected_cost(standardRateNumber))

        tollFreeExample = shortnumberinfo._example_short_number_for_cost("FR", ShortNumberCost.TOLL_FREE)
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost_for_region(_parse(tollFreeExample, "FR"), "FR"))
        tollFreeNumber = PhoneNumber(country_code=33, national_number=int(tollFreeExample))
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost(tollFreeNumber))

        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost_for_region(_parse("12345", "FR"), "FR"))
        unknownCostNumber = PhoneNumber(country_code=33, national_number=12345)
        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost(unknownCostNumber))

        # Test that an invalid number may nevertheless have a cost other than UNKNOWN_COST.
        self.assertFalse(is_valid_short_number_for_region(_parse("116123", "FR"), "FR"))
        invalidNumber = PhoneNumber(country_code=33, national_number=116123)
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost_for_region(_parse("116123", "FR"), "FR"))
        self.assertFalse(is_valid_short_number(invalidNumber))
        self.assertEqual(ShortNumberCost.TOLL_FREE, expected_cost(invalidNumber))

        # Test a nonexistent country code.
        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost_for_region(_parse("911", "US"), "ZZ"))
        unknownCostNumber.country_code = 123
        unknownCostNumber.national_number = 911
        self.assertEqual(ShortNumberCost.UNKNOWN_COST, expected_cost(unknownCostNumber))
    def testIsPossibleShortNumber(self):
        possibleNumber = PhoneNumber(country_code=33, national_number=123456)
        self.assertTrue(is_possible_short_number(possibleNumber))
        self.assertTrue(is_possible_short_number_for_region(_parse("123456", "FR"), "FR"))

        impossibleNumber = PhoneNumber(country_code=33, national_number=9)
        self.assertFalse(is_possible_short_number(impossibleNumber))

        # Note that GB and GG share the country calling code 44, and that this
        # number is possible but not valid.
        self.assertTrue(is_possible_short_number(PhoneNumber(country_code=44, national_number=11001)))

        # Python version extra test: check invalid region code
        self.assertFalse(is_possible_short_number_for_region(_parse("123456", "US"), "XY"))
Ejemplo n.º 20
0
 def test_equal_with_italian_leading_zero_set_to_default(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.italian_leading_zero = False
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = 6502530000L
     # These should still be equal, since the default value for this field
     # is false.
     self.assertEquals(numberA, numberB)
Ejemplo n.º 21
0
 def test_equal_with_country_code_source_set(self):
     numberA = PhoneNumber()
     numberA.raw_input = "+1 650 253 00 00"
     numberA.country_code_source = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN
     numberB = PhoneNumber()
     numberB.raw_input = "+1 650 253 00 00"
     numberB.country_code_source = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN
     self.assertEquals(numberA, numberB)
Ejemplo n.º 22
0
 def test_non_equal_with_differing_raw_input(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.raw_input = "+1 650 253 00 00"
     numberA.country_code_source = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN
     numberB = PhoneNumber()
     # Although these numbers would pass an isNumberMatch test, they are
     # not considered "equal" as objects, since their raw input is
     # different.
     numberB.country_code = 1
     numberB.national_number = 6502530000L
     numberB.raw_input = "+1-650-253-00-00"
     numberB.country_code_source = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN
     self.assertNotEqual(numberA, numberB)
Ejemplo n.º 23
0
 def testEmergencyNumberForSharedCountryCallingCode(self):
     # Test the emergency number 112, which is valid in both Australia and
     # the Christmas Islands.
     self.assertTrue(is_emergency_number("112", "AU"))
     self.assertTrue(
         shortnumberinfo.is_valid_short_number_for_region(
             _parse("112", "AU"), "AU"))
     self.assertEqual(
         ShortNumberCost.TOLL_FREE,
         shortnumberinfo.expected_cost_for_region(_parse("112", "AU"),
                                                  "AU"))
     self.assertTrue(is_emergency_number("112", "CX"))
     self.assertTrue(
         shortnumberinfo.is_valid_short_number_for_region(
             _parse("112", "CX"), "CX"))
     self.assertEqual(
         ShortNumberCost.TOLL_FREE,
         shortnumberinfo.expected_cost_for_region(_parse("112", "CX"),
                                                  "CX"))
     sharedEmergencyNumber = PhoneNumber(country_code=61,
                                         national_number=112)
     self.assertTrue(
         shortnumberinfo.is_valid_short_number(sharedEmergencyNumber))
     self.assertEqual(ShortNumberCost.TOLL_FREE,
                      shortnumberinfo.expected_cost(sharedEmergencyNumber))
Ejemplo n.º 24
0
 def testCoverage(self):
     # Python version extra tests
     invalid_number = PhoneNumber(country_code=210, national_number=123456L)
     self.assertEquals(
         "", geocoder.country_name_for_number(invalid_number, "en"))
     # Add in some script and region specific fictional names
     TEST_GEOCODE_DATA['1650960'] = {
         'en': u'Mountain View, CA',
         "en_GB": u'Mountain View California',
         "en_Latn": u'MountainView'
     }
     self.assertEquals(
         "Mountain View California",
         geocoder.description_for_number(US_NUMBER2, _ENGLISH, region="GB"))
     self.assertEquals(
         "MountainView",
         geocoder.description_for_number(US_NUMBER2,
                                         _ENGLISH,
                                         script="Latn"))
     self.assertEquals(
         "MountainView",
         geocoder.description_for_number(US_NUMBER2,
                                         _ENGLISH,
                                         script="Latn",
                                         region="GB"))
     TEST_GEOCODE_DATA['1650960'] = {'en': u'Mountain View, CA'}
Ejemplo n.º 25
0
 def testGetDescriptionForArgentinianMobileNumber(self):
     self.assertEqual("La Plata", description_for_number(AR_MOBILE_NUMBER, _ENGLISH))
     # Python version extra test
     # Put an invalid number after the mobile token ("9") and lie about
     # this being a valid number
     arInvalidMobileNumber = PhoneNumber(country_code=54, national_number=91)
     self.assertEqual("Argentina", description_for_valid_number(arInvalidMobileNumber, _ENGLISH))
    def testStringConvert(self):
        """Check string conversion"""
        number = PhoneNumber()
        match = PhoneNumberMatch(10, "1 800 234 45 67", number)

        self.assertEquals("PhoneNumberMatch [10,25) 1 800 234 45 67",
                          str(match))
Ejemplo n.º 27
0
def as_phone_number(value):
    if not value or isinstance(value, PhoneNumber):
        return value
    try:
        return phonenumbers.parse(value,
                                  settings.DEFAULT_TELEPHONE_COUNTRY_CODE)
    except NumberParseException:
        return PhoneNumber()
    def testIsPossibleShortNumber(self):
        possibleNumber = PhoneNumber(country_code=33, national_number=123456)
        self.assertTrue(is_possible_short_number_object(possibleNumber))
        self.assertTrue(is_possible_short_number("123456", "FR"))

        impossibleNumber = PhoneNumber(country_code=33, national_number=9)
        self.assertFalse(is_possible_short_number_object(impossibleNumber))
        self.assertFalse(is_possible_short_number("9", "FR"))

        # Python version extra test: check invalid region code
        self.assertFalse(is_possible_short_number("123456", "XY"))
        # Python version extra test: multiple regions with same calling code
        self.assertTrue(is_possible_short_number_object(
                PhoneNumber(country_code=44, national_number=18001)))
        # Python version extra test: multiple regions with same calling code, hit none
        self.assertFalse(is_possible_short_number_object(
                PhoneNumber(country_code=44, national_number=58001)))
 def test_equal_with_preferred_dcc_set(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.preferred_domestic_carrier_code = ""
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = 6502530000L
     numberB.preferred_domestic_carrier_code = ""
     self.assertEquals(numberA, numberB)
def test_parse_phone_number():
    get_site_preferences()["csv_import__phone_number_country"] = "DE"
    fake_number = PhoneNumber(country_code=49, national_number=1635550217)
    assert parse_phone_number("+49-163-555-0217") == fake_number
    assert parse_phone_number("+491635550217") == fake_number
    assert parse_phone_number("0163-555-0217") == fake_number
    assert parse_phone_number("01635550217") == fake_number
    get_site_preferences()["csv_import__phone_number_country"] = "GB"
    assert parse_phone_number("0163-555-0217") != fake_number
    assert parse_phone_number("01635550217") != fake_number
    def testStringConvert(self):
        """Check string conversion"""
        number = PhoneNumber()
        match = PhoneNumberMatch(10, "1 800 234 45 67", number)

        self.assertEqual("PhoneNumberMatch [10,25) 1 800 234 45 67", str(match))
        # Python version extra test
        self.assertEqual("PhoneNumberMatch(start=10, raw_string='1 800 234 45 67', " +
                          "numobj=PhoneNumber(country_code=None, national_number=None, extension=None, " +
                          "italian_leading_zero=False, country_code_source=None, preferred_domestic_carrier_code=None))", repr(match))
Ejemplo n.º 32
0
def PBToPy(numpb):
    """Convert phonenumber_pb2.PhoneNumber to phonenumber.PhoneNumber"""
    return PhoneNumber(country_code=numpb.country_code if numpb.HasField("country_code") else None,
                       national_number=numpb.national_number if numpb.HasField("national_number") else None,
                       extension=numpb.extension if numpb.HasField("extension") else None,
                       italian_leading_zero=numpb.italian_leading_zero if numpb.HasField("italian_leading_zero") else None,
                       number_of_leading_zeros=numpb.number_of_leading_zeros if numpb.HasField("number_of_leading_zeros") else None,
                       raw_input=numpb.raw_input if numpb.HasField("raw_input") else None,
                       country_code_source=numpb.country_code_source if numpb.HasField("country_code_source") else CountryCodeSource.UNSPECIFIED,
                       preferred_domestic_carrier_code=numpb.preferred_domestic_carrier_code if numpb.HasField("preferred_domestic_carrier_code") else None)
    def testGetExpectedCostForSharedCountryCallingCode(self):
        # Test some numbers which have different costs in countries sharing
        # the same country calling code. In Australia, 1234 is premium-rate,
        # 1194 is standard-rate, and 733 is toll-free. These are not known to
        # be valid numbers in the Christmas Islands.
        ambiguousPremiumRateString = "1234"
        ambiguousPremiumRateNumber = PhoneNumber(country_code=61, national_number=1234)
        ambiguousStandardRateString = "1194"
        ambiguousStandardRateNumber = PhoneNumber(country_code=61, national_number=1194)
        ambiguousTollFreeString = "733"
        ambiguousTollFreeNumber = PhoneNumber(country_code=61, national_number=733)

        self.assertTrue(shortnumberinfo.is_valid_short_number(ambiguousPremiumRateNumber))
        self.assertTrue(shortnumberinfo.is_valid_short_number(ambiguousStandardRateNumber))
        self.assertTrue(shortnumberinfo.is_valid_short_number(ambiguousTollFreeNumber))

        self.assertTrue(shortnumberinfo.is_valid_short_number_for_region(_parse(ambiguousPremiumRateString, "AU"), "AU"))
        self.assertEqual(ShortNumberCost.PREMIUM_RATE,
                         shortnumberinfo.expected_cost_for_region(_parse(ambiguousPremiumRateString, "AU"), "AU"))
        self.assertFalse(shortnumberinfo.is_valid_short_number_for_region(_parse(ambiguousPremiumRateString, "CX"), "CX"))
        self.assertEqual(ShortNumberCost.UNKNOWN_COST,
                         shortnumberinfo.expected_cost_for_region(_parse(ambiguousPremiumRateString, "CX"), "CX"))
        # PREMIUM_RATE takes precedence over UNKNOWN_COST.
        self.assertEqual(ShortNumberCost.PREMIUM_RATE,
                         shortnumberinfo.expected_cost(ambiguousPremiumRateNumber))

        self.assertTrue(shortnumberinfo.is_valid_short_number_for_region(_parse(ambiguousStandardRateString, "AU"), "AU"))
        self.assertEqual(ShortNumberCost.STANDARD_RATE,
                         shortnumberinfo.expected_cost_for_region(_parse(ambiguousStandardRateString, "AU"), "AU"))
        self.assertFalse(shortnumberinfo.is_valid_short_number_for_region(_parse(ambiguousStandardRateString, "CX"), "CX"))
        self.assertEqual(ShortNumberCost.UNKNOWN_COST,
                         shortnumberinfo.expected_cost_for_region(_parse(ambiguousStandardRateString, "CX"), "CX"))
        self.assertEqual(ShortNumberCost.UNKNOWN_COST,
                         shortnumberinfo.expected_cost(ambiguousStandardRateNumber))

        self.assertTrue(shortnumberinfo.is_valid_short_number_for_region(_parse(ambiguousTollFreeString, "AU"), "AU"))
        self.assertEqual(ShortNumberCost.TOLL_FREE,
                         shortnumberinfo.expected_cost_for_region(_parse(ambiguousTollFreeString, "AU"), "AU"))
        self.assertFalse(shortnumberinfo.is_valid_short_number_for_region(_parse(ambiguousTollFreeString, "CX"), "CX"))
        self.assertEqual(ShortNumberCost.UNKNOWN_COST,
                         shortnumberinfo.expected_cost_for_region(_parse(ambiguousTollFreeString, "CX"), "CX"))
        self.assertEqual(ShortNumberCost.UNKNOWN_COST,
                         shortnumberinfo.expected_cost(ambiguousTollFreeNumber))
 def test_non_equal_with_preferred_dcc_default(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = to_long(6502530000)
     numberA.preferred_domestic_carrier_code = ""
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = to_long(6502530000)
     self.assertNotEqual(numberA, numberB)
 def test_non_equal_with_italian_leading_zero_set(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.italian_leading_zero = True
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = 6502530000L
     self.assertNotEqual(numberA, numberB)
Ejemplo n.º 36
0
def PBToPy(numpb):
    """Convert phonenumber_pb2.PhoneNumber to phonenumber.PhoneNumber"""
    return PhoneNumber(
        numpb.country_code if numpb.HasField("country_code") else None,
        numpb.national_number if numpb.HasField("national_number") else None,
        numpb.extension if numpb.HasField("extension") else None,
        numpb.italian_leading_zero if numpb.HasField("italian_leading_zero")
        else None, numpb.raw_input if numpb.HasField("raw_input") else None,
        numpb.country_code_source if numpb.HasField("country_code_source") else
        None, numpb.preferred_domestic_carrier_code
        if numpb.HasField("preferred_domestic_carrier_code") else None)
    def testValueTypeSemantics(self):
        number = PhoneNumber()
        match1 = PhoneNumberMatch(10, "1 800 234 45 67", number)
        match2 = PhoneNumberMatch(10, "1 800 234 45 67", number)

        self.assertEquals(match1, match2)
        self.assertEquals(match1.start, match2.start)
        self.assertEquals(match1.end, match2.end)
        self.assertEquals(match1.number, match2.number)
        self.assertEquals(match1.raw_string, match2.raw_string)
        self.assertEquals("1 800 234 45 67", match1.raw_string)
 def test_equal_with_italian_leading_zero_set_to_default(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberA.italian_leading_zero = False
     numberB = PhoneNumber()
     numberB.country_code = 1
     numberB.national_number = 6502530000L
     # These should still be equal, since the default value for this field
     # is false.
     self.assertEquals(numberA, numberB)
 def test_equal_with_country_code_source_set(self):
     numberA = PhoneNumber()
     numberA.raw_input = "+1 650 253 00 00"
     numberA.country_code_source = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN
     numberB = PhoneNumber()
     numberB.raw_input = "+1 650 253 00 00"
     numberB.country_code_source = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN
     self.assertEquals(numberA, numberB)
    def testSequences(self):
        # Test multiple occurrences.
        text = "Call 033316005  or 032316005!"
        region = "NZ"

        number1 = PhoneNumber()
        number1.country_code = phonenumberutil.country_code_for_region(region)
        number1.national_number = 33316005
        match1 = PhoneNumberMatch(5, "033316005", number1)

        number2 = PhoneNumber()
        number2.country_code = phonenumberutil.country_code_for_region(region)
        number2.national_number = 32316005
        match2 = PhoneNumberMatch(19, "032316005", number2)

        matcher = PhoneNumberMatcher(text, region, Leniency.POSSIBLE, sys.maxint)

        self.assertEquals(match1, matcher.next())
        self.assertEquals(match2, matcher.next())
        self.assertFalse(matcher.has_next())
 def testIsCarrierSpecific(self):
     # Python version extra test: hit is_carrier_specific entrypoint
     esNumber = PhoneNumber(country_code=34, national_number=123)
     self.assertTrue(shortnumberinfo.is_carrier_specific(esNumber))
     esNumber.national_number = 512345678
     self.assertFalse(shortnumberinfo.is_carrier_specific(esNumber))
 def test_equal_simple_number(self):
     numberA = PhoneNumber()
     numberA.country_code = 1
     numberA.national_number = 6502530000L
     numberB = PhoneNumber(country_code=1, national_number=6502530000L)
     self.assertEquals(numberA, numberB)