1
"""Tests for Desktop CouchDB"""
10
from desktopcouch.application import local_files
11
from desktopcouch.application.platform import (TestKeyring,
13
from desktopcouch.application.start_local_couchdb import start_couchdb
14
from desktopcouch.application.stop_local_couchdb import stop_couchdb
16
set_application_name('desktopcouch testing')
19
class TestCase(unittest.TestCase):
20
"""Base class for tests."""
22
def __init__(self, *args, **kwargs):
23
super(TestCase, self).__init__(*args, **kwargs)
24
# pylint: disable=C0103
25
if not hasattr(self, 'assertIn'):
26
self.assertIn = lambda value, container, *args: \
27
self.assertTrue(value in container, *args)
28
if not hasattr(self, 'assertNotIn'):
29
self.assertNotIn = lambda value, container, *args: \
30
self.assertTrue(value not in container, *args)
31
if not hasattr(self, 'assertIs'):
32
self.assertIs = lambda value1, value2, *args: \
33
self.assertTrue(value1 is value2, *args)
34
if not hasattr(self, 'assertIsNot'):
35
self.assertIsNot = lambda value1, value2, *args: \
36
self.assertTrue(value1 is not value2, *args)
39
def create_new_test_environment():
40
"""Create a new test environment."""
41
basedir = tempfile.mkdtemp()
42
if not os.path.exists(basedir):
45
cache = os.path.join(basedir, 'cache')
46
data = os.path.join(basedir, 'data')
47
config = os.path.join(basedir, 'config')
48
new_context = local_files.Context(
49
cache, data, config, keyring=TestKeyring())
50
new_context.couchdb_log_level = 'debug'
52
# Add etc folder to config
53
source_tree_etc_folder = os.path.realpath(
54
os.path.join(os.path.split(__file__)[0], "..", "..", "config"))
55
if os.path.isdir(source_tree_etc_folder):
56
os.environ["XDG_CONFIG_DIRS"] = source_tree_etc_folder
58
def stop_test_couch(temp_dir, ctx):
59
"""Stop the test couchdatabase."""
61
shutil.rmtree(temp_dir)
63
start_couchdb(ctx=new_context)
64
atexit.register(stop_test_couch, basedir, new_context)
69
# TODO: Remove these after you're sure nothing we care about uses these env.
70
os.environ['XDG_CACHE_HOME'] = "/cachehome"
71
os.environ['XDG_DATA_HOME'] = "/datahome"
72
os.environ['XDG_CONFIG_HOME'] = "/confighome"
73
os.environ['XDG_DATA_DIRS'] = ''
75
test_context = create_new_test_environment() # pylint: disable=C0103