Permalink
Browse files

100% test coverage

  • Loading branch information...
1 parent f003b5a commit 18ed06c5056bf9ec77b491eeafc97800818c5862 @sontek committed Jan 9, 2012
Showing with 180 additions and 2 deletions.
  1. +2 −2 pyramid_webassets/__init__.py
  2. +1 −0 pyramid_webassets/tests/__init__.py
  3. +173 −0 pyramid_webassets/tests/test_webassets.py
  4. +3 −0 requirements.txt
  5. +1 −0 setup.py
@@ -5,7 +5,7 @@
class IWebAssetsEnvironment(Interface):
pass
-def add_web_asset(config, name, bundle):
+def add_webasset(config, name, bundle):
asset_env = get_webassets_env(config)
asset_env.register(name, bundle)
@@ -51,5 +51,5 @@ def includeme(config):
config.registry.registerUtility(assets_env, IWebAssetsEnvironment)
- config.add_directive('add_webasset', add_web_asset)
+ config.add_directive('add_webasset', add_webasset)
config.add_directive('get_webassets_env', get_webassets_env)
@@ -0,0 +1 @@
+
@@ -0,0 +1,173 @@
+import unittest
+from mock import Mock
+class TestWebAssets(unittest.TestCase):
+ def test_asset_interface(self):
+ from pyramid_webassets import IWebAssetsEnvironment
+
+ def make_env():
+ IWebAssetsEnvironment('1')
+
+ self.assertRaises(TypeError, make_env)
+
+ def test_add_web_asset(self):
+ from pyramid_webassets import add_webasset
+
+ config = Mock()
+ config.registry = Mock()
+ queryUtility = Mock()
+ env = Mock()
+ register = Mock()
+ env.register = register
+ queryUtility.return_value = env
+
+ config.registry.queryUtility = queryUtility
+
+ add_webasset(config, 'foo', 'bar')
+ register.assert_called_with('foo', 'bar')
+
+ def test_get_webassets_env(self):
+ from pyramid_webassets import get_webassets_env
+ from pyramid_webassets import IWebAssetsEnvironment
+
+ config = Mock()
+ config.registry = Mock()
+ queryUtility = Mock()
+
+ config.registry.queryUtility = queryUtility
+
+ env = get_webassets_env(config)
+ queryUtility.assert_called_with(IWebAssetsEnvironment)
+
+ assert env != None
+
+ def test_get_webassets_env_from_settings_no_config(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+
+ settings = {}
+
+ with self.assertRaises(Exception) as cm:
+ get_webassets_env_from_settings(settings)
+
+ assert cm.exception.message == "You need to provide webassets.base_dir in your configuration"
+
+ def test_get_webassets_env_from_settings_no_base_dir(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+
+ settings = {'webassets.base_url': '/static'}
+
+ with self.assertRaises(Exception) as cm:
+ get_webassets_env_from_settings(settings)
+
+ assert cm.exception.message == "You need to provide webassets.base_dir in your configuration"
+
+ def test_get_webassets_env_from_settings_no_base_url(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+
+ settings = {'webassets.base_dir': '/home'}
+
+ with self.assertRaises(Exception) as cm:
+ get_webassets_env_from_settings(settings)
+
+ assert cm.exception.message == "You need to provide webassets.base_url in your configuration"
+
+ def test_get_webassets_env_from_settings_minimal(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+
+ settings = {
+ 'webassets.base_url': '/static',
+ 'webassets.base_dir': '/home/sontek'
+ }
+
+ env = get_webassets_env_from_settings(settings)
+
+ assert env.directory == settings['webassets.base_dir']
+ assert env.url == settings['webassets.base_url']
+
+ def test_get_webassets_env_from_settings_complete(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+ import webassets
+
+ settings = {
+ 'webassets.base_url': '/static',
+ 'webassets.base_dir': '/home/sontek',
+ 'webassets.debug': 'true',
+ 'webassets.cache': 'false',
+ 'webassets.updater': 'always',
+ 'webassets.jst_compiler': 'Handlebars.compile'
+ }
+
+ env = get_webassets_env_from_settings(settings)
+
+ assert env.directory == settings['webassets.base_dir']
+ assert env.url == settings['webassets.base_url']
+ assert env.debug == True
+ assert isinstance(env.updater, webassets.updater.AlwaysUpdater)
+ assert env.config['JST_COMPILER'] == settings['webassets.jst_compiler']
+ assert env.cache == None
+
+ def test_get_webassets_env_from_settings_with_cache(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+
+ settings = {
+ 'webassets.base_url': '/static',
+ 'webassets.base_dir': '/home/sontek',
+ 'webassets.cache': 'true',
+ }
+
+ env = get_webassets_env_from_settings(settings)
+
+ assert env.cache != None
+
+ def test_get_webassets_env_from_settings_prefix_change(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+
+ settings = {
+ 'foo.base_url': '/static',
+ 'foo.base_dir': '/home/sontek',
+ }
+
+ env = get_webassets_env_from_settings(settings, prefix='foo')
+
+ assert env != None
+ assert env.directory == settings['foo.base_dir']
+ assert env.url == settings['foo.base_url']
+
+ def test_get_webassets_env_from_settings_prefix_bad_change(self):
+ from pyramid_webassets import get_webassets_env_from_settings
+
+ settings = {
+ 'foo.base_url': '/static',
+ 'foo.base_dir': '/home/sontek',
+ }
+
+ with self.assertRaises(Exception) as cm:
+ get_webassets_env_from_settings(settings, prefix='webassets')
+
+ assert cm.exception.message == "You need to provide webassets.base_dir in your configuration"
+
+ def test_includeme(self):
+ from pyramid_webassets import includeme
+ from pyramid_webassets import add_webasset
+ from pyramid_webassets import get_webassets_env
+
+ config = Mock()
+ add_directive = Mock()
+ registerUtility = Mock()
+
+ config.registry = Mock()
+ config.registry.registerUtility = registerUtility
+ config.add_directive = add_directive
+
+ settings = {
+ 'webassets.base_url': '/static',
+ 'webassets.base_dir': '/home/sontek',
+ }
+
+ config.registry.settings = settings
+
+ includeme(config)
+
+ expected1 = ('add_webasset', add_webasset)
+ expected2 = ('get_webassets_env', get_webassets_env)
+ assert add_directive.call_args_list[0][0] == expected1
+ assert add_directive.call_args_list[1][0] == expected2
View
@@ -1,3 +1,6 @@
pyramid
webassets
zope.interface
+pytest
+pytest-cov
+mock
View
@@ -49,6 +49,7 @@ def run(self):
zip_safe=False,
test_suite='pyramid_webassets',
install_requires = requires,
+ test_requires = ['pytest', 'mock'],
cmdclass = {'test': PyTest},
paster_plugins=['pyramid'],
)

0 comments on commit 18ed06c

Please sign in to comment.