|
| 1 | +import pytest |
| 2 | +from django.template import Context, Template |
| 3 | +from django.template.loader import get_template |
| 4 | +from django.test import TestCase |
| 5 | +from django_fastdev.apps import FastDevVariableDoesNotExist |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | + |
| 9 | +context = Context({"existing_var": "test_value"}) |
| 10 | + |
| 11 | + |
| 12 | +def test_nonexistent_variable_with_default_filter(): |
| 13 | + template = Template('{{ nonexistent_var|default:"fallback" }}') |
| 14 | + result = template.render(context) |
| 15 | + assert result == "fallback", "Expected fallback value for None with default filter" |
| 16 | + |
| 17 | + |
| 18 | +def test_nonexistent_variable_with_default_if_none_filter(): |
| 19 | + template = Template('{{ nonexistent_var|default_if_none:"fallback" }}') |
| 20 | + with pytest.raises(FastDevVariableDoesNotExist) as cm: |
| 21 | + result = template.render(context) |
| 22 | + assert "nonexistent_var does not exist in context" in str(cm.value) |
| 23 | + |
| 24 | + |
| 25 | +def test_nonexistent_variable_without_filters(): |
| 26 | + template = Template("{{ nonexistent_var }}") |
| 27 | + with pytest.raises(FastDevVariableDoesNotExist) as cm: |
| 28 | + template.render(context) |
| 29 | + assert "nonexistent_var does not exist in context" in str(cm.value) |
| 30 | + |
| 31 | + |
| 32 | +def test_existing_variable_with_default_filter(): |
| 33 | + template = Template('{{ existing_var|default:"fallback" }}') |
| 34 | + result = template.render(context) |
| 35 | + assert result == "test_value", "Expected existing variable value with default filter" |
| 36 | + |
| 37 | + |
| 38 | +def test_existing_variable_with_default_if_none_filter(): |
| 39 | + """Test that an existing variable with |default_if_none filter returns its value.""" |
| 40 | + template = Template('{{ existing_var|default_if_none:"fallback" }}') |
| 41 | + result = template.render(context) |
| 42 | + assert result == "test_value", "Expected existing variable value with default_if_none filter" |
| 43 | + |
| 44 | + |
| 45 | +def test_nested_nonexistent_variable_with_default_filter(): |
| 46 | + template = Template('{{ obj.nonexistent_field|default:"fallback" }}') |
| 47 | + context = Context({"obj": {"existing_field": "value"}}) |
| 48 | + result = template.render(context) |
| 49 | + assert result == "fallback", "Expected fallback value for None with nested default filter" |
| 50 | + |
| 51 | + |
| 52 | +def test_ignored_template_with_filter(): |
| 53 | + template = get_template('ignored/test_ignored_template_with_filter.html') |
| 54 | + result = template.render().strip() |
| 55 | + assert result == "fallback", "Expected Django default behavior for ignored template" |
| 56 | + |
| 57 | + |
| 58 | +def test_nonexistent_variable_with_multiple_filters(): |
| 59 | + template = Template('{{ nonexistent_var|upper|default:"fallback" }}') |
| 60 | + result = template.render(context) |
| 61 | + assert result == "fallback", "Expected fallback value for None with multiple filters including default" |
| 62 | + |
| 63 | + |
| 64 | +def test_nonexistent_variable_with_multiple_filters2(): |
| 65 | + template = Template('{{ nonexistent_var|default:"fallback"|upper }}') |
| 66 | + result = template.render(context) |
| 67 | + assert result == "FALLBACK", "Expected fallback value for None with multiple filters including default" |
0 commit comments