cubicweb/web/test/unittest_propertysheet.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 24 Nov 2016 15:36:26 +0100
changeset 11892 08cf02efc7ce
parent 11057 0b59724cb3f2
child 12287 547bb96ea2a8
permissions -rw-r--r--
Simplify and fix _cw.drop_entity_cache * it's never called with an eid as argument, beside in a useless case in test (removed) * the only place where it's called from outside the tests is in full-text reindexation in server.checkintegrity: we could removed the request implementation and move it in unittest_rset, byt I decided to keep it for consistency with all other entity cache handling methods * get back a fix from Julien Cristau for the connection's implementation, quoting is commit message: When removing an entity from the transaction's cache, clear the entity's own cache May avoid issues where an entity object is still accessible somewhere else (e.g. an operation) after dropping it from the transaction's cache, with a stale attribute or relation cache.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
     1
import os
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
from os.path import join, dirname
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
     3
from shutil import rmtree
10318
09273cb083e7 [web/test] don't delete uicache directory
Julien Cristau <julien.cristau@logilab.fr>
parents: 9844
diff changeset
     4
import errno
09273cb083e7 [web/test] don't delete uicache directory
Julien Cristau <julien.cristau@logilab.fr>
parents: 9844
diff changeset
     5
import tempfile
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
     6
from unittest import TestCase, main
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
     7
9844
e7d7b7793769 [webtests/propertysheet] kill an "import *"
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
     8
from cubicweb.web.propertysheet import PropertySheet, lazystr
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    10
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
DATADIR = join(dirname(__file__), 'data')
10318
09273cb083e7 [web/test] don't delete uicache directory
Julien Cristau <julien.cristau@logilab.fr>
parents: 9844
diff changeset
    12
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    13
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
class PropertySheetTC(TestCase):
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    16
    def setUp(self):
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    17
        uicache = join(DATADIR, 'uicache')
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    18
        try:
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    19
            os.makedirs(uicache)
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    20
        except OSError as err:
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    21
            if err.errno != errno.EEXIST:
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    22
                raise
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    23
        self.cachedir = tempfile.mkdtemp(dir=uicache)
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    24
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    25
    def tearDown(self):
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    26
        rmtree(self.cachedir)
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    27
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    28
    def data(self, filename):
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    29
        return join(DATADIR, filename)
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    30
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
    def test(self):
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    32
        ps = PropertySheet(self.cachedir, datadir_url='http://cwtest.com')
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    33
        ps.load(self.data('sheet1.py'))
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    34
        ps.load(self.data('sheet2.py'))
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
        # defined by sheet1
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    36
        self.assertEqual(ps['logo'], 'http://cwtest.com/logo.png')
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
        # defined by sheet1, overriden by sheet2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    38
        self.assertEqual(ps['bgcolor'], '#FFFFFF')
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        # defined by sheet2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    40
        self.assertEqual(ps['fontcolor'], 'black')
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
        # defined by sheet1, extended by sheet2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    42
        self.assertEqual(ps['stylesheets'], ['http://cwtest.com/cubicweb.css',
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
                                              'http://cwtest.com/mycube.css'])
5938
9cb13d1b2ce4 [uiprops] introduce lazystr to get a chance to change style without having to change all properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5466
diff changeset
    44
        # lazy string defined by sheet1
9cb13d1b2ce4 [uiprops] introduce lazystr to get a chance to change style without having to change all properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5466
diff changeset
    45
        self.assertIsInstance(ps['lazy'], lazystr)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    46
        self.assertEqual(str(ps['lazy']), '#FFFFFF')
5938
9cb13d1b2ce4 [uiprops] introduce lazystr to get a chance to change style without having to change all properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5466
diff changeset
    47
        # test compilation
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    48
        self.assertEqual(ps.compile('a {bgcolor: %(bgcolor)s; size: 1%;}'),
5445
4467ed43d97d [web] use uiprops value to compile css transparently, handlig cache and reloading in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    49
                          'a {bgcolor: #FFFFFF; size: 1%;}')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    50
        self.assertEqual(ps.process_resource(DATADIR, 'pouet.css'),
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    51
                         self.cachedir)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    52
        self.assertFalse(ps.need_reload())
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    53
        os.utime(self.data('sheet1.py'), None)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    54
        self.assertTrue(ps.need_reload())
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    55
        ps.reload()
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    56
        self.assertFalse(ps.need_reload())
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    57
        ps.process_resource(DATADIR, 'pouet.css') # put in cache
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    58
        os.utime(self.data('pouet.css'), None)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    59
        self.assertFalse(ps.need_reload())
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    61
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
if __name__ == '__main__':
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    63
    main()