cubicweb/web/test/unittest_propertysheet.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10897 web/test/unittest_propertysheet.py@7c386161ebd6
child 12287 547bb96ea2a8
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile
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()