cubicweb/server/test/unittest_migractions.py
changeset 11273 c655e19cbc35
parent 11232 25ec9be5f305
child 11277 baed516c6f6e
equal deleted inserted replaced
11272:53fbd5644bff 11273:c655e19cbc35
    18 """unit tests for module cubicweb.server.migractions"""
    18 """unit tests for module cubicweb.server.migractions"""
    19 
    19 
    20 from datetime import date
    20 from datetime import date
    21 import os, os.path as osp
    21 import os, os.path as osp
    22 from contextlib import contextmanager
    22 from contextlib import contextmanager
    23 
    23 import tempfile
    24 from logilab.common.testlib import unittest_main, Tags, tag
    24 
       
    25 from logilab.common.testlib import unittest_main, Tags, tag, with_tempdir
    25 from logilab.common import tempattr
    26 from logilab.common import tempattr
    26 
    27 
    27 from yams.constraints import UniqueConstraint
    28 from yams.constraints import UniqueConstraint
    28 
    29 
    29 from cubicweb import ConfigurationError, ValidationError, ExecutionError
    30 from cubicweb import (ConfigurationError, ValidationError,
       
    31                       ExecutionError, Binary)
    30 from cubicweb.devtools import startpgcluster, stoppgcluster
    32 from cubicweb.devtools import startpgcluster, stoppgcluster
    31 from cubicweb.devtools.testlib import CubicWebTC
    33 from cubicweb.devtools.testlib import CubicWebTC
    32 from cubicweb.server.sqlutils import SQL_PREFIX
    34 from cubicweb.server.sqlutils import SQL_PREFIX
    33 from cubicweb.server.migractions import ServerMigrationHelper
    35 from cubicweb.server.migractions import ServerMigrationHelper
       
    36 from cubicweb.server.sources import storages
    34 
    37 
    35 import cubicweb.devtools
    38 import cubicweb.devtools
    36 
    39 
    37 
    40 
    38 HERE = osp.dirname(osp.abspath(__file__))
    41 HERE = osp.dirname(osp.abspath(__file__))
   486                 'Any N ORDERBY O,N WHERE X is CWAttribute, X relation_type RT, RT name N,'
   489                 'Any N ORDERBY O,N WHERE X is CWAttribute, X relation_type RT, RT name N,'
   487                 'X from_entity FE, FE name "Personne",'
   490                 'X from_entity FE, FE name "Personne",'
   488                 'X ordernum O')]
   491                 'X ordernum O')]
   489             expected = [u'nom', u'prenom', u'sexe', u'promo', u'ass', u'adel', u'titre',
   492             expected = [u'nom', u'prenom', u'sexe', u'promo', u'ass', u'adel', u'titre',
   490                         u'web', u'tel', u'fax', u'datenaiss', u'test', u'tzdatenaiss',
   493                         u'web', u'tel', u'fax', u'datenaiss', u'test', u'tzdatenaiss',
   491                         u'description', u'firstname',
   494                         u'description', u'firstname', u'photo',
   492                         u'creation_date', u'cwuri', u'modification_date']
   495                         u'creation_date', u'cwuri', u'modification_date']
   493             self.assertEqual(expected, rinorder)
   496             self.assertEqual(expected, rinorder)
   494 
   497 
   495             # test permissions synchronization ####################################
   498             # test permissions synchronization ####################################
   496             # new rql expr to add note entity
   499             # new rql expr to add note entity
   753             note = mh.cmd_create_entity('Note', ecrit_par=cnx.user.eid)
   756             note = mh.cmd_create_entity('Note', ecrit_par=cnx.user.eid)
   754             mh.commit()
   757             mh.commit()
   755             mh.drop_relation_definition('Note', 'ecrit_par', 'CWUser')
   758             mh.drop_relation_definition('Note', 'ecrit_par', 'CWUser')
   756             self.assertFalse(mh.sqlexec('SELECT * FROM cw_Note WHERE cw_ecrit_par IS NOT NULL'))
   759             self.assertFalse(mh.sqlexec('SELECT * FROM cw_Note WHERE cw_ecrit_par IS NOT NULL'))
   757 
   760 
       
   761     @with_tempdir
       
   762     def test_storage_changed(self):
       
   763         with self.mh() as (cnx, mh):
       
   764             john = mh.cmd_create_entity('Personne', nom=u'john',
       
   765                                         photo=Binary(b'something'))
       
   766             bill = mh.cmd_create_entity('Personne', nom=u'bill')
       
   767             mh.commit()
       
   768             bfs_storage = storages.BytesFileSystemStorage(tempfile.tempdir)
       
   769             storages.set_attribute_storage(self.repo, 'Personne', 'photo', bfs_storage)
       
   770             mh.cmd_storage_changed('Personne', 'photo')
       
   771             bob = mh.cmd_create_entity('Personne', nom=u'bob')
       
   772             bffss_dir_content = os.listdir(tempfile.tempdir)
       
   773             self.assertEqual(len(bffss_dir_content), 1)
       
   774             john.cw_clear_all_caches()
       
   775             self.assertEqual(john.photo.getvalue(),
       
   776                              osp.join(tempfile.tempdir, bffss_dir_content[0]))
       
   777             bob.cw_clear_all_caches()
       
   778             self.assertIsNone(bob.photo)
       
   779             bill.cw_clear_all_caches()
       
   780             self.assertIsNone(bill.photo)
       
   781             storages.unset_attribute_storage(self.repo, 'Personne', 'photo')
       
   782 
       
   783 
   758 class MigrationCommandsComputedTC(MigrationTC):
   784 class MigrationCommandsComputedTC(MigrationTC):
   759     """ Unit tests for computed relations and attributes
   785     """ Unit tests for computed relations and attributes
   760     """
   786     """
   761     appid = 'datacomputed'
   787     appid = 'datacomputed'
   762 
   788