API Documentation¶
This document specifies SGen’s APIs.
Schema¶
- class SGen¶
- fields(is_positive: bool) List[SchemaField]¶
Returns a list of positive field generators if
is_positive=True- Parameters:
is_positive (bool) –
Trueif positive data generators need to be returned- Returns:
List of objects of type
SchemaField
- positive() Generator:¶
- Returns:
List of valid dictionaries
- Return type:
Generator object
- negative() Generator:¶
- Returns:
List of not valid dictionaries
- Return type:
Generator object
Class
SGencan be used to:Description of the data structure
Generating valid data sets
Generating not valid data sets
Example:
from sgen import Sgen, fields class User(SGen): name = fields.String(required=True) age = fields.Integer(allow_none=False) user_sgen = User() user_sgen.positive() # Will return a set of valid data user_sgen.negative() # Will return a set of not valid data
Fields¶
- class Field¶
- __init__(validate: ValidatorABC | Iterable[ValidatorABC] | None = None, positive_data_from: Callable[[], Iterable] = None, negative_data_from: Callable[[], Iterable] = None, allow_none: bool = True, required: bool = False, default: Any = None)¶
- Parameters:
validate (Any) – Data validator
positive_data_from (Callable[[], Iterable]) – Function that returns a tuple of positive data
negative_data_from (Callable[[], Iterable]) – Function that returns a tuple of negative data
allow_none (bool) –
Trueif the field can accept the valueNonerequired (bool) –
Trueif the field is requireddefault (Any) – Default value
Initializes an instance of a class
- positive()¶
Generates a common set of positive values for all internal field types
- negative()¶
Generates a common set of negative values for all internal field types
- set_positive_values()¶
- Raises:
NotImplemented – If a method is not implemented in parent class
Note
Implement this method in your data type. It should register positive values via self._register(‘some_value’)
Generates a common set of negative values for all internal field types
- set_negative_values()¶
- Raises:
NotImplemented – If a method is not implemented in parent class
Note
Implement this method in your data type. It should register negative values via self._register(‘some_value’)
Generates a common set of negative values for all internal field types
- generate(length: int)¶
- Raises:
NotImplemented – If a method is not implemented in iterable data types when using a validator
Length
Returns an iterable object of length
length
- get_step()¶
- Raises:
NotImplemented – If the method is not implemented in numeric data types when using a validator
Range
Returns the step by which the numeric value will be shifted to obtain negative values (out of the acceptable range of the
Rangevalidator)
- get_other_value(value: Any)¶
- Raises:
NotImplemented – If the method is not implemented in the data type when using validators
Equal,OneOf,NoneOf
Returns a value of the same type as
value, but not equal tovalue
- _register(for_register: Any | List[Any])¶
- Parameters:
for_register (Union[Any, List[Any]]) – Field value or list of values
Stores
for_registerin the list of field values
Note
When creating your own numeric data types, implement their method
get_precisionNote
When creating your own iterable data types, implement their method
generateClass
Fieldis the base class for all internal data typesExample:
from sgen import fields class MyInt(fields.Field) def set_positive_values(self): self._register(1) # Add a valid type value def set_negative_values(self): self._register('not_int') # Add a value that is not an instance of your data type def get_other_value(self, value: int) -> int: if value is None: return 1 return value + 1 def __add__(): pass def __sub__(): pass
- class String¶
-
- generate(length: int)¶
- Parameters:
length (int) – Length of the generated string
- Return type:
str
Generates a string of the specified length.
- get_other_value(value: str | None)¶
- Parameters:
value (str) – Undesired string value
- Return type:
str
Returns a value of type
strthat is not equal tovalue
Class
Stringis a representation of string data typesExample:
from sgen import fields, SGen, validate class User(SGen) name = fields.String(allow_none=False) address = fields.String( validate=validate.Equal(comparable='Pepega street') )
- class Integer¶
- __init__(step: int = 1, *args, **kwargs)¶
- Parameters:
step (int) – Step to generate values
- get_step()¶
- Return type:
int
Returns the step
- get_other_value(value: int | None)¶
- Parameters:
value (int) – Undesired number value
- Return type:
int
Returns a value of type
intthat is not equal tovalue
Class
Integeris a representation of an integer data typeExample:
from sgen import fields, SGen, validate class User(SGen) age = fields.Integer(validate=validate.Range(min=21))
- class Float¶
- __init__(step: float = 1, *args, **kwargs)¶
- Parameters:
step (int) – Step to generate values
- get_step()¶
- Return type:
float
Returns the step
- get_other_value(value: float | None)¶
- Parameters:
value (float) – Undesired number value
- Return type:
float
Returns a value of type
floatthat is not equal tovalue
Class
Floatis a floating point representationExample:
from sgen import fields, SGen, validate class User(SGen) balance = fields.Float( validate=validate.Range(min=0), step=0.0001 )
- class Boolean¶
-
- get_other_value(value: bool | None)¶
- Parameters:
value (bool) – Undesired number value
- Return type:
bool
Returns a value of type
boolthat is not equal tovalue
Class
Booleanis a representation of the boolean data typeExample:
from sgen import fields, SGen, validate class User(SGen) is_admin = fields.Boolean()
- class DateTime¶
- __init__(step: timedelta = timedelta(days=1), *args, **kwargs)¶
- Parameters:
step (int) – Step to generate values
- get_other_value(value: datetime | None)¶
- Parameters:
value (datetime) – Undesired number value
- Return type:
datetime
Returns a value of type
datetimethat is not equal tovalue
- get_step()¶
- Return type:
timedelta
Returns the step
Class
DateTimeis a representation of the data type datetimeExample:
from sgen import fields, SGen, validate class User(SGen) created_at = fields.DateTime()
- class Date¶
- __init__(step: timedelta = timedelta(days=1), *args, **kwargs)¶
- Parameters:
step (int) – Step to generate values
- get_other_value(value: date | None)¶
- Parameters:
value (date) – Undesired number value
- Return type:
date
Returns a value of type
datethat is not equal tovalue
- get_step()¶
- Return type:
timedelta
Returns the step
Class
Dateis a date representationExample:
from sgen import fields, SGen, validate class User(SGen) birth_date = fields.Date()
- class Collection¶
- __init__(data_type: FieldABC | 'SGen', *args, **kwargs)¶
- Parameters:
data_type (Union[FieldABC, 'SGen']) – Collection data type
- _register(for_register: Any | List[Any])¶
- Parameters:
for_register (Union[Any, List[Any]]) – Logged value or list of logged values
Adds a new value/values to the field’s list of values if it is not already present Additionally, it clears lists of the value
Missing, since doing this at the class levelSGenis inconvenient
- set_positive_values()¶
- Return type:
None
Registers a set of positive values for a type
Collection
- set_negative_values()¶
- Return type:
None
Registers a set of negative values for a type
Collection
- generate(length: int)¶
- Parameters:
length (int) – Length of the generated collection
- Return type:
List[Any]
Generates a collection
- get_other_value(value: list | None)¶
- Parameters:
value (list) – Undesired number value
- Return type:
list
Returns a value of type
listthat is not equal tovalue
Class
Collectionis a list representationExample:
from sgen import fields, SGen, validate class Storage(SGen) user_ids = fields.Collection( data_type=fields.Integer() )
- class Nested¶
-
Class
Nestedis an implementation of nested entitiesExample:
from sgen import fields, SGen, validate class Wallet(SGen): currency = fields.String( validate=validate.OneOf(choices=['RUB', 'EU', '$']), required=True, allow_none=False, ) amount = fields.Integer( validate=validate.Range(min=0), required=True, allow_none=False, ) class User(SGen): wallet = fields.Nested(data_type=Wallet(), required=True)
Validators¶
- class Length¶
- __init__(min: int = None, max: int = None, min_inclusive: bool = True, max_inclusive: bool = True)¶
- Parameters:
min (int) – Minimum length
max (int) – Maximum length
min_inclusive (bool) – True, if you need to include
minin the range of valid length valuesmax_inclusive (bool) – True, if you need to include
maxin the range of valid length values
- positive(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a positive data set according to the validation parameters.
- negative(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a negative data set according to the validation parameters.
Represents a collection or string length validator.
Example:
from sgen import fields, SGen, validate class User(SGen): name = fields.String( validate=validate.Length(min=1, max=10) )
Note
For the validator to work correctly, the data type must implement the method
generateof classField
- class Range¶
- __init__(min: int = None, max: int = None, min_inclusive: bool = True, max_inclusive: bool = True)¶
- Parameters:
min (int) – Minimum range limit
max (int) – Maximum range limit
min_inclusive (bool) – True, if you need to include
minin the range of acceptable valuesmax_inclusive (bool) – True, if you need to include
maxin the range of acceptable values
- _get_min(data_type: Field, positive: bool = True)¶
- Parameters:
data_type (Field) – Data type.
positive (bool) – Positive or negative meaning.
- Returns:
Minimum range limit.
Returns the minimum limit of a range
- _get_max(data_type: Field, positive: bool = True)¶
- Parameters:
data_type (Field) – Data type.
positive (bool) – Positive or negative meaning.
- Returns:
Maximum range limit.
Returns the maximum limit of a range
- positive(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a positive data set according to the validation parameters.
- negative(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a negative data set according to the validation parameters.
Represents a number value range validator.
Example:
from sgen import fields, SGen, validate class User(SGen): age = fields.Integer( validate=validate.Range(min=18, max=100) )
Note
For the validator to work correctly, the data type must implement the method
get_stepof classField
- class Equal¶
- __init__(comparable: Any)¶
- Parameters:
comparable (Any) – The value that a field will take in a positive data set
- positive(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a positive data set according to the validation parameters.
- negative(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a negative data set according to the validation parameters.
Represents an equality validator
Example:
from sgen import fields, SGen, validate class User(SGen): age = fields.Integer( validate=validate.Equal(comparable=999) )
Note
For the validator to work correctly, the data type must implement the method
get_other_valueof classField
- class OneOf¶
- __init__(choices: List[Any])¶
- Parameters:
choices (List[Any]) – List of valid field values
- positive(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a positive data set according to the validation parameters.
- negative(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a negative data set according to the validation parameters.
Represents a validator for selecting a valid value from a list
Example:
from sgen import fields, SGen, validate class User(SGen): age = fields.String( validate=validate.OneOf(choices=['Pepega', 'Aboba', 'PSFP5']) )
Note
For the validator to work correctly, the data type must implement the method
get_other_valueof classField
- class NoneOf¶
- __init__(invalid_values: List[Any])¶
- Parameters:
invalid_values (List[Any]) – List of not valid field values
- positive(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a positive data set according to the validation parameters.
- negative(data_type: Field)¶
- Parameters:
data_type (Field) – Type of data to be validated.
- Return type:
List[Any]
Generates a negative data set according to the validation parameters.
Allows you to specify not valid values for a field
Example:
from sgen import fields, SGen, validate class User(SGen): is_admin = fields.Bool( validate=validate.NoneOf(invalid_values=[False]) )
Note
For the validator to work correctly, the data type must implement the method
get_other_valueof classField