Exemplo n.º 1
0
class FinderTest(TestCase):
    def setUp(self):
        self.finder = StaticFilesFinder()

    def test_default_finder(self):
        self.assertIsNotNone(staticassets_finder)

    def test_resolve_nonexistent_file(self):
        self.assertRaises(AssetNotFound, self.finder.resolve, 'nonexistent.js')

    def test_find_nonexistent_asset(self):
        self.assertRaises(AssetNotFound, self.finder.find, 'nonexistent.js')

    def test_resolve_file_with_extension(self):
        name, storage = self.finder.resolve('app.js')
        self.assertEqual(self.fixture_path('app.js'), storage.path(name))

    def test_resolve_file_without_extension(self):
        name, storage = self.finder.resolve('app', content_type='application/javascript')
        self.assertEqual(self.fixture_path('app.js'), storage.path(name))

    def test_resolve_file_with_multiple_extensions(self):
        name, storage = self.finder.resolve('style.css')
        self.assertEqual(self.fixture_path('style.css.sass'), storage.path(name))

    def test_find_asset_index(self):
        self.assertEqual('models/index.js', self.finder.find('models').name)

    def test_cached_assets_equal(self):
        asset1 = self.finder.find('app.js')
        asset2 = self.finder.find('app.js')

        self.assertIs(asset1, asset2)
class StorageTest(TestCase):
    def setUp(self):
        self.finder = StaticFilesFinder()
        self.storage = StaticAssetsStorage()
        self.cached_storage = CachedStaticAssetsStorage()

    def run_collectstatic(self):
        call_command('collectstatic', interactive=False, verbosity='0')

    def tearDown(self):
        super(StorageTest, self).tearDown()
        # shutil.rmtree(settings.STATIC_ROOT)

    @override_settings(STATICFILES_STORAGE='staticassets.storage.StaticAssetsStorage')
    def test_post_process(self):
        self.run_collectstatic()

        self.assertTrue(self.storage.exists('app.js'))
        self.assertTrue(self.storage.exists('app.css'))

        for name in settings.STATICASSETS_MANIFESTS:
            asset = self.finder.find(name, bundle=True)
            content = read_file(self.storage.path(name))
            self.assertEqual(content, asset.content)

    @override_settings(STATICFILES_STORAGE='staticassets.storage.CachedStaticAssetsStorage')
    def test_cached_storage(self):
        self.run_collectstatic()

        self.assertTrue(self.storage.exists('app.733b025a89a1.js'))
        self.assertTrue(self.storage.exists('app.8b57d02a0b57.css'))

        content = read_file(self.storage.path('app.8b57d02a0b57.css'))
        self.assertIn('body {\n    background: url("/static/img/empty.b44917055649.gif");\n}', content)
Exemplo n.º 3
0
class AssetTest(TestCase):
    def setUp(self):
        self.finder = StaticFilesFinder()

    def get_search_regex(self, path):
        return self.finder.get_search_regex(AssetAttributes(path))

    def test_attributes_search_paths(self):
        self.assertEqual([
            'index.js',
            'index/bower.json'
        ], AssetAttributes('index.js').search_paths)

        self.assertEqual([
            'foo.min.js',
            'foo/bower.json',
            'foo/index.min.js'
        ], AssetAttributes('foo.min.js').search_paths)

        self.assertEqual([
            'bar',
            'bar/bower.json',
            'bar/index'
        ], AssetAttributes('bar').search_paths)

    def test_attributes_available_extensions(self):
        self.assertItemsEqual(
            ['.coffee', '.ejs', '.js', '.jst'],
            AssetAttributes('foo.js.coffee').available_extensions)

        self.assertItemsEqual(
            ['.coffee', '.css', '.ejs', '.js', '.jst', '.less', '.sass', '.scss', '.styl'],
            AssetAttributes('foo').available_extensions)

        self.assertItemsEqual(
            ['.css', '.less', '.sass', '.scss', '.styl'],
            AssetAttributes('foo', 'text/css').available_extensions)

    def test_attributes_extensions(self):
        self.assertEqual(['.js', '.coffee'], AssetAttributes('foo.js.coffee').extensions)

    def test_attributes_format_extension(self):
        self.assertEqual('.js', AssetAttributes('foo/bar.js.coffee').format_extension)
        self.assertEqual('.js', AssetAttributes('foo/bar.min.coffee').format_extension)
        self.assertEqual('.css', AssetAttributes('foo/bar.sass').format_extension)

    def test_attributes_format_name(self):
        self.assertEqual('foo/bar.css', AssetAttributes('foo/bar.sass').format_name)
        self.assertEqual('foo/bar.min.css', AssetAttributes('foo/bar.min.sass').format_name)

    def test_attributes_path_without_extensions(self):
        self.assertEqual('foo/bar', AssetAttributes('foo/bar.js.coffee').path_without_extensions)
        self.assertEqual('foo', AssetAttributes('foo').path_without_extensions)

    def test_asset_source(self):
        self.assertEqual('//= require models\n\nvar App = {Models: {}};\n', self.finder.find('app.js').source)

    def test_asset_content(self):
        asset = self.finder.find('models/User.js')
        asset.content = '(function(){%s})' % asset.content
        self.assertEqual('(function(){App.Models.User = {};\n})', asset.content)

    def test_asset_path(self):
        self.assertEqual(self.fixture_path('models/index.js'), self.finder.find('models').path)

    def test_asset_url(self):
        self.assertEqual('/static/style.css', self.finder.find('style').url)
        self.assertEqual('/static/foo.js', self.finder.find('foo.coffee').url)
        self.assertEqual('/static/foo.js', self.finder.find('foo.js').url)

    def test_dependencies(self):
        asset = self.finder.find('app.js')

        app_file = self.fixture_path('app.js')
        models_file = self.fixture_path('models/index.js')
        user_file = self.fixture_path('models/User.js')

        self.assertItemsEqual([
            [app_file, os.path.getmtime(app_file), self.file_digest(app_file)],
            [models_file, os.path.getmtime(models_file), self.file_digest(models_file)],
            [user_file, os.path.getmtime(user_file), self.file_digest(user_file)]
        ], asset.dependencies)

    def test_requirements(self):
        asset = self.finder.find('app.js')

        self.assertEqual([
            self.finder.find('models/index.js').path,
        ], [a.path for a in asset.requirements])

        self.assertEqual([
            self.finder.find('models/User.js').path,
            self.finder.find('models/index.js').path,
            self.finder.find('app.js').path,
        ], [a.path for a in asset])

    def test_circular_require_raises_exception(self):
        self.assertRaises(CircularDependencyError, self.finder.find, 'circular/a.js')
        self.assertRaises(CircularDependencyError, self.finder.find, 'circular/b.js')
        self.assertRaises(CircularDependencyError, self.finder.find, 'circular/c.js')

    def test_require_directory(self):
        content = self.finder.find('base.js', bundle=True).content

        self.assertIn(self.fixture('tree/all/a/a.js'), content)
        self.assertIn(self.fixture('tree/all/a/b.js'), content)