server/test/unittest_storage.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 31 Mar 2010 14:25:26 +0200
branchstable
changeset 5109 5cf83b9356d5
parent 5013 ad91f93bbb93
child 5131 88b5ca8da928
permissions -rw-r--r--
[test] use the new temporary_appojects CM to simplfy unittest_storage
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
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
class StorageTC(CubicWebTC):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    def setup_database(self):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        self.tempdir = tempfile.mkdtemp()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        bfs_storage = storages.BytesFileSystemStorage(self.tempdir)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        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
    49
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    def tearDown(self):
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        super(CubicWebTC, self).tearDown()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        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
    53
        shutil.rmtree(self.tempdir)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    56
    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
    57
        req = self.request()
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        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
    59
                                 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
    60
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    61
    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
    62
        f1 = self.create_file()
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        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
    64
        self.failUnless(osp.isfile(expected_filepath))
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        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
    66
        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
    67
        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
    68
        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
    69
        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
    70
        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
    71
        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
    72
        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
    73
        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
    74
        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
    75
        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
    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.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
    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.commit()
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.failIf(osp.isfile(expected_filepath))
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    82
    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
    83
        f1 = self.create_file()
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        expected_filepath = osp.join(self.tempdir, '%s_data' % f1.eid)
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
    85
        fspath = self.execute('Any fspath(D) WHERE F eid %(f)s, F data D',
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
                              {'f': f1.eid})[0][0]
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        self.assertEquals(fspath.getvalue(), expected_filepath)
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    89
    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
    90
        self.session.transaction_data['fs_importing'] = True
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        f1 = self.session.create_entity('File', data=Binary('/the/path'),
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
                                        data_format=u'text/plain', data_name=u'foo')
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
    93
        fspath = self.execute('Any fspath(D) WHERE F eid %(f)s, F data D',
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
                              {'f': f1.eid})[0][0]
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        self.assertEquals(fspath.getvalue(), '/the/path')
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
5008
385bf22e3c12 [bfss test] some minor refactoring. Test deletion + creation/update/deletion rollback
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4967
diff changeset
    97
    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
    98
        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
    99
            self.create_file()
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
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
   101
    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
   102
        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
   103
                               '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
   104
        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
   105
        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
   106
                               '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
   107
                               '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
   108
        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
   109
        # 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
   110
        # 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
   111
        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
   112
                               '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
   113
                               ' (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
   114
                               '  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
   115
                               ' (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
   116
                               ')')
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
        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
   118
        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
   119
                               '(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
   120
                               ' 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
   121
                               '(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
   122
        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
   123
        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
   124
                               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
   125
        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
   126
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
    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
   128
        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
   129
        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
   130
                            ' (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
   131
                            '  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
   132
                            ' (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
   133
                            ')', {'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
   134
        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
   135
        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
   136
        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
   137
        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
   138
        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
   139
        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
   140
                            {'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
   141
        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
   142
        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
   143
        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
   144
        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
   145
                            ' (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
   146
                            '  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
   147
                            ' (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
   148
                            ')', {'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
   149
        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
   150
        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
   151
        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
   152
        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
   153
        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
   154
        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
   155
                               '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
   156
                               {'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
   157
        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
   158
4967
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
if __name__ == '__main__':
236f1fde6dd0 [server] add unit tests for storages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
    unittest_main()