2010-02-13 01:34:53 +03:00
|
|
|
#!/usr/bin/env python
|
2011-01-07 04:44:19 +03:00
|
|
|
|
|
|
|
# test_lobject.py - unit test for large objects support
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008-2011 James Henstridge <james@jamesh.id.au>
|
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2013-03-20 21:17:10 +04:00
|
|
|
from functools import wraps
|
2008-05-05 11:33:44 +04:00
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
import psycopg2.extensions
|
2012-01-14 21:27:43 +04:00
|
|
|
from testutils import unittest, decorate_all_tests, skip_if_tpc_disabled
|
2013-04-07 03:23:30 +04:00
|
|
|
from testutils import ConnectingTestCase, skip_if_green
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
def skip_if_no_lo(f):
|
2013-03-20 21:17:10 +04:00
|
|
|
@wraps(f)
|
2010-11-19 06:55:37 +03:00
|
|
|
def skip_if_no_lo_(self):
|
|
|
|
if self.conn.server_version < 80100:
|
|
|
|
return self.skipTest("large objects only supported from PG 8.1")
|
|
|
|
else:
|
|
|
|
return f(self)
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
return skip_if_no_lo_
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2013-03-20 21:17:10 +04:00
|
|
|
skip_lo_if_green = skip_if_green("libpq doesn't support LO in async mode")
|
2010-11-19 06:55:37 +03:00
|
|
|
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class LargeObjectTestCase(ConnectingTestCase):
|
2008-05-05 11:33:44 +04:00
|
|
|
def setUp(self):
|
2013-04-07 03:23:30 +04:00
|
|
|
ConnectingTestCase.setUp(self)
|
2008-05-05 11:33:44 +04:00
|
|
|
self.lo_oid = None
|
2008-05-06 13:04:26 +04:00
|
|
|
self.tmpdir = None
|
2008-05-05 11:33:44 +04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2008-05-06 13:04:26 +04:00
|
|
|
if self.tmpdir:
|
|
|
|
shutil.rmtree(self.tmpdir, ignore_errors=True)
|
2011-01-08 12:54:18 +03:00
|
|
|
|
|
|
|
if self.conn.closed:
|
|
|
|
return
|
|
|
|
|
2008-05-05 11:33:44 +04:00
|
|
|
if self.lo_oid is not None:
|
2008-05-06 13:04:26 +04:00
|
|
|
self.conn.rollback()
|
|
|
|
try:
|
|
|
|
lo = self.conn.lobject(self.lo_oid, "n")
|
|
|
|
except psycopg2.OperationalError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
lo.unlink()
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
ConnectingTestCase.tearDown(self)
|
2011-01-10 03:20:20 +03:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class LargeObjectTests(LargeObjectTestCase):
|
2008-05-05 11:33:44 +04:00
|
|
|
def test_create(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.assertNotEqual(lo, None)
|
2011-01-09 04:53:58 +03:00
|
|
|
self.assertEqual(lo.mode[0], "w")
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2013-11-27 16:42:57 +04:00
|
|
|
def test_connection_needed(self):
|
|
|
|
self.assertRaises(TypeError,
|
|
|
|
psycopg2.extensions.lobject, [])
|
|
|
|
|
2008-05-05 11:33:44 +04:00
|
|
|
def test_open_non_existent(self):
|
2008-05-06 13:04:26 +04:00
|
|
|
# By creating then removing a large object, we get an Oid that
|
|
|
|
# should be unused.
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.unlink()
|
|
|
|
self.assertRaises(psycopg2.OperationalError, self.conn.lobject, lo.oid)
|
2008-05-05 11:33:44 +04:00
|
|
|
|
|
|
|
def test_open_existing(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo2 = self.conn.lobject(lo.oid)
|
|
|
|
self.assertNotEqual(lo2, None)
|
|
|
|
self.assertEqual(lo2.oid, lo.oid)
|
2011-01-09 04:53:58 +03:00
|
|
|
self.assertEqual(lo2.mode[0], "r")
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
def test_open_for_write(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo2 = self.conn.lobject(lo.oid, "w")
|
2011-01-09 04:53:58 +03:00
|
|
|
self.assertEqual(lo2.mode[0], "w")
|
2016-08-15 03:55:57 +03:00
|
|
|
lo2.write(b"some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
|
|
|
|
def test_open_mode_n(self):
|
|
|
|
# Openning an object in mode "n" gives us a closed lobject.
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
|
|
|
|
|
|
|
lo2 = self.conn.lobject(lo.oid, "n")
|
|
|
|
self.assertEqual(lo2.oid, lo.oid)
|
|
|
|
self.assertEqual(lo2.closed, True)
|
|
|
|
|
2011-01-08 12:54:18 +03:00
|
|
|
def test_close_connection_gone(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.conn.close()
|
|
|
|
lo.close()
|
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
def test_create_with_oid(self):
|
|
|
|
# Create and delete a large object to get an unused Oid.
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
oid = lo.oid
|
|
|
|
lo.unlink()
|
|
|
|
|
|
|
|
lo = self.conn.lobject(0, "w", oid)
|
|
|
|
self.assertEqual(lo.oid, oid)
|
|
|
|
|
|
|
|
def test_create_with_existing_oid(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.OperationalError,
|
|
|
|
self.conn.lobject, 0, "w", lo.oid)
|
2014-04-05 18:14:00 +04:00
|
|
|
self.assert_(not self.conn.closed)
|
2008-05-06 13:04:26 +04:00
|
|
|
|
|
|
|
def test_import(self):
|
|
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(self.tmpdir, "data.txt")
|
|
|
|
fp = open(filename, "wb")
|
2016-08-15 03:55:57 +03:00
|
|
|
fp.write(b"some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
fp.close()
|
|
|
|
|
|
|
|
lo = self.conn.lobject(0, "r", 0, filename)
|
2011-01-11 01:05:47 +03:00
|
|
|
self.assertEqual(lo.read(), "some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
|
2008-05-05 11:33:44 +04:00
|
|
|
def test_close(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.assertEqual(lo.closed, False)
|
|
|
|
lo.close()
|
|
|
|
self.assertEqual(lo.closed, True)
|
|
|
|
|
|
|
|
def test_write(self):
|
|
|
|
lo = self.conn.lobject()
|
2016-08-15 03:55:57 +03:00
|
|
|
self.assertEqual(lo.write(b"some data"), len("some data"))
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2009-08-09 19:05:16 +04:00
|
|
|
def test_write_large(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
data = "data" * 1000000
|
|
|
|
self.assertEqual(lo.write(data), len(data))
|
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
def test_read(self):
|
|
|
|
lo = self.conn.lobject()
|
2016-10-11 02:10:53 +03:00
|
|
|
lo.write(b"some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
lo.close()
|
|
|
|
|
|
|
|
lo = self.conn.lobject(lo.oid)
|
2011-01-09 02:47:24 +03:00
|
|
|
x = lo.read(4)
|
|
|
|
self.assertEqual(type(x), type(''))
|
|
|
|
self.assertEqual(x, "some")
|
|
|
|
self.assertEqual(lo.read(), " data")
|
|
|
|
|
|
|
|
def test_read_binary(self):
|
|
|
|
lo = self.conn.lobject()
|
2016-10-11 02:10:53 +03:00
|
|
|
lo.write(b"some data")
|
2011-01-09 02:47:24 +03:00
|
|
|
lo.close()
|
|
|
|
|
|
|
|
lo = self.conn.lobject(lo.oid, "rb")
|
|
|
|
x = lo.read(4)
|
2016-08-15 03:55:57 +03:00
|
|
|
self.assertEqual(type(x), type(b''))
|
|
|
|
self.assertEqual(x, b"some")
|
|
|
|
self.assertEqual(lo.read(), b" data")
|
2008-05-06 13:04:26 +04:00
|
|
|
|
2011-01-09 02:47:24 +03:00
|
|
|
def test_read_text(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
snowman = u"\u2603"
|
2016-10-11 02:10:53 +03:00
|
|
|
lo.write(u"some data " + snowman)
|
2011-01-09 02:47:24 +03:00
|
|
|
lo.close()
|
|
|
|
|
|
|
|
lo = self.conn.lobject(lo.oid, "rt")
|
|
|
|
x = lo.read(4)
|
|
|
|
self.assertEqual(type(x), type(u''))
|
|
|
|
self.assertEqual(x, u"some")
|
|
|
|
self.assertEqual(lo.read(), u" data " + snowman)
|
|
|
|
|
2009-08-09 19:05:16 +04:00
|
|
|
def test_read_large(self):
|
|
|
|
lo = self.conn.lobject()
|
2011-01-11 01:05:47 +03:00
|
|
|
data = "data" * 1000000
|
2016-10-11 02:10:53 +03:00
|
|
|
lo.write("some" + data)
|
2009-08-09 19:05:16 +04:00
|
|
|
lo.close()
|
|
|
|
|
|
|
|
lo = self.conn.lobject(lo.oid)
|
2011-01-11 01:05:47 +03:00
|
|
|
self.assertEqual(lo.read(4), "some")
|
|
|
|
data1 = lo.read()
|
|
|
|
# avoid dumping megacraps in the console in case of error
|
|
|
|
self.assert_(data == data1,
|
|
|
|
"%r... != %r..." % (data[:100], data1[:100]))
|
2009-08-09 19:05:16 +04:00
|
|
|
|
2008-05-05 11:33:44 +04:00
|
|
|
def test_seek_tell(self):
|
|
|
|
lo = self.conn.lobject()
|
2016-08-15 03:55:57 +03:00
|
|
|
length = lo.write(b"some data")
|
2008-05-05 11:33:44 +04:00
|
|
|
self.assertEqual(lo.tell(), length)
|
|
|
|
lo.close()
|
|
|
|
lo = self.conn.lobject(lo.oid)
|
|
|
|
|
|
|
|
self.assertEqual(lo.seek(5, 0), 5)
|
|
|
|
self.assertEqual(lo.tell(), 5)
|
2011-01-11 01:05:47 +03:00
|
|
|
self.assertEqual(lo.read(), "data")
|
2008-05-05 11:33:44 +04:00
|
|
|
|
|
|
|
# SEEK_CUR: relative current location
|
2008-05-06 13:04:26 +04:00
|
|
|
lo.seek(5)
|
2008-05-05 11:33:44 +04:00
|
|
|
self.assertEqual(lo.seek(2, 1), 7)
|
|
|
|
self.assertEqual(lo.tell(), 7)
|
2011-01-11 01:05:47 +03:00
|
|
|
self.assertEqual(lo.read(), "ta")
|
2008-05-05 11:33:44 +04:00
|
|
|
|
|
|
|
# SEEK_END: relative to end of file
|
|
|
|
self.assertEqual(lo.seek(-2, 2), length - 2)
|
2011-01-11 01:05:47 +03:00
|
|
|
self.assertEqual(lo.read(), "ta")
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
def test_unlink(self):
|
2008-05-05 11:33:44 +04:00
|
|
|
lo = self.conn.lobject()
|
2008-05-06 13:04:26 +04:00
|
|
|
lo.unlink()
|
|
|
|
|
|
|
|
# the object doesn't exist now, so we can't reopen it.
|
|
|
|
self.assertRaises(psycopg2.OperationalError, self.conn.lobject, lo.oid)
|
|
|
|
# And the object has been closed.
|
|
|
|
self.assertEquals(lo.closed, True)
|
|
|
|
|
|
|
|
def test_export(self):
|
|
|
|
lo = self.conn.lobject()
|
2016-08-15 03:55:57 +03:00
|
|
|
lo.write(b"some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
|
|
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(self.tmpdir, "data.txt")
|
|
|
|
lo.export(filename)
|
|
|
|
self.assertTrue(os.path.exists(filename))
|
2011-01-08 03:40:27 +03:00
|
|
|
f = open(filename, "rb")
|
|
|
|
try:
|
2016-08-15 03:55:57 +03:00
|
|
|
self.assertEqual(f.read(), b"some data")
|
2011-01-08 03:40:27 +03:00
|
|
|
finally:
|
|
|
|
f.close()
|
2008-05-06 13:04:26 +04:00
|
|
|
|
|
|
|
def test_close_twice(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
2008-05-05 11:33:44 +04:00
|
|
|
lo.close()
|
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
def test_write_after_close(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
2016-08-15 03:55:57 +03:00
|
|
|
self.assertRaises(psycopg2.InterfaceError, lo.write, b"some data")
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
def test_read_after_close(self):
|
2008-05-05 11:33:44 +04:00
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
2008-05-06 13:04:26 +04:00
|
|
|
self.assertRaises(psycopg2.InterfaceError, lo.read, 5)
|
|
|
|
|
|
|
|
def test_seek_after_close(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
|
|
|
self.assertRaises(psycopg2.InterfaceError, lo.seek, 0)
|
|
|
|
|
|
|
|
def test_tell_after_close(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
|
|
|
self.assertRaises(psycopg2.InterfaceError, lo.tell)
|
|
|
|
|
|
|
|
def test_unlink_after_close(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
|
|
|
# Unlink works on closed files.
|
2008-05-05 11:33:44 +04:00
|
|
|
lo.unlink()
|
|
|
|
|
2008-05-06 13:04:26 +04:00
|
|
|
def test_export_after_close(self):
|
|
|
|
lo = self.conn.lobject()
|
2016-08-15 03:55:57 +03:00
|
|
|
lo.write(b"some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
lo.close()
|
|
|
|
|
|
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(self.tmpdir, "data.txt")
|
|
|
|
lo.export(filename)
|
|
|
|
self.assertTrue(os.path.exists(filename))
|
2011-01-08 03:40:27 +03:00
|
|
|
f = open(filename, "rb")
|
|
|
|
try:
|
2016-08-15 03:55:57 +03:00
|
|
|
self.assertEqual(f.read(), b"some data")
|
2011-01-08 03:40:27 +03:00
|
|
|
finally:
|
|
|
|
f.close()
|
2008-05-06 13:04:26 +04:00
|
|
|
|
|
|
|
def test_close_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
# Closing outside of the transaction is okay.
|
|
|
|
lo.close()
|
|
|
|
|
|
|
|
def test_write_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.commit()
|
|
|
|
|
2016-08-15 03:55:57 +03:00
|
|
|
self.assertRaises(psycopg2.ProgrammingError, lo.write, b"some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
|
|
|
|
def test_read_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, lo.read, 5)
|
|
|
|
|
|
|
|
def test_seek_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, lo.seek, 0)
|
|
|
|
|
|
|
|
def test_tell_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, lo.tell)
|
|
|
|
|
|
|
|
def test_unlink_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
# Unlink of stale lobject is okay
|
|
|
|
lo.unlink()
|
|
|
|
|
|
|
|
def test_export_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
2016-08-15 03:55:57 +03:00
|
|
|
lo.write(b"some data")
|
2008-05-06 13:04:26 +04:00
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
|
|
filename = os.path.join(self.tmpdir, "data.txt")
|
|
|
|
lo.export(filename)
|
|
|
|
self.assertTrue(os.path.exists(filename))
|
2011-01-08 03:40:27 +03:00
|
|
|
f = open(filename, "rb")
|
|
|
|
try:
|
2016-08-15 03:55:57 +03:00
|
|
|
self.assertEqual(f.read(), b"some data")
|
2011-01-08 03:40:27 +03:00
|
|
|
finally:
|
|
|
|
f.close()
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2011-01-10 03:20:20 +03:00
|
|
|
@skip_if_tpc_disabled
|
|
|
|
def test_read_after_tpc_commit(self):
|
|
|
|
self.conn.tpc_begin('test_lobject')
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.tpc_commit()
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, lo.read, 5)
|
|
|
|
|
|
|
|
@skip_if_tpc_disabled
|
|
|
|
def test_read_after_tpc_prepare(self):
|
|
|
|
self.conn.tpc_begin('test_lobject')
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.tpc_prepare()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, lo.read, 5)
|
|
|
|
finally:
|
|
|
|
self.conn.tpc_commit()
|
|
|
|
|
2014-04-03 05:34:43 +04:00
|
|
|
def test_large_oid(self):
|
|
|
|
# Test we don't overflow with an oid not fitting a signed int
|
|
|
|
try:
|
|
|
|
self.conn.lobject(0xFFFFFFFE)
|
|
|
|
except psycopg2.OperationalError:
|
|
|
|
pass
|
2011-01-10 03:20:20 +03:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
decorate_all_tests(LargeObjectTests, skip_if_no_lo, skip_lo_if_green)
|
2008-05-05 11:33:44 +04:00
|
|
|
|
2010-03-29 02:42:56 +04:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
def skip_if_no_truncate(f):
|
2013-03-20 21:17:10 +04:00
|
|
|
@wraps(f)
|
2010-11-19 06:55:37 +03:00
|
|
|
def skip_if_no_truncate_(self):
|
|
|
|
if self.conn.server_version < 80300:
|
|
|
|
return self.skipTest(
|
|
|
|
"the server doesn't support large object truncate")
|
2010-03-29 02:42:56 +04:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
if not hasattr(psycopg2.extensions.lobject, 'truncate'):
|
|
|
|
return self.skipTest(
|
|
|
|
"psycopg2 has been built against a libpq "
|
|
|
|
"without large object truncate support.")
|
2010-03-29 02:42:56 +04:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
return f(self)
|
2010-03-29 02:42:56 +04:00
|
|
|
|
2013-03-20 21:13:33 +04:00
|
|
|
return skip_if_no_truncate_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class LargeObjectTruncateTests(LargeObjectTestCase):
|
2010-03-29 02:42:56 +04:00
|
|
|
def test_truncate(self):
|
|
|
|
lo = self.conn.lobject()
|
2013-03-20 21:13:33 +04:00
|
|
|
lo.write("some data")
|
2010-03-29 02:42:56 +04:00
|
|
|
lo.close()
|
|
|
|
|
|
|
|
lo = self.conn.lobject(lo.oid, "w")
|
|
|
|
lo.truncate(4)
|
|
|
|
|
|
|
|
# seek position unchanged
|
|
|
|
self.assertEqual(lo.tell(), 0)
|
|
|
|
# data truncated
|
2013-03-20 21:13:33 +04:00
|
|
|
self.assertEqual(lo.read(), "some")
|
2010-03-29 02:42:56 +04:00
|
|
|
|
|
|
|
lo.truncate(6)
|
|
|
|
lo.seek(0)
|
|
|
|
# large object extended with zeroes
|
2013-03-20 21:13:33 +04:00
|
|
|
self.assertEqual(lo.read(), "some\x00\x00")
|
2010-03-29 02:42:56 +04:00
|
|
|
|
|
|
|
lo.truncate()
|
|
|
|
lo.seek(0)
|
|
|
|
# large object empty
|
2013-03-20 21:13:33 +04:00
|
|
|
self.assertEqual(lo.read(), "")
|
2010-03-29 02:42:56 +04:00
|
|
|
|
|
|
|
def test_truncate_after_close(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
lo.close()
|
|
|
|
self.assertRaises(psycopg2.InterfaceError, lo.truncate)
|
|
|
|
|
|
|
|
def test_truncate_after_commit(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
self.lo_oid = lo.oid
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, lo.truncate)
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
decorate_all_tests(LargeObjectTruncateTests,
|
|
|
|
skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate)
|
2010-11-19 06:55:37 +03:00
|
|
|
|
|
|
|
|
2014-09-11 04:38:45 +04:00
|
|
|
def _has_lo64(conn):
|
|
|
|
"""Return (bool, msg) about the lo64 support"""
|
|
|
|
if conn.server_version < 90300:
|
2014-09-11 13:14:22 +04:00
|
|
|
return (False, "server version %s doesn't support the lo64 API"
|
2014-09-11 04:38:45 +04:00
|
|
|
% conn.server_version)
|
|
|
|
|
|
|
|
if 'lo64' not in psycopg2.__version__:
|
|
|
|
return (False, "this psycopg build doesn't support the lo64 API")
|
|
|
|
|
|
|
|
return (True, "this server and build support the lo64 API")
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2014-09-08 20:05:28 +04:00
|
|
|
def skip_if_no_lo64(f):
|
|
|
|
@wraps(f)
|
|
|
|
def skip_if_no_lo64_(self):
|
2014-09-11 04:38:45 +04:00
|
|
|
lo64, msg = _has_lo64(self.conn)
|
2016-10-11 02:10:53 +03:00
|
|
|
if not lo64:
|
|
|
|
return self.skipTest(msg)
|
|
|
|
else:
|
|
|
|
return f(self)
|
2014-09-08 20:05:28 +04:00
|
|
|
|
|
|
|
return skip_if_no_lo64_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2014-09-08 20:05:28 +04:00
|
|
|
class LargeObject64Tests(LargeObjectTestCase):
|
|
|
|
def test_seek_tell_truncate_greater_than_2gb(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
|
|
|
|
length = (1 << 31) + (1 << 30) # 2gb + 1gb = 3gb
|
|
|
|
lo.truncate(length)
|
|
|
|
|
|
|
|
self.assertEqual(lo.seek(length, 0), length)
|
|
|
|
self.assertEqual(lo.tell(), length)
|
|
|
|
|
|
|
|
decorate_all_tests(LargeObject64Tests,
|
|
|
|
skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate, skip_if_no_lo64)
|
|
|
|
|
|
|
|
|
|
|
|
def skip_if_lo64(f):
|
|
|
|
@wraps(f)
|
|
|
|
def skip_if_lo64_(self):
|
2014-09-11 04:38:45 +04:00
|
|
|
lo64, msg = _has_lo64(self.conn)
|
2016-10-11 02:10:53 +03:00
|
|
|
if lo64:
|
|
|
|
return self.skipTest(msg)
|
|
|
|
else:
|
|
|
|
return f(self)
|
2014-09-08 20:05:28 +04:00
|
|
|
|
|
|
|
return skip_if_lo64_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2014-09-08 20:05:28 +04:00
|
|
|
class LargeObjectNot64Tests(LargeObjectTestCase):
|
|
|
|
def test_seek_larger_than_2gb(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
offset = 1 << 32 # 4gb
|
2014-09-11 04:38:45 +04:00
|
|
|
self.assertRaises(
|
2015-02-08 05:04:41 +03:00
|
|
|
(OverflowError, psycopg2.InterfaceError, psycopg2.NotSupportedError),
|
2014-09-11 04:38:45 +04:00
|
|
|
lo.seek, offset, 0)
|
2014-09-08 20:05:28 +04:00
|
|
|
|
|
|
|
def test_truncate_larger_than_2gb(self):
|
|
|
|
lo = self.conn.lobject()
|
|
|
|
length = 1 << 32 # 4gb
|
2014-09-11 04:38:45 +04:00
|
|
|
self.assertRaises(
|
2015-02-08 05:04:41 +03:00
|
|
|
(OverflowError, psycopg2.InterfaceError, psycopg2.NotSupportedError),
|
2014-09-11 04:38:45 +04:00
|
|
|
lo.truncate, length)
|
2014-09-08 20:05:28 +04:00
|
|
|
|
|
|
|
decorate_all_tests(LargeObjectNot64Tests,
|
|
|
|
skip_if_no_lo, skip_lo_if_green, skip_if_no_truncate, skip_if_lo64)
|
|
|
|
|
|
|
|
|
2008-05-05 11:33:44 +04:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
2010-02-13 01:34:53 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-03-29 02:42:56 +04:00
|
|
|
unittest.main()
|