from pytest import mark
from tests import TestCase
import sqlalchemy as sa
from sqlalchemy_utils import PhoneNumberType, PhoneNumber
from sqlalchemy_utils.types import phone_number


@mark.skipif('phone_number.phonenumbers is None')
class TestPhoneNumber(object):
    def setup_method(self, method):
        self.valid_phone_numbers = [
            '040 1234567',
            '+358 401234567',
            '09 2501234',
            '+358 92501234',
            '0800 939393',
            '09 4243 0456',
            '0600 900 500'
        ]
        self.invalid_phone_numbers = [
            'abc',
            '+040 1234567',
            '0111234567',
            '358'
        ]

    def test_valid_phone_numbers(self):
        for raw_number in self.valid_phone_numbers:
            phone_number = PhoneNumber(raw_number, 'FI')
            assert phone_number.is_valid_number()

    def test_invalid_phone_numbers(self):
        for raw_number in self.invalid_phone_numbers:
            try:
                phone_number = PhoneNumber(raw_number, 'FI')
                assert not phone_number.is_valid_number()
            except:
                pass

    def test_phone_number_attributes(self):
        phone_number = PhoneNumber('+358401234567')
        assert phone_number.e164 == u'+358401234567'
        assert phone_number.international == u'+358 40 1234567'
        assert phone_number.national == u'040 1234567'

    def test_phone_number_str_repr(self):
        phone_number = PhoneNumber('+358401234567')
        assert phone_number.__unicode__() == phone_number.national
        assert phone_number.__str__() == phone_number.national.encode('utf-8')


@mark.skipif('phone_number.phonenumbers is None')
class TestPhoneNumberType(TestCase):

    def create_models(self):
        class User(self.Base):
            __tablename__ = 'user'
            id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            phone_number = sa.Column(PhoneNumberType())

        self.User = User

    def setup_method(self, method):
        super(TestPhoneNumberType, self).setup_method(method)
        self.phone_number = PhoneNumber(
            '040 1234567',
            'FI'
        )
        self.user = self.User()
        self.user.name = u'Someone'
        self.user.phone_number = self.phone_number
        self.session.add(self.user)
        self.session.commit()

    def test_query_returns_phone_number_object(self):
        queried_user = self.session.query(self.User).first()
        assert queried_user.phone_number == self.phone_number

    def test_phone_number_is_stored_as_string(self):
        result = self.session.execute(
            'SELECT phone_number FROM "user" WHERE id=:param',
            {'param': self.user.id}
        )
        assert result.first()[0] == u'+358401234567'

    def test_phone_number_is_none(self):
        phone_number = None
        user = self.User()
        user.name = u'Someone'
        user.phone_number = phone_number
        self.session.add(user)
        self.session.commit()
        queried_user = self.session.query(self.User)[1]
        assert queried_user.phone_number is None
        result = self.session.execute(
            'SELECT phone_number FROM "user" WHERE id=:param',
            {'param': user.id}
        )
        assert result.first()[0] is None

    def test_scalar_attributes_get_coerced_to_objects(self):
        user = self.User(phone_number='050111222')

        assert isinstance(user.phone_number, PhoneNumber)

@@ -771,38 +771,32 @@
 rs:%0A            
-phone_
 number = PhoneNu
@@ -829,38 +829,32 @@
          assert 
-phone_
 number.is_valid_
@@ -984,38 +984,32 @@
                 
-phone_
 number = PhoneNu
@@ -1058,22 +1058,16 @@
 ert not 
-phone_
 number.i
@@ -1169,38 +1169,32 @@
 (self):%0A        
-phone_
 number = PhoneNu
@@ -1226,30 +1226,24 @@
      assert 
-phone_
 number.e164 
@@ -1273,30 +1273,24 @@
      assert 
-phone_
 number.inter
@@ -1331,30 +1331,24 @@
      assert 
-phone_
 number.natio
@@ -1412,38 +1412,32 @@
 (self):%0A        
-phone_
 number = PhoneNu
@@ -1465,38 +1465,32 @@
 %0A        assert 
-phone_
 number.__unicode
@@ -1489,38 +1489,32 @@
 _unicode__() == 
-phone_
 number.national%0A
@@ -1528,22 +1528,16 @@
  assert 
-phone_
 number._
@@ -1548,22 +1548,16 @@
 __() == 
-phone_
 number.n

Fix pep8 compat