示例#1
0
def test_get_title():
    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
    )

    assert view.get_title() == "Dummy"
示例#2
0
def test_introspect_inclusion(temp_builds_dir):
    """
    Template introspection to cover includes
    """
    basepath = temp_builds_dir.join("views_base_introspect_inclusion")

    # Create directory structure
    templates_dir = os.path.join(basepath.strpath, "templates")
    os.makedirs(templates_dir)

    # Create dummy templates
    skeleton_template = os.path.join(templates_dir, "skeleton.html")
    sample_template = os.path.join(templates_dir, "sample.html")
    include_template = os.path.join(templates_dir, "inclusion.html")
    with open(skeleton_template, "w") as fp:
        fp.write(("""<html><body>"""
                  """{% block content %}Nope{% endblock %}"""
                  """</body></html>"""))
    with open(sample_template, "w") as fp:
        fp.write((
            """{% extends "skeleton.html" %}"""
            """{% block content %}Hello World! {% include 'inclusion.html' %}"""
            """{% endblock %}"""))

    with open(include_template, "w") as fp:
        fp.write(("""I'm an inclusion"""))

    # Dummy settings
    settings = DummySettings()

    # Init Jinja environment
    jinja_env = Jinja2Environment(loader=FileSystemLoader(templates_dir), )

    # Make a view to render
    view = PageViewBase(
        title="Dummy",
        destination="{language_code}/sample.html",
        template_name="sample.html",
        lang="fr",
        settings=settings,
    )

    assert view._recurse_template_search(jinja_env, "sample.html") == [
        "skeleton.html",
        "inclusion.html",
    ]

    assert view.introspect(jinja_env) == [
        "sample.html",
        "skeleton.html",
        "inclusion.html",
    ]
示例#3
0
def test_get_template_name(name, attempted):
    """
    Check template name with and without lang placeholder
    """
    settings = DummySettings()

    view = PageViewBase(
        title="Dummy",
        destination="dummy.html",
        template_name=name,
        lang="fr",
        settings=settings,
    )

    assert view.get_template_name() == attempted
示例#4
0
def test_get_destination(destination, path):
    """
    Check destination
    """
    settings = DummySettings()

    view = PageViewBase(
        title="Dummy",
        destination=destination,
        template_name="bar.html",
        lang="fr",
        settings=settings,
    )

    assert view.get_destination() == path
示例#5
0
def test_get_lang_code():
    """
    'lang' view attribute defined as a simple string (the locale code name)
    """
    settings = DummySettings()

    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
        lang="fr",
        settings=settings,
    )

    assert view.get_lang().label == "fr"
示例#6
0
def test_get_lang_default():
    """
    Default 'get_lang' value come from 'LANGUAGE_CODE' settings when 'lang'
    view attribute is empty.
    """
    settings = DummySettings()

    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
        settings=settings,
    )

    assert view.get_lang().label == "en"
示例#7
0
def test_get_relative_position(destination, position):
    """
    Check determined relative position for destination from the root (publish
    dir)
    """
    settings = DummySettings()

    view = PageViewBase(
        title="Dummy",
        destination=destination,
        template_name="dummy.html",
        lang="fr",
        settings=settings,
    )

    assert view.get_relative_position() == position
示例#8
0
def test_settings_through_setter():
    """
    Given settings through setter
    """
    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
    )

    with pytest.raises(ViewImproperlyConfigured):
        assert view.settings == 42

    view.settings = 42

    assert view.settings == 42
示例#9
0
def test_get_lang_object():
    """
    'lang' view attribute defined with a LangBase object
    """
    settings = DummySettings()

    locale = LangBase(code="fr")

    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
        lang=locale,
        settings=settings,
    )

    assert view.get_lang().label == "fr"
    assert view.get_lang() == locale
示例#10
0
def test_required_attrs_through_args():
    """
    Define required attributes through arguments
    """
    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
    )

    assert view.title == "Dummy"
示例#11
0
def test_settings_through_arg():
    """
    Given settings through argument
    """
    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
        settings=42,
    )

    assert view.settings == 42
示例#12
0
def test_settings_missing():
    """
    PageViewBase validate required settings object within getter
    """
    view = PageViewBase(
        title="Dummy",
        destination="Foo",
        template_name="Bar",
    )

    with pytest.raises(ViewImproperlyConfigured):
        view.settings
