~bryce/gtg/dbus-service-name

« back to all changes in this revision

Viewing changes to GTG/tests/test_backends.py

  • Committer: Luca Invernizzi
  • Date: 2010-06-22 20:19:47 UTC
  • mto: This revision was merged to the branch mainline in revision 825.
  • Revision ID: invernizzi.l@gmail.com-20100622201947-mixmodtcf6qvowqi
second batch

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
"""Tests for GTG backends.
 
21
 
 
22
Some of these tests will generate files in
 
23
xdg.BaseDirectory.xdg_data_home/gtg directory.
 
24
"""
 
25
 
 
26
# Standard imports
 
27
import unittest
 
28
import os
 
29
import xdg
 
30
 
 
31
# GTG imports
 
32
from GTG.backends import backend_localfile as localfile
 
33
from GTG.core import datastore
 
34
from GTG.tools import cleanxml
 
35
from GTG.core import CoreConfig
 
36
 
 
37
 
 
38
class GtgBackendsUniTests(unittest.TestCase):
 
39
    """Tests for GTG backends."""
 
40
 
 
41
    def __init__(self, test):
 
42
        unittest.TestCase.__init__(self, test)
 
43
        self.taskfile = ''
 
44
        self.datafile = ''
 
45
        self.taskpath = ''
 
46
        self.datapath = ''
 
47
    
 
48
    def SetUp(self):
 
49
        CoreConfig().set_data_dir("./test_data")
 
50
        CoreConfig().set_conf_dir("./test_data")
 
51
 
 
52
    def test_localfile_get_name(self):
 
53
        """Tests for localfile/get_name function :
 
54
        - a string is expected.
 
55
        """
 
56
        res = localfile.Backend.get_name()
 
57
        expectedres = "backend_localfile"
 
58
        self.assertEqual(res, expectedres)
 
59
 
 
60
    def test_localfile_get_description(self):
 
61
        """Tests for localfile/get_description function :
 
62
        - a string is expected.
 
63
        """
 
64
        res = localfile.Backend.get_description()
 
65
        expectedres = "Your tasks are saved"
 
66
        self.assertEqual(res[:len(expectedres)], expectedres)
 
67
 
 
68
 
 
69
    def test_localfile_get_static_parameters(self):
 
70
        """Tests for localfile/get_static_parameters function:
 
71
        - a string is expected.
 
72
        """
 
73
        res = localfile.Backend.get_static_parameters()
 
74
        self.assertEqual(res['path']['type'], "string")
 
75
 
 
76
    def test_localfile_get_type(self):
 
77
        """Tests for localfile/get_type function:
 
78
        - a string is expected.
 
79
        """
 
80
        res = localfile.Backend.get_type()
 
81
        expectedres = "readwrite"
 
82
        self.assertEqual(res, expectedres)
 
83
 
 
84
 
 
85
    def test_localfile_backend_method3(self):
 
86
        """Tests for localfile/Backend/remove_task method:
 
87
        - parse task file to check if task has been removed.
 
88
        """
 
89
        self.create_test_environment()
 
90
        doc, configxml = cleanxml.openxmlfile(self.datapath, 'config')
 
91
        xmlproject = doc.getElementsByTagName('backend')
 
92
        for domobj in xmlproject:
 
93
            dic = {}
 
94
            if domobj.hasAttribute("module"):
 
95
                dic["module"] = str(domobj.getAttribute("module"))
 
96
                dic["pid"] = str(domobj.getAttribute("pid"))
 
97
                dic["xmlobject"] = domobj
 
98
                dic["Enabled"] = True
 
99
                dic["path"] = self.taskpath
 
100
        beobj = localfile.Backend(dic)
 
101
        expectedres = True
 
102
        beobj.remove_task("0@1")
 
103
        beobj.quit()
 
104
        dataline = open(self.taskpath, 'r').read()
 
105
        print dataline
 
106
        if "0@1" in dataline:
 
107
            res = False
 
108
        else:
 
109
            res = True
 
110
        expectedres = True
 
111
        self.assertEqual(res, expectedres)
 
