Skip to content
This repository
Browse code

some Q support

  • Loading branch information...
commit f81e01892d97b8a2225662f6d28eefae8c39a0e4 1 parent f68a252
Mikko Hellsing authored
28 mockups/generators.py
@@ -432,15 +432,17 @@ class InstanceGenerator(Generator):
432 432 '''
433 433 def __init__(self, mockup, limit_choices_to=None, *args, **kwargs):
434 434 self.mockup = mockup
435   - limit_choices_to = limit_choices_to or {}
436   - for lookup, value in limit_choices_to.items():
437   - bits = lookup.split('__')
438   - if len(bits) == 1 or \
439   - len(bits) == 2 and bits[1] in ('exact', 'iexact'):
440   - params = {
441   - bits[0]: StaticGenerator(value)
442   - }
443   - self.mockup.update_fieldname_generator(**params)
  435 + from django.db.models import Q
  436 + if not isinstance(limit_choices_to, Q): # no Q support
  437 + limit_choices_to = limit_choices_to or {}
  438 + for lookup, value in limit_choices_to.items():
  439 + bits = lookup.split('__')
  440 + if len(bits) == 1 or \
  441 + len(bits) == 2 and bits[1] in ('exact', 'iexact'):
  442 + params = {
  443 + bits[0]: StaticGenerator(value)
  444 + }
  445 + self.mockup.update_fieldname_generator(**params)
444 446 super(InstanceGenerator, self).__init__(*args, **kwargs)
445 447
446 448 def generate(self):
@@ -472,10 +474,14 @@ class InstanceSelector(Generator):
472 474 def __init__(self, queryset, min_count=None, max_count=None, fallback=None,
473 475 limit_choices_to=None, *args, **kwargs):
474 476 from django.db.models.query import QuerySet
  477 + from django.db.models import Q
475 478 if not isinstance(queryset, QuerySet):
476 479 queryset = queryset._default_manager.all()
477   - limit_choices_to = limit_choices_to or {}
478   - self.queryset = queryset.filter(**limit_choices_to)
  480 + if isinstance(limit_choices_to, Q):
  481 + self.queryset = queryset.filter(limit_choices_to)
  482 + else:
  483 + limit_choices_to = limit_choices_to or {}
  484 + self.queryset = queryset.filter(**limit_choices_to)
479 485 self.fallback = fallback
480 486 self.min_count = min_count
481 487 self.max_count = max_count
6 mockups/tests/mockups_test/models.py
@@ -2,6 +2,7 @@
2 2 import os
3 3 from datetime import datetime
4 4 from django.db import models
  5 +from django.db.models import Q
5 6
6 7
7 8 filepath = os.path.dirname(os.path.abspath(__file__))
@@ -91,6 +92,11 @@ class RelatedModel(models.Model):
91 92 limit_choices_to={'name__exact': 'foo'}, related_name='rel2',
92 93 null=True, blank=True)
93 94
  95 +class RelatedModelQ(models.Model):
  96 + related = models.ForeignKey(BasicModel, related_name='rel1q')
  97 + limitedfk = models.ForeignKey(SimpleModel,
  98 + limit_choices_to=Q(name='foo'), related_name='rel2q',
  99 + null=True, blank=True)
94 100
95 101 class O2OModel(models.Model):
96 102 o2o = models.OneToOneField(SimpleModel)
59 mockups/tests/mockups_test/tests.py
@@ -6,12 +6,11 @@
6 6 from mockups import generators
7 7 from mockups import Factory
8 8 from mockups.base import Mockup, CreateInstanceError, Link
9   -from mockups_test.models import (y2k, SimpleModel, OtherSimpleModel,
10   - DeepLinkModel1, DeepLinkModel2, NullableFKModel, BasicModel,
11   - UniqueTestModel, UniqueTogetherTestModel, RelatedModel, O2OModel,
12   - M2MModel, ThroughModel, M2MModelThrough)
  9 +from mockups_test.models import *
13 10
14 11
  12 +RELATED_MODELS = [ RelatedModel, RelatedModelQ ]
  13 +
15 14 class SimpleFactory(Factory):
16 15 name = generators.StaticGenerator('foo')
17 16
@@ -78,12 +77,13 @@ class M(Mockup):
78 77
79 78 class TestRelations(TestCase):
80 79 def test_generate_foreignkeys(self):
81   - filler = Mockup(
82   - RelatedModel,
83   - generate_fk=True)
84   - for obj in filler.create(100):
85   - self.assertEqual(obj.related.__class__, BasicModel)
86   - self.assertEqual(obj.limitedfk.name, 'foo')
  80 + for m in RELATED_MODELS:
  81 + filler = Mockup(
  82 + m,
  83 + generate_fk=True)
  84 + for obj in filler.create(100):
  85 + self.assertEqual(obj.related.__class__, BasicModel)
  86 + self.assertEqual(obj.limitedfk.name, 'foo')
87 87
88 88 def test_deep_generate_foreignkeys(self):
89 89 filler = Mockup(
@@ -105,12 +105,13 @@ def test_deep_generate_foreignkeys2(self):
105 105 self.assertEqual(obj.related.related2, None)
106 106
107 107 def test_generate_only_some_foreignkeys(self):
108   - filler = Mockup(
109   - RelatedModel,
110   - generate_fk=('related',))
111   - for obj in filler.create(100):
112   - self.assertEqual(obj.related.__class__, BasicModel)
113   - self.assertEqual(obj.limitedfk, None)
  108 + for m in RELATED_MODELS:
  109 + filler = Mockup(
  110 + m,
  111 + generate_fk=('related',))
  112 + for obj in filler.create(100):
  113 + self.assertEqual(obj.related.__class__, BasicModel)
  114 + self.assertEqual(obj.limitedfk, None)
114 115
115 116 def test_follow_foreignkeys(self):
116 117 related = Mockup(BasicModel).create()[0]
@@ -119,12 +120,13 @@ def test_follow_foreignkeys(self):
119 120 simple = SimpleModel.objects.create(name='foo')
120 121 simple2 = SimpleModel.objects.create(name='bar')
121 122
122   - filler = Mockup(
123   - RelatedModel,
124   - follow_fk=True)
125   - for obj in filler.create(100):
126   - self.assertEqual(obj.related, related)
127   - self.assertEqual(obj.limitedfk, simple)
  123 + for m in RELATED_MODELS:
  124 + filler = Mockup(
  125 + m,
  126 + follow_fk=True)
  127 + for obj in filler.create(100):
  128 + self.assertEqual(obj.related, related)
  129 + self.assertEqual(obj.limitedfk, simple)
128 130
129 131 def test_follow_only_some_foreignkeys(self):
130 132 related = Mockup(BasicModel).create()[0]
@@ -133,12 +135,13 @@ def test_follow_only_some_foreignkeys(self):
133 135 simple = SimpleModel.objects.create(name='foo')
134 136 simple2 = SimpleModel.objects.create(name='bar')
135 137
136   - filler = Mockup(
137   - RelatedModel,
138   - follow_fk=('related',))
139   - for obj in filler.create(100):
140   - self.assertEqual(obj.related, related)
141   - self.assertEqual(obj.limitedfk, None)
  138 + for m in RELATED_MODELS:
  139 + filler = Mockup(
  140 + m,
  141 + follow_fk=('related',))
  142 + for obj in filler.create(100):
  143 + self.assertEqual(obj.related, related)
  144 + self.assertEqual(obj.limitedfk, None)
142 145
143 146 def test_follow_fk_for_o2o(self):
144 147 # OneToOneField is the same as a ForeignKey with unique=True
2  setup.py
@@ -3,7 +3,7 @@
3 3
4 4 setup(
5 5 name = 'django-mockups',
6   - version = '0.4.7',
  6 + version = '0.4.8',
7 7 description = 'Provides tools to auto generate content.',
8 8 long_description = open('README.rst').read(),
9 9 author = 'Mikko Hellsing',

0 comments on commit f81e018

Please sign in to comment.
Something went wrong with that request. Please try again.