Tidy up mocking code (#438)[First commit]

This commit is contained in:
Nick Hall 2017-09-17 11:03:39 +10:00 committed by Sam Manzi
parent c5f2717623
commit 8ebb4d35fa
3 changed files with 14 additions and 17 deletions

View File

@ -29,7 +29,7 @@ Make an 'Unknown' primary object
# Python modules # Python modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from time import strftime, localtime, time from time import strftime, time
import os import os
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -147,7 +147,7 @@ def make_unknown(class_arg, explanation, class_func, commit_func, transaction,
if not hasattr(make_unknown, 'count'): if not hasattr(make_unknown, 'count'):
make_unknown.count = 1 #primitive static variable make_unknown.count = 1 #primitive static variable
obj.set_name(_("Unknown, was missing %(time)s (%(count)d)") % { obj.set_name(_("Unknown, was missing %(time)s (%(count)d)") % {
'time': strftime('%x %X', localtime()), 'time': strftime('%x %X'),
'count': make_unknown.count}) 'count': make_unknown.count})
make_unknown.count += 1 make_unknown.count += 1
else: else:
@ -167,7 +167,7 @@ def create_explanation_note(dbase):
""" """
note = Note( _('Objects referenced by this note ' note = Note( _('Objects referenced by this note '
'were missing in a file imported on %s.') % 'were missing in a file imported on %s.') %
strftime('%x %X', localtime())) strftime('%x %X'))
note.set_handle(create_id()) note.set_handle(create_id())
note.set_gramps_id(dbase.find_next_note_gramps_id()) note.set_gramps_id(dbase.find_next_note_gramps_id())
# Use defaults for privacy, format and type. # Use defaults for privacy, format and type.

View File

@ -213,7 +213,7 @@ class ProgenOptions(ManagedWindow):
# initial values # initial values
text = "Pro-Gen Import" text = "Pro-Gen Import"
fname = os.path.basename(filename).split('\\')[-1] fname = os.path.basename(filename).split('\\')[-1]
date = time.strftime('%Y-%m-%d', time.localtime()) date = time.strftime("%Y-%m-%d")
# add import source title/confidence # add import source title/confidence
# citation page/confidence/privacy/attribute # citation page/confidence/privacy/attribute

View File

@ -26,7 +26,7 @@ import os
import sys import sys
import re import re
import locale import locale
from time import localtime, strptime from time import strptime, strftime
from unittest.mock import patch from unittest.mock import patch
#import logging #import logging
@ -65,6 +65,12 @@ def mock_localtime(*args):
""" """
return strptime("25 Dec 1999", "%d %b %Y") return strptime("25 Dec 1999", "%d %b %Y")
def mock_strftime(*args):
"""
Mock up a dummy to replace the varying 'time string results'
"""
return strftime(args[0], (1999, 12, 25, 0, 0, 0, 5, 359, -1))
class TestImports(unittest.TestCase): class TestImports(unittest.TestCase):
"""The test class cases will be dynamically created at import time from """The test class cases will be dynamically created at import time from
files to be tested. The following defs are used by the test cases files to be tested. The following defs are used by the test cases
@ -193,21 +199,12 @@ def make_tst_function(tstfile, file_name):
""" This is here to support the dynamic function creation. This creates """ This is here to support the dynamic function creation. This creates
the test function (a method, to be precise). the test function (a method, to be precise).
""" """
@patch('gramps.gen.utils.unknown.strftime', side_effect=mock_strftime)
@patch('gramps.plugins.db.dbapi.dbapi.time') @patch('time.strftime', side_effect=mock_strftime)
@patch('gramps.plugins.db.bsddb.write.time') def tst(self, mstrftime1, mstrftime2):
@patch('gramps.gen.utils.unknown.localtime')
@patch('gramps.gen.utils.unknown.time')
@patch('time.localtime')
def tst(self, mockptime, mocktime, mockltime, mockwtime, mockdtime):
""" This compares the import file with the expected result '.gramps' """ This compares the import file with the expected result '.gramps'
file. file.
""" """
mockptime.side_effect = mock_localtime
mocktime.side_effect = mock_time
mockltime.side_effect = mock_localtime
mockwtime.side_effect = mock_time
mockdtime.side_effect = mock_time
fn1 = os.path.join(TEST_DIR, tstfile) fn1 = os.path.join(TEST_DIR, tstfile)
fn2 = os.path.join(TEST_DIR, (file_name + ".gramps")) fn2 = os.path.join(TEST_DIR, (file_name + ".gramps"))
fres = os.path.join(TEMP_DIR, (file_name + ".difs")) fres = os.path.join(TEMP_DIR, (file_name + ".difs"))