class UserAutoFixture(AutoFixture): field_values = { 'username': generators.ChoicesGenerator(choices=user_usernames), 'password': generators.StaticGenerator(make_password('demo')), 'first_name': generators.ChoicesGenerator(choices=user_first_names), 'last_name': generators.ChoicesGenerator(choices=user_last_names), }
class OrderAutoFixture(AutoFixture): field_values = { 'name': generators.ChoicesGenerator(values=("Eggs", "Cheese", "Bacon", "Milk", "Oranges", "Apples", "Pears", "Lemons")), 'quantity': generators.ChoicesGenerator(values=("1", )), 'date': generators.ChoicesGenerator(values=('01.02.2019', )), }
class ProductAutoFixture(AutoFixture): field_values = { 'name': generators.ChoicesGenerator(values=("Eggs", "Cheese", "Bacon", "Milk", "Oranges", "Apples", "Pears", "Lemons")), 'description': generators.ChoicesGenerator(values=("A type of food.", "Something to consume.", "Very tasty!", "Healthy.", "Now on sale!")), 'price': generators.ChoicesGenerator(values=('15.45', '2.00', '14.00', '10.05')), }
class Values: title = generators.ChoicesGenerator(values=TITLES) desc = 'About Job Seeker' gender = generators.ChoicesGenerator(values=GENDERS) owner = generators.ChoicesGenerator(values=RECRUITERS) industry = generators.ChoicesGenerator(values=INDUSTRIES) location = generators.ChoicesGenerator(values=LOCATIONS) skills = generators.ChoicesGenerator(values=JOBSKILLS) experience = random.choice(EXPERIENCE) min_age = generators.ChoicesGenerator(values=MIN_AGE) max_age = generators.ChoicesGenerator(values=MAX_AGE) edu = generators.ChoicesGenerator(values=EDU)
class SetCollectionAutoFixture(AutoFixture): field_values = { 'set_collection_name': generators.ChoicesGenerator( values="Peter MacStewart, Museum of Modern Art, Heath, ".split( ',')) }
class ExhibitionLookupAutoFixture(AutoFixture): field_values = { 'exhibition_name': generators.ChoicesGenerator( values= "Arbitrary Relevance: 15 Years of Progress, Decadent Chemistry: Edith Heath and the Avant Garde, The Politics of Extravaganza: A Juried Show of Change, Parsing Charm: The Ceramic Art of Dilettantism, The Politics of Possibilities: Constructing a Praxis of Sameness, Ravilious, Royal Academy Summer Exhibition" .split(',')) }
class Values(object): ST_DV, DIS, TWN = generate_triple() RST_DV, RDIS, RTWN = generate_triple() CIR = 1 NMULTI_B = generators.ChoicesGenerator(choices=list( itertools.chain.from_iterable( dict(F101.NMULTI_B_CHOICES).values())))
class ProjectAutoFixture(AutoFixture): field_values = { 'name': generators.CallableGenerator(fake.company), 'slug': generators.CallableGenerator(fake.slug), 'image': generators.ImageGenerator(sizes=IMAGESIZES), 'is_draft': generators.ChoicesGenerator(values=[True, False, False]), }
class PieceAutoFixture(AutoFixture): field_values = { 'cataloguer': generators.ChoicesGenerator( values= "Erminia Galdamez, Angila Weekly, Milton Burnley, Earle Hampshire, Lael Warford, Leilani Dennis, Porsha Nodine, Tammie Wert, Tonie Schweinsberg, Meryl Belville, Latricia Landrum, Margorie Galeano, Josiah Torgrimson, Kurt Picklesimer, Veola Callaham, Rolf Humphery, Shari Carpenter , Belen Hott " .split(',')) }
class GlazeLookupAutoFixture(AutoFixture): field_values = { 'glaze_name': generators.ChoicesGenerator( values= "aqua,black,blue,fuchsia,gray,green,lime,maroon,navy, olive,orange,purple,red,silver,teal,white,yellow" .split(',')) }
class PublicationLookupAutoFixture(AutoFixture): field_values = { 'publication_name': generators.ChoicesGenerator( values= "The Judicious Ceramicist, The Methodical Ceramicist, The Meticulous Ceramicist, The Punctilious Ceramicist, Ceramic News, The Ceramic Advisor, Ceramic Intelligence" .split(',')) }
class DocumentationAutoFixture(AutoFixture): field_values = { 'documentation_name': generators.ChoicesGenerator( values= "annual report, charter, concept statement , dossier, Green Paper, invitation to tender, palimpsest, screed, wordle, vignette" .split(',')) }
class CarAutoFixture(AutoFixture): field_values = { 'name': generators.ChoicesGenerator(values=("Demio", "Rio", "Accent", "Fit", "Fiesta", "Sonic", "Versa", "Mirage")), 'body_type': generators.ChoicesGenerator(values=("City Cars", "Superminis", "Hatchbacks", "Mini-vps", "MPVs", "Saloon", "Estates", "Four-door Coupes", "Crossovers", "SUVs", "Cabriolet/convertibles")), 'brand': generators.ChoicesGenerator(values=("BMW", "Ford", "Honda", "Jeep", "Toyota", "Mazda", "Audi", "Fiat", "Citroen", "Mitsubishi")), 'color': generators.ChoicesGenerator(values=("Grey", "White", "Black", "Silver", "Red", "Brown", "Blue", "Dark Blue", "Dark Green", "Purple")), 'condition': generators.ChoicesGenerator(values=("EXCELLENT", "FINE", "VERY GOOD", "GOOD", "OKAY")), 'fuel_type': generators.ChoicesGenerator(values=("Premium unleaded petrol", "Super unleaded petrol", "Diesel", "LPG Autogas", "Biodiesel", "Bioethanol")), 'image_url': generators.ChoicesGenerator( values=("http://127.0.0.1:8000/static/images/pic01.jpg", "http://127.0.0.1:8000/static/images/pic02.jpg", "http://127.0.0.1:8000/static/images/pic03.jpg", "http://127.0.0.1:8000/static/images/pic04.jpg", "http://127.0.0.1:8000/static/images/pic05.jpg")), 'price_per_unit': generators.ChoicesGenerator(values=(50, 60, 45, 110, 51, 90, 80, 144, 97, 56)), 'seats': generators.ChoicesGenerator(values=(1, 2, 3, 4)), 'year': generators.ChoicesGenerator(values=("2001", "2002", "2003", "2015", "2016", "2017", "2018")), }
class PhaseAutoFixture(AutoFixture): field_values = { 'name': generators.CallableGenerator(fake.company), 'type': generators.ChoicesGenerator(choices=content.as_choices()), } follow_pk = True constraints = (only_two_phases_per_module)
class RoleVersionAutoFixture(AutoFixture): choices = [] for major in range(0, 3): for minor in range(0, 9): choices.append("v%d.%d" % (major, minor)) field_values = { 'name': generators.ChoicesGenerator(values=choices), 'loose_version': generators.StaticGenerator("0.0"), }
class ProductAutoFixture(AutoFixture): field_values = { 'name': generators.ChoicesGenerator(values=( "Iphone", "Sumsung", "Xiaomi", "Watches", "Honor", "Redmi", )), 'description': generators.ChoicesGenerator(values=("Some phone", "Very good", "Best phone ever", "Now on sale!")), 'price': generators.ChoicesGenerator(values=('15.45', '2.00', '14.00', '10.05')), }
class CarAutoFixture(AutoFixture): field_values = { 'name': generators.StringGenerator(min_length=3, max_length=12), 'launched': generators.DateTimeGenerator(min_date=datetime(1950, 1, 1), max_date=datetime(2015, 12, 31)), 'type': generators.ChoicesGenerator(values=('se', '4x', 'co', 'br')), }
class ManufacturerAutoFixture(AutoFixture): field_values = { 'name': generators.StringGenerator(min_length=3, max_length=12), 'created': generators.DateTimeGenerator(min_date=datetime(1900, 1, 1), max_date=datetime(2015, 12, 31)), 'country_code': generators.ChoicesGenerator(values=(COUNTRIES.keys())), }
class UserRegistration(AutoFixture): field_values = { 'name': generators.StaticGenerator('user_registration'), 'datetime': generators.DateTimeGenerator(min_date=datetime(2018, 1, 1), max_date=datetime(2018, 1, 30)), 'increment': generators.ChoicesGenerator(values=(-1, 1, 1, 1, 1, 1, 1, 1, 1)) }
def test_field_values(self): int_value = 1 char_values = (u'a', u'b') filler = AutoFixture( BasicModel, field_values={ 'intfield': 1, 'chars': generators.ChoicesGenerator(values=char_values), 'shortchars': lambda: u'ab', }) for obj in filler.create(100): self.assertEqual(obj.intfield, int_value) self.assertTrue(obj.chars in char_values) self.assertEqual(obj.shortchars, u'ab')
class CarAutoFixture(AutoFixture): field_values = { 'model_year': generators.DateGenerator(), 'registered_day': generators.DateGenerator(), 'color': generators.ChoicesGenerator(COLOR_CHOICES), 'fuel': generators.ChoicesGenerator(Car.FUEL_CHOICES), 'sub_model': generators.ChoicesGenerator(SUB_MODEL_CHOICES), 'gear': generators.ChoicesGenerator(Car.GEAR_CHOICES), 'kms': generators.PositiveIntegerGenerator(), 'region': generators.ChoicesGenerator(REGION_CHOICE), 'status': generators.ChoicesGenerator(Car.STATUS_CHOICES), }
class CategoryAutoFixture(AutoFixture): field_values = { 'name': generators.ChoicesGenerator(values=("Food", "Fruit", "Veg")), }
def handle(self, *args, **options): AutoFixture( Metric, field_values={ 'name': generators.StaticGenerator('user_registration'), 'datetime': generators.DateTimeGenerator(min_date=datetime(2018, 1, 1), max_date=datetime(2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 'data': generators.StaticGenerator({'dummy': True}), }).create(160) AutoFixture(Metric, field_values={ 'name': generators.StaticGenerator('event_registration'), 'datetime': generators.DateGenerator(min_date=datetime(2018, 1, 1), max_date=datetime( 2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(2, )), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 1, 'event_id': 1 }), }).create(int(114 * 3)) AutoFixture(Metric, field_values={ 'name': generators.StaticGenerator('event_attendance'), 'datetime': generators.DateGenerator(min_date=datetime(2018, 1, 1), max_date=datetime( 2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(1, )), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 1, 'event_id': 1 }), }).create(int(114 * 3)) AutoFixture( Metric, field_values={ 'name': generators.StaticGenerator('group_join'), 'datetime': generators.DateTimeGenerator(min_date=datetime(2018, 1, 1), max_date=datetime(2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 1, 'event_id': 1 }), }).create(140) AutoFixture( Metric, field_values={ 'name': generators.StaticGenerator('create_event'), 'datetime': generators.DateTimeGenerator(min_date=datetime(2018, 1, 1), max_date=datetime(2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 1, 'event_id': 1 }), }).create(140) AutoFixture(Metric, field_values={ 'name': generators.StaticGenerator('event_registration'), 'datetime': generators.DateGenerator(min_date=datetime(2018, 1, 1), max_date=datetime( 2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(2, )), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 2 }), }).create(int(114 * 3)) AutoFixture(Metric, field_values={ 'name': generators.StaticGenerator('event_attendance'), 'datetime': generators.DateGenerator(min_date=datetime(2018, 1, 1), max_date=datetime( 2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(1, )), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 2 }), }).create(int(114 * 3)) AutoFixture( Metric, field_values={ 'name': generators.StaticGenerator('group_join'), 'datetime': generators.DateTimeGenerator(min_date=datetime(2018, 1, 1), max_date=datetime(2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 2 }), }).create(140) AutoFixture( Metric, field_values={ 'name': generators.StaticGenerator('create_event'), 'datetime': generators.DateTimeGenerator(min_date=datetime(2018, 1, 1), max_date=datetime(2018, 4, 24)), 'increment': generators.ChoicesGenerator(values=(-1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 'data': generators.StaticGenerator({ 'dummy': True, 'group_id': 2 }), }).create(140)
class HeathLineLookupAutoFixture(AutoFixture): field_values = { 'heath_line_name': generators.ChoicesGenerator( values="Coupe, Rim, Chez Pannise, Alabama Chanin".split(',')) }
def get_generator(self, field): ''' Return a value generator based on the field instance that is passed to this method. This function may return ``None`` which means that the specified field will be ignored (e.g. if no matching generator was found). ''' if isinstance(field, fields.AutoField): return None if self.is_inheritance_parent(field): return None if ( field.default is not fields.NOT_PROVIDED and not self.overwrite_defaults and field.name not in self.field_values): return None kwargs = {} if field.name in self.field_values: value = self.field_values[field.name] if isinstance(value, generators.Generator): return value elif isinstance(value, AutoFixture): return generators.InstanceGenerator(autofixture=value) elif callable(value): return generators.CallableGenerator(value=value) return generators.StaticGenerator(value=value) if field.null: kwargs['empty_p'] = self.none_p if field.choices: return generators.ChoicesGenerator(choices=field.choices, **kwargs) if isinstance(field, related.ForeignKey): # if generate_fk is set, follow_fk is ignored. is_self_fk = (get_remote_field_to(field)().__class__ == self.model) if field.name in self.generate_fk and not is_self_fk: return generators.InstanceGenerator( autofixture.get( get_remote_field_to(field), follow_fk=self.follow_fk.get_deep_links(field.name), generate_fk=self.generate_fk.get_deep_links(field.name)), limit_choices_to=get_remote_field(field).limit_choices_to) if field.name in self.follow_fk: selected = generators.InstanceSelector( get_remote_field_to(field), limit_choices_to=get_remote_field(field).limit_choices_to) if selected.get_value() is not None: return selected if field.blank or field.null: return generators.NoneGenerator() if is_self_fk and not field.null: raise CreateInstanceError( u'Cannot resolve self referencing field "%s" to "%s" without null=True' % ( field.name, '%s.%s' % ( get_remote_field_to(field)._meta.app_label, get_remote_field_to(field)._meta.object_name, ) )) raise CreateInstanceError( u'Cannot resolve ForeignKey "%s" to "%s". Provide either ' u'"follow_fk" or "generate_fk" parameters.' % ( field.name, '%s.%s' % ( get_remote_field_to(field)._meta.app_label, get_remote_field_to(field)._meta.object_name, ) )) if isinstance(field, related.ManyToManyField): if field.name in self.generate_m2m: min_count, max_count = self.generate_m2m[field.name] return generators.MultipleInstanceGenerator( autofixture.get(get_remote_field_to(field)), limit_choices_to=get_remote_field(field).limit_choices_to, min_count=min_count, max_count=max_count, **kwargs) if field.name in self.follow_m2m: min_count, max_count = self.follow_m2m[field.name] return generators.InstanceSelector( get_remote_field_to(field), limit_choices_to=get_remote_field(field).limit_choices_to, min_count=min_count, max_count=max_count, **kwargs) if field.blank or field.null: return generators.StaticGenerator([]) raise CreateInstanceError( u'Cannot assign instances of "%s" to ManyToManyField "%s". ' u'Provide either "follow_m2m" or "generate_m2m" argument.' % ( '%s.%s' % ( get_remote_field_to(field)._meta.app_label, get_remote_field_to(field)._meta.object_name, ), field.name, )) if isinstance(field, fields.FilePathField): return generators.FilePathGenerator( path=field.path, match=field.match, recursive=field.recursive, max_length=field.max_length, **kwargs) if isinstance(field, fields.CharField): if isinstance(field, fields.SlugField): generator = generators.SlugGenerator elif isinstance(field, fields.EmailField): return generators.EmailGenerator( max_length=min(field.max_length, 30)) elif isinstance(field, fields.URLField): return generators.URLGenerator( max_length=min(field.max_length, 25)) elif field.max_length > 15: return generators.LoremSentenceGenerator( common=False, max_length=field.max_length) else: generator = generators.StringGenerator return generator(max_length=field.max_length) if isinstance(field, fields.DecimalField): return generators.DecimalGenerator( decimal_places=field.decimal_places, max_digits=field.max_digits) if hasattr(fields, 'BigIntegerField'): if isinstance(field, fields.BigIntegerField): return generators.IntegerGenerator( min_value=-field.MAX_BIGINT - 1, max_value=field.MAX_BIGINT, **kwargs) if isinstance(field, ImageField): return generators.ImageGenerator(storage=field.storage, **kwargs) for field_class, generator in self.field_to_generator.items(): if isinstance(field, field_class): return generator(**kwargs) return None
class MaterialLookupAutoFixture(AutoFixture): field_values = { 'material_name': generators.ChoicesGenerator( values="Clay, Slip, Mixed materials".split(',')) }
class MethodLookupAutoFixture(AutoFixture): field_values = { 'method_name': generators.ChoicesGenerator( values="Hand thrown, Hand built, Cast, Jiggered".split(',')) }
class CategoryAutoFixture(AutoFixture): field_values = { 'name': generators.ChoicesGenerator(values=("Phone", "Headphones", "Watches")), }
class LogoAutoFixture(AutoFixture): field_values = { 'Logo_name': generators.ChoicesGenerator( values="Stamped, Incised, Drawn".split(',')) }
class TeamAutoFixture(AutoFixture): field_values = { 'name': generators.ChoicesGenerator(choices=team_names), 'created_by_id': 1 }