~saurabhanandiit/gtg/exportFixed

« back to all changes in this revision

Viewing changes to GTG/tests/test_twokeydict.py

Merge of my work on liblarch newbase and all the backends ported to liblarch
(which mainly means porting the datastore).
One failing test, will check it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# -----------------------------------------------------------------------------
 
3
# Gettings Things Gnome! - a personal organizer for the GNOME desktop
 
4
# Copyright (c) 2008-2009 - 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
'''
 
21
Tests for the TwoKeyDict class
 
22
'''
 
23
 
 
24
import unittest
 
25
import uuid
 
26
 
 
27
from GTG.tools.twokeydict import TwoKeyDict
 
28
 
 
29
 
 
30
 
 
31
class TestTwoKeyDict(unittest.TestCase):
 
32
    '''
 
33
    Tests for the TwoKeyDict object.
 
34
    '''
 
35
 
 
36
    
 
37
    def test_add_and_gets(self):
 
38
        '''
 
39
        Test for the __init__, _get_by_first, _get_by_second function
 
40
        '''
 
41
        triplets = [(uuid.uuid4(), uuid.uuid4(), uuid.uuid4()) \
 
42
                    for a in xrange(10)]
 
43
        tw_dict = TwoKeyDict(*triplets)
 
44
        for triplet in triplets:
 
45
            self.assertEqual(tw_dict._get_by_primary(triplet[0]), triplet[2])
 
46
            self.assertEqual(tw_dict._get_by_secondary(triplet[1]), triplet[2])
 
47
 
 
48
    def test_remove_by_first_or_second(self):
 
49
        '''
 
50
        Test for removing triplets form the TwoKeyDict
 
51
        '''
 
52
        triplet_first = (1, 'I', 'one')
 
53
        triplet_second = (2, 'II', 'two')
 
54
        tw_dict = TwoKeyDict(triplet_first, triplet_second)
 
55
        tw_dict._remove_by_primary(triplet_first[0])
 
56
        tw_dict._remove_by_secondary(triplet_second[1])
 
57
        missing_first = 0
 
58
        missing_second = 0
 
59
        try:
 
60
            tw_dict._get_by_primary(triplet_first[0])
 
61
        except KeyError:
 
62
            missing_first += 1
 
63
        try:
 
64
            tw_dict._get_by_secondary(triplet_second[0])
 
65
        except KeyError:
 
66
            missing_first += 1
 
67
        try:
 
68
            tw_dict._get_by_secondary(triplet_first[1])
 
69
        except KeyError:
 
70
            missing_second += 1
 
71
        try:
 
72
            tw_dict._get_by_secondary(triplet_second[1])
 
73
        except KeyError:
 
74
            missing_second += 1
 
75
        self.assertEqual(missing_first, 2)
 
76
        self.assertEqual(missing_second, 2)
 
77
        #check for memory leaks
 
78
        dict_len = 0
 
79
        for key in tw_dict._primary_to_value.iterkeys():
 
80
            dict_len += 1
 
81
        self.assertEqual(dict_len, 0)
 
82
 
 
83
    def test_get_primary_and_secondary_key(self):
 
84
        '''
 
85
        Test for fetching the objects stored in the TwoKeyDict
 
86
        '''
 
87
        triplets = [(uuid.uuid4(), uuid.uuid4(), uuid.uuid4()) \
 
88
                    for a in xrange(10)]
 
89
        tw_dict = TwoKeyDict(*triplets)
 
90
        for triplet in triplets:
 
91
            self.assertEqual(tw_dict._get_secondary_key(triplet[0]), \
 
92
                             triplet[1])
 
93
            self.assertEqual(tw_dict._get_primary_key(triplet[1]), \
 
94
                             triplet[0])
 
95
 
 
96
def test_suite():
 
97
    return unittest.TestLoader().loadTestsFromTestCase(TestTwoKeyDict)
 
98