web/test/unittest_propertysheet.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 23 Jan 2012 13:25:02 +0100
changeset 8190 2a3c1b787688
parent 7791 31bb51ea5485
child 9674 96549de9dd70
permissions -rw-r--r--
[vreg] move base registry implementation to logilab.common. Closes #1916014 A new logilab.common.registry module has been created with content from * cw.vreg (the whole things that was in there) * cw.appobject (base selectors and all). In the process, we've done some renaming: * former selector functions are now known as "predicate", though you still use predicates to build an object'selector * hence `objectify_selector` decorator is now `objectify_predicate` * the top level registry is now `RegistryStore` (was `VRegistry`) Also there is no more need for the @lltrace decorator. On the CubicWeb side, the `selectors` module has been renamed to `predicates`. There should be full backward compat with proper deprecation warnings.

import os
from os.path import join, dirname
from shutil import rmtree

from logilab.common.testlib import TestCase, unittest_main

from cubicweb.web.propertysheet import *

DATADIR = join(dirname(__file__), 'data')
CACHEDIR = join(DATADIR, 'uicache')

class PropertySheetTC(TestCase):

    def tearDown(self):
        rmtree(CACHEDIR)

    def test(self):
        ps = PropertySheet(CACHEDIR, datadir_url='http://cwtest.com')
        ps.load(join(DATADIR, 'sheet1.py'))
        ps.load(join(DATADIR, 'sheet2.py'))
        # defined by sheet1
        self.assertEqual(ps['logo'], 'http://cwtest.com/logo.png')
        # defined by sheet1, overriden by sheet2
        self.assertEqual(ps['bgcolor'], '#FFFFFF')
        # defined by sheet2
        self.assertEqual(ps['fontcolor'], 'black')
        # defined by sheet1, extended by sheet2
        self.assertEqual(ps['stylesheets'], ['http://cwtest.com/cubicweb.css',
                                              'http://cwtest.com/mycube.css'])
        # lazy string defined by sheet1
        self.assertIsInstance(ps['lazy'], lazystr)
        self.assertEqual(str(ps['lazy']), '#FFFFFF')
        # test compilation
        self.assertEqual(ps.compile('a {bgcolor: %(bgcolor)s; size: 1%;}'),
                          'a {bgcolor: #FFFFFF; size: 1%;}')
        self.assertEqual(ps.process_resource(DATADIR, 'pouet.css'),
                          CACHEDIR)
        self.assertTrue('pouet.css' in ps._cache)
        self.assertFalse(ps.need_reload())
        os.utime(join(DATADIR, 'sheet1.py'), None)
        self.assertTrue('pouet.css' in ps._cache)
        self.assertTrue(ps.need_reload())
        self.assertTrue('pouet.css' in ps._cache)
        ps.reload()
        self.assertFalse('pouet.css' in ps._cache)
        self.assertFalse(ps.need_reload())
        ps.process_resource(DATADIR, 'pouet.css') # put in cache
        os.utime(join(DATADIR, 'pouet.css'), None)
        self.assertFalse(ps.need_reload())
        self.assertFalse('pouet.css' in ps._cache)

if __name__ == '__main__':
    unittest_main()