Added clidbmanager.remove_database(), use in tests for cleanup

* clidbmanager.remove_database - removes database by name
* cli_test - clean up database created in tests
* cli_test - uses a better random name
This commit is contained in:
Doug Blank 2015-12-17 11:17:21 -05:00
parent 7eb8015aee
commit 392e9eb399
2 changed files with 33 additions and 4 deletions

View File

@ -380,6 +380,32 @@ class CLIDbManager(object):
return True
return False
def remove_database(self, dbname):
"""
Deletes a database folder given its proper name.
"""
dbdir = os.path.expanduser(config.get('behavior.database-path'))
match_list = []
for dpath in os.listdir(dbdir):
dirpath = os.path.join(dbdir, dpath)
path_name = os.path.join(dirpath, NAME_FILE)
if os.path.isfile(path_name):
file = open(path_name, 'r', encoding='utf8')
name = file.readline().strip()
file.close()
if name == dbname: # currently exact match; could add re.match
match_list.append(dirpath)
# now delete them:
for directory in match_list:
try:
for (top, dirs, files) in os.walk(directory):
for filename in files:
os.unlink(os.path.join(top, filename))
os.rmdir(directory)
except (IOError, OSError) as msg:
CLIDbManager.ERROR(_("Could not delete Family Tree"),
str(msg))
def rename_database(self, filepath, new_text):
"""
Renames the database by writing the new value to the name.txt file

View File

@ -169,29 +169,32 @@ class CLITest(unittest.TestCase):
os.remove(example_copy)
def setUp(self):
from gramps.cli.clidbman import CLIDbManager
self.tearDown()
self.dbstate = DbState()
#we need a manager for the CLI session
self.user = User(auto_accept=True, quiet=False)
self.climanager = CLIManager(self.dbstate, setloader=True, user=self.user)
self.clidbmanager = CLIDbManager(self.dbstate)
#load the plugins
self.climanager.do_reg_plugins(self.dbstate, uistate=None)
def test1_cli(self):
def test1a_cli(self):
# handle the arguments
argparser = ArgParser([None, "-C", "Test", "--import", example])
argparser = ArgParser([None, "-C", "Test: test1_cli", "--import", example])
argparser.need_gui() # initializes some variables
handler = ArgHandler(self.dbstate, argparser, self.climanager)
# create a manager to manage the database
handler.handle_args_cli(should_exit=False)
def test2_cli(self):
def test1b_cli(self):
# handle the arguments
argparser = ArgParser([None, "-O", "Test", "--export", example_copy])
argparser = ArgParser([None, "-O", "Test: test1_cli", "--export", example_copy])
argparser.need_gui() # initializes some variables
handler = ArgHandler(self.dbstate, argparser, self.climanager)
# create a manager to manage the database
handler.handle_args_cli(should_exit=False)
self.clidbmanager.remove_database("Test: test1_cli")
if __name__ == "__main__":
unittest.main()