#-*- coding: utf-8 -*-
from django.test.testcases import TestCase
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
from phonenumber_field.phonenumber import PhoneNumber
import phonenumber_field


###############
# Test Models #
###############


class MandatoryPhoneNumber(models.Model):
    phone_number = PhoneNumberField()


class OptionalPhoneNumber(models.Model):
    phone_number = PhoneNumberField(blank=True, default='')


class NullablePhoneNumber(models.Model):
    phone_number = PhoneNumberField(blank=True, null=True)


##############
# Test Cases #
##############


class PhoneNumberFieldTestCase(TestCase):
    test_number_1 = '+414204242'

    def create_fixtures(self):
        self.mandatory = MandatoryPhoneNumber(phone_number=self.test_number_1)
        self.mandatory.save()

    def test_can_create_model_with_string(self):
        obj = MandatoryPhoneNumber.objects.create(phone_number=self.test_number_1)
        # refetch it
        obj2 = MandatoryPhoneNumber.objects.get(pk=obj.pk)
        self.assertEqual(obj.phone_number, obj2.phone_number)

    def test_can_assign_string_phone_number(self):
        opt_phone = OptionalPhoneNumber()
        opt_phone.phone_number = self.test_number_1

        self.assertEqual(type(opt_phone.phone_number), PhoneNumber)
        self.assertEqual(opt_phone.phone_number.as_e164, self.test_number_1)
@@ -88,16 +88,17 @@
  models%0A
+%0A
 from pho
@@ -206,33 +206,8 @@
 ber%0A
-import phonenumber_field%0A
 %0A%0A##
@@ -249,25 +249,24 @@
 ##########%0A%0A
-%0A
 class Mandat
@@ -442,110 +442,8 @@
 )%0A%0A%0A
-class NullablePhoneNumber(models.Model):%0A    phone_number = PhoneNumberField(blank=True, null=True)%0A%0A%0A
 ####
@@ -564,118 +564,590 @@
 42'%0A
-%0A
     
-def create_fixtures(self):%0A        self.mandatory = MandatoryPhoneNumber(phone_number=self.test
+equal_number_strings = %5B'+44 113 8921113', '+441138921113'%5D%0A    local_numbers = %5B%0A        ('GB', '01606 751 78'),%0A        ('DE', '0176/96842671'),%0A    %5D%0A    invalid_numbers = %5B'+44 113 892111', %5D%0A%0A    def test_valid_numbers_are_valid(self):%0A        numbers = %5BPhoneNumber.from_string(number_string)%0A                   for number_string in self.equal_number_strings%5D%0A        self.assertTrue(all(%5Bnumber.is_valid() for number in numbers%5D))%0A        numbers = %5BPhoneNumber.from_string(number_string, region=region)%0A                   for region, number_string in self.local
 _number
-_1)
+s%5D
 %0A   
@@ -1160,237 +1160,800 @@
 elf.
-mandatory.save()%0A%0A    def test_can_create_model_with_string(self):%0A        obj = MandatoryPhoneNumber.objects.create(phone_number=self.test_number_1)%0A        # refetch it%0A        obj2 = MandatoryPhoneNumber.objects.get(pk=obj.pk)
+assertTrue(all(%5Bnumber.is_valid() for number in numbers%5D))%0A%0A    def test_invalid_numbers_are_invalid(self):%0A        numbers = %5BPhoneNumber.from_string(number_string)%0A                   for number_string in self.invalid_numbers%5D%0A        self.assertTrue(all(%5Bnot number.is_valid() for number in numbers%5D))%0A%0A%0A    def test_objects_with_same_number_are_equal(self):%0A        numbers = %5B%0A            MandatoryPhoneNumber.objects.create(%0A                phone_number=number_string).phone_number%0A            for number_string in self.equal_number_strings%5D%0A        self.assertTrue(all(n==numbers%5B0%5D for n in numbers))%0A%0A    def test_field_returns_correct_type(self):%0A        model = OptionalPhoneNumber()%0A        self.assertEqual(model.phone_number, None)%0A        model.phone_number = '+49 176 96842671'
 %0A   
@@ -1974,19 +1974,26 @@
 rtEqual(
-obj
+type(model
 .phone_n
@@ -2001,22 +2001,17 @@
 mber
+)
 , 
-obj2.p
+P
 hone
-_n
+N
 umbe
@@ -2159,17 +2159,16 @@
 umber_1%0A
-%0A
         

Rework tests