def test_union():
    """Should perform union between the value and the argument."""
    out = render_string("{{ set1 | union(set2) }}", {
        "set1": {1, 2, 3},
        "set2": set()
    })
    assert out == "{1, 2, 3}"
    out = render_string("{{ set1 | union(set2) }}", {
        "set1": {1, 2, 3, 4},
        "set2": {2, 3}
    })
    assert out == "{1, 2, 3, 4}"
    out = render_string("{{ set1 | union(set2) }}", {
        "set1": [{
            "x": 0
        }],
        "set2": [{
            "x": 1
        }]
    })
    assert out == """[{'x': 0}, {'x': 1}]"""
    out = render_string("{{ set1 | union(set2) }}", {
        "set1": [1, 2, 3, 4],
        "set2": [2, 3]
    })
    assert out == "{1, 2, 3, 4}"
def test_split():
    """Should split a string"""
    string = "a,b,c"
    out = render_string("{{ string | split }}", {"string": string})
    assert out == repr(string.split())
    out = render_string("""{{ string | split(',') }}""", {"string": string})
    assert out == repr(string.split(","))
    out = render_string("""{{ string | split(',', maxsplit=2) }}""",
                        {"string": string})
    assert out == repr(string.split(",", maxsplit=2))
def test_unique():
    """Should ensure that the output values are unique."""
    for _ in range(10):
        values = random.choices("0123456789", k=10)
        out = render_string("{{ values | unique | sort }}", {"values": values})
        assert out == repr(list(sorted(set(values))))
    out = render_string("{{ values | unique | sort }}",
                        {"values": [{
                            "x": 1
                        }, {
                            "x": 1
                        }]})
    assert out == """[{'x': 1}]"""
def test_difference():
    """Should perform set difference between the value and the other iterables."""
    out = render_string("{{ set1 | difference(set2) }}", {
        "set1": {1, 2, 3, 4},
        "set2": {4, 5}
    })
    assert out == "{1, 2, 3}"
def test_to_json():
    """Should produce JSON string."""
    out = render_string("{{ var | to_json }}",
                        {"var": {
                            "a": [0, 1],
                            "b": None
                        }})
    assert out == """{"a": [0, 1], "b": null}"""
Exemple #6
0
def test_jinja_do_extension_enabled():
    """Jinja2's extension for 'do' keyword should be enabled."""
    out = render_string(
        '{%- do some_dict.update({"var": 1}) -%}'
        "{{ some_dict.var }}",
        {"some_dict": {}},
    )
    assert out == "1"
def test_symmetric_difference():
    """Should perform synnetric difference."""
    out = render_string(
        "{{ set1 | symmetric_difference(set2) }}",
        {
            "set1": {1, 2, 3, 4},
            "set2": {4, 5}
        },
    )
    assert out == "{1, 2, 3, 5}"
    out = render_string(
        "{{ set2 | symmetric_difference(set1) }}",
        {
            "set1": {1, 2, 3, 4},
            "set2": {4, 5}
        },
    )
    assert out == "{1, 2, 3, 5}"
def test_intersect():
    """Should perform intersection between the value and the argument."""
    out = render_string("{{ set1 | intersect(set2) }}", {
        "set1": {1, 2, 3, 4},
        "set2": set()
    })
    assert out == "set()"
    out = render_string("{{ set1 | intersect(set2) }}", {
        "set1": {1, 2, 3, 4},
        "set2": {2, 3}
    })
    assert out == "{2, 3}"
    out = render_string(
        "{{ set1 | intersect(set2) }}",
        {
            "set1": [[0, 1], [1, 2]],
            "set2": [[2, 3], [1, 2]]
        },
    )
    assert out == "[[1, 2]]"
Exemple #9
0
def test_iglob(iglob):
    """Should call glob.iglob()."""
    iglob.return_value = ["a", "b", "c"]
    out = render_string("""{{ iglob('path') }}""", {})
    iglob.assert_called_once_with("path")
    assert out == repr(iglob.return_value)
Exemple #10
0
def test_round():
    """Builtin round should be callable from Jinja."""
    out = render_string("{{ round(x) }}", {"x": 3.14})
    assert out == "3"
    out = render_string("{{ round(x, 1) }}", {"x": 3.14})
    assert out == "3.1"
Exemple #11
0
def test_glob(glob):
    """Should call glob.glob()."""
    glob.return_value = ["a", "b", "c"]
    out = render_string("""{{ glob('pattern') }}""", {})
    glob.assert_called_once_with("pattern")
    assert out == repr(glob.return_value)
Exemple #12
0
def test_max():
    """Builtin max should be callable from Jinja."""
    out = render_string("{{ max(x, y) }}", {"x": 42, "y": 0})
    assert out == "42"
Exemple #13
0
def test_abs():
    """Builtin abs should be callable from Jinja."""
    out = render_string("{{ abs(x) }}", {
        "x": -42,
    })
    assert out == "42"
Exemple #14
0
def test_min():
    """Builtin min should be callable from Jinja."""
    out = render_string("{{ min(x, y) }}", {"x": 42, "y": 6 * 9})
    assert out == "42"
Exemple #15
0
def test_zip():
    """Builtin zip should be callable from Jinja."""
    out = render_string("{{ zip(x, y) }}", {"x": [0, 1], "y": [1, 2]})
    assert out == "[(0, 1), (1, 2)]"
Exemple #16
0
def test_raise_error_on_undefined_variable():
    """Be strict about undefined variables, should raise an error."""
    with raises(jinja2.exceptions.UndefinedError):
        render_string("{{ not_found }}", {})
Exemple #17
0
def test_fatal_error():
    """Should raise a RuntimeError with the specified message."""
    msg = "the end of the universe"
    with raises(RuntimeError, match=msg):
        render_string("{% do fatal_error(msg) %}", {"msg": msg})
Exemple #18
0
def test_to_pretty_json():
    """Should produce a more pretty JSON string."""
    var = {"a": [0, 1], "b": None}
    out = render_string("{{ var | to_pretty_json }}", {"var": var})
    assert out == json.dumps(var, indent=4, sort_keys=True)
Exemple #19
0
def test_context(variables):
    """Context shloud return the whole variable space."""
    out = render_string("{{ context().keys() }}", variables)
    assert out == repr(variables.keys())
Exemple #20
0
def test_render_string():
    """Should render a given template with the specified variables."""
    assert render_string("some text", {}) == "some text"
    assert render_string("{{ var }}", {"var": "hello"}) == "hello"