1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Getting Things GNOME! - a personal organizer for the GNOME desktop
4
# Copyright (c) 2008-2013 - Lionel Dricot & Bertrand Rousseau
6
# This program is free software: you can redistribute it and/or modify it under
7
# the terms of the GNU General Public License as published by the Free Software
8
# Foundation, either version 3 of the License, or (at your option) any later
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program. If not, see <http://www.gnu.org/licenses/>.
18
# -----------------------------------------------------------------------------
20
""" Tests for the TwoKeyDict class """
25
from GTG.tools.twokeydict import TwoKeyDict
28
class TestTwoKeyDict(unittest.TestCase):
29
""" Tests for the TwoKeyDict object. """
31
def test_add_and_gets(self):
32
""" Test for the __init__, _get_by_first, _get_by_second function """
33
triplets = [(uuid.uuid4(), uuid.uuid4(), uuid.uuid4())
35
tw_dict = TwoKeyDict(*triplets)
36
for triplet in triplets:
37
self.assertEqual(tw_dict._get_by_primary(triplet[0]), triplet[2])
38
self.assertEqual(tw_dict._get_by_secondary(triplet[1]), triplet[2])
40
def test_remove_by_first_or_second(self):
41
""" Test for removing triplets form the TwoKeyDict """
42
triplet_first = (1, 'I', 'one')
43
triplet_second = (2, 'II', 'two')
44
tw_dict = TwoKeyDict(triplet_first, triplet_second)
45
tw_dict._remove_by_primary(triplet_first[0])
46
tw_dict._remove_by_secondary(triplet_second[1])
50
tw_dict._get_by_primary(triplet_first[0])
54
tw_dict._get_by_secondary(triplet_second[0])
58
tw_dict._get_by_secondary(triplet_first[1])
62
tw_dict._get_by_secondary(triplet_second[1])
65
self.assertEqual(missing_first, 2)
66
self.assertEqual(missing_second, 2)
67
# check for memory leaks
69
for key in tw_dict._primary_to_value.keys():
71
self.assertEqual(dict_len, 0)
73
def test_get_primary_and_secondary_key(self):
74
""" Test for fetching the objects stored in the TwoKeyDict """
75
triplets = [(uuid.uuid4(), uuid.uuid4(), uuid.uuid4())
77
tw_dict = TwoKeyDict(*triplets)
78
for triplet in triplets:
79
self.assertEqual(tw_dict._get_secondary_key(triplet[0]),
81
self.assertEqual(tw_dict._get_primary_key(triplet[1]),
84
def test_missing_and_then_add(self):
86
local_id = '3ea957cb-417f-4944-be6c-02a1b0a84bd2'
88
remote_id = 'https://api.launchpad.net/1.0/bugs/345808'
91
tw_dict = TwoKeyDict()
92
self.assertRaises(KeyError, tw_dict._get_secondary_key, remote_id)
93
tw_dict.add((local_id, remote_id, value))
94
self.assertEqual(remote_id, tw_dict._get_secondary_key(local_id))
98
return unittest.TestLoader().loadTestsFromTestCase(TestTwoKeyDict)