plato.formclasses module¶
Implementation of Plato’s core formclasses API.
Plato’s core API consists out of
the
formclassdecorator to annotate classes defining the hierarchical structure of desired test data,and the
samplefunction 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 thederivedfielddecorator 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 aProviderthat is used to generate values when usingsample.Like
dataclasses, a formclass supports theInitVartype. 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) andderivedfieldmethods (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
formclassfield 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 theformclassinstance. You get access to your fields (includingInitVarfields) by declaring additional arguments for thederivedfieldusing the same name as the field.- Parameters
fn (DerivedFieldT) –
-
plato.formclasses.sample(form, context=None)[source]¶ Generates a dataclass with concrete values from a
formclassinstance.Recursively processes a
formclassinstance and returns an analogousdataclass()where allProviderhave been replaced with values generated from these providers. The returneddataclasswill also have fields added forderivedfieldannotated methods.This function uses a context to provide deterministic random number seeds based on the field names and allow information to be shared between
Providerinstances. 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
formclassinstance to be processed. But can also be aProviderinstance which will forward the call to the provider’sProvider.samplemethod. 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
formclassadataclassinstance is returned withProviderinstances replaced by sampled values andderviedfieldmethods added as fields. For aProviderthe 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