django-customfields / tests / test_project / apps / testapp / tests.py

  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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
from django.test import TestCase

from models import *

class CachedManyToManyTests(TestCase):
    def test_runtime_model(self):
        d = TestModelC()
        self.assertEquals(d.cmtm_a_cache, set())
        self.assertEquals(d.cmtm_b_cache, set())

    def test_cache_field(self):
        c = TestModelC()
        assert hasattr(c, 'cmtm_a_cache')
        assert hasattr(c, 'cmtm_b_cache')

    def test_empty(self):
        c = TestModelC()
        self.assertEquals(c.cmtm_a_cache, set())
        self.assertEquals(c.cmtm_b_cache, set())

    def test_empty_save_load(self):
        c = TestModelC()
        c.save()
        pk = c.pk
        c = TestModelC.objects.get(pk=pk)
        self.assertEquals(c.cmtm_a_cache, set())
        self.assertEquals(c.cmtm_b_cache, set())

    def test_simple_sync(self):
        a = TestModelA()
        a.save()
        b = TestModelB()
        b.save()
        c = TestModelC()
        c.save() #muste save before addign mtm relations
        c.cmtm_a.add(a)
        c.cmtm_b.add(b)
        self.assertEquals(c.cmtm_a_cache, set([a.pk]))
        self.assertEquals(c.cmtm_b_cache, set([b.pk]))
        c.save() #save and reload the object
        c = TestModelC.objects.all()[0]
        self.assertEquals(c.cmtm_a_cache, set([a.pk]))
        self.assertEquals(c.cmtm_b_cache, set([b.pk]))

    def test_multiple_sync(self):
        objs = [TestModelA() for x in range(5)]
        for o in objs:
            o.save()
        objs_pks = [obj.pk for obj in objs]
        c = TestModelC()
        c.save() #muste save before adding mtm relations
        c.cmtm_a.add(*objs)
        self.assertEquals(c.cmtm_a_cache, set(objs_pks))
        new_obj = TestModelA()
        new_obj.save()
        new_objs_pks = objs_pks + [new_obj.pk]
        c.cmtm_a.add(new_obj)
        self.assertEquals(c.cmtm_a_cache, set(new_objs_pks))
        c.cmtm_a.remove(*objs)
        self.assertEquals(c.cmtm_a_cache, set([new_obj.pk]))
        c.cmtm_a.clear()
        self.assertEquals(c.cmtm_a_cache, set())

    def test_save_load_sequence(self):
        objs = [TestModelA() for x in range(5)]
        for o in objs:
            o.save()
        objs_pks = [obj.pk for obj in objs]
        c = TestModelC()
        c.save() #muste save before adding mtm relations
        c_pk = c.pk
        c.cmtm_a.add(*objs)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set(objs_pks))
        new_obj = TestModelA()
        new_obj.save()
        new_objs_pks = objs_pks + [new_obj.pk]
        c.cmtm_a.add(new_obj)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set(new_objs_pks))
        c.cmtm_a.remove(*objs)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set([new_obj.pk]))
        c.cmtm_a.clear()
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set())

    def test_set_field(self):
        sf = cachedmtmfield.SetField()

    def test_lookup(self):
        c = TestModelC()
        c.save()
        self.assertRaises(TypeError, lambda: TestModelC.objects.filter(cmtm_a_cache=1))

