cubicweb/server/test/unittest_migractions.py
changeset 11744 a6dc650bc230
parent 11416 9c2fbb872e91
child 11763 39df042f4ab4
equal deleted inserted replaced
11743:48d70d143dc1 11744:a6dc650bc230
    22 from datetime import date
    22 from datetime import date
    23 from contextlib import contextmanager
    23 from contextlib import contextmanager
    24 import tempfile
    24 import tempfile
    25 from hashlib import md5
    25 from hashlib import md5
    26 
    26 
    27 from logilab.common.testlib import unittest_main, Tags, tag, with_tempdir
    27 from logilab.common.testlib import unittest_main, Tags, tag
    28 from logilab.common import tempattr
    28 from logilab.common import tempattr
    29 
    29 
    30 from yams.constraints import UniqueConstraint
    30 from yams.constraints import UniqueConstraint
    31 
    31 
    32 from cubicweb import (ConfigurationError, ValidationError,
    32 from cubicweb import (ConfigurationError, ValidationError,
    33                       ExecutionError, Binary)
    33                       ExecutionError, Binary)
    34 from cubicweb.devtools import startpgcluster, stoppgcluster
    34 from cubicweb.devtools import startpgcluster, stoppgcluster
    35 from cubicweb.devtools.testlib import CubicWebTC
    35 from cubicweb.devtools.testlib import CubicWebTC, TemporaryDirectory
    36 from cubicweb.server.sqlutils import SQL_PREFIX
    36 from cubicweb.server.sqlutils import SQL_PREFIX
    37 from cubicweb.server.migractions import ServerMigrationHelper
    37 from cubicweb.server.migractions import ServerMigrationHelper
    38 from cubicweb.server.sources import storages
    38 from cubicweb.server.sources import storages
    39 from cubicweb.server.schema2sql import build_index_name
    39 from cubicweb.server.schema2sql import build_index_name
    40 
    40 
   796             note = mh.cmd_create_entity('Note', ecrit_par=cnx.user.eid)
   796             note = mh.cmd_create_entity('Note', ecrit_par=cnx.user.eid)
   797             mh.commit()
   797             mh.commit()
   798             mh.drop_relation_definition('Note', 'ecrit_par', 'CWUser')
   798             mh.drop_relation_definition('Note', 'ecrit_par', 'CWUser')
   799             self.assertFalse(mh.sqlexec('SELECT * FROM cw_Note WHERE cw_ecrit_par IS NOT NULL'))
   799             self.assertFalse(mh.sqlexec('SELECT * FROM cw_Note WHERE cw_ecrit_par IS NOT NULL'))
   800 
   800 
   801     @with_tempdir
       
   802     def test_storage_changed(self):
   801     def test_storage_changed(self):
   803         with self.mh() as (cnx, mh):
   802         with self.mh() as (cnx, mh):
   804             john = mh.cmd_create_entity('Personne', nom=u'john',
   803             john = mh.cmd_create_entity('Personne', nom=u'john',
   805                                         photo=Binary(b'something'))
   804                                         photo=Binary(b'something'))
   806             bill = mh.cmd_create_entity('Personne', nom=u'bill')
   805             bill = mh.cmd_create_entity('Personne', nom=u'bill')
   807             mh.commit()
   806             mh.commit()
   808             bfs_storage = storages.BytesFileSystemStorage(tempfile.tempdir)
   807             with TemporaryDirectory() as tempdir:
   809             storages.set_attribute_storage(self.repo, 'Personne', 'photo', bfs_storage)
   808                 bfs_storage = storages.BytesFileSystemStorage(tempdir)
   810             mh.cmd_storage_changed('Personne', 'photo')
   809                 storages.set_attribute_storage(self.repo, 'Personne', 'photo', bfs_storage)
   811             bob = mh.cmd_create_entity('Personne', nom=u'bob')
   810                 mh.cmd_storage_changed('Personne', 'photo')
   812             bffss_dir_content = os.listdir(tempfile.tempdir)
   811                 bob = mh.cmd_create_entity('Personne', nom=u'bob')
   813             self.assertEqual(len(bffss_dir_content), 1)
   812                 bffss_dir_content = os.listdir(tempdir)
   814             john.cw_clear_all_caches()
   813                 self.assertEqual(len(bffss_dir_content), 1)
   815             self.assertEqual(john.photo.getvalue(),
   814                 john.cw_clear_all_caches()
   816                              osp.join(tempfile.tempdir,
   815                 self.assertEqual(john.photo.getvalue(),
   817                                       bffss_dir_content[0]).encode('utf8'))
   816                                  osp.join(tempdir,
   818             bob.cw_clear_all_caches()
   817                                           bffss_dir_content[0]).encode('utf8'))
   819             self.assertIsNone(bob.photo)
   818                 bob.cw_clear_all_caches()
   820             bill.cw_clear_all_caches()
   819                 self.assertIsNone(bob.photo)
   821             self.assertIsNone(bill.photo)
   820                 bill.cw_clear_all_caches()
   822             storages.unset_attribute_storage(self.repo, 'Personne', 'photo')
   821                 self.assertIsNone(bill.photo)
       
   822                 storages.unset_attribute_storage(self.repo, 'Personne', 'photo')
   823 
   823 
   824 
   824 
   825 class MigrationCommandsComputedTC(MigrationTC):
   825 class MigrationCommandsComputedTC(MigrationTC):
   826     """ Unit tests for computed relations and attributes
   826     """ Unit tests for computed relations and attributes
   827     """
   827     """