djenerator package

Submodules

djenerator.djenerator module

Module for the exportable functions for djenerator.

djenerator.djenerator.djenerator(app_path, size, output_file, **size_options)[source]

Generates a sample data for all models in a given app and exports the data to a json file. If the file object given is None, then the data will be dumped in the current database. (A temporary database is created otherwise, while generating the data.)

Parameters:
  • app_path (str) – The path of the app.
  • size (int) – The number of models generated for each model in the models.
  • output_file (File) – A file in which the data will be dumped.
  • size_options (dict) – A dictionary that maps str:model_name to int:model_size, which is the number of generated instances for the model. If a model isn’t in size_options, then the default ‘size’ will be used.
Return type:

None

djenerator.fields_generator module

This module has a function that matches django fields to the corresponding random value generator.

djenerator.fields_generator.generate_random_value(field)[source]

Generate a random value for a given field, by matching to the corresponding random generator in values_generator.

Parameters:field (DjangoField) – A reference to the field to get values for.
Returns:A random value generated for the given field.
djenerator.fields_generator.generate_random_values(field, size=50)[source]

Generate a list of random values for a given field. The size of the output list might be less than ‘size’, if the total number of the possible values are less than ‘size’, like in Booleans.

Parameters:
  • field (DjangoField) – A reference to the field to get values for.
  • size (int) – The size of the output list.
Return type:

List

Returns:

A list of random values generated for the given field.

djenerator.generate_test_data module

This file generates random test data from sample given data for given models.

djenerator.generate_test_data.create_model(model, val)[source]

Creates a new model given a reference to it’s class and a list of the values of it’s variables.

Parameters:
  • model (DjangoModel) – A reference to the class of the model that will be created.
  • val (tuple(pair(str, ))) – A list of pairs having the format(field name, field value).
Returns:

A model with the values given.

djenerator.generate_test_data.dependencies(model)[source]

Retrieves the models the must be generated before a given model.

Parameters:model (DjangoModel) – A reference to the class of the given model.
Return type:List
Returns:list of references to the classes of the models.
djenerator.generate_test_data.dfs(instances, cur_tuple, index, to_be_computed, constraints, model, to_be_shuffled)[source]

Value generator for the fields of a given model by simulating a depth first search. The model will be saved in a (temporary) database.

The interface of the predicate should be:
boolean predicate(cur_tuple, model, field)
  • cur_tuple: List of tuples of the filled values of the field being

    filled, in the format (str:field_name , field_value).

  • model: A reference to the class of the given model.

  • field: A reference to the class of the field being generated

The function should handle that the given tuple might be not full, and it should depend that the previously generated models are stored in the temporary database, and it should return a boolean value that’s true only if the required constraint is satisfied.

Parameters:
  • instances (int) – The target number of generated instances of the model.
  • cur_tuple (List(pair(str, ))) – A list of pairs str:field_name, field_value of the values of the filled fields.
  • index (int) – The index of the field being filled in the list of fields.
  • to_be_computed (List) – A list used for accumulation of the ignored fields.
  • constraints (List) – A list of predicate functions that will constraint the output.
  • model (DjangoModel) – A reference to the class of the given model.
  • to_be_shuffled (boolean) – A boolean variable that will determine if the sample data will be shuffled or not.
Return type:

None

djenerator.generate_test_data.field_sample_values(field)[source]

Retrieves the list of sample values for a given field.

Parameters:field (DjangoField) – A reference to the class of the field.
Return type:List
Returns:A list of sample values for the given field.
djenerator.generate_test_data.generate_model(model, size, shuffle=None)[source]

Generate ‘size’ sample models given a model and stores them in a temporary data base.

Parameters:
  • model (DjangoModel) – A reference to the class of the given model.
  • size (int) – An integer of the size of the sample models to be generated.
  • shuffle (boolean) – An boolean to decide if the sample input will be shuffled or not. Shuffles by default.
Return type:

tuple

Returns:

A tuple that contains a reference to the class of the given model, and list of field that’s not computed.

djenerator.generate_test_data.generate_test_data(app_models, size, **size_options)[source]

Generates a list of ‘size’ random data for each model in the models module in the given path, If the sample data is not enough for generating ‘size’ models, then all of the sample data will be used. If the models are inconsistent then no data will be generated. The data will be stored in a temporary database used for generation.

Parameters:
  • app_models (str) – A string that contains the path of the models module.
  • size (int) – An integer that specifies the size of the generated data.
  • size_options (dict) – A dictionary of that maps a str:model_name to int:model_size, that will be used as a size of the generated models. If a model is not in size_options then the default value ‘size’ will be used.
Return type:

None

djenerator.generate_test_data.recompute(model, field)[source]

Recompute the previously ignored fields in the models.

Parameters:
  • model (DjangoModel) – A reference to the class of the given model.
  • field (DjangoField) – A reference to the class of the non-computed field.
Return type:

None

djenerator.generate_test_data.topological_sort(models)[source]

Sort a given list of models according to the dependencies of the relations between the models.

Parameters:models (List) – A list of references to the classes of the given models.
Return type:List
Returns:A list of references to the classes of the given models.

djenerator.model_reader module

This module has utility functions for reading models and their fields.

djenerator.model_reader.field_type(field)[source]

Retrieves the type of a given field.