示例#13
0
def test_get_context_from_zero():
    """
    Check get_context from empty context
    """
    settings = DummySettings()

    locale = LangBase(code="fr")

    view = PageViewBase(
        title="Dummy",
        destination="{language_code}/hip/../hop.html",
        template_name="foo/bar.html",
        lang=locale,
        settings=settings,
    )

    assert view.get_context() == {
        "page_template_name": "foo/bar.html",
        "page_title": "Dummy",
        "page_relative_position": "../",
        "page_lang": locale,
        "page_destination": "fr/hop.html",
    }
示例#14
0
def test_render(temp_builds_dir):
    """
    Render a basic page
    """
    basepath = temp_builds_dir.join("views_base_render")

    # Create directory structure
    templates_dir = os.path.join(basepath.strpath, "templates")
    os.makedirs(templates_dir)

    # Create dummy templates
    skeleton_template = os.path.join(templates_dir, "skeleton.html")
    sample_template = os.path.join(templates_dir, "sample.html")
    with open(skeleton_template, "w") as fp:
        fp.write(("""<html><body>"""
                  """{% block content %}Nope{% endblock %}"""
                  """</body></html>"""))
    with open(sample_template, "w") as fp:
        fp.write(("""{% extends "skeleton.html" %}"""
                  """{% block content %}Hello World!{% endblock %}"""))

    # Make a view to render
    settings = DummySettings()

    view = PageViewBase(
        title="Dummy",
        destination="{language_code}/sample.html",
        template_name="sample.html",
        lang="fr",
        settings=settings,
    )

    # Init Jinja environment
    jinja_env = Jinja2Environment(loader=FileSystemLoader(templates_dir), )

    assert view.render(jinja_env) == "<html><body>Hello World!</body></html>"
