Browse Source

Full coverage of nilmdb.utils.mustclose

tags/nilmdb-0.1
Jim Paris 10 years ago
parent
commit
b09362fde1
2 changed files with 44 additions and 4 deletions
  1. +1
    -1
      tests/test_interval.py
  2. +43
    -3
      tests/test_mustclose.py

+ 1
- 1
tests/test_interval.py View File

@@ -341,7 +341,7 @@ class TestIntervalTree:
render(iset, "In-order insertion")

class TestIntervalSpeed:
#@unittest.skip("this is slow")
@unittest.skip("this is slow")
def test_interval_speed(self):
import yappi
import time


+ 43
- 3
tests/test_mustclose.py View File

@@ -9,6 +9,7 @@ from testutil.helpers import *

import sys
import cStringIO
import gc

err = cStringIO.StringIO()

@@ -23,8 +24,22 @@ class Foo:
def close(self):
fprintf(err, "Closing\n")

@nilmdb.utils.must_close(errorfile = err)
@nilmdb.utils.must_close(errorfile = err, wrap_verify = True)
class Bar:
def __init__(self):
fprintf(err, "Init\n")

def __del__(self):
fprintf(err, "Deleting\n")

def close(self):
fprintf(err, "Closing\n")

def blah(self):
fprintf(err, "Blah\n")

@nilmdb.utils.must_close(errorfile = err)
class Baz:
pass

class TestMustClose(object):
@@ -34,26 +49,51 @@ class TestMustClose(object):
# garbage collect the object (and call its __del__ function)
# right after a "del x".

# Trigger error
err.truncate()
x = Foo()
del x
gc.collect()
eq_(err.getvalue(),
"Init\n"
"error: Foo.close() wasn't called!\n"
"Deleting\n")

# No error
err.truncate(0)

y = Foo()
y.close()
del y
gc.collect()
eq_(err.getvalue(),
"Init\n"
"Closing\n"
"Deleting\n")

# Verify errors
err.truncate(0)

z = Bar()
z.blah()
z.close()
with assert_raises(AssertionError):
z.blah()
# Since the most recent assertion references 'z',
# we need to raise another assertion here so that
# 'z' will get properly deleted.
with assert_raises(AssertionError):
raise AssertionError()
del z
gc.collect()
eq_(err.getvalue(),
"Init\n"
"Blah\n"
"Closing\n"
"Deleting\n")

# Class with missing methods
err.truncate(0)
w = Baz()
w.close()
del w
eq_(err.getvalue(), "")


Loading…
Cancel
Save