summaryrefslogtreecommitdiff
path: root/service/test/unit/adapter/test_tag.py
blob: e6d2771d758c4965fbe4a3bef7f91efa5e5a48d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
from twisted.trial import unittest

from pixelated.adapter.model.tag import Tag


class TestTag(unittest.TestCase):

    def test_from_dict_sets_all_tag_attributes(self):
        tag_dict = {'name': 'a_tag',
                    'default': False,
                    'counts': {'total': 3,
                               'read': 1,
                               'starred': 1,
                               'replied': 1},
                    'mails': [1, 2, 3]}

        tag = Tag.from_dict(tag_dict)

        self.assertEquals(tag_dict['name'], tag.name)
        self.assertEquals(tag_dict['default'], tag.default)
        self.assertEquals(tag_dict['counts']['total'], tag.total)
        # Checks if mail ids are aways restored as set()
        self.assertEquals(type(tag.mails), type(set()))
        self.assertEquals(set(tag_dict['mails']), tag.mails)

    def test_as_dict_puts_all_tag_attributes_in_the_returning_dict(self):
        tag = Tag('some_tag', default=True)
        tag.counts = {'total': 0, 'read': 0, 'starred': 0, 'replied': 0}
        tag.mails = [1, 2, 3]

        tag_dict = tag.as_dict()

        self.assertEquals(tag.name, tag_dict['name'])
        self.assertEquals(tag.default, tag_dict['default'])
        self.assertEquals(tag.total, tag_dict['counts']['total'])
        self.assertEquals(tag.mails, tag_dict['mails'])

    def test_increments_total_count_and_adds_mails_id_to_mails(self):
        tag = Tag('another')
        tag.increment(12)

        self.assertIn(12, tag.mails)
        self.assertEquals(1, tag.total)

    def test_decrement_does_nothing_if_mail_has_not_the_tag(self):
        tag = Tag('tag')
        tag.decrement(2000)

        self.assertEquals(0, tag.total)

    def test_increment_does_nothing_if_mail_already_has_the_tag(self):
        tag = Tag('tag')
        tag.mails = set([12])
        tag.increment(12)

        self.assertEquals(1, tag.total)

    def test_decrements_total_count_and_removes_mails_id_from_mails(self):
        tag = Tag('one_more')
        tag.mails = set([12])
        tag.decrement(12)

        self.assertNotIn(12, tag.mails)
        self.assertEquals(0, tag.total)