112
 
 
113
#    def test_localfile_backend_method4(self):
 
114
#        """Tests for localfile/Backend/get_task method:
 
115
#        - Compares task titles to check if method works.
 
116
#        """
 
117
#        self.create_test_environment()
 
118
#        doc, configxml = cleanxml.openxmlfile(self.datapath, 'config')
 
119
#        xmlproject = doc.getElementsByTagName('backend')
 
120
#        for domobj in xmlproject:
 
121
#            dic = {}
 
122
#            if domobj.hasAttribute("module"):
 
123
#                dic["module"] = str(domobj.getAttribute("module"))
 
124
#                dic["pid"] = str(domobj.getAttribute("pid"))
 
125
#                dic["xmlobject"] = domobj
 
126
#                dic["filename"] = self.taskfile
 
127
#        beobj = localfile.Backend(dic)
 
128
#        dstore = datastore.DataStore()
 
129
#        newtask = dstore.new_task(tid="0@2", pid="1", newtask=True)
 
130
#        beobj.get_task(newtask, "0@1")
 
131
#        self.assertEqual(newtask.get_title(), u"Ceci est un test")
 
132
 
 
133
#    def test_localfile_backend_method5(self):
 
134
#        """Tests for localfile/Backend/set_task method:
 
135
#        - parses task file to check if new task has been stored.
 
136
#        """
 
137
#        self.create_test_environment()
 
138
#        doc, configxml = cleanxml.openxmlfile(self.datapath, 'config')
 
139
#        xmlproject = doc.getElementsByTagName('backend')
 
140
#        for domobj in xmlproject:
 
141
#            dic = {}
 
142
#            if domobj.hasAttribute("module"):
 
143
#                dic["module"] = str(domobj.getAttribute("module"))
 
144
#                dic["pid"] = str(domobj.getAttribute("pid"))
 
145
#                dic["xmlobject"] = domobj
 
146
#                dic["filename"] = self.taskfile
 
147
#        beobj = localfile.Backend(dic)
 
148
#        dstore = datastore.DataStore()
 
149
#        newtask = dstore.new_task(tid="0@2", pid="1", newtask=True)
 
150
#        beobj.set_task(newtask)
 
151
#        dataline = open(self.taskpath, 'r').read()
 
152
#        if "0@2" in dataline:
 
153
#            res = True
 
154
#        else:
 
155
#            res = False
 
156
#        expectedres = True
 
157
#        self.assertEqual(res, expectedres)
 
158
 
 
159
    def create_test_environment(self):
 
160
        """Create the test environment"""
 
161
        self.taskfile = 'test.xml'
 
162
        self.datafile = 'projectstest.xml'
 
163
        tasks = [
 
164
            '<?xml version="1.0" ?>\n',
 
165
            '<project>\n',
 
166
            '\t<task id="0@1" status="Active" tags="">\n',
 
167
            '\t\t<title>\n',
 
168
            '\t\t\tCeci est un test\n',
 
169
            '\t\t</title>\n',
 
170
            '\t</task>\n',
 
171
            '</project>\n',
 
172
            ]
 
173
        data = [
 
174
            '<?xml version="1.0" ?>\n',
 
175
            '<config>\n',
 
176
            '\t<backend filename="test.xml" module="localfile" pid="1"/>\n',
 
177
            '</config>\n',
 
178
            ]
 
179
        self.testdir = os.path.join(xdg.BaseDirectory.xdg_data_home, 'gtg')
 
180
        if not os.path.exists(self.testdir):
 
181
            os.makedirs(self.testdir)
 
182
        self.taskpath = os.path.join(self.testdir, self.taskfile)
 
183
        self.datapath = os.path.join(self.testdir, self.datafile)
 
184
        open(self.taskpath, 'w').writelines(tasks)
 
185
        open(self.datapath, 'w').writelines(data)
 
186
 
 
187
 
 
188
def test_suite():
 
189
    CoreConfig().set_data_dir("./test_data")
 
190
    CoreConfig().set_conf_dir("./test_data")
 
191
    return unittest.TestLoader().loadTestsFromName(__name__)