test_serializers.py 2.34 KB
Newer Older
okhin's avatar
okhin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from collections import OrderedDict

from django.test import TestCase

from picampaign.contact.models import Phone, Contact
from picampaign.campaign.models import Argumentary, Campaign, Organization, CampaignContact
from picampaign.campaign.serializers import ArgumentarySerializer, CampaignContactSerializer

class ArgumentarySerializerTest(TestCase):
    def test_serializers(self):
        organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        campaign = Campaign.objects.create(
                title='A campaign',
                start_date='2000-01-01',
                end_date='2100-12-31',
                organization=organization
        )
        Argumentary.objects.create(
                text='A pertinent argument',
23
                title='A title',
okhin's avatar
okhin committed
24
25
                campaign = campaign
        )
26
        self.assertEqual(ArgumentarySerializer(Argumentary.objects.all(), many=True).data, [OrderedDict([('text', 'A pertinent argument'), ('title', 'A title')])])
okhin's avatar
okhin committed
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

class CampaignContactSerializerTest(TestCase):
    def test_to_representation(self):
        organization = Organization.objects.create(
                name='Majestic 12',
                sip_key='majestic-12'
        )
        campaign = Campaign.objects.create(
                title='A campaign',
                start_date='2000-01-01',
                end_date='2100-12-31',
                organization=organization
        )
        contact = Contact.objects.create(
                first_name='Victor',
                last_name='Hugo',
                birthdate='1887-02-26',
        )
        Phone.objects.create(
                phone='0123456789',
                valid=True,
                contact=contact
        )
        Phone.objects.create(
                phone='9876543210',
                valid=True,
                contact=contact
        )
        campaigncontact = CampaignContact(
                campaign=campaign,
                contact=contact
        )
        serializer = CampaignContactSerializer(campaigncontact)
        self.assertEqual(serializer.to_representation(campaigncontact)['phone'], contact.phones.first().phone)
        # Let's add a phone filter
        campaign.phone_filter='98'
        self.assertEqual(serializer.to_representation(campaigncontact)['phone'], contact.phones.last().phone)