File: test_ddf_teaching_and_lessons.py

package info (click to toggle)
python-django-dynamic-fixture 4.0.1-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 616 kB
  • sloc: python: 3,909; makefile: 237; sh: 6
file content (223 lines) | stat: -rw-r--r-- 10,568 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import re

from django.test import TestCase
import pytest

from django_dynamic_fixture.models_test import *
from django_dynamic_fixture.ddf import *
from django_dynamic_fixture.fixture_algorithms.sequential_fixture import SequentialDataFixture


data_fixture = SequentialDataFixture()


class DDFTestCase(TestCase):
    def setUp(self):
        self.ddf = DynamicFixture(data_fixture)
        DDFLibrary.get_instance().clear()


class TeachAndLessonsTest(DDFTestCase):
    def test_teach_a_default_lesson_for_a_model(self):
        self.ddf.teach(ModelForLibrary, integer=1000)
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer == 1000

    def test_default_lesson_may_be_overrided_although_it_is_an_anti_pattern(self):
        self.ddf.teach(ModelForLibrary, integer=1000)
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer == 1000
        self.ddf.teach(ModelForLibrary, integer=1001)
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer == 1001

    def test_it_must_NOT_raise_an_error_if_user_try_to_use_a_not_saved_default_configuration(self):
        self.ddf.get(ModelForLibrary)

    def test_it_must_raise_an_error_if_try_to_set_a_static_value_to_a_field_with_unicity(self):
        with pytest.raises(InvalidConfigurationError):
            self.ddf.teach(ModelForLibrary, integer_unique=1000)

    def test_it_allows_to_use_masks_as_lessons_for_unique_integer_fields(self):
        self.ddf.teach(ModelForLibrary, integer_unique=Mask('1###'))
        instance = self.ddf.get(ModelForLibrary)
        assert 1000 <= int(instance.integer_unique) <= 1999

    def test_it_allows_to_use_masks_as_lessons_for_unique_char_fields(self):
        self.ddf.teach(ModelWithUniqueCharField, text_unique=Mask('---- ### __'))
        instance = self.ddf.get(ModelWithUniqueCharField)
        assert re.match(r'[A-Z]{4} [0-9]{3} [a-z]{2}', instance.text_unique)

    def test_it_must_accept_dynamic_values_for_fields_with_unicity(self):
        self.ddf.teach(ModelForLibrary, integer_unique=lambda field: 1000)

    def test_it_must_NOT_propagate_lessons_for_internal_dependencies(self):
        self.ddf.teach(ModelForLibrary, foreignkey=DynamicFixture(data_fixture, integer=1000))
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer != 1000
        assert instance.foreignkey.integer == 1000

    def test_it_must_use_lessons_for_internal_dependencies(self):
        # ModelForLibrary.foreignkey is a `ModelForLibrary2`
        self.ddf.teach(ModelForLibrary, integer=1000)
        self.ddf.teach(ModelForLibrary2, integer=1001)
        instance = self.ddf.get(ModelForLibrary, foreignkey=DynamicFixture(data_fixture))
        assert instance.integer == 1000
        assert instance.foreignkey.integer == 1001

    def test_it_uses_lessons_for_base_model_when_creating_a_proxy_model(self):
        self.ddf.teach(ModelForLibrary, integer=123)
        instance = self.ddf.get(ProxyModelForLibrary)
        assert instance.__class__ is ProxyModelForLibrary
        assert instance.integer == 123

    def test_it_uses_lessons_for_proxy_models_when_creating_the_base_model(self):
        self.ddf.teach(ProxyModelForLibrary, integer=456)
        instance = self.ddf.get(ModelForLibrary)
        assert instance.__class__ is ModelForLibrary
        assert instance.integer == 456

    def test_it_uses_lessons_for_proxy_models_when_creating_the_proxy_model(self):
        self.ddf.teach(ProxyModelForLibrary, integer=789)
        instance = self.ddf.get(ProxyModelForLibrary)
        assert instance.__class__ is ProxyModelForLibrary
        assert instance.integer == 789

    # Not implemented yet
    # def test_teaching_must_store_ddf_configs_too(self):
    #     self.ddf.teach(ModelForLibrary, fill_nullable_fields=False)
    #     instance = self.ddf.get(ModelForLibrary)
    #     assert instance.integer is None

    #     DDFLibrary.get_instance().clear()
    #     self.ddf.teach(ModelForLibrary, fill_nullable_fields=True)
    #     instance = self.ddf.get(ModelForLibrary)
    #     assert instance.integer is not None

    # Not implemented yet
    # def test_teaching_ddf_configs_must_NOT_be_propagated_to_another_models(self):
    #     self.ddf.teach(ModelForLibrary, fill_nullable_fields=False)
    #     instance = self.ddf.get(ModelForLibrary)
    #     assert instance.integer is None
    #     assert instance.foreignkey.integer is None

    #     DDFLibrary.get_instance().clear()
    #     self.ddf.teach(ModelForLibrary, fill_nullable_fields=True)
    #     instance = self.ddf.get(ModelForLibrary)
    #     assert instance.integer is not None
    #     assert instance.foreignkey.integer is None # not populated


