1 | # -*- coding: utf-8 -*-
|
---|
2 |
|
---|
3 | import os
|
---|
4 | from unittest import TestCase
|
---|
5 | from postman.models import Member, MailingList
|
---|
6 |
|
---|
7 |
|
---|
8 | class TestMember(TestCase):
|
---|
9 | def setUp(self):
|
---|
10 | self.member = Member('test@example.com')
|
---|
11 |
|
---|
12 | def test__validate_address(self):
|
---|
13 | self.assertEqual(self.member._validate_address(self.member.address),
|
---|
14 | self.member.address)
|
---|
15 | with self.assertRaises(ValueError):
|
---|
16 | self.member._validate_address('notavalidemail')
|
---|
17 |
|
---|
18 |
|
---|
19 | class TestMailingList(TestCase):
|
---|
20 | def setUp(self):
|
---|
21 | configfile = os.path.join(os.path.dirname(__file__), 'postman.conf')
|
---|
22 | self.mailing_list = MailingList('test_list', 'test_list@example.com',
|
---|
23 | members={}, configfile=configfile)
|
---|
24 | self.member = Member('test@example.com')
|
---|
25 |
|
---|
26 | def test__validate_member_object(self):
|
---|
27 | self.assertEqual(self.mailing_list._validate_member_object(self.member),
|
---|
28 | self.member)
|
---|
29 | with self.assertRaises(TypeError):
|
---|
30 | self.mailing_list._validate_member_object(None)
|
---|
31 | self.mailing_list._validate_member_object('Not a member object')
|
---|
32 | self.mailing_list._validate_member_object(self.mailing_list)
|
---|
33 |
|
---|
34 | def test__validate_member(self):
|
---|
35 | # At first the member object is not a member of the list
|
---|
36 | self.assertFalse(self.mailing_list._validate_member(self.member))
|
---|
37 | self.mailing_list.add_member(self.member)
|
---|
38 | self.assertTrue(self.mailing_list._validate_member(self.member))
|
---|
39 | with self.assertRaises(TypeError):
|
---|
40 | self.mailing_list._validate_member(None)
|
---|
41 | self.mailing_list._validate_member('Not a member object')
|
---|
42 | self.mailing_list._validate_member(self.mailing_list)
|
---|
43 |
|
---|
44 | def test__validate_member_by_address(self):
|
---|
45 | self.assertFalse(self.mailing_list._validate_member_by_address(self.member.address))
|
---|
46 | self.mailing_list.add_member(self.member)
|
---|
47 | self.assertTrue(self.mailing_list._validate_member_by_address(self.member.address))
|
---|
48 | with self.assertRaises(ValueError):
|
---|
49 | self.mailing_list._validate_member_by_address(self.member)
|
---|
50 | self.mailing_list._validate_member_by_address(None)
|
---|
51 | self.mailing_list._validate_member_by_address('Not a member object')
|
---|
52 | self.mailing_list._validate_member_by_address(self.mailing_list)
|
---|
53 |
|
---|
54 | def test_members_addresses(self):
|
---|
55 | self.assertEqual(self.mailing_list.members_addresses(), [])
|
---|
56 | self.mailing_list.add_member(self.member)
|
---|
57 | self.assertTrue(self.member.address in \
|
---|
58 | self.mailing_list.members_addresses())
|
---|
59 |
|
---|
60 | def test_add_member(self):
|
---|
61 | self.assertTrue(self.mailing_list.add_member(self.member))
|
---|
62 | self.assertTrue(self.member.address in \
|
---|
63 | self.mailing_list.members_addresses())
|
---|
64 | # check what happens if the member is already there:
|
---|
65 | self.assertFalse(self.mailing_list.add_member(self.member))
|
---|
66 | # check what happens if we try to add something that is not
|
---|
67 | # a valid Member instance
|
---|
68 | with self.assertRaises(TypeError):
|
---|
69 | self.mailing_list.add_member(None)
|
---|
70 | self.mailing_list.add_member('Not a member object')
|
---|
71 | self.mailing_list.add_member(self.mailing_list)
|
---|
72 |
|
---|
73 | def test_add_member_by_address(self):
|
---|
74 | self.assertTrue(self.mailing_list.add_member_by_address(self.member.address))
|
---|
75 | self.assertTrue(self.member.address in \
|
---|
76 | self.mailing_list.members_addresses())
|
---|
77 | # check what happens if the member is already there:
|
---|
78 | self.assertFalse(self.mailing_list.add_member_by_address(self.member.address))
|
---|
79 | # check what happens if we try to add something that is not
|
---|
80 | # a valid Member instance
|
---|
81 | with self.assertRaises(ValueError):
|
---|
82 | self.mailing_list.add_member_by_address(self.member)
|
---|
83 | self.mailing_list.add_member_by_address(None)
|
---|
84 | self.mailing_list.add_member_by_address('Not a member object')
|
---|
85 | self.mailing_list.add_member_by_address(self.mailing_list)
|
---|
86 |
|
---|
87 | def test_delete_member(self):
|
---|
88 | self.assertFalse(self.mailing_list.delete_member(self.member))
|
---|
89 | self.mailing_list.add_member(self.member)
|
---|
90 | self.assertTrue(self.member.address in \
|
---|
91 | self.mailing_list.members_addresses())
|
---|
92 | self.assertTrue(self.mailing_list.delete_member(self.member))
|
---|
93 | self.assertFalse(self.member.address in \
|
---|
94 | self.mailing_list.members_addresses())
|
---|
95 | with self.assertRaises(TypeError):
|
---|
96 | self.mailing_list.delete_member(None)
|
---|
97 | self.mailing_list.delete_member('Not a member object')
|
---|
98 | self.mailing_list.delete_member(self.mailing_list)
|
---|