source: mailjam/postman/tests/daemon.py@ 5:573fdae8b1f6

Last change on this file since 5:573fdae8b1f6 was 5:573fdae8b1f6, checked in by Francisco de Borja Lopez Rio <borja@…>, 12 years ago

Added tests for postman.daemon.Postman

Added a method to postman.daemon.Postman to "clear" all the storage associated
to a given postman instance (removing all the files from disk)

Fixed some bugs, mostly caused by typos through the sources, detected while
writing more tests

Removed the storage and archive attributes from postman.models.MailingList,
now they are methods "marked" as properties using the @property decorator.
This keeps the storage objects from being serialized into json objects by
the storage backend (de-serializing them caused some trouble and it is not
necessary at all)

Added create() and delete() methods to postman.storage.JsonStorage. the first
one ensures the path to the storage file exists (creating all subdirs if
necessary) and the second one deletes the storage file from disk

File size: 5.1 KB
Line 
1# -*- coding: utf-8 -*-
2
3import os
4from unittest import TestCase
5from postman.daemon import Postman
6from postman.models import Member, MailingList
7from postman.storage import JsonStorage as Storage
8
9
10class TestPostman(TestCase):
11 """
12 postman.daemon.Postman tests.
13
14 Remember to call the .clear() method of postman after each test, so
15 the temp storage files are deleted
16 """
17 def setUp(self):
18 config = {'private': False, 'archive': '/tmp/postman-tests/archive',
19 'storage': '/tmp/postman-tests/storage'}
20 self.mailing_list = MailingList('test_list', 'test_list@example.com',
21 members={}, config=config)
22 self.member = Member('test@example.com')
23
24 def test___init__(self):
25 postman = Postman()
26 self.assertIsInstance(postman, Postman)
27 self.assertEqual(postman.mailings, {})
28 self.assertEqual(postman.mailings_addresses, [])
29 self.assertIsInstance(postman.dbs, dict)
30 self.assertTrue('mailings' in postman.dbs.keys())
31 self.assertTrue('members' in postman.dbs.keys())
32 self.assertIsInstance(postman.dbs['mailings'], Storage)
33 self.assertIsInstance(postman.dbs['members'], Storage)
34
35 def test_save(self):
36 postman = Postman()
37 self.assertFalse(postman.save())
38 postman.add_mailing_list(self.mailing_list.info())
39 self.assertTrue(postman.save())
40 # FIXME: We have to test here that the generated json file
41 # contains the data it should contain
42
43 # Clear the files created by the tests
44 postman.clear()
45
46 def test_load(self):
47 postman = Postman()
48 self.assertFalse(postman.mailings)
49 self.assertFalse(postman.mailings_addresses)
50 self.assertFalse(postman.load())
51 postman.add_mailing_list(self.mailing_list.info())
52 self.assertTrue(postman.load())
53
54 # Check that another postman instance is able to load the saved data
55 postman_load = Postman()
56 self.assertFalse(postman_load.mailings)
57 self.assertFalse(postman_load.mailings_addresses)
58 postman_load.load()
59 self.assertTrue(postman_load.mailings)
60 self.assertIsInstance(postman_load.mailings, dict)
61 self.assertTrue(postman_load.mailings_addresses)
62 self.assertIsInstance(postman_load.mailings_addresses, list)
63
64 # Clear the files created by the tests
65 postman.clear()
66
67 def test_clear(self):
68 postman = Postman()
69 self.assertFalse(postman.clear())
70 postman.add_mailing_list(self.mailing_list.info())
71 self.assertTrue(postman.clear())
72
73 def test_add_mailing_list(self):
74 postman = Postman()
75 with self.assertRaises(TypeError):
76 # test improper info values
77 postman.add_mailing_list(['a list', 'is an', 'invalid parameter'])
78 postman.add_mailing_list(self.mailing_list)
79 with self.assertRaises(ValueError):
80 #test incomplete/missing info values
81 postman.add_mailing_list()
82 postman.add_mailing_list({'name': 'missing info'})
83 postman.add_mailing_list({'address': 'missing info'})
84 postman.add_mailing_list({'name': 'missing info',
85 'address': 'missing info'})
86 # test mailing lists can be added
87 self.assertTrue(postman.add_mailing_list(self.mailing_list.info()))
88 self.assertTrue(postman.mailings)
89 self.assertIsInstance(postman.mailings, dict)
90 self.assertTrue(postman.mailings_addresses)
91 self.assertIsInstance(postman.mailings_addresses, list)
92 with self.assertRaises(IndexError):
93 # test what happens when the mailing has been already added
94 postman.add_mailing_list(self.mailing_list.info())
95
96 # Clear the files created by the tests
97 postman.clear()
98
99 def test_add_mailing_member(self):
100 postman = Postman()
101 postman.add_mailing_list(self.mailing_list.info())
102 with self.assertRaises(ValueError):
103 # test what happens if we call the method without proper
104 # parameters
105 postman.add_mailing_member()
106 postman.add_mailing_member(None, None)
107 postman.add_mailing_member(None, 'test_list@example.net')
108 postman.add_mailing_member('test@example.net', None)
109 postman.add_mailing_member('test@example', 'test_list@example.net')
110 with self.assertRaises(IndexError):
111 # test if we try to add a member to a non-existing mailing list
112 postman.add_mailing_member('test@example.net',
113 'test_list_b@example.net')
114 # Test adding a member
115 self.assertTrue(postman.add_mailing_member('test@example.net',
116 self.mailing_list.address))
117 # Test trying to re-add that user
118 self.assertFalse(postman.add_mailing_member('test@example.net',
119 self.mailing_list.address))
120
121 # Clear the files created by the tests
122 postman.clear()
Note: See TracBrowser for help on using the repository browser.