class TeachingAndCustomLessonsTest(DDFTestCase):
    def test_a_model_can_have_custom_lessons(self):
        self.ddf.teach(ModelForLibrary, integer=1000, ddf_lesson=None)
        self.ddf.teach(ModelForLibrary, integer=1001, ddf_lesson='a name')
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer == 1000
        instance = self.ddf.get(ModelForLibrary, ddf_lesson='a name')
        assert instance.integer == 1001

    def test_custom_lessons_must_not_be_used_if_not_explicity_specified(self):
        self.ddf.teach(ModelForLibrary, integer=1000, ddf_lesson='a name')
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer != 1000

    def test_a_model_can_have_many_custom_lessons(self):
        self.ddf.teach(ModelForLibrary, integer=1000, ddf_lesson='a name')
        self.ddf.teach(ModelForLibrary, integer=1001, ddf_lesson='a name 2')

        instance = self.ddf.get(ModelForLibrary, ddf_lesson='a name')
        assert instance.integer == 1000

        instance = self.ddf.get(ModelForLibrary, ddf_lesson='a name 2')
        assert instance.integer == 1001

    def test_it_must_raise_an_error_if_user_try_to_use_a_not_saved_configuration(self):
        with pytest.raises(InvalidConfigurationError):
            self.ddf.get(ModelForLibrary, ddf_lesson='a not teached lesson')

    def test_default_lesson_and_custom_lesson_must_work_together(self):
        # regression test
        self.ddf.teach(ModelForLibrary, integer=1000, ddf_lesson='a name')
        self.ddf.teach(ModelForLibrary, integer=1001, ddf_lesson=True)
        self.ddf.teach(ModelForLibrary, integer=1002, ddf_lesson='a name2')
        instance = self.ddf.get(ModelForLibrary, ddf_lesson='a name')
        assert instance.integer == 1000
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer == 1001
        instance = self.ddf.get(ModelForLibrary, ddf_lesson='a name2')
        assert instance.integer == 1002

    def test_default_lesson_and_custom_lesson_must_work_together_for_different_models(self):
        # regression test
        self.ddf.teach(ModelForLibrary, integer=1000, ddf_lesson='a name')
        self.ddf.teach(ModelForLibrary, integer=1001, ddf_lesson=True)
        self.ddf.teach(ModelForLibrary, integer=1002, ddf_lesson='a name2')
        self.ddf.teach(ModelForLibrary2, integer=2000, ddf_lesson='a name')
        self.ddf.teach(ModelForLibrary2, integer=2001, ddf_lesson=True)
        self.ddf.teach(ModelForLibrary2, integer=2002, ddf_lesson='a name2')

        instance = self.ddf.get(ModelForLibrary, ddf_lesson='a name')
        assert instance.integer == 1000
        instance = self.ddf.get(ModelForLibrary)
        assert instance.integer == 1001
        instance = self.ddf.get(ModelForLibrary, ddf_lesson='a name2')
        assert instance.integer == 1002

        instance = self.ddf.get(ModelForLibrary2, ddf_lesson='a name')
        assert instance.integer == 2000
        instance = self.ddf.get(ModelForLibrary2)
        assert instance.integer == 2001
        instance = self.ddf.get(ModelForLibrary2, ddf_lesson='a name2')
        assert instance.integer == 2002


class DDFLibraryTest(TestCase):
    def setUp(self):
        self.lib = DDFLibrary()

    def test_add_and_get_configuration_without_string_name(self):
        self.lib.add_configuration(ModelForLibrary, {'a': 1})
        assert self.lib.get_configuration(ModelForLibrary) == {'a': 1}
        assert self.lib.get_configuration(ModelForLibrary, name=DDFLibrary.DEFAULT_KEY) == {'a': 1}
        assert self.lib.get_configuration(ModelForLibrary, name=None) == {'a': 1}

        self.lib.clear()
        self.lib.add_configuration(ModelForLibrary, {'a': 2}, name=None)
        assert self.lib.get_configuration(ModelForLibrary) == {'a': 2}
        assert self.lib.get_configuration(ModelForLibrary, name=DDFLibrary.DEFAULT_KEY) == {'a': 2}
        assert self.lib.get_configuration(ModelForLibrary, name=None) == {'a': 2}

        self.lib.clear()
        self.lib.add_configuration(ModelForLibrary, {'a': 3}, name=True)
        assert self.lib.get_configuration(ModelForLibrary) == {'a': 3}
        assert self.lib.get_configuration(ModelForLibrary, name=DDFLibrary.DEFAULT_KEY) == {'a': 3}
        assert self.lib.get_configuration(ModelForLibrary, name=None) == {'a': 3}

    def test_add_and_get_configuration_with_name(self):
        self.lib.add_configuration(ModelForLibrary, {'a': 1}, name='x')
        assert self.lib.get_configuration(ModelForLibrary, name='x') == {'a': 1}

    def test_clear_config(self):
        self.lib.clear_configuration(ModelForLibrary) # run ok if empty
        self.lib.add_configuration(ModelForLibrary, {'a': 1})
        self.lib.add_configuration(ModelForLibrary, {'a': 2}, name='x')
        self.lib.add_configuration(ModelForLibrary2, {'a': 3})
        self.lib.clear_configuration(ModelForLibrary)
        assert self.lib.get_configuration(ModelForLibrary) == {}
        with pytest.raises(Exception):
            self.lib.get_configuration(ModelForLibrary, name='x')
        assert self.lib.get_configuration(ModelForLibrary2) == {'a': 3}

    def test_clear(self):
        self.lib.add_configuration(ModelForLibrary, {'a': 1})
        self.lib.add_configuration(ModelForLibrary, {'a': 2}, name='x')
        self.lib.add_configuration(ModelForLibrary2, {'a': 3})
        self.lib.add_configuration(ModelForLibrary2, {'a': 4}, name='x')
        self.lib.clear()
        assert self.lib.get_configuration(ModelForLibrary) == {}
        with pytest.raises(Exception):
            self.lib.get_configuration(ModelForLibrary, name='x')
        assert self.lib.get_configuration(ModelForLibrary2) == {}
        with pytest.raises(Exception):
            self.lib.get_configuration(ModelForLibrary2, name='x')