summaryrefslogtreecommitdiff
path: root/service/test/unit/adapter/tag_index_test.py
blob: 21564fc5da365288748ed626eeaa2362ece2565d (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
80
81
82
83
84
85
86
87
88

#
# 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/>.
import unittest
import os
import uuid

from pixelated.adapter.tag_index import TagIndex
from pixelated.adapter.tag import Tag


class TestTagIndex(unittest.TestCase):

    def setUp(self):
        self.db_path = '/tmp/test_tag_index_' + str(uuid.uuid4())
        self.tag_index = TagIndex(self.db_path)

    def tearDown(self):
        self.tag_index._close_db()
        os.remove(self.db_path + '.db')

    def test_get_and_set_works(self):
        tag = Tag('a_tag')
        self.tag_index.set(tag)
        self.assertEquals(tag, self.tag_index.get('a_tag'))

    def test_values_returns_all_values_in_the_index(self):
        tag_a = Tag('tag_a')
        self.tag_index.set(tag_a)
        tag_b = Tag('tag_b')
        self.tag_index.set(tag_b)
        tag_c = Tag('tag_c')
        self.tag_index.set(tag_c)

        self.assertEquals(set([tag_a, tag_b, tag_c]), self.tag_index.values())

    def test_changes_are_visible_between_instances_using_same_file(self):
        tag = Tag('some_tag')
        self.tag_index.set(tag)

        other_tag_index = TagIndex(self.db_path)
        self.assertIn(tag, other_tag_index.values())

    def test_add_does_not_replace_existent_tag_with_same_name(self):
        tag = Tag('tag', True)
        self.tag_index.set(tag)

        same_name_tag = Tag('tag', False)
        self.tag_index.add(same_name_tag)

        self.assertEquals(True, self.tag_index.get('tag').default)

    def test_empty_returns_true_if_there_are_no_values(self):
        self.assertTrue(self.tag_index.empty())

    def test_empty_returns_false_if_there_are_values(self):
        self.tag_index.set(Tag('tag'))
        self.assertFalse(self.tag_index.empty())

    def test_remove_deletes_the_tag_with_the_given_key_from_the_index(self):
        self.tag_index.set(Tag('tag'))
        self.tag_index.remove('tag')
        self.assertEquals(None, self.tag_index.get('tag'))

    def test_remove_does_not_raises_exception_if_key_is_not_present(self):
        self.tag_index.remove('not_there')

    def test_removals_are_visible_between_instances_using_same_file(self):
        tag = Tag('some_tag')
        self.tag_index.set(tag)

        other_tag_index = TagIndex(self.db_path)
        other_tag_index.remove('some_tag')

        self.assertIsNone(self.tag_index.get('some_tag'))