Parameters:field (DjangoField) – A reference to the given field.
Return type:str
Returns:The type of the field.
djenerator.model_reader.is_auto_field(field)[source]

Test if a given field is an Auto-Field.

Parameters:field (DjangoField) – A reference to the given field.
Return type:boolean
Returns:A boolean value that’s true only if the given field is an Auto-Field.
djenerator.model_reader.is_instance_of_django_model(reference)[source]

Tests if a given reference is a reference to a class that extends django.db.models.Model

Parameters:reference – A given Anonymous reference.
Return type:boolean
Returns:A boolean value that is true only if the given reference is a reference to a Model class.

Test if a given field is a related field.

Parameters:field (DjangoField) – A reference to the given field.
Return type:boolean
Returns:A boolean value that is true only if the given field is related.
djenerator.model_reader.is_required(field)[source]

Test if a given field is required.

Parameters:fields (DjangoField) – A reference to the given field.
Return type:boolean
Returns:A boolean value that is true only if the given field is required.

Test if a given field is a reverse related field.

Parameters:field (DjangoField) – A reference to the given field.
Return type:boolean
Returns:A boolean value that is true only if the field is reverse related.
djenerator.model_reader.list_of_fields(model)[source]

Retrieves the fields of a given model.

Parameters:model (DjangoModel) – A reference to the class of a given model.
Return type:List(DjangoFieldClass)
Returns:A list of references to the classes of fields of the given model.
djenerator.model_reader.list_of_models(models_module, keep_abstract=None)[source]

This function filters the models from the instances in given module.

Parameters:
  • models_module – A reference to a given models’ module.
  • keep_abstact (boolean) – A boolean value that decides filtering of abstract models.
Return type:

List

Returns:

A list of reference to the classes of the models in the imported models file.

djenerator.model_reader.module_import(app_path)[source]

Imports a module in a given path.

Parameters:app_path (str) – A string that contains the path of the module and it’s name.
Returns:A reference to the module in the given path.
djenerator.model_reader.names_of_fields(model)[source]

Retrieves the names of the fields of a given model.

Parameters:model (DjagoModel) – A reference to the class of a given model.
Return type:str
Returns:A list of strings of the attributes’ names of the field of the given model.
djenerator.model_reader.relation_type(field)[source]

Retrieves the type of the relation of a given related field.

Parameters:field (DjangoField) – A reference to the given field.
Return type:str
Returns:A string that contains the type of the relation of the field.

djenerator.utility module

This module contains utiltiy functions that are used in generating data.

djenerator.utility.sort_unique_tuple(var_tuple, model)[source]

Sorts a tuple of names of a fields for a given model, in the order of which field comes first.

Parameters:
  • var_tuple (tuple) – A tuple of strings of the names of some fields in the given model.
  • model (DjangoModel) – A reference to the class of the given model.
Return type:

tuple(str)

Returns:

A tuple of the names of the fields.

djenerator.utility.sort_unique_tuples(var_tuples, model)[source]

Sort lexicographically the tuples of fields according to what appears first in the model.

Parameters:
  • var_tuples (List(tuple(str))) – a list of tuples of names of some fields.
  • model – A reference to the class of the given model.
Return type:

List(tuple(str))

Returns:

A list of tuples of names of some fields.

djenerator.utility.unique_items(var_tuple)[source]

Generate a function that can be used to check the uniqueness constraint.

Parameters:var_tuple (tuple) – A tuple of the names of the fields that should be unique together
Return type:function
Returns:
boolean function(variable, model, field):
variable: A list of tuples in the form (field name, field value) model: A reference to the class of the given model. field: A reference to the class of the given field.

djenerator.values_generator module

This module has functions that generated random values for django fields.

djenerator.values_generator.generate_big_integer()[source]
djenerator.values_generator.generate_boolean(null_allowed=False)[source]
djenerator.values_generator.generate_comma_separated_int(max_length)[source]
djenerator.values_generator.generate_date(auto_now=False, tz=None)[source]
djenerator.values_generator.generate_date_time(auto_now=False, tz=None)[source]
djenerator.values_generator.generate_decimal(max_digits, decimal_places)[source]
djenerator.values_generator.generate_email(max_length, exact_len=False)[source]
djenerator.values_generator.generate_file_name(length=15, extension='')[source]
djenerator.values_generator.generate_file_path()[source]
djenerator.values_generator.generate_float(max_digits=50, decimal_places=30)[source]
djenerator.values_generator.generate_int()[source]
djenerator.values_generator.generate_integer(bits=32, negative_allowed=True)[source]
djenerator.values_generator.generate_ip()[source]
djenerator.values_generator.generate_png(width=128, height=128)[source]
djenerator.values_generator.generate_positive_integer()[source]
djenerator.values_generator.generate_positive_small_integer()[source]
djenerator.values_generator.generate_sentence(max_length, lower=True, upper=False, digits=False, seperators=[' '], end_char=['.'], exact=False)[source]
djenerator.values_generator.generate_small_integer()[source]
djenerator.values_generator.generate_string(max_length, lower=True, upper=True, digits=True, special=True, null_allowed=False, exact_len=False)[source]
djenerator.values_generator.generate_text(max_length, exact=False)[source]
djenerator.values_generator.generate_time(auto_now=False, tz=None)[source]
djenerator.values_generator.generate_url(max_length)[source]
djenerator.values_generator.generate_uuid()[source]

Module contents