Permalink
Browse files

Merge branch 'master' of http://github.com/sorl/django-autofixture

  • Loading branch information...
2 parents bb8a5c9 + 9387242 commit ebcef485d99485448bf9e9aaf2c1651dd78d9199 @gregmuellegger committed Oct 14, 2010
View
@@ -89,7 +89,7 @@ class IGNORE_FIELD(object):
follow_m2m = {'ALL': (1,5)}
generate_m2m = False
- none_chance = 0.2
+ none_p = 0.2
tries = 1000
field_to_generator = SortedDict((
@@ -113,7 +113,7 @@ class IGNORE_FIELD(object):
constraints.unique_together_constraint]
def __init__(self, model,
- field_values=None, none_chance=None, overwrite_defaults=None,
+ field_values=None, none_p=None, overwrite_defaults=None,
constraints=None, follow_fk=None, generate_fk=None,
follow_m2m=None, generate_m2m=None):
'''
@@ -125,7 +125,7 @@ def __init__(self, model,
a ``Generator`` instance that generates a value on the fly or a
callable which takes no arguments and returns the wanted value.
- ``none_chance``: The chance (between 0 and 1, 1 equals 100%) to
+ ``none_p``: The chance (between 0 and 1, 1 equals 100%) to
assign ``None`` to nullable fields.
``overwrite_defaults``: All default values of fields are preserved
@@ -161,8 +161,8 @@ def __init__(self, model,
self.field_values = Values(self.__class__.field_values)
self.field_values += Values(field_values)
self.constraints = constraints or []
- if none_chance is not None:
- self.none_chance = none_chance
+ if none_p is not None:
+ self.none_p = none_p
if overwrite_defaults is not None:
self.overwrite_defaults = overwrite_defaults
@@ -249,7 +249,7 @@ def get_generator(self, field):
return generators.StaticGenerator(value=value)
if field.null:
- kwargs['none_chance'] = self.none_chance
+ kwargs['empty_p'] = self.none_p
if field.choices:
return generators.ChoicesGenerator(choices=field.choices, **kwargs)
if isinstance(field, related.ForeignKey):
View
@@ -31,10 +31,12 @@ def relpath(path, start=os.curdir):
class Generator(object):
coerce_type = staticmethod(lambda x: x)
- none_value = None
+ empty_value = None
+ empty_p = 0
- def __init__(self, none_chance=0, coerce=None):
- self.none_chance = none_chance
+ def __init__(self, empty_p=None, coerce=None):
+ if empty_p is not None:
+ self.empty_p = empty_p
if coerce:
self.coerce_type = coerce
@@ -45,8 +47,8 @@ def generate(self):
raise NotImplementedError
def get_value(self):
- if random.random() < self.none_chance:
- return self.none_value
+ if random.random() < self.empty_p:
+ return self.empty_value
value = self.generate()
return self.coerce(value)
@@ -76,7 +78,7 @@ def generate(self):
class NoneGenerator(Generator):
def generate(self):
- return self.none_value
+ return self.empty_value
class StringGenerator(Generator):
@@ -432,7 +434,7 @@ def generate(self):
class MultipleInstanceGenerator(InstanceGenerator):
- none_value = []
+ empty_value = []
def __init__(self, *args, **kwargs):
self.min_count = kwargs.pop('min_count', 1)
@@ -451,7 +453,7 @@ class InstanceSelector(Generator):
'''
Select one or more instances from a queryset.
'''
- none_value = []
+ empty_value = []
def __init__(self, queryset, min_count=None, max_count=None, fallback=None,
limit_choices_to=None, *args, **kwargs):
@@ -42,11 +42,11 @@ class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('-d', '--overwrite-defaults', action='store_true',
- dest='overwrite_defaults', default=False, help=
+ dest='overwrite_defaults', default=None, help=
u'Generate values for fields with default values. Default is '
u'to use default values.'),
make_option('--no-follow-fk', action='store_true', dest='no_follow_fk',
- default=False, help=
+ default=None, help=
u'Ignore foreignkeys while creating model instances.'),
make_option('--generate-fk', action='store', dest='generate_fk',
default=None, help=
@@ -55,19 +55,19 @@ class Command(BaseCommand):
u'comma sperated list of field names or ALL to indicate that '
u'all foreignkeys should be generated automatically.'),
make_option('--no-follow-m2m', action='store_true',
- dest='no_follow_m2m', default=False, help=
+ dest='no_follow_m2m', default=None, help=
u'Ignore many to many fields while creating model '
u'instances.'),
make_option('--follow-m2m', action='store', dest='follow_m2m',
- default='1:5', help=
+ default=None, help=
u'Specify minimum and maximum number of instances that are '
u'assigned to a m2m relation. Use two, colon separated '
u'numbers in the form of: min,max. Default is 1,5.\n'
u'You can limit following of many to many relations to '
u'specific fields using the following format:\n'
u'field1:min:max,field2:min:max ...'),
make_option('--generate-m2m', action='store', dest='generate_m2m',
- default='', help=
+ default=None, help=
u'Specify minimum and maximum number of instances that are '
u'newly created and assigned to a m2m relation. Use two, '
u'colon separated numbers in the form of: min:max. Default is '
@@ -122,49 +122,65 @@ def print_instance(self, sender, model, instance, **kwargs):
def handle(self, *attrs, **options):
from django.db.models import get_model
- follow_fk = not options['no_follow_fk']
- follow_m2m = not options['no_follow_m2m']
- fks = options['generate_fk']
- generate_fk = None if fks is None else fks.split(',')
-
- use = options['use']
- if use:
- use = use.split('.')
- use = getattr(import_module('.'.join(use[:-1])), use[-1])
-
error_option = None
- try:
- if follow_m2m:
- value = [i for i in options['follow_m2m'].split(',')]
+ #
+ # follow options
+ #
+ if options['no_follow_fk'] is None:
+ follow_fk = None
+ else:
+ follow_fk = False
+ if options['no_follow_m2m'] is None:
+ follow_m2m = None
+ # this is the only chance for the follow_m2m options to be parsed
+ if options['follow_m2m']:
+ try:
+ value = options['follow_m2m'].split(',')
+ if len(value) == 1 and value[0].count(':') == 1:
+ follow_m2m = [int(i) for i in value[0].split(':')]
+ else:
+ follow_m2m = {}
+ for field in value:
+ key, minval, maxval = field.split(':')
+ follow_m2m[key] = int(minval), int(maxval)
+ except ValueError:
+ error_option = '--follow-m2m=%s' % options['follow_m2m']
+ else:
+ follow_m2m = False
+ #
+ # generation options
+ #
+ if options['generate_fk'] is None:
+ generate_fk = None
+ else:
+ generate_fk = options['generate_fk'].split(',')
+ generate_m2m = None
+ if options['generate_m2m']:
+ try:
+ value = [v for v in options['generate_m2m'].split(',') if v]
if len(value) == 1 and value[0].count(':') == 1:
- follow_m2m = [int(i) for i in value[0].split(':')]
+ generate_m2m = [int(i) for i in value[0].split(':')]
else:
- follow_m2m = {}
+ generate_m2m = {}
for field in value:
key, minval, maxval = field.split(':')
- follow_m2m[key] = int(minval), int(maxval)
- except ValueError:
- error_option = '--follow-m2m=%s' % options['follow_m2m']
- try:
- value = [v for v in options['generate_m2m'].split(',') if v]
- if len(value) == 1 and value[0].count(':') == 1:
- generate_m2m = [int(i) for i in value[0].split(':')]
- else:
- generate_m2m = {}
- for field in value:
- key, minval, maxval = field.split(':')
- generate_m2m[key] = int(minval), int(maxval)
- except ValueError:
- error_option = '--generate-m2m=%s' % options['generate_m2m']
+ generate_m2m[key] = int(minval), int(maxval)
+ except ValueError:
+ error_option = '--generate-m2m=%s' % options['generate_m2m']
+
if error_option:
raise CommandError(
u'Invalid option %s\n'
u'Expected: %s=field:min:max,field2:min:max... (min and max must be numbers)' % (
error_option,
error_option.split('=', 1)[0]))
- overwrite_defaults = options['overwrite_defaults']
+ use = options['use']
+ if use:
+ use = use.split('.')
+ use = getattr(import_module('.'.join(use[:-1])), use[-1])
+ overwrite_defaults = options['overwrite_defaults']
self.verbosity = int(options['verbosity'])
models = []
@@ -206,3 +222,4 @@ def handle(self, *attrs, **options):
fixture.create(count)
else:
autofixture.create(model, count, **kwargs)
+
@@ -192,7 +192,7 @@ def test_follow_only_some_m2m(self):
filler = AutoFixture(
M2MModel,
- none_chance=0,
+ none_p=0,
follow_m2m={
'm2m': (2, 10),
})
@@ -203,7 +203,7 @@ def test_follow_only_some_m2m(self):
def test_generate_m2m(self):
filler = AutoFixture(
M2MModel,
- none_chance=0,
+ none_p=0,
generate_m2m=(1, 5))
all_m2m = set()
all_secondm2m = set()
@@ -218,7 +218,7 @@ def test_generate_m2m(self):
def test_generate_only_some_m2m(self):
filler = AutoFixture(
M2MModel,
- none_chance=0,
+ none_p=0,
generate_m2m={
'm2m': (1, 5),
})
@@ -407,12 +407,12 @@ def setUp(self):
from autofixture.management.commands.loadtestdata import Command
self.command = Command()
self.options = {
- 'overwrite_defaults': False,
- 'no_follow_fk': False,
- 'no_follow_m2m': False,
- 'generate_fk': '',
- 'follow_m2m': '1:5',
- 'generate_m2m': '',
+ 'overwrite_defaults': None,
+ 'no_follow_fk': None,
+ 'no_follow_m2m': None,
+ 'generate_fk': None,
+ 'follow_m2m': None,
+ 'generate_m2m': None,
'verbosity': '0',
'use': '',
}

0 comments on commit ebcef48

Please sign in to comment.