Test factories#
FairDM ships factory_boy factories for its models, so a portal’s tests do not have to build projects, datasets, people and credits by hand. This page lists each factory, what it fills in for you, and the arguments it takes. It does not show how to use them in a test, which How to test a portal with FairDM factories does, and it does not explain why metadata is opt-in, which Why factories create metadata only when asked does.
Import every factory from fairdm.factories:
from fairdm.factories import DatasetFactory, PersonFactory, ProjectFactory
Each factory is a DjangoModelFactory. It takes any field of its model as a keyword argument, and it has the usual factory_boy methods: create(), build(), create_batch(n) and build_batch(n). Calling the class creates a record and saves it.
Arguments every record factory takes#
ProjectFactory, DatasetFactory, SampleFactory and MeasurementFactory take these opt-in arguments. Nothing is created unless you pass one.
Argument |
Type |
Creates |
|---|---|---|
|
|
That many descriptions on the record |
|
|
The addresses of the types the descriptions take, in order |
|
|
That many key dates on the record |
|
|
The addresses of the types the dates take, in order |
Without
descriptions__typesordates__types, the types are taken in order from the record kind’s collection, sodescriptions=2on a project creates an abstract and an introduction. The vocabularies must be loaded, as the test suite does once per session withsync_vocabularies.A value that is not an
intraisesTypeError.A type that is not in the record kind’s collection raises
ValueErrorlisting the valid types.More descriptions or dates than there are types raises
ValueError. Passdescriptions__typesordates__typeswith at least that many.A record holds one description and one date of each type, which is why the types differ.
The factories do not create identifiers, keywords or credits. Use the metadata factories below, or
ContributionFactory.
The valid types for each record kind, by identifier, are:
Record |
|
|
|---|---|---|
Project |
|
|
Dataset |
|
|
Sample |
|
|
Measurement |
|
|
A type’s address is its vocabulary’s address, a slash and the identifier, as in f"{DESCRIPTION_TYPES}/introduction" with DESCRIPTION_TYPES from fairdm.core.vocabularies. ProjectDescription.get_type_concepts() and the same method on the other description and date models give the concepts in code. Every factory that makes a row with a type, and SampleFactory for status, takes the concept’s address as well as the concept.
Images#
ProjectFactory, DatasetFactory, ContributorFactory, PersonFactory and OrganizationFactory write no image unless you ask.
Argument |
Effect |
|---|---|
|
Generates a placeholder image. 800 by 600 pixels for a project or dataset, 400 by 400 for a contributor |
|
Uses that file |
Core records#
Factory |
Model |
Fills in |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
SampleFactory and MeasurementFactory are abstract. Sample and Measurement cannot be created directly, so you subclass the factory for each concrete type your portal defines. The framework ships no concrete sample or measurement factory. See How to add a factory for a portal model.
MeasurementFactory gives sample no default. Pass a concrete sample, or declare a sample in your subclass.
FAIRDM_DEFAULT_LICENSE is described in Settings.
Contributors and credits#
Factory |
Model |
Fills in |
Needs from you |
|---|---|---|---|
|
The portal’s user model, |
|
Nothing. Pass |
|
|
|
Nothing |
|
|
|
Nothing. Pass |
|
|
The fields of |
Nothing |
|
|
|
|
|
|
|
Nothing |
|
|
|
Nothing. Pass |
Personhas no username. A person signs in by email.PersonFactorybuilds a new person on each call because the email is a sequence. Passing anemailthat already exists returns that person instead of creating a second one.The unusable password of a
PersonFactoryperson suits a contributor added for attribution. Useforce_loginorforce_authenticatein a test, or pass a password.ContributorIdentifierFactorygenerates a uniquevaluebecause an identifier’s value is unique across every record that carries one, not only across contributors.ContributionFactorysets nolevel. Pass one when a test depends on what the person may do. The levels areContributionLevel.VIEW,EDITandMANAGE, fromfairdm.contrib.contributors.choices.
Metadata and relations#
These factories each create one row of a record’s metadata. None picks the record for you, so pass related, or the fields named below.
Factory |
Model |
Default |
Other defaults |
Needs from you |
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Nothing |
|
|
|
Nothing |
Faker provider#
Importing fairdm.factories registers FairDMProvider with factory_boy’s Faker, so a factory can ask for these generators by name.
Generator |
Returns |
|---|---|
|
A random point as a WKT string, |
|
Paragraphs wrapped in |
|
Paragraphs of plain text separated by blank lines. |
|
A date written as a year, a year and month, or a full date, chosen at random |
|
A random existing record. Pass |
import factory
date_collected = factory.Faker("partial_date")
fairdm.factories.utils.randint(min_value, max_value) returns a function that gives a random integer in the range, for use with factory.LazyFunction.