web/test/unittest_propertysheet.py
author Rémi Cardona <remi.cardona@logilab.fr>
Fri, 18 Sep 2015 11:58:29 +0200
changeset 10707 3f4f190f8e35
parent 10507 d54a942ee6da
child 10897 7c386161ebd6
permissions -rw-r--r--
[py3k] unicode → six.text_type
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)
9674
96549de9dd70 [test] use assertIn where appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 7791
diff changeset
    52
        self.assertIn('pouet.css', ps._cache)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    53
        self.assertFalse(ps.need_reload())
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    54
        os.utime(self.data('sheet1.py'), None)
9674
96549de9dd70 [test] use assertIn where appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 7791
diff changeset
    55
        self.assertIn('pouet.css', ps._cache)
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.assertTrue(ps.need_reload())
9674
96549de9dd70 [test] use assertIn where appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 7791
diff changeset
    57
        self.assertIn('pouet.css', ps._cache)
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
    58
        ps.reload()
9675
8aabfefc8a81 [test] use assertNotIn where appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 9674
diff changeset
    59
        self.assertNotIn('pouet.css', ps._cache)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    60
        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
    61
        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
    62
        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
    63
        self.assertFalse(ps.need_reload())
9675
8aabfefc8a81 [test] use assertNotIn where appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 9674
diff changeset
    64
        self.assertNotIn('pouet.css', ps._cache)
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    66
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
if __name__ == '__main__':
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    68
    main()