def test_mediapath(self): """ Test media path variables. """ # Create database dbstate = DbState() db = dbstate.make_database("bsddb") path = get_empty_tempdir("utils_file_test") db.write_version(path) db.load(path) dbstate.change_database(db) # Test without db.mediapath set self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME)))) self.assertTrue(os.path.exists(media_path(db))) # Test with absolute db.mediapath db.set_mediapath(os.path.abspath(USER_HOME) + "/test_abs") self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_abs")))) # Test with relative db.mediapath db.set_mediapath("test_rel") self.assertEqual( media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(TEMP_DIR + "/utils_file_test/test_rel"))) ) # Test with environment variable db.set_mediapath("/test/{VERSION}/test_var") self.assertEqual( media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/test/" + VERSION + "/test_var"))) ) db.set_mediapath("{USER_PLUGINS}/test_var") self.assertEqual( media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_PLUGINS + "/test_var"))) ) db.set_mediapath("{VERSION}/test_var") self.assertEqual( media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(TEMP_DIR + "/utils_file_test/" + VERSION + "/test_var"))), ) # Test with $GRAMPSHOME environment variable not set old_env = os.environ.copy() if "GRAMPSHOME" in os.environ: del os.environ["GRAMPSHOME"] db.set_mediapath("{GRAMPSHOME}/test_var") self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_var")))) # Test with $GRAMPSHOME environment variable set os.environ["GRAMPSHOME"] = "/this/is/a/test" db.set_mediapath("{GRAMPSHOME}/test_var") self.assertEqual( media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/this/is/a/test/test_var"))) ) # Restore environment os.environ = old_env
def test_editreference(self): dbstate = DbState() db = dbstate.make_database("bsddb") path = "/tmp/edit_ref_test" try: os.mkdir(path) except: pass db.write_version(path) db.load(path) dbstate.change_database(db) source = Place() source.gramps_id = "P0001" dbstate.db.place_map[source.handle] = source.serialize() editor = MockEditReference(dbstate, uistate=None, track=[], source=source, source_ref=None, update=None) with patch('gramps.gui.editors.editreference.ErrorDialog') as MockED: editor.check_for_duplicate_id("Place") self.assertTrue(MockED.called)
def test_mediapath(self): """ Test media path variables. """ # Create database dbstate = DbState() db = dbstate.make_database("bsddb") path = get_empty_tempdir("utils_file_test") db.write_version(path) db.load(path) dbstate.change_database(db) # Test without db.mediapath set self.assertEqual( media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME)))) self.assertTrue(os.path.exists(media_path(db))) # Test with absolute db.mediapath db.set_mediapath(os.path.abspath(USER_HOME) + "/test_abs") self.assertEqual( media_path(db), os.path.normcase( os.path.normpath(os.path.abspath(USER_HOME + "/test_abs")))) # Test with relative db.mediapath db.set_mediapath("test_rel") self.assertEqual( media_path(db), os.path.normcase( os.path.normpath( os.path.abspath(TEMP_DIR + "/utils_file_test/test_rel")))) # Test with environment variable db.set_mediapath("/test/{VERSION}/test_var") self.assertEqual( media_path(db), os.path.normcase( os.path.normpath( os.path.abspath("/test/" + VERSION + "/test_var")))) db.set_mediapath("{USER_PLUGINS}/test_var") self.assertEqual( media_path(db), os.path.normcase( os.path.normpath(os.path.abspath(USER_PLUGINS + "/test_var")))) db.set_mediapath("{VERSION}/test_var") self.assertEqual( media_path(db), os.path.normcase( os.path.normpath( os.path.abspath(TEMP_DIR + "/utils_file_test/" + VERSION + "/test_var")))) # Test with $GRAMPSHOME environment variable not set old_env = os.environ.copy() if 'GRAMPSHOME' in os.environ: del os.environ['GRAMPSHOME'] db.set_mediapath("{GRAMPSHOME}/test_var") self.assertEqual( media_path(db), os.path.normcase( os.path.normpath(os.path.abspath(USER_HOME + "/test_var")))) # Test with $GRAMPSHOME environment variable set os.environ['GRAMPSHOME'] = "/this/is/a/test" db.set_mediapath("{GRAMPSHOME}/test_var") self.assertEqual( media_path(db), os.path.normcase( os.path.normpath(os.path.abspath("/this/is/a/test/test_var")))) # Restore environment os.environ = old_env
def print_db_content(db): for h in db.get_person_handles(): print("DB contains: person %s" % h) for h in db.get_source_handles(): print("DB contains: source %s" % h) tmpdir = tempfile.mkdtemp() try: filename1 = os.path.join(tmpdir, 'test1.grdb') filename2 = os.path.join(tmpdir, 'test2.grdb') print("\nUsing Database file: %s" % filename1) dbstate = DbState() dbman = CLIDbManager(dbstate) dirpath, name = dbman.create_new_db_cli(filename1, dbid="bsddb") db = dbstate.make_database("bsddb") db.load(dirpath, None) print("Add person 1") add_person(db, "Anton", "Albers", True, False) print("Add source") add_source(db, "A short test", True, False) print("Add person 2 without commit") add_person(db, "Bernd", "Beta", False, False) print("Add source") add_source(db, "A short test", True, False) print("Add person 3") add_person(db, "Chris", "Connor", True, False) print_db_content(db) print("Closing Database file: %s" % filename1) db.close() tran = None
class GrampsDbBaseTest(unittest.TestCase): """Base class for unittest that need to be able to create test databases.""" def setUp(self): def dummy_callback(dummy): pass self.dbstate = DbState() self.dbman = CLIDbManager(self.dbstate) dirpath, name = self.dbman.create_new_db_cli("Test: bsddb", dbid="bsddb") self._db = self.dbstate.make_database("bsddb") self._db.load(dirpath, None) def tearDown(self): self._db.close() self.dbman.remove_database("Test: bsddb") def _populate_database(self, num_sources=1, num_persons=0, num_families=0, num_events=0, num_places=0, num_media=0, num_links=1): # start with sources sources = [] for i in range(num_sources): sources.append(self._add_source()) # now for each of the other tables. Give each entry a link # to num_link sources, sources are chosen on a round robin # basis for num, add_func in ((num_persons, self._add_person_with_sources), (num_families, self._add_family_with_sources), (num_events, self._add_event_with_sources), (num_places, self._add_place_with_sources), (num_media, self._add_media_with_sources)): source_idx = 1 for person_idx in range(num): # Get the list of sources to link lnk_sources = set() for i in range(num_links): lnk_sources.add(sources[source_idx - 1]) source_idx = (source_idx + 1) % len(sources) try: add_func(lnk_sources) except: print("person_idx = ", person_idx) print("lnk_sources = ", repr(lnk_sources)) raise return def _add_source(self, repos=None): # Add a Source with DbTxn("Add Source and Citation", self._db) as tran: source = Source() if repos is not None: repo_ref = RepoRef() repo_ref.set_reference_handle(repos.get_handle()) source.add_repo_reference(repo_ref) self._db.add_source(source, tran) self._db.commit_source(source, tran) citation = Citation() citation.set_reference_handle(source.get_handle()) self._db.add_citation(citation, tran) self._db.commit_citation(citation, tran) return citation def _add_repository(self): # Add a Repository with DbTxn("Add Repository", self._db) as tran: repos = Repository() self._db.add_repository(repos, tran) self._db.commit_repository(repos, tran) return repos def _add_object_with_source(self, citations, object_class, add_method, commit_method): object = object_class() with DbTxn("Add Object", self._db) as tran: for citation in citations: object.add_citation(citation.get_handle()) add_method(object, tran) commit_method(object, tran) return object def _add_person_with_sources(self, citations): return self._add_object_with_source(citations, Person, self._db.add_person, self._db.commit_person) def _add_family_with_sources(self, citations): return self._add_object_with_source(citations, Family, self._db.add_family, self._db.commit_family) def _add_event_with_sources(self, citations): return self._add_object_with_source(citations, Event, self._db.add_event, self._db.commit_event) def _add_place_with_sources(self, citations): return self._add_object_with_source(citations, Place, self._db.add_place, self._db.commit_place) def _add_media_with_sources(self, citations): return self._add_object_with_source(citations, Media, self._db.add_media, self._db.commit_media)
class GrampsDbBaseTest(unittest.TestCase): """Base class for unittest that need to be able to create test databases.""" def setUp(self): def dummy_callback(dummy): pass self.dbstate = DbState() self.dbman = CLIDbManager(self.dbstate) dirpath, name = self.dbman.create_new_db_cli("Test: bsddb", dbid="bsddb") self._db = self.dbstate.make_database("bsddb") self._db.load(dirpath, None) def tearDown(self): self._db.close() self.dbman.remove_database("Test: bsddb") def _populate_database(self, num_sources = 1, num_persons = 0, num_families = 0, num_events = 0, num_places = 0, num_media_objects = 0, num_links = 1): # start with sources sources = [] for i in range(num_sources): sources.append(self._add_source()) # now for each of the other tables. Give each entry a link # to num_link sources, sources are chosen on a round robin # basis for num, add_func in ((num_persons, self._add_person_with_sources), (num_families, self._add_family_with_sources), (num_events, self._add_event_with_sources), (num_places, self._add_place_with_sources), (num_media_objects, self._add_media_object_with_sources)): source_idx = 1 for person_idx in range(num): # Get the list of sources to link lnk_sources = set() for i in range(num_links): lnk_sources.add(sources[source_idx-1]) source_idx = (source_idx+1) % len(sources) try: add_func(lnk_sources) except: print ("person_idx = ", person_idx) print ("lnk_sources = ", repr(lnk_sources)) raise return def _add_source(self,repos=None): # Add a Source with DbTxn("Add Source and Citation", self._db) as tran: source = Source() if repos is not None: repo_ref = RepoRef() repo_ref.set_reference_handle(repos.get_handle()) source.add_repo_reference(repo_ref) self._db.add_source(source, tran) self._db.commit_source(source, tran) citation = Citation() citation.set_reference_handle(source.get_handle()) self._db.add_citation(citation, tran) self._db.commit_citation(citation, tran) return citation def _add_repository(self): # Add a Repository with DbTxn("Add Repository", self._db) as tran: repos = Repository() self._db.add_repository(repos, tran) self._db.commit_repository(repos, tran) return repos def _add_object_with_source(self, citations, object_class, add_method, commit_method): object = object_class() with DbTxn("Add Object", self._db) as tran: for citation in citations: object.add_citation(citation.get_handle()) add_method(object, tran) commit_method(object, tran) return object def _add_person_with_sources(self, citations): return self._add_object_with_source(citations, Person, self._db.add_person, self._db.commit_person) def _add_family_with_sources(self, citations): return self._add_object_with_source(citations, Family, self._db.add_family, self._db.commit_family) def _add_event_with_sources(self, citations): return self._add_object_with_source(citations, Event, self._db.add_event, self._db.commit_event) def _add_place_with_sources(self, citations): return self._add_object_with_source(citations, Place, self._db.add_place, self._db.commit_place) def _add_media_object_with_sources(self, citations): return self._add_object_with_source(citations, MediaObject, self._db.add_object, self._db.commit_media_object)
def print_db_content(db): for h in db.get_person_handles(): print("DB contains: person %s" % h) for h in db.get_source_handles(): print("DB contains: source %s" % h) tmpdir = tempfile.mkdtemp() try: filename1 = os.path.join(tmpdir,'test1.grdb') filename2 = os.path.join(tmpdir,'test2.grdb') print("\nUsing Database file: %s" % filename1) dbstate = DbState() dbman = CLIDbManager(dbstate) dirpath, name = dbman.create_new_db_cli(filename1, dbid="bsddb") db = dbstate.make_database("bsddb") db.load(dirpath, None) print("Add person 1") add_person( db,"Anton", "Albers",True,False) print("Add source") add_source( db,"A short test",True,False) print("Add person 2 without commit") add_person( db,"Bernd","Beta",False,False) print("Add source") add_source( db,"A short test",True,False) print("Add person 3") add_person( db,"Chris","Connor",True,False) print_db_content( db) print("Closing Database file: %s" % filename1) db.close() tran = None