plato.formclasses module¶
Implementation of Plato’s core formclasses API.
Plato’s core API consists out of
the
formclass
decorator to annotate classes defining the hierarchical structure of desired test data,and the
sample
function to generate instances of concrete test data from instances of such form classes.
-
@
plato.formclasses.
formclass
[source]¶ Class decorator to process a class definition as formclass.
The formclass decorator is one of the main parts of the Plato API. A class annotated with it will be processed to enable Plato’s feature. In particular, it will become a
dataclass()
and support for thederivedfield
decorator will be added.Similar to a
dataclass()
, you can define fields in a formclass using type annotations. In addition to normal default values andfield()
assignments, you can assign aProvider
that is used to generate values when usingsample
.Like
dataclasses
, a formclass supports theInitVar
type. A field with such a type will not be available on the instance, but will be passed as argument to the__post_init__
method (in order of declaration) andderivedfield
methods (as keyword argument by name).Example
fake = plato.providers.faker.FromFaker() @formclass class MyFormclass: field: str = "value" generated_field: str = fake.first_name() data = sample(MyFormclass()) print(data.field) print(data.generated_field)
value Alicia
-
@
plato.formclasses.
derivedfield
[source]¶ Method decorator to derive a
formclass
field from other fields.When instantiating a
formclass
, the decorated method will be run after initializing all normal fields. The returned value will be used to add a field with the method’s name to theformclass
instance. You get access to your fields (includingInitVar
fields) by declaring additional arguments for thederivedfield
using the same name as the field.- Parameters
fn (DerivedFieldT) –
-
plato.formclasses.
sample
(form, context=None)[source]¶ Generates a dataclass with concrete values from a
formclass
instance.Recursively processes a
formclass
instance and returns an analogousdataclass()
where allProvider
have been replaced with values generated from these providers. The returneddataclass
will also have fields added forderivedfield
annotated methods.This function uses a context to provide deterministic random number seeds based on the field names and allow information to be shared between
Provider
instances. Usually it will not be necessary to provide this context as it will be automatically initialized for each top-level invocation.- Parameters
form (T) – Usually a
formclass
instance to be processed. But can also be aProvider
instance which will forward the call to the provider’sProvider.sample
method. Any other type of object will be returned unchanged.context (Optional[plato.context.Context]) – Context of the sample operation, for example, the random number seed to use. Usually this argument has not to be set manually and will be initialized automatically.
- Returns
For a
formclass
adataclass
instance is returned withProvider
instances replaced by sampled values andderviedfield
methods added as fields. For aProvider
the sampled value will be returned and for all other objects, the object itself is returned.- Return type
T
Examples
With
formclass
:fake = plato.providers.faker.FromFaker() @formclass class MyFormclass: field: str = "value" generated_field: str = fake.first_name() data = sample(MyFormclass()) print(data.field) print(data.generated_field)
value Alicia
With
Provider
:fake = plato.providers.faker.FromFaker() print(sample(fake.first_name()))
Thomas
Any other object:
print(sample("foo"))
foo