1# 2# Gramps - a GTK+/GNOME based genealogy program 3# 4# Copyright (C) 2013 Doug Blank <doug.blank@gmail.com> 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program; if not, write to the Free Software 18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19# 20 21""" Unittest for editreference.py """ 22 23import unittest 24import sys 25import os 26try: 27 if sys.version_info < (3,3): 28 from mock import Mock, patch 29 else: 30 from unittest.mock import Mock, patch 31 MOCKING = True 32except: 33 MOCKING = False 34 35from gramps.gen.lib import (Person, Family, Event, Source, Place, Citation, 36 Repository, Media, Note, Tag) 37from gramps.cli.user import User 38from gramps.gen.dbstate import DbState 39from gramps.gen.db.utils import make_database 40from gramps.gui.editors.editreference import EditReference 41 42class MockWindow: 43 def set_transient_for(self, *args, **kwargs): 44 pass 45 def show_all(self): 46 pass 47 48class MockEditReference(EditReference): 49 def __init__(self, dbstate, uistate, track, source, source_ref, update): 50 self.window = MockWindow() 51 super().__init__(dbstate, uistate, track, source, source_ref, update) 52 53 54class TestEditReference(unittest.TestCase): 55 56 @unittest.skipUnless(MOCKING, "Requires unittest.mock to run") 57 def test_editreference(self): 58 dbstate = DbState() 59 db = make_database("bsddb") 60 path = "/tmp/edit_ref_test" 61 try: 62 os.mkdir(path) 63 except: 64 pass 65 db.load(path) 66 dbstate.change_database(db) 67 source = Place() 68 source.gramps_id = "P0001" 69 dbstate.db.place_map[source.handle] = source.serialize() 70 editor = MockEditReference(dbstate, uistate=None, track=[], 71 source=source, source_ref=None, update=None) 72 with patch('gramps.gui.editors.editreference.ErrorDialog') as MockED: 73 editor.check_for_duplicate_id("Place") 74 self.assertTrue(MockED.called) 75 76if __name__ == "__main__": 77 unittest.main() 78