Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

some Q support

  • Loading branch information...
commit f81e01892d97b8a2225662f6d28eefae8c39a0e4 1 parent f68a252
@sorl authored
View
28 mockups/generators.py
@@ -432,15 +432,17 @@ class InstanceGenerator(Generator):
'''
def __init__(self, mockup, limit_choices_to=None, *args, **kwargs):
self.mockup = mockup
- limit_choices_to = limit_choices_to or {}
- for lookup, value in limit_choices_to.items():
- bits = lookup.split('__')
- if len(bits) == 1 or \
- len(bits) == 2 and bits[1] in ('exact', 'iexact'):
- params = {
- bits[0]: StaticGenerator(value)
- }
- self.mockup.update_fieldname_generator(**params)
+ from django.db.models import Q
+ if not isinstance(limit_choices_to, Q): # no Q support
+ limit_choices_to = limit_choices_to or {}
+ for lookup, value in limit_choices_to.items():
+ bits = lookup.split('__')
+ if len(bits) == 1 or \
+ len(bits) == 2 and bits[1] in ('exact', 'iexact'):
+ params = {
+ bits[0]: StaticGenerator(value)
+ }
+ self.mockup.update_fieldname_generator(**params)
super(InstanceGenerator, self).__init__(*args, **kwargs)
def generate(self):
@@ -472,10 +474,14 @@ class InstanceSelector(Generator):
def __init__(self, queryset, min_count=None, max_count=None, fallback=None,
limit_choices_to=None, *args, **kwargs):
from django.db.models.query import QuerySet
+ from django.db.models import Q
if not isinstance(queryset, QuerySet):
queryset = queryset._default_manager.all()
- limit_choices_to = limit_choices_to or {}
- self.queryset = queryset.filter(**limit_choices_to)
+ if isinstance(limit_choices_to, Q):
+ self.queryset = queryset.filter(limit_choices_to)
+ else:
+ limit_choices_to = limit_choices_to or {}
+ self.queryset = queryset.filter(**limit_choices_to)
self.fallback = fallback
self.min_count = min_count
self.max_count = max_count
View
6 mockups/tests/mockups_test/models.py
@@ -2,6 +2,7 @@
import os
from datetime import datetime
from django.db import models
+from django.db.models import Q
filepath = os.path.dirname(os.path.abspath(__file__))
@@ -91,6 +92,11 @@ class RelatedModel(models.Model):
limit_choices_to={'name__exact': 'foo'}, related_name='rel2',
null=True, blank=True)
+class RelatedModelQ(models.Model):
+ related = models.ForeignKey(BasicModel, related_name='rel1q')
+ limitedfk = models.ForeignKey(SimpleModel,
+ limit_choices_to=Q(name='foo'), related_name='rel2q',
+ null=True, blank=True)
class O2OModel(models.Model):
o2o = models.OneToOneField(SimpleModel)
View
59 mockups/tests/mockups_test/tests.py
@@ -6,12 +6,11 @@
from mockups import generators
from mockups import Factory
from mockups.base import Mockup, CreateInstanceError, Link
-from mockups_test.models import (y2k, SimpleModel, OtherSimpleModel,
- DeepLinkModel1, DeepLinkModel2, NullableFKModel, BasicModel,
- UniqueTestModel, UniqueTogetherTestModel, RelatedModel, O2OModel,
- M2MModel, ThroughModel, M2MModelThrough)
+from mockups_test.models import *
+RELATED_MODELS = [ RelatedModel, RelatedModelQ ]
+
class SimpleFactory(Factory):
name = generators.StaticGenerator('foo')
@@ -78,12 +77,13 @@ class M(Mockup):
class TestRelations(TestCase):
def test_generate_foreignkeys(self):
- filler = Mockup(
- RelatedModel,
- generate_fk=True)
- for obj in filler.create(100):
- self.assertEqual(obj.related.__class__, BasicModel)
- self.assertEqual(obj.limitedfk.name, 'foo')
+ for m in RELATED_MODELS:
+ filler = Mockup(
+ m,
+ generate_fk=True)
+ for obj in filler.create(100):
+ self.assertEqual(obj.related.__class__, BasicModel)
+ self.assertEqual(obj.limitedfk.name, 'foo')
def test_deep_generate_foreignkeys(self):
filler = Mockup(
@@ -105,12 +105,13 @@ def test_deep_generate_foreignkeys2(self):
self.assertEqual(obj.related.related2, None)
def test_generate_only_some_foreignkeys(self):
- filler = Mockup(
- RelatedModel,
- generate_fk=('related',))
- for obj in filler.create(100):
- self.assertEqual(obj.related.__class__, BasicModel)
- self.assertEqual(obj.limitedfk, None)
+ for m in RELATED_MODELS:
+ filler = Mockup(
+ m,
+ generate_fk=('related',))
+ for obj in filler.create(100):
+ self.assertEqual(obj.related.__class__, BasicModel)
+ self.assertEqual(obj.limitedfk, None)
def test_follow_foreignkeys(self):
related = Mockup(BasicModel).create()[0]
@@ -119,12 +120,13 @@ def test_follow_foreignkeys(self):
simple = SimpleModel.objects.create(name='foo')
simple2 = SimpleModel.objects.create(name='bar')
- filler = Mockup(
- RelatedModel,
- follow_fk=True)
- for obj in filler.create(100):
- self.assertEqual(obj.related, related)
- self.assertEqual(obj.limitedfk, simple)
+ for m in RELATED_MODELS:
+ filler = Mockup(
+ m,
+ follow_fk=True)
+ for obj in filler.create(100):
+ self.assertEqual(obj.related, related)
+ self.assertEqual(obj.limitedfk, simple)
def test_follow_only_some_foreignkeys(self):
related = Mockup(BasicModel).create()[0]
@@ -133,12 +135,13 @@ def test_follow_only_some_foreignkeys(self):
simple = SimpleModel.objects.create(name='foo')
simple2 = SimpleModel.objects.create(name='bar')
- filler = Mockup(
- RelatedModel,
- follow_fk=('related',))
- for obj in filler.create(100):
- self.assertEqual(obj.related, related)
- self.assertEqual(obj.limitedfk, None)
+ for m in RELATED_MODELS:
+ filler = Mockup(
+ m,
+ follow_fk=('related',))
+ for obj in filler.create(100):
+ self.assertEqual(obj.related, related)
+ self.assertEqual(obj.limitedfk, None)
def test_follow_fk_for_o2o(self):
# OneToOneField is the same as a ForeignKey with unique=True
View
2  setup.py
@@ -3,7 +3,7 @@
setup(
name = 'django-mockups',
- version = '0.4.7',
+ version = '0.4.8',
description = 'Provides tools to auto generate content.',
long_description = open('README.rst').read(),
author = 'Mikko Hellsing',
Please sign in to comment.
Something went wrong with that request. Please try again.