-
Notifications
You must be signed in to change notification settings - Fork 412
/
phonenumberutiltest.py
executable file
·2447 lines (2162 loc) · 138 KB
/
phonenumberutiltest.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""Unit tests for phonenumberutil.py"""
# Based on original Java code:
# java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.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 unittest
import phonenumbers
from phonenumbers import PhoneNumber, PhoneMetadata
from phonenumbers import FrozenPhoneNumber, PhoneNumberDesc
from phonenumbers import PhoneNumberType, PhoneNumberFormat, NumberParseException
from phonenumbers import ValidationResult, NumberFormat, CountryCodeSource
# Access internal functions of phonenumberutil.py
from phonenumbers import phonenumberutil
from phonenumbers.util import u, to_long
# Override library metadata with the test metadata.
REAL_REGION_METADATA = PhoneMetadata.region_metadata
REAL_CC_TO_RC = phonenumberutil.COUNTRY_CODE_TO_REGION_CODE
PhoneMetadata.region_metadata = {}
phonenumberutil.COUNTRY_CODE_TO_REGION_CODE = {}
# Import the test data; this will re-populate the
# PhoneMetadata.region_metadata map
from .testdata import _COUNTRY_CODE_TO_REGION_CODE as TEST_CC_TO_RC
TEST_REGION_METADATA = PhoneMetadata.region_metadata
def reinstate_real_metadata():
"""Reinstate real phone number metadata"""
phonenumberutil.COUNTRY_CODE_TO_REGION_CODE = REAL_CC_TO_RC
PhoneMetadata.region_metadata = REAL_REGION_METADATA
def insert_test_metadata():
"""Insert test metadata into library"""
phonenumberutil.COUNTRY_CODE_TO_REGION_CODE = TEST_CC_TO_RC
PhoneMetadata.region_metadata = TEST_REGION_METADATA
# Reinstate the real metadata so any importers of this module are not affected
reinstate_real_metadata()
# Set up some test numbers to re-use.
ALPHA_NUMERIC_NUMBER = FrozenPhoneNumber(country_code=1, national_number=80074935247)
AR_MOBILE = FrozenPhoneNumber(country_code=54, national_number=91187654321)
AR_NUMBER = FrozenPhoneNumber(country_code=54, national_number=1187654321)
AU_NUMBER = FrozenPhoneNumber(country_code=61, national_number=236618300)
BS_MOBILE = FrozenPhoneNumber(country_code=1, national_number=2423570000)
BS_NUMBER = FrozenPhoneNumber(country_code=1, national_number=2423651234)
# Note that this is the same as the example number for DE in the metadata.
DE_NUMBER = FrozenPhoneNumber(country_code=49, national_number=30123456)
DE_SHORT_NUMBER = FrozenPhoneNumber(country_code=49, national_number=1234)
GB_MOBILE = FrozenPhoneNumber(country_code=44, national_number=7912345678)
GB_NUMBER = FrozenPhoneNumber(country_code=44, national_number=2070313000)
IT_MOBILE = FrozenPhoneNumber(country_code=39, national_number=345678901)
IT_NUMBER = FrozenPhoneNumber(country_code=39, national_number=236618300, italian_leading_zero=True)
JP_STAR_NUMBER = FrozenPhoneNumber(country_code=81, national_number=2345)
# Numbers to test the formatting rules from Mexico.
MX_MOBILE1 = FrozenPhoneNumber(country_code=52, national_number=12345678900)
MX_MOBILE2 = FrozenPhoneNumber(country_code=52, national_number=15512345678)
MX_NUMBER1 = FrozenPhoneNumber(country_code=52, national_number=3312345678)
MX_NUMBER2 = FrozenPhoneNumber(country_code=52, national_number=8211234567)
NZ_NUMBER = FrozenPhoneNumber(country_code=64, national_number=33316005)
SG_NUMBER = FrozenPhoneNumber(country_code=65, national_number=65218000)
# A too-long and hence invalid US number.
US_LONG_NUMBER = FrozenPhoneNumber(country_code=1, national_number=65025300001)
US_NUMBER = FrozenPhoneNumber(country_code=1, national_number=6502530000)
US_PREMIUM = FrozenPhoneNumber(country_code=1, national_number=9002530000)
# Too short, but still possible US numbers.
US_LOCAL_NUMBER = FrozenPhoneNumber(country_code=1, national_number=2530000)
US_SHORT_BY_ONE_NUMBER = FrozenPhoneNumber(country_code=1, national_number=650253000)
US_TOLLFREE = FrozenPhoneNumber(country_code=1, national_number=8002530000)
US_SPOOF = FrozenPhoneNumber(country_code=1, national_number=0)
US_SPOOF_WITH_RAW_INPUT = FrozenPhoneNumber(country_code=1, national_number=0, raw_input="000-000-0000")
INTERNATIONAL_TOLL_FREE = FrozenPhoneNumber(country_code=800, national_number=12345678)
INTERNATIONAL_TOLL_FREE_TOO_LONG = FrozenPhoneNumber(country_code=800, national_number=1234567890)
# A number with an invalid region code
XY_NUMBER = FrozenPhoneNumber(country_code=999, national_number=1234567890)
class PhoneNumberUtilTest(unittest.TestCase):
"""Unit tests for phonenumbers/__init__.py
Note that these tests use the metadata contained in the files in
tests/data, not the normal metadata files, so should not be used for
regression test purposes - these tests are illustrative only and test
functionality.
"""
def setUp(self):
insert_test_metadata()
def tearDown(self):
reinstate_real_metadata()
def testSupportedRegions(self):
self.assertTrue(len(phonenumbers.SUPPORTED_REGIONS) > 0)
# Python version extra test
# Check the pseudo-code for non-geographic entities is not supported
self.assertFalse("001" in phonenumbers.SUPPORTED_REGIONS)
# Check a non-US NANPA country is correctly listed as supported
self.assertTrue("BS" in phonenumbers.SUPPORTED_REGIONS)
def testGetInstanceLoadUSMetadata(self):
metadata = PhoneMetadata.region_metadata["US"]
self.assertEqual("US", metadata.id)
self.assertEqual(1, metadata.country_code)
self.assertEqual("011", metadata.international_prefix)
self.assertTrue(metadata.national_prefix is not None)
self.assertEqual(2, len(metadata.number_format))
self.assertEqual("(\\d{3})(\\d{3})(\\d{4})", metadata.number_format[1].pattern)
self.assertEqual("\\1 \\2 \\3", metadata.number_format[1].format)
self.assertEqual("[13-689]\\d{9}|2[0-35-9]\\d{8}",
metadata.general_desc.national_number_pattern)
self.assertEqual("\\d{7}(?:\\d{3})?", metadata.general_desc.possible_number_pattern)
self.assertTrue(metadata.general_desc == metadata.fixed_line)
self.assertEqual("\\d{10}", metadata.toll_free.possible_number_pattern)
self.assertEqual("900\\d{7}", metadata.premium_rate.national_number_pattern)
# No shared-cost data is available, so it should be initialised to "NA".
self.assertEqual("NA", metadata.shared_cost.national_number_pattern)
self.assertEqual("NA", metadata.shared_cost.possible_number_pattern)
def testGetInstanceLoadDEMetadata(self):
metadata = PhoneMetadata.region_metadata["DE"]
self.assertEqual("DE", metadata.id)
self.assertEqual(49, metadata.country_code)
self.assertEqual("00", metadata.international_prefix)
self.assertEqual("0", metadata.national_prefix)
self.assertEqual(6, len(metadata.number_format))
self.assertEqual(1, len(metadata.number_format[5].leading_digits_pattern))
self.assertEqual("900", metadata.number_format[5].leading_digits_pattern[0])
self.assertEqual("(\\d{3})(\\d{3,4})(\\d{4})",
metadata.number_format[5].pattern)
self.assertEqual("\\1 \\2 \\3", metadata.number_format[5].format)
self.assertEqual("(?:[24-6]\\d{2}|3[03-9]\\d|[789](?:[1-9]\\d|0[2-9]))\\d{1,8}",
metadata.fixed_line.national_number_pattern)
self.assertEqual("\\d{2,14}", metadata.fixed_line.possible_number_pattern)
self.assertEqual("30123456", metadata.fixed_line.example_number)
self.assertEqual("\\d{10}", metadata.toll_free.possible_number_pattern)
self.assertEqual("900([135]\\d{6}|9\\d{7})", metadata.premium_rate.national_number_pattern)
def testGetInstanceLoadARMetadata(self):
metadata = PhoneMetadata.region_metadata["AR"]
self.assertEqual("AR", metadata.id)
self.assertEqual(54, metadata.country_code)
self.assertEqual("00", metadata.international_prefix)
self.assertEqual("0", metadata.national_prefix)
self.assertEqual("0(?:(11|343|3715)15)?", metadata.national_prefix_for_parsing)
self.assertEqual("9\\1", metadata.national_prefix_transform_rule)
self.assertEqual("\\2 15 \\3-\\4", metadata.number_format[2].format)
self.assertEqual("(9)(\\d{4})(\\d{2})(\\d{4})",
metadata.number_format[3].pattern)
self.assertEqual("(9)(\\d{4})(\\d{2})(\\d{4})",
metadata.intl_number_format[3].pattern)
self.assertEqual("\\1 \\2 \\3 \\4", metadata.intl_number_format[3].format)
def testGetInstanceLoadInternationalTollFreeMetadata(self):
metadata = PhoneMetadata.country_code_metadata[800]
self.assertEqual("001", metadata.id)
self.assertEqual(800, metadata.country_code)
self.assertEqual("\\1 \\2", metadata.number_format[0].format)
self.assertEqual("(\\d{4})(\\d{4})", metadata.number_format[0].pattern)
self.assertEqual("12345678", metadata.general_desc.example_number)
self.assertEqual("12345678", metadata.toll_free.example_number)
def testIsLeadingZeroPossible(self):
self.assertTrue(phonenumberutil._is_leading_zero_possible(39)) # Italy
self.assertFalse(phonenumberutil._is_leading_zero_possible(1)) # USA
self.assertFalse(phonenumberutil._is_leading_zero_possible(800))
self.assertFalse(phonenumberutil._is_leading_zero_possible(888)) # Not in metadata file, just default to False
def testGetLengthOfGeographicalAreaCode(self):
# Google MTV, which has area code "650".
self.assertEqual(3, phonenumbers.length_of_geographical_area_code(US_NUMBER))
# A North America toll-free number, which has no area code.
self.assertEqual(0, phonenumbers.length_of_geographical_area_code(US_TOLLFREE))
# Google London, which has area code "20".
self.assertEqual(2, phonenumbers.length_of_geographical_area_code(GB_NUMBER))
# A UK mobile phone, which has no area code.
self.assertEqual(0, phonenumbers.length_of_geographical_area_code(GB_MOBILE))
# Google Buenos Aires, which has area code "11".
self.assertEqual(2, phonenumbers.length_of_geographical_area_code(AR_NUMBER))
# Google Sydney, which has area code "2".
self.assertEqual(1, phonenumbers.length_of_geographical_area_code(AU_NUMBER))
# Google Singapore. Singapore has no area code and no national prefix.
self.assertEqual(0, phonenumbers.length_of_geographical_area_code(SG_NUMBER))
# An invalid US number (1 digit shorter), which has no area code.
self.assertEqual(0, phonenumbers.length_of_geographical_area_code(US_SHORT_BY_ONE_NUMBER))
# An international toll free number, which has no area code.
self.assertEqual(0, phonenumbers.length_of_geographical_area_code(INTERNATIONAL_TOLL_FREE))
def testGetLengthOfNationalDestinationCode(self):
# Google MTV, which has national destination code (NDC) "650".
self.assertEqual(3, phonenumbers.length_of_national_destination_code(US_NUMBER))
# A North America toll-free number, which has NDC "800".
self.assertEqual(3, phonenumbers.length_of_national_destination_code(US_TOLLFREE))
# Google London, which has NDC "20".
self.assertEqual(2, phonenumbers.length_of_national_destination_code(GB_NUMBER))
# A UK mobile phone, which has NDC "7912".
self.assertEqual(4, phonenumbers.length_of_national_destination_code(GB_MOBILE))
# Google Buenos Aires, which has NDC "11".
self.assertEqual(2, phonenumbers.length_of_national_destination_code(AR_NUMBER))
# An Argentinian mobile which has NDC "911".
self.assertEqual(3, phonenumbers.length_of_national_destination_code(AR_MOBILE))
# Google Sydney, which has NDC "2".
self.assertEqual(1, phonenumbers.length_of_national_destination_code(AU_NUMBER))
# Google Singapore, which has NDC "6521".
self.assertEqual(4, phonenumbers.length_of_national_destination_code(SG_NUMBER))
# An invalid US number (1 digit shorter), which has no NDC.
self.assertEqual(0, phonenumbers.length_of_national_destination_code(US_SHORT_BY_ONE_NUMBER))
# A number containing an invalid country calling code, which shouldn't have any NDC.
number = PhoneNumber(country_code=123, national_number=6502530000)
self.assertEqual(0, phonenumbers.length_of_national_destination_code(number))
# An international toll free number, which has NDC "1234".
self.assertEqual(4, phonenumbers.length_of_national_destination_code(INTERNATIONAL_TOLL_FREE))
# Python version extra test
# A number with an extension; still has NDC "7912"
number2 = PhoneNumber()
number2.merge_from(GB_MOBILE)
number2.extension = "1234"
self.assertEqual(4, phonenumbers.length_of_national_destination_code(number2))
def testGetNationalSignificantNumber(self):
self.assertEqual("6502530000", phonenumbers.national_significant_number(US_NUMBER))
# An Italian mobile number.
self.assertEqual("345678901", phonenumbers.national_significant_number(IT_MOBILE))
# An Italian fixed line number.
self.assertEqual("0236618300", phonenumbers.national_significant_number(IT_NUMBER))
self.assertEqual("12345678", phonenumbers.national_significant_number(INTERNATIONAL_TOLL_FREE))
def testGetExampleNumber(self):
self.assertEqual(DE_NUMBER, phonenumbers.example_number("DE"))
self.assertEqual(DE_NUMBER,
phonenumbers.example_number_for_type("DE", PhoneNumberType.FIXED_LINE))
self.assertEqual(None,
phonenumbers.example_number_for_type("DE", PhoneNumberType.MOBILE))
# For the US, the example number is placed under general description,
# and hence should be used for both fixed line and mobile, so neither
# of these should return None.
self.assertTrue(phonenumbers.example_number_for_type("US", PhoneNumberType.FIXED_LINE) is not None)
self.assertTrue(phonenumbers.example_number_for_type("US", PhoneNumberType.MOBILE) is not None)
# CS is an invalid region, so we have no data for it.
self.assertTrue(phonenumbers.example_number_for_type("CS", PhoneNumberType.MOBILE) is None)
# Python version extra test
self.assertTrue(phonenumbers.example_number_for_type("US", PhoneNumberType.UNKNOWN) is not None)
# RegionCode 001 is reserved for supporting non-geographical country
# calling code. We don't support getting an example number for it with
# this method.
self.assertTrue(phonenumbers.example_number("001") is None)
def testGetExampleNumberForNonGeoEntity(self):
self.assertEqual(INTERNATIONAL_TOLL_FREE, phonenumbers.example_number_for_non_geo_entity(800))
def testConvertAlphaCharactersInNumber(self):
input = "1800-ABC-DEF"
# Alpha chars are converted to digits; everything else is left untouched.
expectedOutput = "1800-222-333"
self.assertEqual(expectedOutput, phonenumberutil.convert_alpha_characters_in_number(input))
def testNormaliseRemovePunctuation(self):
inputNumber = "034-56&+#234"
expectedOutput = "03456234"
self.assertEqual(expectedOutput,
phonenumberutil._normalize(inputNumber),
msg="Conversion did not correctly remove punctuation")
def testNormaliseReplaceAlphaCharacters(self):
inputNumber = "034-I-am-HUNGRY"
expectedOutput = "034426486479"
self.assertEqual(expectedOutput,
phonenumberutil._normalize(inputNumber),
msg="Conversion did not correctly replace alpha characters")
def testNormaliseOtherDigits(self):
inputNumber = u("\uFF125\u0665")
expectedOutput = "255"
self.assertEqual(expectedOutput,
phonenumberutil._normalize(inputNumber),
msg="Conversion did not correctly replace non-latin digits")
# Eastern-Arabic digits.
inputNumber = u("\u06F52\u06F0")
expectedOutput = "520"
self.assertEqual(expectedOutput,
phonenumberutil._normalize(inputNumber),
msg="Conversion did not correctly replace non-latin digits")
def testNormaliseStripAlphaCharacters(self):
inputNumber = "034-56&+a#234"
expectedOutput = "03456234"
self.assertEqual(expectedOutput,
phonenumbers.normalize_digits_only(inputNumber),
msg="Conversion did not correctly remove alpha character")
def testFormatUSNumber(self):
self.assertEqual("650 253 0000", phonenumbers.format_number(US_NUMBER, PhoneNumberFormat.NATIONAL))
self.assertEqual("+1 650 253 0000", phonenumbers.format_number(US_NUMBER, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("800 253 0000", phonenumbers.format_number(US_TOLLFREE, PhoneNumberFormat.NATIONAL))
self.assertEqual("+1 800 253 0000", phonenumbers.format_number(US_TOLLFREE, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("900 253 0000", phonenumbers.format_number(US_PREMIUM, PhoneNumberFormat.NATIONAL))
self.assertEqual("+1 900 253 0000", phonenumbers.format_number(US_PREMIUM, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+1-900-253-0000", phonenumbers.format_number(US_PREMIUM, PhoneNumberFormat.RFC3966))
# Numbers with all zeros in the national number part will be formatted by using the raw_input
# if that is available no matter which format is specified.
self.assertEqual("000-000-0000",
phonenumbers.format_number(US_SPOOF_WITH_RAW_INPUT, PhoneNumberFormat.NATIONAL))
self.assertEqual("0", phonenumbers.format_number(US_SPOOF, PhoneNumberFormat.NATIONAL))
def testFormatBSNumber(self):
self.assertEqual("242 365 1234", phonenumbers.format_number(BS_NUMBER, PhoneNumberFormat.NATIONAL))
self.assertEqual("+1 242 365 1234", phonenumbers.format_number(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL))
def testFormatGBNumber(self):
self.assertEqual("(020) 7031 3000", phonenumbers.format_number(GB_NUMBER, PhoneNumberFormat.NATIONAL))
self.assertEqual("+44 20 7031 3000", phonenumbers.format_number(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("(07912) 345 678", phonenumbers.format_number(GB_MOBILE, PhoneNumberFormat.NATIONAL))
self.assertEqual("+44 7912 345 678", phonenumbers.format_number(GB_MOBILE, PhoneNumberFormat.INTERNATIONAL))
def testFormatDENumber(self):
deNumber = PhoneNumber(country_code=49, national_number=301234)
self.assertEqual("030/1234", phonenumbers.format_number(deNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("+49 30/1234", phonenumbers.format_number(deNumber, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+49-30-1234", phonenumbers.format_number(deNumber, PhoneNumberFormat.RFC3966))
deNumber.clear()
deNumber.country_code = 49
deNumber.national_number = to_long(291123)
self.assertEqual("0291 123", phonenumbers.format_number(deNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("+49 291 123", phonenumbers.format_number(deNumber, PhoneNumberFormat.INTERNATIONAL))
deNumber.clear()
deNumber.country_code = 49
deNumber.national_number = to_long(29112345678)
self.assertEqual("0291 12345678", phonenumbers.format_number(deNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("+49 291 12345678", phonenumbers.format_number(deNumber, PhoneNumberFormat.INTERNATIONAL))
deNumber.clear()
deNumber.country_code = 49
deNumber.national_number = to_long(912312345)
self.assertEqual("09123 12345", phonenumbers.format_number(deNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("+49 9123 12345", phonenumbers.format_number(deNumber, PhoneNumberFormat.INTERNATIONAL))
deNumber.clear()
deNumber.country_code = 49
deNumber.national_number = to_long(80212345)
self.assertEqual("08021 2345", phonenumbers.format_number(deNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("+49 8021 2345", phonenumbers.format_number(deNumber, PhoneNumberFormat.INTERNATIONAL))
# Note this number is correctly formatted without national prefix. Most of the numbers that
# are treated as invalid numbers by the library are short numbers, and they are usually not
# dialed with national prefix.
self.assertEqual("1234", phonenumbers.format_number(DE_SHORT_NUMBER, PhoneNumberFormat.NATIONAL))
self.assertEqual("+49 1234", phonenumbers.format_number(DE_SHORT_NUMBER, PhoneNumberFormat.INTERNATIONAL))
deNumber.clear()
deNumber.country_code = 49
deNumber.national_number = 41341234
self.assertEqual("04134 1234", phonenumbers.format_number(deNumber, PhoneNumberFormat.NATIONAL))
def testFormatITNumber(self):
self.assertEqual("02 3661 8300", phonenumbers.format_number(IT_NUMBER, PhoneNumberFormat.NATIONAL))
self.assertEqual("+39 02 3661 8300", phonenumbers.format_number(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+390236618300", phonenumbers.format_number(IT_NUMBER, PhoneNumberFormat.E164))
self.assertEqual("345 678 901", phonenumbers.format_number(IT_MOBILE, PhoneNumberFormat.NATIONAL))
self.assertEqual("+39 345 678 901", phonenumbers.format_number(IT_MOBILE, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+39345678901", phonenumbers.format_number(IT_MOBILE, PhoneNumberFormat.E164))
def testFormatAUNumber(self):
self.assertEqual("02 3661 8300", phonenumbers.format_number(AU_NUMBER, PhoneNumberFormat.NATIONAL))
self.assertEqual("+61 2 3661 8300", phonenumbers.format_number(AU_NUMBER, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+61236618300", phonenumbers.format_number(AU_NUMBER, PhoneNumberFormat.E164))
auNumber = PhoneNumber(country_code=61, national_number=1800123456)
self.assertEqual("1800 123 456", phonenumbers.format_number(auNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("+61 1800 123 456", phonenumbers.format_number(auNumber, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+611800123456", phonenumbers.format_number(auNumber, PhoneNumberFormat.E164))
def testFormatARNumber(self):
self.assertEqual("011 8765-4321", phonenumbers.format_number(AR_NUMBER, PhoneNumberFormat.NATIONAL))
self.assertEqual("+54 11 8765-4321", phonenumbers.format_number(AR_NUMBER, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+541187654321", phonenumbers.format_number(AR_NUMBER, PhoneNumberFormat.E164))
self.assertEqual("011 15 8765-4321", phonenumbers.format_number(AR_MOBILE, PhoneNumberFormat.NATIONAL))
self.assertEqual("+54 9 11 8765 4321", phonenumbers.format_number(AR_MOBILE, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+5491187654321", phonenumbers.format_number(AR_MOBILE, PhoneNumberFormat.E164))
def testFormatMXNumber(self):
self.assertEqual("045 234 567 8900", phonenumbers.format_number(MX_MOBILE1, PhoneNumberFormat.NATIONAL))
self.assertEqual("+52 1 234 567 8900", phonenumbers.format_number(MX_MOBILE1, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+5212345678900", phonenumbers.format_number(MX_MOBILE1, PhoneNumberFormat.E164))
self.assertEqual("045 55 1234 5678", phonenumbers.format_number(MX_MOBILE2, PhoneNumberFormat.NATIONAL))
self.assertEqual("+52 1 55 1234 5678", phonenumbers.format_number(MX_MOBILE2, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+5215512345678", phonenumbers.format_number(MX_MOBILE2, PhoneNumberFormat.E164))
self.assertEqual("01 33 1234 5678", phonenumbers.format_number(MX_NUMBER1, PhoneNumberFormat.NATIONAL))
self.assertEqual("+52 33 1234 5678", phonenumbers.format_number(MX_NUMBER1, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+523312345678", phonenumbers.format_number(MX_NUMBER1, PhoneNumberFormat.E164))
self.assertEqual("01 821 123 4567", phonenumbers.format_number(MX_NUMBER2, PhoneNumberFormat.NATIONAL))
self.assertEqual("+52 821 123 4567", phonenumbers.format_number(MX_NUMBER2, PhoneNumberFormat.INTERNATIONAL))
self.assertEqual("+528211234567", phonenumbers.format_number(MX_NUMBER2, PhoneNumberFormat.E164))
def testFormatOutOfCountryCallingNumber(self):
self.assertEqual("00 1 900 253 0000",
phonenumbers.format_out_of_country_calling_number(US_PREMIUM, "DE"))
self.assertEqual("1 650 253 0000",
phonenumbers.format_out_of_country_calling_number(US_NUMBER, "BS"))
self.assertEqual("00 1 650 253 0000",
phonenumbers.format_out_of_country_calling_number(US_NUMBER, "PL"))
self.assertEqual("011 44 7912 345 678",
phonenumbers.format_out_of_country_calling_number(GB_MOBILE, "US"))
self.assertEqual("00 49 1234",
phonenumbers.format_out_of_country_calling_number(DE_SHORT_NUMBER, "GB"))
# Note this number is correctly formatted without national
# prefix. Most of the numbers that are treated as invalid numbers by
# the library are short numbers, and they are usually not dialed with
# national prefix.
self.assertEqual("1234", phonenumbers.format_out_of_country_calling_number(DE_SHORT_NUMBER, "DE"))
self.assertEqual("011 39 02 3661 8300",
phonenumbers.format_out_of_country_calling_number(IT_NUMBER, "US"))
self.assertEqual("02 3661 8300",
phonenumbers.format_out_of_country_calling_number(IT_NUMBER, "IT"))
self.assertEqual("+39 02 3661 8300",
phonenumbers.format_out_of_country_calling_number(IT_NUMBER, "SG"))
self.assertEqual("6521 8000",
phonenumbers.format_out_of_country_calling_number(SG_NUMBER, "SG"))
self.assertEqual("011 54 9 11 8765 4321",
phonenumbers.format_out_of_country_calling_number(AR_MOBILE, "US"))
self.assertEqual("011 800 1234 5678",
phonenumbers.format_out_of_country_calling_number(INTERNATIONAL_TOLL_FREE, "US"))
arNumberWithExtn = PhoneNumber()
arNumberWithExtn.merge_from(AR_MOBILE)
arNumberWithExtn.extension = "1234"
self.assertEqual("011 54 9 11 8765 4321 ext. 1234",
phonenumbers.format_out_of_country_calling_number(arNumberWithExtn, "US"))
self.assertEqual("0011 54 9 11 8765 4321 ext. 1234",
phonenumbers.format_out_of_country_calling_number(arNumberWithExtn, "AU"))
self.assertEqual("011 15 8765-4321 ext. 1234",
phonenumbers.format_out_of_country_calling_number(arNumberWithExtn, "AR"))
# Python version extra tests
self.assertEqual("1234567890",
phonenumbers.format_out_of_country_calling_number(XY_NUMBER, "AR"))
self.assertEqual("1234567890",
phonenumbers.format_out_of_country_calling_number(XY_NUMBER, "XX"))
def testFormatOutOfCountryWithInvalidRegion(self):
# AQ/Antarctica isn't a valid region code for phone number formatting,
# so this falls back to intl formatting.
self.assertEqual("+1 650 253 0000",
phonenumbers.format_out_of_country_calling_number(US_NUMBER, "AQ"))
# For region code 001, the out-of-country format always turns into the
# international format.
self.assertEqual("+1 650 253 0000",
phonenumbers.format_out_of_country_calling_number(US_NUMBER, "001"))
def testFormatOutOfCountryWithPreferredIntlPrefix(self):
# This should use 0011, since that is the preferred international
# prefix (both 0011 and 0012 are accepted as possible international
# prefixes in our test metadta.)
self.assertEqual("0011 39 02 3661 8300",
phonenumbers.format_out_of_country_calling_number(IT_NUMBER, "AU"))
# For region code 001, the out-of-country format always turns into the international format.
self.assertEqual("+1 650 253 0000",
phonenumbers.format_out_of_country_calling_number(US_NUMBER, "001"))
def testFormatOutOfCountryKeepingAlphaChars(self):
alphaNumericNumber = PhoneNumber(country_code=1, national_number=8007493524)
alphaNumericNumber.raw_input = "1800 six-flag"
self.assertEqual("0011 1 800 SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AU"))
alphaNumericNumber.raw_input = "1-800-SIX-flag"
self.assertEqual("0011 1 800-SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AU"))
alphaNumericNumber.raw_input = "Call us from UK: 00 1 800 SIX-flag"
self.assertEqual("0011 1 800 SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AU"))
alphaNumericNumber.raw_input = "800 SIX-flag"
self.assertEqual("0011 1 800 SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AU"))
# Formatting from within the NANPA region.
self.assertEqual("1 800 SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "US"))
self.assertEqual("1 800 SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "BS"))
# Testing that if the raw input doesn't exist, it is formatted using
# formatOutOfCountryCallingNumber.
alphaNumericNumber.raw_input = None
self.assertEqual("00 1 800 749 3524",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "DE"))
# Testing AU alpha number formatted from Australia.
alphaNumericNumber.country_code = 61
alphaNumericNumber.national_number = to_long(827493524)
alphaNumericNumber.raw_input = "+61 82749-FLAG"
# This number should have the national prefix fixed.
self.assertEqual("082749-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AU"))
alphaNumericNumber.raw_input = "082749-FLAG"
self.assertEqual("082749-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AU"))
alphaNumericNumber.national_number = to_long(18007493524)
alphaNumericNumber.raw_input = "1-800-SIX-flag"
# This number should not have the national prefix prefixed, in
# accordance with the override for this specific formatting rule.
self.assertEqual("1-800-SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AU"))
# The metadata should not be permanently changed, since we copied it
# before modifying patterns. Here we check this.
alphaNumericNumber.national_number = to_long(1800749352)
self.assertEqual("1800 749 352",
phonenumbers.format_out_of_country_calling_number(alphaNumericNumber, "AU"))
# Testing a region with multiple international prefixes.
self.assertEqual("+61 1-800-SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "SG"))
# Testing the case of calling from a non-supported region.
self.assertEqual("+61 1-800-SIX-FLAG",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AQ"))
# Testing the case with an invalid country calling code.
alphaNumericNumber.country_code = 0
alphaNumericNumber.national_number = to_long(18007493524)
alphaNumericNumber.raw_input = "1-800-SIX-flag"
# Uses the raw input only.
self.assertEqual("1-800-SIX-flag",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "DE"))
# Testing the case of an invalid alpha number.
alphaNumericNumber.country_code = 1
alphaNumericNumber.national_number = to_long(80749)
alphaNumericNumber.raw_input = "180-SIX"
# No country-code stripping can be done.
self.assertEqual("00 1 180-SIX",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "DE"))
# Testing the case of calling from a non-supported region.
alphaNumericNumber.country_code = 1
alphaNumericNumber.national_number = to_long(80749)
alphaNumericNumber.raw_input = "180-SIX"
# No country-code stripping can be done since the number is invalid.
self.assertEqual("+1 180-SIX",
phonenumbers.format_out_of_country_keeping_alpha_chars(alphaNumericNumber, "AQ"))
def testFormatWithCarrierCode(self):
# We only support this for AR in our test metadata, and only for mobile numbers starting with
# certain values.
arMobile = PhoneNumber(country_code=54, national_number=92234654321)
self.assertEqual("02234 65-4321", phonenumbers.format_number(arMobile, PhoneNumberFormat.NATIONAL))
# Here we force 14 as the carrier code.
self.assertEqual("02234 14 65-4321",
phonenumbers.format_national_number_with_carrier_code(arMobile, "14"))
# Here we force the number to be shown with no carrier code.
self.assertEqual("02234 65-4321",
phonenumbers.format_national_number_with_carrier_code(arMobile, ""))
# Here the international rule is used, so no carrier code should be present.
self.assertEqual("+5492234654321", phonenumbers.format_number(arMobile, PhoneNumberFormat.E164))
# We don't support this for the US so there should be no change.
self.assertEqual("650 253 0000", phonenumbers.format_national_number_with_carrier_code(US_NUMBER, "15"))
# Python version extra test
self.assertEqual("1234567890",
phonenumbers.format_national_number_with_carrier_code(XY_NUMBER, "123"))
def testFormatWithPreferredCarrierCode(self):
# We only support this for AR in our test metadata.
arNumber = PhoneNumber()
arNumber.country_code = 54
arNumber.national_number = to_long(91234125678)
# Test formatting with no preferred carrier code stored in the number itself.
self.assertEqual("01234 15 12-5678",
phonenumbers.format_national_number_with_preferred_carrier_code(arNumber, "15"))
self.assertEqual("01234 12-5678",
phonenumbers.format_national_number_with_preferred_carrier_code(arNumber, ""))
# Test formatting with preferred carrier code present.
arNumber.preferred_domestic_carrier_code = "19"
self.assertEqual("01234 12-5678", phonenumbers.format_number(arNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("01234 19 12-5678",
phonenumbers.format_national_number_with_preferred_carrier_code(arNumber, "15"))
self.assertEqual("01234 19 12-5678",
phonenumbers.format_national_number_with_preferred_carrier_code(arNumber, ""))
# Python version extra test: check string conversion with preferred carrier code
self.assertEqual('Country Code: 54 National Number: 91234125678 '
'Leading Zero: False Preferred Domestic Carrier Code: 19',
str(arNumber))
self.assertEqual("PhoneNumber(country_code=54, national_number=91234125678, extension=None, "
"italian_leading_zero=False, country_code_source=None, preferred_domestic_carrier_code='19')",
repr(arNumber))
# When the preferred_domestic_carrier_code is present (even when it
# contains an empty string), use it instead of the default carrier
# code passed in.
arNumber.preferred_domestic_carrier_code = ""
self.assertEqual("01234 12-5678",
phonenumbers.format_national_number_with_preferred_carrier_code(arNumber, "15"))
# We don't support this for the US so there should be no change.
usNumber = PhoneNumber(country_code=1, national_number=4241231234, preferred_domestic_carrier_code="99")
self.assertEqual("424 123 1234", phonenumbers.format_number(usNumber, PhoneNumberFormat.NATIONAL))
self.assertEqual("424 123 1234",
phonenumbers.format_national_number_with_preferred_carrier_code(usNumber, "15"))
def testFormatNumberForMobileDialing(self):
# US toll free numbers are marked as noInternationalDialling in the
# test metadata for testing purposes.
self.assertEqual("800 253 0000",
phonenumbers.format_number_for_mobile_dialing(US_TOLLFREE, "US",
True)) # Keep formatting
self.assertEqual("", phonenumbers.format_number_for_mobile_dialing(US_TOLLFREE, "CN", True))
self.assertEqual("+1 650 253 0000",
phonenumbers.format_number_for_mobile_dialing(US_NUMBER, "US", True))
usNumberWithExtn = PhoneNumber()
usNumberWithExtn.merge_from(US_NUMBER)
usNumberWithExtn.extension = "1234"
self.assertEqual("+1 650 253 0000",
phonenumbers.format_number_for_mobile_dialing(usNumberWithExtn, "US", True))
self.assertEqual("8002530000",
phonenumbers.format_number_for_mobile_dialing(US_TOLLFREE, "US",
False)) # Remove formatting
self.assertEqual("", phonenumbers.format_number_for_mobile_dialing(US_TOLLFREE, "CN", False))
self.assertEqual("+16502530000",
phonenumbers.format_number_for_mobile_dialing(US_NUMBER, "US", False))
self.assertEqual("+16502530000",
phonenumbers.format_number_for_mobile_dialing(usNumberWithExtn, "US", False))
# An invalid US number, which is one digit too long.
self.assertEqual("+165025300001",
phonenumbers.format_number_for_mobile_dialing(US_LONG_NUMBER, "US", False))
self.assertEqual("+1 65025300001",
phonenumbers.format_number_for_mobile_dialing(US_LONG_NUMBER, "US", True))
# Star numbers. In real life they appear in Israel, but we have them
# in JP in our test metadata.
self.assertEqual("*2345",
phonenumbers.format_number_for_mobile_dialing(JP_STAR_NUMBER, "JP", False))
self.assertEqual("*2345",
phonenumbers.format_number_for_mobile_dialing(JP_STAR_NUMBER, "JP", True))
# Python version extra tests
number = PhoneNumber()
number.merge_from(XY_NUMBER)
self.assertEqual("", phonenumbers.format_number_for_mobile_dialing(number, "US", False))
number.raw_input = " 123-456-7890"
self.assertEqual(" 123-456-7890", phonenumbers.format_number_for_mobile_dialing(number, "US", False))
self.assertEqual("+80012345678",
phonenumbers.format_number_for_mobile_dialing(INTERNATIONAL_TOLL_FREE, "JP", False))
self.assertEqual("+800 1234 5678",
phonenumbers.format_number_for_mobile_dialing(INTERNATIONAL_TOLL_FREE, "JP", True))
def testFormatByPattern(self):
newNumFormat = NumberFormat(pattern="(\\d{3})(\\d{3})(\\d{4})", format="(\\1) \\2-\\3")
newNumFormat._mutable = True
newNumberFormats = [newNumFormat]
self.assertEqual("(650) 253-0000", phonenumbers.format_by_pattern(US_NUMBER, PhoneNumberFormat.NATIONAL,
newNumberFormats))
self.assertEqual("+1 (650) 253-0000", phonenumbers.format_by_pattern(US_NUMBER,
PhoneNumberFormat.INTERNATIONAL,
newNumberFormats))
self.assertEqual("+1-650-253-0000", phonenumbers.format_by_pattern(US_NUMBER,
PhoneNumberFormat.RFC3966,
newNumberFormats))
# $NP is set to '1' for the US. Here we check that for other NANPA
# countries the US rules are followed.
newNumFormat.national_prefix_formatting_rule = "$NP ($FG)"
newNumFormat.format = "\\1 \\2-\\3"
self.assertEqual("1 (242) 365-1234",
phonenumbers.format_by_pattern(BS_NUMBER, PhoneNumberFormat.NATIONAL,
newNumberFormats))
self.assertEqual("+1 242 365-1234",
phonenumbers.format_by_pattern(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL,
newNumberFormats))
newNumFormat.pattern = "(\\d{2})(\\d{5})(\\d{3})"
newNumFormat.format = "\\1-\\2 \\3"
newNumberFormats[0] = newNumFormat
self.assertEqual("02-36618 300",
phonenumbers.format_by_pattern(IT_NUMBER, PhoneNumberFormat.NATIONAL,
newNumberFormats))
self.assertEqual("+39 02-36618 300",
phonenumbers.format_by_pattern(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL,
newNumberFormats))
newNumFormat.national_prefix_formatting_rule = "$NP$FG"
newNumFormat.pattern = "(\\d{2})(\\d{4})(\\d{4})"
newNumFormat.format = "\\1 \\2 \\3"
newNumberFormats[0] = newNumFormat
self.assertEqual("020 7031 3000",
phonenumbers.format_by_pattern(GB_NUMBER, PhoneNumberFormat.NATIONAL,
newNumberFormats))
newNumFormat.national_prefix_formatting_rule = "($NP$FG)"
self.assertEqual("(020) 7031 3000",
phonenumbers.format_by_pattern(GB_NUMBER, PhoneNumberFormat.NATIONAL,
newNumberFormats))
newNumFormat.national_prefix_formatting_rule = ""
self.assertEqual("20 7031 3000",
phonenumbers.format_by_pattern(GB_NUMBER, PhoneNumberFormat.NATIONAL,
newNumberFormats))
self.assertEqual("+44 20 7031 3000",
phonenumbers.format_by_pattern(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL,
newNumberFormats))
# Python version extra tests
self.assertEqual("1234567890",
phonenumbers.format_by_pattern(XY_NUMBER, PhoneNumberFormat.E164,
newNumberFormats))
# None of the patterns in the list match (because it's an empty list)
self.assertEqual("6502530000", phonenumbers.format_by_pattern(US_NUMBER, PhoneNumberFormat.NATIONAL, []))
def testFormatE164Number(self):
self.assertEqual("+16502530000", phonenumbers.format_number(US_NUMBER, PhoneNumberFormat.E164))
self.assertEqual("+4930123456", phonenumbers.format_number(DE_NUMBER, PhoneNumberFormat.E164))
self.assertEqual("+80012345678", phonenumbers.format_number(INTERNATIONAL_TOLL_FREE, PhoneNumberFormat.E164))
def testFormatNumberWithExtension(self):
nzNumber = PhoneNumber()
nzNumber.merge_from(NZ_NUMBER)
nzNumber.extension = "1234"
# Uses default extension prefix:
self.assertEqual("03-331 6005 ext. 1234", phonenumbers.format_number(nzNumber, PhoneNumberFormat.NATIONAL))
# Uses RFC 3966 syntax.
self.assertEqual("+64-3-331-6005;ext=1234", phonenumbers.format_number(nzNumber, PhoneNumberFormat.RFC3966))
# Extension prefix overridden in the territory information for the US:
usNumberWithExtension = PhoneNumber()
usNumberWithExtension.merge_from(US_NUMBER)
usNumberWithExtension.extension = "4567"
self.assertEqual("650 253 0000 extn. 4567", phonenumbers.format_number(usNumberWithExtension,
PhoneNumberFormat.NATIONAL))
def testFormatInOriginalFormat(self):
number1 = phonenumbers.parse("+442087654321", "GB", keep_raw_input=True)
self.assertEqual("+44 20 8765 4321", phonenumbers.format_in_original_format(number1, "GB"))
number2 = phonenumbers.parse("02087654321", "GB", keep_raw_input=True)
self.assertEqual("(020) 8765 4321", phonenumbers.format_in_original_format(number2, "GB"))
number3 = phonenumbers.parse("011442087654321", "US", keep_raw_input=True)
self.assertEqual("011 44 20 8765 4321", phonenumbers.format_in_original_format(number3, "US"))
number4 = phonenumbers.parse("442087654321", "GB", keep_raw_input=True)
self.assertEqual("44 20 8765 4321", phonenumbers.format_in_original_format(number4, "GB"))
number5 = phonenumbers.parse("+442087654321", "GB")
self.assertEqual("(020) 8765 4321", phonenumbers.format_in_original_format(number5, "GB"))
# Invalid numbers that we have a formatting pattern for should be
# formatted properly. Note area codes starting with 7 are
# intentionally excluded in the test metadata for testing purposes.
number6 = phonenumbers.parse("7345678901", "US", keep_raw_input=True)
self.assertEqual("734 567 8901", phonenumbers.format_in_original_format(number6, "US"))
# US is not a leading zero country, and the presence of the leading zero leads us to format the
# number using raw_input.
number7 = phonenumbers.parse("0734567 8901", "US", keep_raw_input=True)
self.assertEqual("0734567 8901", phonenumbers.format_in_original_format(number7, "US"))
# This number is valid, but we don't have a formatting pattern for
# it. Fall back to the raw input.
number8 = phonenumbers.parse("02-4567-8900", "KR", keep_raw_input=True)
self.assertEqual("02-4567-8900", phonenumbers.format_in_original_format(number8, "KR"))
number9 = phonenumbers.parse("01180012345678", "US", keep_raw_input=True)
self.assertEqual("011 800 1234 5678", phonenumbers.format_in_original_format(number9, "US"))
number10 = phonenumbers.parse("+80012345678", "KR", keep_raw_input=True)
self.assertEqual("+800 1234 5678", phonenumbers.format_in_original_format(number10, "KR"))
# US local numbers are formatted correctly, as we have formatting patterns for them.
localNumberUS = phonenumbers.parse("2530000", "US", keep_raw_input=True)
self.assertEqual("253 0000", phonenumbers.format_in_original_format(localNumberUS, "US"))
numberWithNationalPrefixUS = phonenumbers.parse("18003456789", "US", keep_raw_input=True)
self.assertEqual("1 800 345 6789",
phonenumbers.format_in_original_format(numberWithNationalPrefixUS, "US"))
numberWithoutNationalPrefixGB = phonenumbers.parse("2087654321", "GB", keep_raw_input=True)
self.assertEqual("20 8765 4321",
phonenumbers.format_in_original_format(numberWithoutNationalPrefixGB, "GB"))
# Make sure no metadata is modified as a result of the previous function call.
self.assertEqual("(020) 8765 4321", phonenumbers.format_in_original_format(number5, "GB"))
numberWithNationalPrefixMX = phonenumbers.parse("013312345678", "MX", keep_raw_input=True)
self.assertEqual("01 33 1234 5678",
phonenumbers.format_in_original_format(numberWithNationalPrefixMX, "MX"))
numberWithoutNationalPrefixMX = phonenumbers.parse("3312345678", "MX", keep_raw_input=True)
self.assertEqual("33 1234 5678",
phonenumbers.format_in_original_format(numberWithoutNationalPrefixMX, "MX"))
italianFixedLineNumber = phonenumbers.parse("0212345678", "IT", keep_raw_input=True)
self.assertEqual("02 1234 5678",
phonenumbers.format_in_original_format(italianFixedLineNumber, "IT"))
numberWithNationalPrefixJP = phonenumbers.parse("00777012", "JP", keep_raw_input=True)
self.assertEqual("0077-7012",
phonenumbers.format_in_original_format(numberWithNationalPrefixJP, "JP"))
numberWithoutNationalPrefixJP = phonenumbers.parse("0777012", "JP", keep_raw_input=True)
self.assertEqual("0777012",
phonenumbers.format_in_original_format(numberWithoutNationalPrefixJP, "JP"))
numberWithCarrierCodeBR = phonenumbers.parse("012 3121286979", "BR", keep_raw_input=True)
self.assertEqual("012 3121286979",
phonenumbers.format_in_original_format(numberWithCarrierCodeBR, "BR"))
# The default national prefix used in this case is 045. When a number
# with national prefix 044 is entered, we return the raw input as we
# don't want to change the number entered.
numberWithNationalPrefixMX1 = phonenumbers.parse("044(33)1234-5678", "MX", keep_raw_input=True)
self.assertEqual("044(33)1234-5678",
phonenumbers.format_in_original_format(numberWithNationalPrefixMX1, "MX"))
numberWithNationalPrefixMX2 = phonenumbers.parse("045(33)1234-5678", "MX", keep_raw_input=True)
self.assertEqual("045 33 1234 5678",
phonenumbers.format_in_original_format(numberWithNationalPrefixMX2, "MX"))
# The default international prefix used in this case is 0011. When a
# number with international prefix 0012 is entered, we return the raw
# input as we don't want to change the number entered.
outOfCountryNumberFromAU1 = phonenumbers.parse("0012 16502530000", "AU", keep_raw_input=True)
self.assertEqual("0012 16502530000",
phonenumbers.format_in_original_format(outOfCountryNumberFromAU1, "AU"))
outOfCountryNumberFromAU2 = phonenumbers.parse("0011 16502530000", "AU", keep_raw_input=True)
self.assertEqual("0011 1 650 253 0000",
phonenumbers.format_in_original_format(outOfCountryNumberFromAU2, "AU"))
# Python version extra tests
number101 = phonenumbers.parse("87654321", None, keep_raw_input=True, _check_region=False)
self.assertEqual("87654321", phonenumbers.format_in_original_format(number101, "US"))
def testIsPremiumRate(self):
self.assertEqual(PhoneNumberType.PREMIUM_RATE, phonenumbers.number_type(US_PREMIUM))
premiumRateNumber = PhoneNumber(country_code=39, national_number=892123)
self.assertEqual(PhoneNumberType.PREMIUM_RATE,
phonenumbers.number_type(premiumRateNumber))
premiumRateNumber.clear()
premiumRateNumber.country_code = 44
premiumRateNumber.national_number = to_long(9187654321)
self.assertEqual(PhoneNumberType.PREMIUM_RATE,
phonenumbers.number_type(premiumRateNumber))
premiumRateNumber.clear()
premiumRateNumber.country_code = 49
premiumRateNumber.national_number = to_long(9001654321)
self.assertEqual(PhoneNumberType.PREMIUM_RATE,
phonenumbers.number_type(premiumRateNumber))
premiumRateNumber.clear()
premiumRateNumber.country_code = 49
premiumRateNumber.national_number = to_long(90091234567)
self.assertEqual(PhoneNumberType.PREMIUM_RATE,
phonenumbers.number_type(premiumRateNumber))
def testIsTollFree(self):
tollFreeNumber = PhoneNumber(country_code=1, national_number=8881234567)
self.assertEqual(PhoneNumberType.TOLL_FREE,
phonenumbers.number_type(tollFreeNumber))
tollFreeNumber.clear()
tollFreeNumber.country_code = 39
tollFreeNumber.national_number = to_long(803123)
self.assertEqual(PhoneNumberType.TOLL_FREE,
phonenumbers.number_type(tollFreeNumber))
tollFreeNumber.clear()
tollFreeNumber.country_code = 44
tollFreeNumber.national_number = to_long(8012345678)
self.assertEqual(PhoneNumberType.TOLL_FREE,
phonenumbers.number_type(tollFreeNumber))
tollFreeNumber.clear()
tollFreeNumber.country_code = 49
tollFreeNumber.national_number = to_long(8001234567)
self.assertEqual(PhoneNumberType.TOLL_FREE,
phonenumbers.number_type(tollFreeNumber))
self.assertEqual(PhoneNumberType.TOLL_FREE,
phonenumbers.number_type(INTERNATIONAL_TOLL_FREE))
def testIsMobile(self):
self.assertEqual(PhoneNumberType.MOBILE, phonenumbers.number_type(BS_MOBILE))
self.assertEqual(PhoneNumberType.MOBILE, phonenumbers.number_type(GB_MOBILE))
self.assertEqual(PhoneNumberType.MOBILE, phonenumbers.number_type(IT_MOBILE))
self.assertEqual(PhoneNumberType.MOBILE, phonenumbers.number_type(AR_MOBILE))
mobileNumber = PhoneNumber(country_code=49, national_number=15123456789)
self.assertEqual(PhoneNumberType.MOBILE, phonenumbers.number_type(mobileNumber))
def testIsFixedLine(self):
self.assertEqual(PhoneNumberType.FIXED_LINE, phonenumbers.number_type(BS_NUMBER))
self.assertEqual(PhoneNumberType.FIXED_LINE, phonenumbers.number_type(IT_NUMBER))
self.assertEqual(PhoneNumberType.FIXED_LINE, phonenumbers.number_type(GB_NUMBER))
self.assertEqual(PhoneNumberType.FIXED_LINE, phonenumbers.number_type(DE_NUMBER))
def testIsFixedLineAndMobile(self):
self.assertEqual(PhoneNumberType.FIXED_LINE_OR_MOBILE,
phonenumbers.number_type(US_NUMBER))
fixedLineAndMobileNumber = PhoneNumber(country_code=54, national_number=1987654321)
self.assertEqual(PhoneNumberType.FIXED_LINE_OR_MOBILE,
phonenumbers.number_type(fixedLineAndMobileNumber))
def testIsSharedCost(self):
gbNumber = PhoneNumber(country_code=44, national_number=8431231234)
self.assertEqual(PhoneNumberType.SHARED_COST, phonenumbers.number_type(gbNumber))
def testIsVoip(self):
gbNumber = PhoneNumber(country_code=44, national_number=5631231234)
self.assertEqual(PhoneNumberType.VOIP, phonenumbers.number_type(gbNumber))
def testIsPersonalNumber(self):
gbNumber = PhoneNumber(country_code=44, national_number=7031231234)
self.assertEqual(PhoneNumberType.PERSONAL_NUMBER,
phonenumbers.number_type(gbNumber))
def testIsUnknown(self):
# Invalid numbers should be of type UNKNOWN.
self.assertEqual(PhoneNumberType.UNKNOWN, phonenumbers.number_type(US_LOCAL_NUMBER))
def testIsValidNumber(self):
self.assertTrue(phonenumbers.is_valid_number(US_NUMBER))
self.assertTrue(phonenumbers.is_valid_number(IT_NUMBER))
self.assertTrue(phonenumbers.is_valid_number(GB_MOBILE))
self.assertTrue(phonenumbers.is_valid_number(INTERNATIONAL_TOLL_FREE))
nzNumber = PhoneNumber(country_code=64, national_number=21387835)
self.assertTrue(phonenumbers.is_valid_number(nzNumber))
def testIsValidForRegion(self):
# This number is valid for the Bahamas, but is not a valid US number.
self.assertTrue(phonenumbers.is_valid_number(BS_NUMBER))
self.assertTrue(phonenumbers.is_valid_number_for_region(BS_NUMBER, "BS"))
self.assertFalse(phonenumbers.is_valid_number_for_region(BS_NUMBER, "US"))
bsInvalidNumber = PhoneNumber(country_code=1, national_number=2421232345)
# This number is no longer valid.
self.assertFalse(phonenumbers.is_valid_number(bsInvalidNumber))
# La Mayotte and Reunion use 'leadingDigits' to differentiate them.
reNumber = PhoneNumber(country_code=262, national_number=262123456)
self.assertTrue(phonenumbers.is_valid_number(reNumber))
self.assertTrue(phonenumbers.is_valid_number_for_region(reNumber, "RE"))
self.assertFalse(phonenumbers.is_valid_number_for_region(reNumber, "YT"))
# Now change the number to be a number for La Mayotte.
reNumber.national_number = to_long(269601234)
self.assertTrue(phonenumbers.is_valid_number_for_region(reNumber, "YT"))
self.assertFalse(phonenumbers.is_valid_number_for_region(reNumber, "RE"))
# This number is no longer valid for La Reunion.
reNumber.national_number = to_long(269123456)
self.assertFalse(phonenumbers.is_valid_number_for_region(reNumber, "YT"))
self.assertFalse(phonenumbers.is_valid_number_for_region(reNumber, "RE"))
self.assertFalse(phonenumbers.is_valid_number(reNumber))
# However, it should be recognised as from La Mayotte, since it is valid for this region.
self.assertEqual("YT", phonenumbers.region_code_for_number(reNumber))
# This number is valid in both places.
reNumber.national_number = to_long(800123456)
self.assertTrue(phonenumbers.is_valid_number_for_region(reNumber, "YT"))
self.assertTrue(phonenumbers.is_valid_number_for_region(reNumber, "RE"))