~amachattie/gtg/patch_one

« back to all changes in this revision

Viewing changes to GTG/tests/test_twokeydict.py

  • Committer: Nimit Shah
  • Date: 2014-03-10 03:37:32 UTC
  • mfrom: (1360.2.18 test-cleanup)
  • Revision ID: nimit.svnit@gmail.com-20140310033732-n4od1z8npybs4ooc
Rework of test-suite, by Izidor Matušov

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
5
 
#
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
9
 
# version.
10
 
#
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
14
 
# details.
15
 
#
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
 
# -----------------------------------------------------------------------------
19
 
 
20
 
""" Tests for the TwoKeyDict class """
21
 
 
22
 
import unittest
23
 
import uuid
24
 
 
25
 
from GTG.tools.twokeydict import TwoKeyDict
26
 
 
27
 
 
28
 
class TestTwoKeyDict(unittest.TestCase):
29
 
    """ Tests for the TwoKeyDict object. """
30
 
 
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())
34
 
                    for a in range(10)]
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])
39
 
 
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])
47
 
        missing_first = 0
48
 
        missing_second = 0
49
 
        try:
50
 
            tw_dict._get_by_primary(triplet_first[0])
51
 
        except KeyError:
52
 
            missing_first += 1
53
 
        try:
54
 
            tw_dict._get_by_secondary(triplet_second[0])
55
 
        except KeyError:
56
 
            missing_first += 1
57
 
        try:
58
 
            tw_dict._get_by_secondary(triplet_first[1])
59
 
        except KeyError:
60
 
            missing_second += 1
61
 
        try:
62
 
            tw_dict._get_by_secondary(triplet_second[1])
63
 
        except KeyError:
64
 
            missing_second += 1
65
 
        self.assertEqual(missing_first, 2)
66
 
        self.assertEqual(missing_second, 2)
67
 
        # check for memory leaks
68
 
        dict_len = 0
69
 
        for key in tw_dict._primary_to_value.keys():
70
 
            dict_len += 1
71
 
        self.assertEqual(dict_len, 0)
72
 
 
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())
76
 
                    for a in range(10)]
77
 
        tw_dict = TwoKeyDict(*triplets)
78
 
        for triplet in triplets:
79
 
            self.assertEqual(tw_dict._get_secondary_key(triplet[0]),
80
 
                             triplet[1])
81
 
            self.assertEqual(tw_dict._get_primary_key(triplet[1]),
82
 
                             triplet[0])
83
 
 
84
 
    def test_missing_and_then_add(self):
85
 
        # Primary
86
 
        local_id = '3ea957cb-417f-4944-be6c-02a1b0a84bd2'
87
 
        # Secondary
88
 
        remote_id = 'https://api.launchpad.net/1.0/bugs/345808'
89
 
        value = "Hello world"
90
 
 
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))
95
 
 
96
 
 
97
 
def test_suite():
98
 
    return unittest.TestLoader().loadTestsFromTestCase(TestTwoKeyDict)