server/test/unittest_storage.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 08 Apr 2010 09:59:59 +0200
branchstable
changeset 5183 8d66003351f8
parent 5131 88b5ca8da928
child 5196 d14bfd477c44
child 5240 2af0427e8ead
permissions -rw-r--r--
[storage] consider fs_importing on update operations too
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""unit tests for module cubicweb.server.sources.storages
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
:copyright: 2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
"""
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
5109
5cf83b9356d5 [test] use the new temporary_appojects CM to simplfy unittest_storage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5013
diff changeset
     9
from __future__ import with_statement
5cf83b9356d5 [test] use the new temporary_appojects CM to simplfy unittest_storage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5013
diff changeset
    10
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
from logilab.common.testlib import unittest_main
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from cubicweb.devtools.testlib import CubicWebTC
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
import os.path as osp
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
import shutil
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
import tempfile
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
5013
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
    18
from cubicweb import Binary, QueryError
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
from cubicweb.selectors import implements
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
from cubicweb.server.sources import storages
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
from cubicweb.server.hook import Hook, Operation
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
class DummyBeforeHook(Hook):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    __regid__ = 'dummy-before-hook'
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
    __select__ = Hook.__select__ & implements('File')
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
    events = ('before_add_entity',)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
    def __call__(self):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
        self._cw.transaction_data['orig_file_value'] = self.entity.data.getvalue()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
class DummyAfterHook(Hook):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
    __regid__ = 'dummy-after-hook'
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
    __select__ = Hook.__select__ & implements('File')
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    events = ('after_add_entity',)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    def __call__(self):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        # new value of entity.data should be the same as before
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        oldvalue = self._cw.transaction_data['orig_file_value']
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        assert oldvalue == self.entity.data.getvalue()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
class StorageTC(CubicWebTC):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    def setup_database(self):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        self.tempdir = tempfile.mkdtemp()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        bfs_storage = storages.BytesFileSystemStorage(self.tempdir)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        storages.set_attribute_storage(self.repo, 'File', 'data', bfs_storage)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    def tearDown(self):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
        super(CubicWebTC, self).tearDown()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        storages.unset_attribute_storage(self.repo, 'File', 'data')
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        shutil.rmtree(self.tempdir)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    55
    def create_file(self, content='the-data'):
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        req = self.request()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        return req.create_entity('File', data=Binary(content),
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
                                 data_format=u'text/plain', data_name=u'foo')
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
5183
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
    60
    def fspath(self, entity):
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
    61
        fspath = self.execute('Any fspath(D) WHERE F eid %(f)s, F data D',
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
    62
                              {'f': entity.eid})[0][0]
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
    63
        return fspath.getvalue()
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
    64
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    65
    def test_bfss_storage(self):
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    66
        f1 = self.create_file()
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
        expected_filepath = osp.join(self.tempdir, '%s_data' % f1.eid)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        self.failUnless(osp.isfile(expected_filepath))
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        self.assertEquals(file(expected_filepath).read(), 'the-data')
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    70
        self.rollback()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    71
        self.failIf(osp.isfile(expected_filepath))
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    72
        f1 = self.create_file()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    73
        self.commit()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    74
        self.assertEquals(file(expected_filepath).read(), 'the-data')
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    75
        f1.set_attributes(data=Binary('the new data'))
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    76
        self.rollback()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    77
        self.assertEquals(file(expected_filepath).read(), 'the-data')
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    78
        f1.delete()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    79
        self.failUnless(osp.isfile(expected_filepath))
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    80
        self.rollback()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    81
        self.failUnless(osp.isfile(expected_filepath))
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    82
        f1.delete()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    83
        self.commit()
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    84
        self.failIf(osp.isfile(expected_filepath))
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    86
    def test_bfss_sqlite_fspath(self):
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    87
        f1 = self.create_file()
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
        expected_filepath = osp.join(self.tempdir, '%s_data' % f1.eid)
5183
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
    89
        self.assertEquals(self.fspath(f1), expected_filepath)
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    91
    def test_bfss_fs_importing_doesnt_touch_path(self):
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        self.session.transaction_data['fs_importing'] = True
5131
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
    93
        filepath = osp.abspath(__file__)
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
    94
        f1 = self.session.create_entity('File', data=Binary(filepath),
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
                                        data_format=u'text/plain', data_name=u'foo')
5183
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
    96
        self.assertEquals(self.fspath(f1), filepath)
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    98
    def test_source_storage_transparency(self):
5109
5cf83b9356d5 [test] use the new temporary_appojects CM to simplfy unittest_storage
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5013
diff changeset
    99
        with self.temporary_appobjects(DummyBeforeHook, DummyAfterHook):
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
   100
            self.create_file()
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
5013
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   102
    def test_source_mapped_attribute_error_cases(self):
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   103
        ex = self.assertRaises(QueryError, self.execute,
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   104
                               'Any X WHERE X data ~= "hop", X is File')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   105
        self.assertEquals(str(ex), 'can\'t use File.data (X data ILIKE "hop") in restriction')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   106
        ex = self.assertRaises(QueryError, self.execute,
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   107
                               'Any X, Y WHERE X data D, Y data D, '
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   108
                               'NOT X identity Y, X is File, Y is File')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   109
        self.assertEquals(str(ex), "can't use D as a restriction variable")
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   110
        # query returning mix of mapped / regular attributes (only file.data
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   111
        # mapped, not image.data for instance)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   112
        ex = self.assertRaises(QueryError, self.execute,
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   113
                               'Any X WITH X BEING ('
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   114
                               ' (Any NULL)'
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   115
                               '  UNION '
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   116
                               ' (Any D WHERE X data D, X is File)'
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   117
                               ')')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   118
        self.assertEquals(str(ex), 'query fetch some source mapped attribute, some not')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   119
        ex = self.assertRaises(QueryError, self.execute,
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   120
                               '(Any D WHERE X data D, X is File)'
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   121
                               ' UNION '
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   122
                               '(Any D WHERE X data D, X is Image)')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   123
        self.assertEquals(str(ex), 'query fetch some source mapped attribute, some not')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   124
        ex = self.assertRaises(QueryError,
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   125
                               self.execute, 'Any D WHERE X data D')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   126
        self.assertEquals(str(ex), 'query fetch some source mapped attribute, some not')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   127
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   128
    def test_source_mapped_attribute_advanced(self):
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   129
        f1 = self.create_file()
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   130
        rset = self.execute('Any X,D WITH D,X BEING ('
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   131
                            ' (Any D, X WHERE X eid %(x)s, X data D)'
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   132
                            '  UNION '
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   133
                            ' (Any D, X WHERE X eid %(x)s, X data D)'
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   134
                            ')', {'x': f1.eid}, 'x')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   135
        self.assertEquals(len(rset), 2)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   136
        self.assertEquals(rset[0][0], f1.eid)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   137
        self.assertEquals(rset[1][0], f1.eid)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   138
        self.assertEquals(rset[0][1].getvalue(), 'the-data')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   139
        self.assertEquals(rset[1][1].getvalue(), 'the-data')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   140
        rset = self.execute('Any X,LENGTH(D) WHERE X eid %(x)s, X data D',
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   141
                            {'x': f1.eid}, 'x')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   142
        self.assertEquals(len(rset), 1)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   143
        self.assertEquals(rset[0][0], f1.eid)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   144
        self.assertEquals(rset[0][1], len('the-data'))
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   145
        rset = self.execute('Any X,LENGTH(D) WITH D,X BEING ('
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   146
                            ' (Any D, X WHERE X eid %(x)s, X data D)'
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   147
                            '  UNION '
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   148
                            ' (Any D, X WHERE X eid %(x)s, X data D)'
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   149
                            ')', {'x': f1.eid}, 'x')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   150
        self.assertEquals(len(rset), 2)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   151
        self.assertEquals(rset[0][0], f1.eid)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   152
        self.assertEquals(rset[1][0], f1.eid)
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   153
        self.assertEquals(rset[0][1], len('the-data'))
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   154
        self.assertEquals(rset[1][1], len('the-data'))
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   155
        ex = self.assertRaises(QueryError, self.execute,
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   156
                               'Any X,UPPER(D) WHERE X eid %(x)s, X data D',
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   157
                               {'x': f1.eid}, 'x')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   158
        self.assertEquals(str(ex), 'UPPER can not be called on mapped attribute')
ad91f93bbb93 [source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5008
diff changeset
   159
5131
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   160
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   161
    def test_bfss_fs_importing_transparency(self):
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   162
        self.session.transaction_data['fs_importing'] = True
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   163
        filepath = osp.abspath(__file__)
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   164
        f1 = self.session.create_entity('File', data=Binary(filepath),
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   165
                                        data_format=u'text/plain', data_name=u'foo')
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   166
        self.assertEquals(f1.data.getvalue(), file(filepath).read(),
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   167
                          'files content differ')
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   168
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   169
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   170
    def test_bfss_update_with_existing_data(self):
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   171
        # use self.session to use server-side cache
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   172
        f1 = self.session.create_entity('File', data=Binary('some data'),
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   173
                                        data_format=u'text/plain', data_name=u'foo')
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   174
        # NOTE: do not use set_attributes() which would automatically
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   175
        #       update f1's local dict. We want the pure rql version to work
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   176
        self.execute('SET F data %(d)s WHERE F eid %(f)s',
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   177
                     {'d': Binary('some other data'), 'f': f1.eid})
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   178
        self.assertEquals(f1.data.getvalue(), 'some other data')
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   179
        self.commit()
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   180
        f2 = self.entity('Any F WHERE F eid %(f)s, F is File', {'f': f1.eid})
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   181
        self.assertEquals(f2.data.getvalue(), 'some other data')
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   182
88b5ca8da928 [storages] fix fs_importing side-effect on entity.data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5109
diff changeset
   183
5183
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   184
    def test_bfss_update_with_fs_importing(self):
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   185
        # use self.session to use server-side cache
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   186
        f1 = self.session.create_entity('File', data=Binary('some data'),
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   187
                                        data_format=u'text/plain', data_name=u'foo')
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   188
        old_fspath = self.fspath(f1)
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   189
        self.session.transaction_data['fs_importing'] = True
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   190
        new_fspath = osp.join(self.tempdir, 'newfile.txt')
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   191
        file(new_fspath, 'w').write('the new data')
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   192
        self.execute('SET F data %(d)s WHERE F eid %(f)s',
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   193
                     {'d': Binary(new_fspath), 'f': f1.eid})
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   194
        self.commit()
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   195
        self.assertEquals(f1.data.getvalue(), 'the new data')
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   196
        self.assertEquals(self.fspath(f1), new_fspath)
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   197
        self.failIf(osp.isfile(old_fspath))
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   198
8d66003351f8 [storage] consider fs_importing on update operations too
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5131
diff changeset
   199
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
if __name__ == '__main__':
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
    unittest_main()