class InheritedFieldTests(TestCase):
    def test_model_field_inheritance_broken_parent(self):
        b = TestModel2()
        b.save()
        b.foo = 'abc'
        self.assertEquals(b.foo, 'abc')
        self.assertEquals(b.foo_inherited_value, 'abc')
        self.assertEquals(b.is_foo_inherited, False)

        # this tests handling for broken FKs
        b = TestModel2()
        b.parent_id = 99999
        b.foo = 'abc'
        self.assertEquals(b.foo, 'abc')
        self.assertEquals(b.foo_inherited_value, 'abc')
        self.assertEquals(b.is_foo_inherited, False)

    def test_model_field_inheritance_simple(self):
        a = TestModel1(bar="123")
        a.save()

        b = TestModel2(parent=a)
        b.save()

        self.assertEquals(a.bar, '123')
        self.assertEquals(b.foo, '123')
        self.assertEquals(b.is_foo_inherited, True)

        b.foo = 'abc'
        self.assertEquals(a.bar, '123')
        self.assertEquals(b.foo, 'abc')
        self.assertEquals(b.foo_inherited_value, 'abc')
        self.assertEquals(b.is_foo_inherited, False)

        self.assertEquals(b.ifoo, '123')
        a.bar = 'qwe'
        self.assertEquals(b.ifoo, 'qwe')

        from dcfields.inheritedfield import InheritedOnlyException
        try:
            b.ifoo = 'fail'
        except InheritedOnlyException, e:
            self.assertEquals(e.args[0], "Can't set value for field ifoo on TestModel2 object (field is inherit_only). Try to set it on parent.bar.")

        else:
            self.fail("Didn't raise any TypeError")

    def test_model_field_inheritance_silly(self):
        class TestModel3_Valid(models.Model):
            parent = models.ForeignKey(TestModel1)
            foo = inheritedfield.InheritedField('parent', 'bar')
            m2mrel = inheritedfield.InheritedField('parent')


        class TestModel_Temp2(models.Model):
            parent = models.ForeignKey("TestModel_Temp1")
            m2mrel = inheritedfield.InheritedField('parent')
        class TestModel_Temp1(models.Model):
            m2mrel = models.ManyToManyField(Stuff)

    def test_model_field_inheritance_validation(self):
        try:
            class TestModel4_Invalid(models.Model):
                foo = inheritedfield.InheritedField('no_parent', 'bar')
                parent = models.ForeignKey(TestModel1)

        except TypeError, e:
            self.assertEquals(e.args[0], "InheritedField: no_parent does not exist on <class 'test_project.apps.testapp.tests.TestModel4_Invalid'>.")
        else:
            self.fail("Didn't raise any TypeError (no_parent test)")

        try:
            class TestModel5_Invalid(models.Model):
                parent = models.ForeignKey(TestModel1)
                foo = inheritedfield.InheritedField('parent', 'no_bar')
        except TypeError, e:
            self.assertEquals(e.args[0], "InheritedField: no_bar does not exist in <class 'test_project.apps.testapp.models.TestModel1'>.")
        else:
            self.fail("Didn't raise any TypeError (no_bar (target field) test)")

        try:
            class TestModel6_Invalid(models.Model):
                parent = models.ForeignKey(TestModel2)
                foo = inheritedfield.InheritedField('parent', 'bar')
            class TestModel7_Invalid(models.Model):
                parent = models.ForeignKey(TestModel6_Invalid)
                foo = inheritedfield.InheritedField('parent', 'no_bar')
        except TypeError, e:
            self.assertEquals(e.args[0], "InheritedField: no_bar does not exist in <class 'test_project.apps.testapp.tests.TestModel6_Invalid'>.")
        else:
            self.fail("Didn't raise any TypeError (no_bar (target field) test)")

    def test_parent_not_a_rel(self):
        try:
            class TestModel8_Invalid(models.Model):
                parent = models.BooleanField()
                foo = inheritedfield.InheritedField('parent', 'bar')
        except TypeError, e:
            self.assertEquals(e.args[0], "InheritedField: parent is a <class 'django.db.models.fields.BooleanField'> instead of a RelatedField.")
        else:
            self.fail("Didn't raise any TypeError (no_bar (target field) test)")

        class TestModel9_Invalid(models.Model):
            parent = models.BooleanField()
            foo = inheritedfield.InheritedField('parent', 'bar', validate=False)
        #should work :)


    def test_select_related(self):
        self.assertFalse(hasattr(TestModel8, "FIELD_INHERITANCE_REL"))
        qs = TestModel8.objects.filter()
        self.assertTrue(hasattr(TestModel8, "FIELD_INHERITANCE_REL"))
        self.assertEquals(`qs.query.select_related`, "{'parent_for_8': {'parent_for_7': {'parent_for_6': {}}}}")
        self.assertEquals(TestModel8.FIELD_INHERITANCE_REL, set(['parent_for_8__parent_for_7__parent_for_6']))
        qs = TestModel2.objects.filter()
        self.assertEquals(`qs.query.select_related`, "{'parent': {}}")

    def test_model_field_double_inheritance(self):
        a = TestModel1(bar="123")
        a.save()

        b = TestModel6(parent_for_6=a)
        b.save()

        c = TestModel7(parent_for_7=b)
        c.save()

        d = TestModel8(parent_for_8=c)
        d.save()

        self.assertEquals(b.foo, '123')
        self.assertEquals(d.goo, '123')
        self.assertEquals(b.is_foo_inherited, True)
        self.assertEquals(d.is_goo_inherited, True)

        c.boo = 'abc'
        self.assertEquals(c.boo, 'abc')
        self.assertEquals(c.is_boo_inherited, False)
        self.assertEquals(c.get_boo_display(), "abc")
        self.assertEquals(d.goo, 'abc')
        self.assertEquals(d.is_goo_inherited, True)
        self.assertEquals(d.get_goo_display(), "abc *Inherited")