示例#15
0
def test_add_page_advanced(temp_builds_dir, caplog):
    """
    Add pages to registry with introspection and all
    """
    basepath = temp_builds_dir.join("page_registry_add_page_advanced")

    # Create directory structure
    templates_dir = os.path.join(basepath.strpath, "templates")
    hiphop_dir = os.path.join(basepath.strpath, "templates", "hip")
    os.makedirs(templates_dir)
    os.makedirs(hiphop_dir)

    # Create dummy templates
    skeleton_template = os.path.join(templates_dir, "skeleton.html")
    index_template = os.path.join(templates_dir, "index.html")
    dummy_template = os.path.join(templates_dir, "dummy.html")
    base_template = os.path.join(templates_dir, "hip/base.html")
    hiphop_template = os.path.join(templates_dir, "hip/hop.html")
    inclusion_template = os.path.join(templates_dir, "_inclusion.html")
    with open(skeleton_template, "w") as fp:
        fp.write(
            (
                """<html><body>"""
                """{% block content %}Nope{% endblock %}"""
                """</body></html>"""
            )
        )
    with open(index_template, "w") as fp:
        fp.write(
            (
                """{% extends "skeleton.html" %}"""
                """{% block content %}Index{% endblock %}"""
            )
        )
    with open(dummy_template, "w") as fp:
        fp.write(
            (
                """{% extends "skeleton.html" %}"""
                """{% block content %}Hello World!{% endblock %}"""
            )
        )
    with open(base_template, "w") as fp:
        fp.write(
            (
                """{% extends "skeleton.html" %}"""
                """{% block content %}Base{% endblock %}"""
            )
        )
    with open(hiphop_template, "w") as fp:
        fp.write(
            (
                """{% extends "hip/base.html" %}"""
                """{% block content %}Base {% include '_inclusion.html' %}"""
                """{% endblock %}"""
            )
        )
    with open(inclusion_template, "w") as fp:
        fp.write(("""I'm an inclusion"""))

    # Dummy settings and registry
    settings = DummySettings()
    reg = PageRegistry()

    # Init Jinja environment
    jinja_env = Jinja2Environment(
        loader=FileSystemLoader(templates_dir),
    )

    # Make some views using templates
    index_view = PageViewBase(
        title="Index",
        destination="index.html",
        template_name="index.html",
        settings=settings,
    )

    foo_view = PageViewBase(
        title="Foo",
        destination="foo.html",
        template_name="dummy.html",
        settings=settings,
    )

    bar_view = PageViewBase(
        title="Bar",
        destination="bar.html",
        template_name="dummy.html",
        settings=settings,
    )

    french_view = PageViewBase(
        title="French",
        destination="localized/{language_code}.html",
        template_name="hip/hop.html",
        settings=settings,
    )

    english_view = PageViewBase(
        title="English",
        destination="localized/{language_code}.html",
        template_name="hip/hop.html",
        lang="fr",
        settings=settings,
    )

    # Add page views without introspection
    reg.add_page(index_view, index_view.introspect(jinja_env))
    reg.add_page(foo_view, foo_view.introspect(jinja_env))
    reg.add_page(bar_view, bar_view.introspect(jinja_env))
    reg.add_page(french_view, french_view.introspect(jinja_env))
    reg.add_page(english_view, english_view.introspect(jinja_env))

    print(reg.elements)

    assert reg.elements == {
        "index.html": set(
            [
                "index.html",
            ]
        ),
        "dummy.html": set(
            [
                "bar.html",
                "foo.html",
            ]
        ),
        "hip/base.html": set(
            [
                "localized/fr.html",
                "localized/en.html",
            ]
        ),
        "_inclusion.html": set(
            [
                "localized/fr.html",
                "localized/en.html",
            ]
        ),
        "hip/hop.html": set(
            [
                "localized/fr.html",
                "localized/en.html",
            ]
        ),
        "skeleton.html": set(
            [
                "index.html",
                "bar.html",
                "foo.html",
                "localized/fr.html",
                "localized/en.html",
            ]
        ),
    }

    assert reg.get_pages_from_dependency("index.html") == [
        index_view,
    ]

    # Checking skeleton usage from pages
    results = sorted(
        reg.get_pages_from_dependency("skeleton.html"), key=lambda obj: obj.destination
    )
    attempted = sorted(
        [
            index_view,
            foo_view,
            bar_view,
            english_view,
            french_view,
        ],
        key=lambda obj: obj.destination,
    )

    assert set(results) == set(attempted)

    # Checking hip base usage from pages
    results = sorted(
        reg.get_pages_from_dependency("hip/base.html"), key=lambda obj: obj.destination
    )
    attempted = sorted(
        [
            english_view,
            french_view,
        ],
        key=lambda obj: obj.destination,
    )

    # Checking inclusion usage from pages
    results = sorted(
        reg.get_pages_from_dependency("_inclusion.html"),
        key=lambda obj: obj.destination,
    )
    attempted = sorted(
        [
            english_view,
            french_view,
        ],
        key=lambda obj: obj.destination,
    )

    assert set(results) == set(attempted)
示例#16
0
def test_empty():
    """
    PageViewBase validate required attributes
    """
    with pytest.raises(ViewImproperlyConfigured):
        PageViewBase()
