view tests/test_data_assetor.py @ 196:154b8df04829

processing: Add Compass and Sass processors. The Sass processor is similar to the Less processor, i.e. it tries to be part of the structured pipeline processing by using the mapfile produced by the Sass compiler in order to provide a list of dependencies. The Compass processor is completely acting outside of the pipeline, so the server won't know what's up to date and what's not. It's expected that the user will run `compass watch` to keep things up to date. However, it will require to pass the server's cache directory to put things in, so we'll need to add some easy way to get that path for the user.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 11 Jan 2015 23:08:49 -0800
parents 3471ffa059b2
children eb958151c8dc
line wrap: on
line source

import pytest
from mock import MagicMock
from piecrust.data.assetor import (Assetor, UnsupportedAssetsError,
        build_base_url)
from .mockutil import mock_fs, mock_fs_scope


@pytest.mark.parametrize('fs, expected', [
        (mock_fs().withPage('pages/foo/bar'), {}),
        (mock_fs()
            .withPage('pages/foo/bar')
            .withPageAsset('pages/foo/bar', 'one.txt', 'one'),
            {'one': 'one'}),
        (mock_fs()
            .withPage('pages/foo/bar')
            .withPageAsset('pages/foo/bar', 'one.txt', 'one')
            .withPageAsset('pages/foo/bar', 'two.txt', 'two'),
            {'one': 'one', 'two': 'two'})
        ])
def test_assets(fs, expected):
    with mock_fs_scope(fs):
        page = MagicMock()
        page.app = fs.getApp(cache=False)
        page.app.env.base_asset_url_format = '%uri%'
        page.path = fs.path('/kitchen/pages/foo/bar.md')
        assetor = Assetor(page, '/foo/bar')
        for en in expected.keys():
            assert hasattr(assetor, en)
            path = '/foo/bar/%s.txt' % en
            assert getattr(assetor, en) == path
            assert assetor[en] == path


def test_missing_asset():
    with pytest.raises(KeyError):
        fs = mock_fs().withPage('pages/foo/bar')
        with mock_fs_scope(fs):
            page = MagicMock()
            page.app = fs.getApp(cache=False)
            page.path = fs.path('/kitchen/pages/foo/bar.md')
            assetor = Assetor(page, '/foo/bar')
            assetor['this_doesnt_exist']


def test_multiple_assets_with_same_name():
    with pytest.raises(UnsupportedAssetsError):
        fs = (mock_fs()
                .withPage('pages/foo/bar')
                .withPageAsset('pages/foo/bar', 'one.txt', 'one text')
                .withPageAsset('pages/foo/bar', 'one.jpg', 'one picture'))
        with mock_fs_scope(fs):
            page = MagicMock()
            page.app = fs.getApp(cache=False)
            page.path = fs.path('/kitchen/pages/foo/bar.md')
            assetor = Assetor(page, '/foo/bar')
            assetor['one']


@pytest.mark.parametrize('url_format, pretty_urls, uri, expected', [
        ('%uri%', True, '/foo', '/foo/'),
        ('%uri%', True, '/foo.ext', '/foo.ext/'),
        ('%uri%', False, '/foo.html', '/foo/'),
        ('%uri%', False, '/foo.ext', '/foo/'),
        ])
def test_build_base_url(url_format, pretty_urls, uri, expected):
    app = MagicMock()
    app.env = MagicMock()
    app.env.base_asset_url_format = url_format
    app.config = {
            'site/pretty_urls': pretty_urls}
    assets_path = 'foo/bar-assets'
    actual = build_base_url(app, uri, assets_path)
    assert actual == expected