class TestModelA(models.Model): # used to test cached many to many field
    x = models.CharField(max_length=1)

class TestModelB(models.Model): # used to thest cached many to many field
    x = models.CharField(max_length=1)

class TestModelC(models.Model): # used to thest cached many to many field
    cmtm_a = cachedmtmfield.CachedManyToManyField(TestModelA)
    cmtm_b = cachedmtmfield.CachedManyToManyField(TestModelB)

class CachedManyToManyTests(TestCase):
    #def test_that_is_not_a_test(self):
        #reload(cachedmtmfield)

    def test_inherited_field_compat(self):
        class TestModel3_Cached_Valid(models.Model):
            parent = models.ForeignKey(TestModel1_Cached)
            foo = inheritedfield.InheritedField('parent', 'bar')
            m2mrel = inheritedfield.InheritedField('parent')


    def test_runtime_model(self):
        class TestModelD(models.Model): # used to thest cached many to many field
            cmtm_a = cachedmtmfield.CachedManyToManyField(TestModelA)
            cmtm_b = cachedmtmfield.CachedManyToManyField(TestModelB)
        d = TestModelC()
        self.assertEquals(d.cmtm_a_cache, set())
        self.assertEquals(d.cmtm_b_cache, set())

    def test_cache_field(self):
        c = TestModelC()
        assert hasattr(c, 'cmtm_a_cache')
        assert hasattr(c, 'cmtm_b_cache')

    def test_empty(self):
        c = TestModelC()
        self.assertEquals(c.cmtm_a_cache, set())
        self.assertEquals(c.cmtm_b_cache, set())

    def test_empty_save_load(self):
        c = TestModelC()
        c.save()
        pk = c.pk
        c = TestModelC.objects.get(pk=pk)
        self.assertEquals(c.cmtm_a_cache, set())
        self.assertEquals(c.cmtm_b_cache, set())

    def test_simple_sync(self):
        a = TestModelA()
        a.save()
        b = TestModelB()
        b.save()
        c = TestModelC()
        c.save() #muste save before addign mtm relations
        c.cmtm_a.add(a)
        c.cmtm_b.add(b)
        self.assertEquals(c.cmtm_a_cache, set([a.pk]))
        self.assertEquals(c.cmtm_b_cache, set([b.pk]))
        c.save() #save and reload the object
        c = TestModelC.objects.all()[0]
        self.assertEquals(c.cmtm_a_cache, set([a.pk]))
        self.assertEquals(c.cmtm_b_cache, set([b.pk]))

    def test_multiple_sync(self):
        objs = [TestModelA() for x in range(5)]
        for o in objs:
            o.save()
        objs_pks = [obj.pk for obj in objs]
        c = TestModelC()
        c.save() #muste save before adding mtm relations
        c.cmtm_a.add(*objs)
        self.assertEquals(c.cmtm_a_cache, set(objs_pks))
        new_obj = TestModelA()
        new_obj.save()
        new_objs_pks = objs_pks + [new_obj.pk]
        c.cmtm_a.add(new_obj)
        self.assertEquals(c.cmtm_a_cache, set(new_objs_pks))
        c.cmtm_a.remove(*objs)
        self.assertEquals(c.cmtm_a_cache, set([new_obj.pk]))
        c.cmtm_a.clear()
        self.assertEquals(c.cmtm_a_cache, set())

    def test_save_load_sequence(self):
        objs = [TestModelA() for x in range(5)]
        for o in objs:
            o.save()
        objs_pks = [obj.pk for obj in objs]
        c = TestModelC()
        c.save() #muste save before adding mtm relations
        c_pk = c.pk
        c.cmtm_a.add(*objs)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set(objs_pks))
        new_obj = TestModelA()
        new_obj.save()
        new_objs_pks = objs_pks + [new_obj.pk]
        c.cmtm_a.add(new_obj)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set(new_objs_pks))
        c.cmtm_a.remove(*objs)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set([new_obj.pk]))
        c.cmtm_a.clear()
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set())

    def test_set_field(self):
        sf = cachedmtmfield.SetField()

    def test_lookup(self):
        c = TestModelC()
        c.save()
        self.assertRaises(TypeError, lambda: TestModelC.objects.filter(cmtm_a_cache=1))


