forked from daviddrysdale/python-phonenumbers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamplenumberstest.py
More file actions
208 lines (184 loc) · 10.4 KB
/
Copy pathexamplenumberstest.py
File metadata and controls
208 lines (184 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
"""Unit tests for phonenumberutil.py"""
# Based on original Java code:
# java/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java
#
# Copyright (C) 2009 The Libphonenumber Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import re
import unittest
from phonenumbers import PhoneNumberType, PhoneMetadata, NumberParseException
from phonenumbers import phonenumberutil, PhoneNumber
from phonenumbers.util import prnt
from phonenumbers.re_util import fullmatch
class ExampleNumbersTest(unittest.TestCase):
"""Verifies all of the example numbers in the metadata are valid and of
the correct type. If no example number exists for a particular type, the
test still passes."""
def setUp(self):
self.invalid_cases = []
self.wrong_type_cases = []
def tearDown(self):
pass
def _checkNumbersValidAndCorrectType(self,
exampleNumberRequestedType,
possibleExpectedTypes):
"""
Arguments:
exampleNumberRequestedType -- type we are requesting an example number for
possibleExpectedTypes -- acceptable types that this number should match, such as
FIXED_LINE and FIXED_LINE_OR_MOBILE for a fixed line example number.
"""
for regionCode in phonenumberutil.SUPPORTED_REGIONS:
exampleNumber = phonenumberutil.example_number_for_type(regionCode, exampleNumberRequestedType)
if exampleNumber is not None:
if not phonenumberutil.is_valid_number(exampleNumber):
self.invalid_cases.append(exampleNumber)
prnt("Failed validation for %s" % exampleNumber, file=sys.stderr)
else:
# We know the number is valid, now we check the type.
exampleNumberType = phonenumberutil.number_type(exampleNumber)
if exampleNumberType not in possibleExpectedTypes:
self.wrong_type_cases.append(exampleNumber)
prnt("Wrong type for %s: got %s" % (exampleNumber, exampleNumberType), file=sys.stderr)
prnt("Expected types: ", file=sys.stderr)
for phone_type in possibleExpectedTypes:
prnt(" %s" % phone_type, file=sys.stderr)
def testFixedLine(self):
fixedLineTypes = set((PhoneNumberType.FIXED_LINE, PhoneNumberType.FIXED_LINE_OR_MOBILE))
self._checkNumbersValidAndCorrectType(PhoneNumberType.FIXED_LINE, fixedLineTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testMobile(self):
mobileTypes = set((PhoneNumberType.MOBILE, PhoneNumberType.FIXED_LINE_OR_MOBILE,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.MOBILE, mobileTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testTollFree(self):
tollFreeTypes = set((PhoneNumberType.TOLL_FREE,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.TOLL_FREE, tollFreeTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testPremiumRate(self):
premiumRateTypes = set((PhoneNumberType.PREMIUM_RATE,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.PREMIUM_RATE, premiumRateTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testVoip(self):
voipTypes = set((PhoneNumberType.VOIP,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.VOIP, voipTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testPager(self):
pagerTypes = set((PhoneNumberType.PAGER,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.PAGER, pagerTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testUan(self):
uanTypes = set((PhoneNumberType.UAN,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.UAN, uanTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testVoicemail(self):
voicemailTypes = set((PhoneNumberType.VOICEMAIL,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.VOICEMAIL, voicemailTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testSharedCost(self):
sharedCostTypes = set((PhoneNumberType.SHARED_COST,))
self._checkNumbersValidAndCorrectType(PhoneNumberType.SHARED_COST, sharedCostTypes)
self.assertEqual(0, len(self.invalid_cases))
self.assertEqual(0, len(self.wrong_type_cases))
def testCanBeInternationallyDialled(self):
for regionCode in phonenumberutil.SUPPORTED_REGIONS:
exampleNumber = None
metadata = PhoneMetadata.region_metadata.get(regionCode, None)
desc = None
if metadata is not None:
desc = metadata.no_international_dialling
try:
if desc.example_number is not None:
exampleNumber = phonenumberutil.parse(desc.example_number, regionCode)
except NumberParseException:
_, e, _ = sys.exc_info()
prnt("Failed parse: %s" % e, file=sys.stderr)
if (exampleNumber is not None and
phonenumberutil._can_be_internationally_dialled(exampleNumber)):
self.wrong_type_cases.append(exampleNumber)
print >> sys.stderr, "Number %s should not be internationally diallable" % exampleNumber
self.assertEqual(0, len(self.wrong_type_cases))
# TODO: Update this to use connectsToEmergencyNumber or similar once that
# is implemented.
def testEmergency(self):
wrongTypeCounter = 0
for regionCode in phonenumberutil.SUPPORTED_REGIONS:
metadata = PhoneMetadata.region_metadata.get(regionCode, None)
desc = metadata.emergency
if desc.example_number is not None:
exampleNumber = desc.example_number
if (not fullmatch(re.compile(desc.possible_number_pattern), exampleNumber) or
not fullmatch(re.compile(desc.national_number_pattern), exampleNumber)):
wrongTypeCounter += 1
print >> sys.stderr, "Emergency example number test failed for %s" % regionCode
self.assertEqual(0, wrongTypeCounter)
def testGlobalNetworkNumbers(self):
for callingCode in PhoneMetadata.country_code_metadata.keys():
exampleNumber = phonenumberutil.example_number_for_non_geo_entity(callingCode)
self.assertTrue(exampleNumber is not None,
msg="No example phone number for calling code %s" % callingCode)
if not phonenumberutil.is_valid_number(exampleNumber):
self.invalidCases.append(exampleNumber)
print >> sys.stderr, "Failed validation for %s" % exampleNumber
def testEveryRegionHasAnExampleNumber(self):
for regionCode in phonenumberutil.SUPPORTED_REGIONS:
exampleNumber = phonenumberutil.example_number(regionCode)
self.assertTrue(exampleNumber is not None,
msg="None found for region %s" % regionCode)
# Extra tests that need access to the real metadata
def testBlankMetadata(self):
# Python version extra test
# Some metadata is blank; check that we cope with this.
# Example: MH (+692)
number = phonenumberutil.parse("+6927654321", "US")
self.assertEqual("Country Code: 692 National Number: 7654321 Leading Zero: False", str(number))
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=12345678)
coNumberMobile = PhoneNumber(country_code=57, national_number=3211234567)
peNumberFixed = PhoneNumber(country_code=51, national_number=11234567)
brNumberFixed = PhoneNumber(country_code=55, national_number=1123456789)
brNumberMobile = PhoneNumber(country_code=55, national_number=1161234567,
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))