If the CREATE TABLE statement fails, the setup would fail
without committing or rolling back the active transaction, so the
transaction would hold onto its resources indefinitely.
Normally, the transaction would be closed when the connection is closed
in the `tearDown` function. However, `tearDown` is not called if there
was an error during `setUp` ([as specified by the `unittest` docs](https://docs.python.org/3/library/unittest.html#unittest.TestCase.tearDown)), so
we need to handle this case specially.
Previously, this test had a bug, because if the CREATE TABLE statement
failed, the setup would fail without committing or rolling back the
active transaction.
Make sure the object is assigned only to one variable at time, across
obj, curs, rv (we create it on obj, pass it to curs when we know the
type is right, pass it to rv when we know there was no error).
This follows the semantics of Python file objects. When the object is
garbage collected and it has not been closed, emit a ResourceWarning.
This allows library users to notice inconsistent and non-deterministic
resource management and fix it. Users should use context managers and
finally blocks to always close cursors and connections when they are not
longer required.
For example, in Python, not closing a file results in the following:
$ python3 -Walways
>>> f = open('foo', 'w')
>>> del f
__main__:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='foo' mode='w' encoding='UTF-8'>
ResourceWarning: Enable tracemalloc to get the object allocation traceback
psycopg now acts the same way:
$ python3 -Walways
>>> import psycopg2
>>> c = psycopg2.connect(database='psycopg2_test')
>>> del c
<stdin>:1: ResourceWarning: unclosed connection <connection object at 0x7fc4aec38d50; dsn: 'dbname=psycopg2_test', closed: 0>
ResourceWarning: Enable tracemalloc to get the object allocation traceback
All warnings noticed during testing has been fixed.