Bring back the separate web-side entity cache
Prior to changeset 635cfac73d28 "[repoapi] fold ClientConnection into
Connection", we had two entity caches: one on the client/dbapi/web side,
and one on the server/repo side. This is a waste, but it is actually
needed as long as we have the magic _cw attribute on entities which must
sometimes be a request and sometimes a cnx. Removing the duplication
caused weird problems with entity._cw alternating between both types of
objects, which is unexpected by both the repo and the web sides.
We add an entity cache on ConnectionCubicWebRequestBase, separate from
the Connection's, and bring back the
_cw_update_attr_cache/dont-cache-attrs mechanism, to let the
server/edition code let caches know which attributes have been modified
Entity.as_rset can be cached again, as ResultSet no longer modifies the
entity when fetching it from a cache.
Contrary to the pre-3.21 code, _cw_update_attr_cache now handles web
requests and connections in the same way (otherwise the cache ends up
with wrong values if a hook modifies attributes), but dont-cache-attrs
is never set for (inlined) relations.
Closes #6863543
from six import PY2
from unittest import TestCase
from tempfile import NamedTemporaryFile
import os.path as osp
from logilab.common.shellutils import tempdir
from cubicweb import Binary
class BinaryTC(TestCase):
def test_init(self):
Binary()
Binary(b'toto')
Binary(bytearray(b'toto'))
if PY2:
Binary(buffer('toto'))
else:
Binary(memoryview(b'toto'))
with self.assertRaises((AssertionError, TypeError)):
# TypeError is raised by BytesIO if python runs with -O
Binary(u'toto')
def test_write(self):
b = Binary()
b.write(b'toto')
b.write(bytearray(b'toto'))
if PY2:
b.write(buffer('toto'))
else:
b.write(memoryview(b'toto'))
with self.assertRaises((AssertionError, TypeError)):
# TypeError is raised by BytesIO if python runs with -O
b.write(u'toto')
def test_gzpickle_roundtrip(self):
old = (u'foo', b'bar', 42, {})
new = Binary.zpickle(old).unzpickle()
self.assertEqual(old, new)
self.assertIsNot(old, new)
def test_from_file_to_file(self):
with tempdir() as dpath:
fpath = osp.join(dpath, 'binary.bin')
with open(fpath, 'wb') as fobj:
Binary(b'binaryblob').to_file(fobj)
bobj = Binary.from_file(fpath)
self.assertEqual(bobj.getvalue(), b'binaryblob')
if __name__ == '__main__':
from unittest import main
main()