Skip to content

Commit

Permalink
Refined and filled out tests a little more
Browse files Browse the repository at this point in the history
  • Loading branch information
scnerd committed Apr 5, 2018
1 parent c2b117c commit 687c3f3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion miniutils/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ def __setitem__(self, key, value):
if not self.settable:
raise AttributeError("{} is not settable".format(self))
self._known[key] = value
self._on_modified()
if key in self._cache and self._cache[key] is not value:
self._on_modified()

def __delitem__(self, key):
if key in self._known:
Expand Down
2 changes: 1 addition & 1 deletion miniutils/py2_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __call__(self, func):
function_code = ''
function_name = func
else:
raise AttributeError("MakePython2 must be given either a function or an expression string to execute")
raise TypeError("MakePython2 must be given either a function or an expression string to execute")

self.proc = sp.Popen([self.python2_path, MakePython2.template], executable=self.python2_path,
stdin=sp.PIPE, stdout=sp.PIPE)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ def test_cached_dict(self):
w.f[2] = 7
self.assertEqual(w.b, 8)
self.assertEqual(w.a, 10)
del w.f[2]
self.assertEqual(w.b, 5)
self.assertEqual(w.a, 7)
w.f.update({1: 0, 2: 0})
self.assertEqual(w.b, 0)
self.assertEqual(w.a, 2)
Expand All @@ -304,7 +307,6 @@ def test_cached_dict(self):
except AttributeError:
pass

self.assertListEqual(w.calls, ['a', 'b', 'f(1)', 'f(2)', 'b', 'f(5)', 'f(4)', 'f(5)', 'f(2)', 'a', 'b', 'f(1)',
'b', 'a', 'b', 'a', 'g(3)'])
self.assertListEqual(w.calls, 'a b f(1) f(2) b f(5) f(4) f(5) f(2) a b f(1) b a b f(2) a b a g(3)'.split())

self.assertIn('G docstring', w.g.__doc__)
14 changes: 10 additions & 4 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def test_log_dir(self):
self.assertIn('TEST', log_files)

def test_log_dir_not_exists(self):
from miniutils.logs import enable_logging
from miniutils.logs import enable_logging, disable_logging

dir_path = '__test_logs'
dir_path = '.test_logs'
assert not os.path.exists(dir_path)

try:
Expand All @@ -58,7 +58,13 @@ def test_log_dir_not_exists(self):

log.critical('TEST')

for handler in log.handlers:
import logging
if isinstance(handler, logging.FileHandler):
handler.close()
log.removeHandler(handler)
del log
disable_logging()

log_files = [os.path.join(dir_path, f) for f in os.listdir(dir_path)]
log_files = [f for f in log_files if os.path.isfile(f)]
Expand All @@ -68,5 +74,5 @@ def test_log_dir_not_exists(self):
except Exception:
raise
finally:
if os.path.exists(dir_path):
shutil.rmtree(dir_path, ignore_errors=True)
print("DELETING TEMP LOG DIR '{}'".format(dir_path), file=sys.__stderr__)
shutil.rmtree(dir_path)
6 changes: 6 additions & 0 deletions tests/test_make_python2.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def fail():
return True

self.assertRaises(ValueError, MakePython2, fail, imports=[('os', 'path', 'exists')])
self.assertRaises(ValueError, MakePython2, fail, imports={1: 2})
self.assertRaises(ValueError, MakePython2, fail, imports={'123invalid_name': 5})
self.assertRaises(ValueError, MakePython2, fail, imports={'invalid name': 5})
self.assertRaises(ValueError, MakePython2, fail, global_values={1: 2})
self.assertRaises(ValueError, MakePython2, fail, global_values={'123invalid_name': 5})
self.assertRaises(ValueError, MakePython2, fail, global_values={'invalid name': 5})

def test_bad_func(self):
self.assertRaises(TypeError, MakePython2, object())

0 comments on commit 687c3f3

Please sign in to comment.