示例#17
0
def test_add_page_advanced(temp_builds_dir, filedescriptor, caplog):
    """
    Add pages to registry with introspection and all
    """
    basepath = temp_builds_dir.join('page_registry_add_page_advanced')

    # Create directory structure
    templates_dir = os.path.join(basepath.strpath, 'templates')
    hiphop_dir = os.path.join(basepath.strpath, 'templates', 'hip')
    os.makedirs(templates_dir)
    os.makedirs(hiphop_dir)

    # Create dummy templates
    skeleton_template = os.path.join(templates_dir, "skeleton.html")
    index_template = os.path.join(templates_dir, "index.html")
    dummy_template = os.path.join(templates_dir, "dummy.html")
    base_template = os.path.join(templates_dir, "hip/base.html")
    hiphop_template = os.path.join(templates_dir, "hip/hop.html")
    with io.open(skeleton_template, filedescriptor) as fp:
        fp.write(("""<html><body>"""
                  """{% block content %}Nope{% endblock %}"""
                  """</body></html>"""))
    with io.open(index_template, filedescriptor) as fp:
        fp.write(("""{% extends "skeleton.html" %}"""
                  """{% block content %}Index{% endblock %}"""))
    with io.open(dummy_template, filedescriptor) as fp:
        fp.write(("""{% extends "skeleton.html" %}"""
                  """{% block content %}Hello World!{% endblock %}"""))
    with io.open(base_template, filedescriptor) as fp:
        fp.write(("""{% extends "skeleton.html" %}"""
                  """{% block content %}Base{% endblock %}"""))
    with io.open(hiphop_template, filedescriptor) as fp:
        fp.write(("""{% extends "hip/base.html" %}"""
                  """{% block content %}Base{% endblock %}"""))

    # Dummy settings and registry
    settings = DummySettings()
    reg = PageRegistry()

    # Init Jinja environment
    jinja_env = Jinja2Environment(
        loader=FileSystemLoader(templates_dir),
    )

    # Make some views using templates
    index_view = PageViewBase(
        title='Index',
        destination='index.html',
        template_name='index.html',
        settings=settings,
    )

    foo_view = PageViewBase(
        title='Foo',
        destination='foo.html',
        template_name='dummy.html',
        settings=settings,
    )

    bar_view = PageViewBase(
        title='Bar',
        destination='bar.html',
        template_name='dummy.html',
        settings=settings,
    )

    french_view = PageViewBase(
        title='French',
        destination='localized/{language_code}.html',
        template_name='hip/hop.html',
        settings=settings,
    )

    english_view = PageViewBase(
        title='English',
        destination='localized/{language_code}.html',
        template_name='hip/hop.html',
        lang='fr',
        settings=settings,
    )

    # Add page views without introspection
    reg.add_page(index_view, index_view.introspect(jinja_env))
    reg.add_page(foo_view, foo_view.introspect(jinja_env))
    reg.add_page(bar_view, bar_view.introspect(jinja_env))
    reg.add_page(french_view, french_view.introspect(jinja_env))
    reg.add_page(english_view, english_view.introspect(jinja_env))

    print(reg.elements)

    assert reg.elements == {
        'index.html': set([
            'index.html',
        ]),
        'dummy.html': set([
            'bar.html',
            'foo.html',
        ]),
        'hip/base.html': set([
            'localized/fr.html',
            'localized/en.html',
        ]),
        'hip/hop.html': set([
            'localized/fr.html',
            'localized/en.html',
        ]),
        'skeleton.html': set([
            'index.html',
            'bar.html',
            'foo.html',
            'localized/fr.html',
            'localized/en.html',
        ]),
    }

    assert reg.get_pages_from_dependency('index.html') == [
        index_view,
    ]

    # Checking skeleton usage from pages
    results = sorted(reg.get_pages_from_dependency('skeleton.html'),
                     key=lambda obj: obj.destination)
    attempted = sorted([
        index_view,
        foo_view,
        bar_view,
        english_view,
        french_view,
    ], key=lambda obj: obj.destination)

    assert set(results) == set(attempted)

    # Checking hip base usage from pages
    results = sorted(reg.get_pages_from_dependency('hip/base.html'),
                     key=lambda obj: obj.destination)
    attempted = sorted([
        english_view,
        french_view,
    ], key=lambda obj: obj.destination)

    assert set(results) == set(attempted)
示例#18
0
def test_add_page_basic(caplog):
    """
    Add dummy pages to registry without real introspection
    """
    settings = DummySettings()

    reg = PageRegistry()

    index_view = PageViewBase(
        title="Index",
        destination="index.html",
        template_name="index.html",
        settings=settings,
    )

    foo_view = PageViewBase(
        title="Foo",
        destination="foo.html",
        template_name="foo.html",
        settings=settings,
    )

    bar_view = PageViewBase(
        title="Bar",
        destination="bar.html",
        template_name="foo.html",
        settings=settings,
    )

    # Add page views without introspection
    reg.add_page(index_view, [index_view.template_name])
    reg.add_page(foo_view, [foo_view.template_name])
    reg.add_page(bar_view, [bar_view.template_name])

    assert reg.elements == {
        "index.html": set(
            [
                "index.html",
            ]
        ),
        "foo.html": set(
            [
                "bar.html",
                "foo.html",
            ]
        ),
    }

    assert reg.get_pages_from_dependency("index.html") == [
        index_view,
    ]

    # Use set and sorted to deal with arbitrary order
    results = sorted(
        reg.get_pages_from_dependency("foo.html"), key=lambda obj: obj.destination
    )
    attempted = sorted(
        [
            foo_view,
            bar_view,
        ],
        key=lambda obj: obj.destination,
    )

    assert set(results) == set(attempted)