######################################################




class CachedManyToManyTests(TestCase):
    #def test_that_is_not_a_test(self):
        #reload(cachedmtmfield)

    def test_inherited_field_compat(self):
        class TestModel3_Cached_Valid(models.Model):
            parent = models.ForeignKey(TestModel1_Cached)
            foo = inheritedfield.InheritedField('parent', 'bar')
            m2mrel = inheritedfield.InheritedField('parent')


    def test_runtime_model(self):
        class TestModelD(models.Model): # used to thest cached many to many field
            cmtm_a = cachedmtmfield.CachedManyToManyField(TestModelA)
            cmtm_b = cachedmtmfield.CachedManyToManyField(TestModelB)
        d = TestModelC()
        self.assertEquals(d.cmtm_a_cache, set())
        self.assertEquals(d.cmtm_b_cache, set())

    def test_cache_field(self):
        c = TestModelC()
        assert hasattr(c, 'cmtm_a_cache')
        assert hasattr(c, 'cmtm_b_cache')

    def test_empty(self):
        c = TestModelC()
        self.assertEquals(c.cmtm_a_cache, set())
        self.assertEquals(c.cmtm_b_cache, set())

    def test_empty_save_load(self):
        c = TestModelC()
        c.save()
        pk = c.pk
        c = TestModelC.objects.get(pk=pk)
        self.assertEquals(c.cmtm_a_cache, set())
        self.assertEquals(c.cmtm_b_cache, set())

    def test_simple_sync(self):
        a = TestModelA()
        a.save()
        b = TestModelB()
        b.save()
        c = TestModelC()
        c.save() #muste save before addign mtm relations
        c.cmtm_a.add(a)
        c.cmtm_b.add(b)
        self.assertEquals(c.cmtm_a_cache, set([a.pk]))
        self.assertEquals(c.cmtm_b_cache, set([b.pk]))
        c.save() #save and reload the object
        c = TestModelC.objects.all()[0]
        self.assertEquals(c.cmtm_a_cache, set([a.pk]))
        self.assertEquals(c.cmtm_b_cache, set([b.pk]))

    def test_multiple_sync(self):
        objs = [TestModelA() for x in range(5)]
        for o in objs:
            o.save()
        objs_pks = [obj.pk for obj in objs]
        c = TestModelC()
        c.save() #muste save before adding mtm relations
        c.cmtm_a.add(*objs)
        self.assertEquals(c.cmtm_a_cache, set(objs_pks))
        new_obj = TestModelA()
        new_obj.save()
        new_objs_pks = objs_pks + [new_obj.pk]
        c.cmtm_a.add(new_obj)
        self.assertEquals(c.cmtm_a_cache, set(new_objs_pks))
        c.cmtm_a.remove(*objs)
        self.assertEquals(c.cmtm_a_cache, set([new_obj.pk]))
        c.cmtm_a.clear()
        self.assertEquals(c.cmtm_a_cache, set())

    def test_save_load_sequence(self):
        objs = [TestModelA() for x in range(5)]
        for o in objs:
            o.save()
        objs_pks = [obj.pk for obj in objs]
        c = TestModelC()
        c.save() #muste save before adding mtm relations
        c_pk = c.pk
        c.cmtm_a.add(*objs)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set(objs_pks))
        new_obj = TestModelA()
        new_obj.save()
        new_objs_pks = objs_pks + [new_obj.pk]
        c.cmtm_a.add(new_obj)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set(new_objs_pks))
        c.cmtm_a.remove(*objs)
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set([new_obj.pk]))
        c.cmtm_a.clear()
        c.save()
        c = TestModelC.objects.get(pk=c_pk)
        self.assertEquals(c.cmtm_a_cache, set())

    def test_set_field(self):
        sf = cachedmtmfield.SetField()

    def test_lookup(self):
        c = TestModelC()
        c.save()
        self.assertRaises(TypeError, lambda: TestModelC.objects.filter(cmtm_a_cache=1))
        
        
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.