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
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 GTG backends.
22
Some of these tests will generate files in
23
xdg.BaseDirectory.xdg_data_home/gtg directory.
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
38
class GtgBackendsUniTests(unittest.TestCase):
39
"""Tests for GTG backends."""
41
def __init__(self, test):
42
unittest.TestCase.__init__(self, test)
49
CoreConfig().set_data_dir("./test_data")
50
CoreConfig().set_conf_dir("./test_data")
52
def test_localfile_get_name(self):
53
"""Tests for localfile/get_name function :
54
- a string is expected.
56
res = localfile.Backend.get_name()
57
expectedres = "backend_localfile"
58
self.assertEqual(res, expectedres)
60
def test_localfile_get_description(self):
61
"""Tests for localfile/get_description function :
62
- a string is expected.
64
res = localfile.Backend.get_description()
65
expectedres = "Your tasks are saved"
66
self.assertEqual(res[:len(expectedres)], expectedres)
69
def test_localfile_get_static_parameters(self):
70
"""Tests for localfile/get_static_parameters function:
71
- a string is expected.
73
res = localfile.Backend.get_static_parameters()
74
self.assertEqual(res['path']['type'], "string")
76
def test_localfile_get_type(self):
77
"""Tests for localfile/get_type function:
78
- a string is expected.
80
res = localfile.Backend.get_type()
81
expectedres = "readwrite"
82
self.assertEqual(res, expectedres)
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.
89
self.create_test_environment()
90
doc, configxml = cleanxml.openxmlfile(self.datapath, 'config')
91
xmlproject = doc.getElementsByTagName('backend')
92
for domobj in xmlproject:
94
if domobj.hasAttribute("module"):
95
dic["module"] = str(domobj.getAttribute("module"))
96
dic["pid"] = str(domobj.getAttribute("pid"))
97
dic["xmlobject"] = domobj
99
dic["path"] = self.taskpath
100
beobj = localfile.Backend(dic)
102
beobj.remove_task("0@1")
104
dataline = open(self.taskpath, 'r').read()
106
if "0@1" in dataline:
111
self.assertEqual(res, expectedres)
113
# def test_localfile_backend_method4(self):
114
# """Tests for localfile/Backend/get_task method:
115
# - Compares task titles to check if method works.
117
# self.create_test_environment()
118
# doc, configxml = cleanxml.openxmlfile(self.datapath, 'config')
119
# xmlproject = doc.getElementsByTagName('backend')
120
# for domobj in xmlproject:
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")
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.
137
# self.create_test_environment()
138
# doc, configxml = cleanxml.openxmlfile(self.datapath, 'config')
139
# xmlproject = doc.getElementsByTagName('backend')
140
# for domobj in xmlproject:
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:
157
# self.assertEqual(res, expectedres)
159
def create_test_environment(self):
160
"""Create the test environment"""
161
self.taskfile = 'test.xml'
162
self.datafile = 'projectstest.xml'
164
'<?xml version="1.0" ?>\n',
166
'\t<task id="0@1" status="Active" tags="">\n',
168
'\t\t\tCeci est un test\n',
174
'<?xml version="1.0" ?>\n',
176
'\t<backend filename="test.xml" module="localfile" pid="1"/>\n',
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)
189
CoreConfig().set_data_dir("./test_data")
190
CoreConfig().set_conf_dir("./test_data")
191
return unittest.TestLoader().loadTestsFromName(__name__)