# plugins_api.testing

*module*

Helpers for the tests of a plugin.

They need ``jsonschema``, which the ``testing`` extra installs
(``plugins-api[testing]``). Import them from this module in the plugin's own
tests; madbot itself does not use them.

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/plugin_api/plugins_api/testing.py#L1-1)

## plugins_api.testing.assert_imported_metadata_valid

*function*

```python
def assert_imported_metadata_valid(plugin: type[MetadataFieldDefinitionPlugin] | MetadataFieldDefinitionPlugin, results: Mapping[str, list[PluginMetadata] | PluginError] | Iterable[PluginMetadata], madbot_field_schemas: Mapping[str, dict[str, Any]] | None = None) -> None
```

Check that madbot will store every metadata value a plugin returns.

Use it in the tests of a data plugin that defines metadata fields, on the
result of ``get_metadata_for_data_objects``. Each value must target a
field of the plugin (``source`` set to the plugin slug) or a field of the
madbot referential (``source="referential"``), and match the JSON schema
of that field. Madbot skips any other value when it imports metadata.

**Parameters**

- `plugin` (`MetadataFieldDefinitionPlugin class or instance`) — The plugin that returned the metadata. Its fields are read from ``get_all_metadata_object_types``.
- `results` (`dict[str, list[PluginMetadata] | PluginError] or list[PluginMetadata]`) — What ``get_metadata_for_data_objects`` returned (per-id errors are ignored), or a plain list of metadata.
- `madbot_field_schemas` (`dict[str, dict]`) (default: `None`) — JSON schema of madbot referential fields, by slug. A value for a referential field is checked against its schema when given here; otherwise only its source is checked, since the referential lives in madbot.

**Raises**

- `AssertionError` — If a value targets a field the plugin does not define, a source that
is neither the plugin nor the referential, or does not match the
schema of its field. The message lists every problem, each with the
data id and the field slug.

> **Notes**
>
> The check uses the JSON schema of the field (draft 2019-09) and the
> standard formats of ``jsonschema``. Madbot runs some additional format
> checks (for example looking up a DOI or an ORCID online), so a value can
> pass here and still be skipped by madbot when the lookup fails.

**Examples**

```pycon
>>> results = plugin.get_metadata_for_data_objects(["12345/data.csv"])
>>> assert_imported_metadata_valid(plugin, results)
```

[View source](https://gitlab.com/ifb-elixirfr/madbot/madbot/-/blob/c7108faae5e7dd06591be53c8d5bd5e75d880ebe/plugin_api/plugins_api/testing.py#L141-239)
