Passed
Push — master ( 69718c...20d9e6 )
by Swen
02:10
created

LocalizedBleachFieldTestCase   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_pre_save() 0 9 1
A test_pre_save_none() 0 8 2
B _validate() 0 24 5
A _get_test_model() 0 11 1
A test_pre_save_none_values() 0 12 1
A _get_test_value() 0 10 2
1
import bleach
2
from django.conf import settings
3
from django.test import TestCase
4
from django_bleach.utils import get_bleach_default_options
5
6
from localized_fields.fields import LocalizedBleachField, LocalizedValue
7
8
9
class TestModel:
10
    """Used to declare a bleach-able field on."""
11
12
    def __init__(self, value):
13
        """Initializes a new instance of :see:TestModel.
14
15
        Arguments:
16
            The value to initialize with.
17
        """
18
19
        self.value = value
20
21
22
class LocalizedBleachFieldTestCase(TestCase):
23
    """Tests the :see:LocalizedBleachField class."""
24
25
    def test_pre_save(self):
26
        """Tests whether the :see:pre_save function
27
        bleaches all values in a :see:LocalizedValue."""
28
29
        value = self._get_test_value()
30
        model, field = self._get_test_model(value)
31
32
        bleached_value = field.pre_save(model, False)
33
        self._validate(value, bleached_value)
34
35
    def test_pre_save_none(self):
36
        """Tests whether the :see:pre_save function
37
        works properly when specifying :see:None."""
38
39
        model, field = self._get_test_model(None)
40
41
        bleached_value = field.pre_save(model, False)
42
        assert not bleached_value
43
44
    def test_pre_save_none_values(self):
45
        """Tests whether the :see:pre_save function
46
        works properly when one of the languages has
47
        no text and is None."""
48
49
        value = self._get_test_value()
50
        value.set(settings.LANGUAGE_CODE, None)
51
52
        model, field = self._get_test_model(value)
53
54
        bleached_value = field.pre_save(model, False)
55
        self._validate(value, bleached_value)
56
57
    @staticmethod
58
    def _get_test_model(value):
59
        """Gets a test model and a artifically
60
        constructed :see:LocalizedBleachField
61
        instance to test with."""
62
63
        model = TestModel(value)
64
65
        field = LocalizedBleachField()
66
        field.attname = 'value'
67
        return model, field
68
69
    @staticmethod
70
    def _get_test_value():
71
        """Gets a :see:LocalizedValue instance for testing."""
72
73
        value = LocalizedValue()
74
75
        for lang_code, lang_name in settings.LANGUAGES:
76
            value.set(lang_code, '<script>%s</script>' % lang_name)
77
78
        return value
79
80
    @staticmethod
81
    def _validate(non_bleached_value, bleached_value):
82
        """Validates whether the specified non-bleached
83
        value ended up being correctly bleached.
84
85
        Arguments:
86
            non_bleached_value:
87
                The value before bleaching.
88
89
            bleached_value:
90
                The value after bleaching.
91
        """
92
93
        for lang_code, _ in settings.LANGUAGES:
94
            if not non_bleached_value.get(lang_code):
95
                assert not bleached_value.get(lang_code)
96
                continue
97
98
            expected_value = bleach.clean(
99
                non_bleached_value.get(lang_code),
100
                get_bleach_default_options()
101
            )
102
103
            assert bleached_value.get(lang_code) == expected_value
104