#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_address.py $
$Id: test_address.py 23372 2004-01-23 20:45:08Z dbinger $
"""

from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.address import Address, is_valid_phone_number

tested_modules = ["dulcinea.address"]

class AddressTest (TestScenario):

    test_cases = ['init', 'complete', 'valid']

    def check_init (self):
        self.test_stmt('Address()')
        self.test_stmt('a=Address(street1="s1", street2="s2")')
        self.test_val('a.street1', 's1')
        self.test_val('a.street2', 's2')
        self.test_exc('Address(bogus="goofy")', TypeError)

    def check_complete(self):
        addr = Address()
        addr.contact_name = "Bogus Contact"
        addr.contact_phone_number = "888 555 1212"
        self.test_true("addr.has_valid_phone_number()")

        # Check a US address
        addr.street1 = '1320 St.'
        addr.city = 'Arlington'
        addr.state = 'VA' ; addr.country_code = 'US'
        addr.zip = '22201'
        self.test_seq('addr.check_complete()', [])

        # Test an empty contact phone number
        addr.contact_phone_number = ''
        self.test_seq('addr.check_complete()', [('contact_phone_number',
                                                 'missing')])
        addr.contact_phone_number = "888 555 1212"

        # Test the checks on the state field
        addr.state = ''
        self.test_seq('addr.check_complete()',
                      [('state', 'missing')])

        addr.state = 'VAR'
        self.test_seq('addr.check_complete()',
                      [('state', 'bad')])

        addr.state = 'VB'
        self.test_seq('addr.check_complete()',
                      [('state', 'bad')])

        addr.state = 'VA'
        addr.zip = '29999-'
        self.test_seq('addr.check_complete()',
                      [('zip', 'bad')])

        addr.state = 'XX'
        addr.zip = None
        self.test_seq('addr.check_complete()',
                      [('state', 'bad'),
                       ('zip', 'missing')])

        # Will a Canadian province work?
        addr.country_code = 'CA' ; addr.state = 'QC'
        self.test_seq('addr.check_complete()', [])


    def check_valid (self):
        addr = Address()
        self.test_true("addr.is_empty()")
        self.test_true("len(addr.check_complete()) > 0")
        self.test_false("addr.is_valid()")

        addr.street1 = "123 Main St."
        self.test_false("addr.is_empty()")
        self.test_true("len(addr.check_complete()) > 0")
        self.test_false("addr.is_valid()")

        addr.city = "Paris"
        addr.country_code = "FR"
        self.test_seq("addr.check_complete()", [('contact_name',
                                                 'missing'),
                                                ('contact_phone_number',
                                                 'missing')])
        self.test_false("addr.is_empty()")
        self.test_false("addr.is_valid()")
        addr.contact_name = 'foo'
        addr.contact_phone_number = '1112223333'
        self.test_true("addr.is_valid()")

    def check_friendly (self):
        "check that we can get human-friendly attribute names"
        addr = Address()
        self.test_val("addr.get_friendly_attr('contact_phone_number')",
                      "phone number")
        self.test_val("addr.get_friendly_attr('city')", "city")
        self.test_val("addr.get_friendly_attr('street1')", "street address")
        self.test_val("addr.get_friendly_attr('zip')", "postal/ZIP code")
        self.test_exc("addr.get_friendly_attr('foo')", AttributeError)

    def check_phone (self):
        "test is_valid_phone_number()"

        # Let's see, how many ways are there to write a North
        # American phone number...
        self.test_true("is_valid_phone_number('514-482-7346')")
        self.test_true("is_valid_phone_number('1-514-482-7346')")
        self.test_true("is_valid_phone_number('+1-514-482-7346')")
        self.test_true("is_valid_phone_number('5144827346')")
        self.test_true("is_valid_phone_number('514 482 7346')")
        self.test_true("is_valid_phone_number('(514) 482-7346')")
        self.test_true("is_valid_phone_number('1 (514) 482-7346')")
        self.test_true("is_valid_phone_number('+1 (514) 482-7346')")

        # Sorry, gotta have the area code.
        self.test_false("is_valid_phone_number('482 7346')")
        self.test_false("is_valid_phone_number('482-7346')")

        # Lots of offices have extensions, and there are lots of ways to
        # spell them.
        self.test_true("is_valid_phone_number('703-620-8990 x123')")
        self.test_true("is_valid_phone_number('703-620-8990 x 123')")
        self.test_true("is_valid_phone_number('703-620-8990 ext123')")
        self.test_true("is_valid_phone_number('703-620-8990 ext. 123')")
        self.test_true("is_valid_phone_number('703-620-8990 X123')")
        self.test_true("is_valid_phone_number('703-620-8990 X 123')")
        self.test_true("is_valid_phone_number('703-620-8990 Ext123')")
        self.test_true("is_valid_phone_number('703-620-8990 Ext. 123')")

        # Try some international numbers.
        self.test_true("is_valid_phone_number('+44 (0)20 8681 9603')")
        self.test_true("is_valid_phone_number('+33 01 47 42 20 34')")
        self.test_true("is_valid_phone_number('+33 03 83 27 56 52')")

if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios (scenarios, options)

