Permalink
Browse files

some Q support

  • Loading branch information...
sorl committed May 3, 2011
1 parent f68a252 commit f81e01892d97b8a2225662f6d28eefae8c39a0e4
Showing with 55 additions and 40 deletions.
  1. +17 −11 mockups/generators.py
  2. +6 −0 mockups/tests/mockups_test/models.py
  3. +31 −28 mockups/tests/mockups_test/tests.py
  4. +1 −1 setup.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
@@ -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)
@@ -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
@@ -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',

0 comments on commit f81e018

Please sign in to comment.