tests.py 1.9 KB
Newer Older
okhin's avatar
okhin committed
1
2
from unittest import mock

3
from django.test import TestCase
okhin's avatar
okhin committed
4
5
6
from django.core.files import File

from picampaign.contact.models import Contact, Phone
7

okhin's avatar
okhin committed
8
9
class MockImageField(object):
    url = 'http://example.com/photo1.jpg'
Mindiell's avatar
Mindiell committed
10
11
12
13
14

class ContactMethodsTest(TestCase):
    def test_full_name(self):
        contact = Contact(first_name='Victor', last_name='Hugo')
        self.assertEqual(contact.full_name(), 'Victor Hugo')
okhin's avatar
okhin committed
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

    def test_set_valid_phone(self):
        contact = Contact.objects.create(
                first_name='Victor',
                last_name='Hugo'
        )
        phone1 = Phone.objects.create(
                phone='0123456789',
                contact=contact,
                valid = True
        )
        phone2 = Phone.objects.create(
                phone='0123456789',
                contact=contact,
                valid=False
        )
        # Let's check we're in the correct state first
        self.assertEqual(phone1.valid, True)
        self.assertEqual(phone2.valid, False)
        # Let's change the validity
        contact.set_valid_phone(phone2)
        phone1.refresh_from_db()
        phone2.refresh_from_db()
        self.assertEqual(phone1.valid, False)
        self.assertEqual(phone2.valid, True)

    def test_get_photo_url(self):
        contact = Contact.objects.create(
                first_name='Victor',
                last_name='Ugo',
        )
        self.assertEqual(contact.get_photo_url(), '')
        contact.photo = MockImageField()
        self.assertEqual(contact.get_photo_url(), 'http://example.com/photo1.jpg')

class PhoneTest(TestCase):
    def test_str(self):
        contact = Contact.objects.create(
                first_name='Victor',
                last_name='Hugo'
        )
        phone = Phone.objects.create(
                phone='0123456789',
                contact=contact,
                valid=False
        )
        self.assertEqual(str(phone), phone.phone)