Skip to content

Testing

Every name on this page is importable from sci_etl_core.testing, which needs the testing extra.

sci_etl_core.testing.extractors

The contract suite for extractors.

Subclass :class:ExtractorContract in a test module under a name pytest collects, such as TestMyExtractor, and give it a make_extractor fixture. Serve the listing from a stub, such as an httpx.MockTransport, so the suite runs offline:

import httpx
import pytest

from sci_etl_core.testing import ExtractorContract

from my_package import MyExtractor
from my_tests.stubs import serve_listing


class TestMyExtractor(ExtractorContract):
    @pytest.fixture
    def make_extractor(self):
        return lambda: MyExtractor(httpx.AsyncClient(transport=httpx.MockTransport(serve_listing)))

The tests are plain functions that run each scenario with :func:asyncio.run, so pytest-asyncio is not needed.

ExtractorContract

Tests every :class:~sci_etl_core.extractors.async_base.AsyncExtractor must pass.

A subclass provides the make_extractor fixture. It returns a callable that takes no argument and returns a new extractor over the same listing each time it is called, as each run of a pipeline would build it. The suite calls it inside a running event loop. The listing must be offline, must answer the same request the same way every time, must hold at least two pages of :attr:page_size entries for :attr:query, and must end within :attr:max_pages pages.

A subclass may also override the make_unavailable_extractor fixture to check that an unreachable source raises :class:~sci_etl_core.exceptions.UpstreamError rather than returning an empty page.

Some tests check an optional capability and skip when the implementation lacks it: the offset test, when the extractor is not an :class:~sci_etl_core.extractors.async_base.OffsetListing, and the unavailable-source test, when make_unavailable_extractor returns None.

query class-attribute

query: str = 'contract'

The query every test lists.

page_size class-attribute

page_size: int = 2

The page size every test asks for.

max_pages class-attribute

max_pages: int = 50

The most pages the listing may take to end.

make_unavailable_extractor

make_unavailable_extractor() -> (
    Callable[[], AsyncExtractor] | None
)

Return a callable that builds an extractor whose source fails every request, or None.

The source answers every request with a server-side failure, such as status 503, and the extractor must not wait between retries. The default returns None, which skips the unavailable-source test.

test_the_first_page_is_a_listing_page

test_the_first_page_is_a_listing_page(
    make_extractor: Callable[[], AsyncExtractor],
) -> None

cursor=None returns a well-formed first page with records and a next cursor.

test_a_cursor_resumes_in_a_new_extractor

test_a_cursor_resumes_in_a_new_extractor(
    make_extractor: Callable[[], AsyncExtractor],
) -> None

A next cursor returns the same page again, and from a new extractor, as a resumed run asks for it.

test_the_listing_ends

test_the_listing_ends(
    make_extractor: Callable[[], AsyncExtractor],
) -> None

Following next cursors reaches a page without one, and every page is well formed.

test_offset_cursors_match_the_listing

test_offset_cursors_match_the_listing(
    make_extractor: Callable[[], AsyncExtractor],
) -> None

For an OffsetListing, offset 0 is the first page and the next page's offset is its next page.

test_full_text_is_text

test_full_text_is_text(
    make_extractor: Callable[[], AsyncExtractor],
) -> None

fetch_full_text returns a string for a listed record.

test_an_unavailable_source_raises_upstream_error

test_an_unavailable_source_raises_upstream_error(
    make_unavailable_extractor: Callable[[], AsyncExtractor]
    | None,
) -> None

A source that fails every request raises UpstreamError instead of returning an empty page.

sci_etl_core.testing.state

The contract suite for state managers.

Subclass :class:StateManagerContract in a test module under a name pytest collects, such as TestMyStateManager, and give it a make_state_manager fixture:

import pytest

from sci_etl_core.testing import StateManagerContract

from my_package import MyStateManager


class TestMyStateManager(StateManagerContract):
    @pytest.fixture
    def make_state_manager(self, tmp_path):
        return lambda: MyStateManager(tmp_path / "state.db")

The tests are plain functions that run each scenario with :func:asyncio.run, so pytest-asyncio is not needed.

StateManagerContract

Tests every :class:~sci_etl_core.state.async_base.AsyncStateManager must pass.

A subclass provides the make_state_manager fixture. It returns a callable that takes no argument and returns a new state manager over the same storage each time it is called, as each run of a pipeline opens it, and the storage starts empty in every test. The suite calls it inside a running event loop, and ends each instance as a run ends: it awaits flush(), then aclose() when the instance has one. A state manager that keeps its state in memory returns the same instance on every call.

A subclass may also override the write_newer_schema fixture to check that state from a newer release is refused.

Some tests check an optional capability and skip when the implementation lacks it: failed attempts, when record_failure keeps the default that tracks nothing; forget, when the state manager is not :class:~sci_etl_core.state.async_base.Forgetting; reuse after aclose(), when it has no aclose; and the schema-version test, when write_newer_schema returns None.

write_newer_schema

write_newer_schema() -> Callable[[], None] | None

Return a callable that writes state from a newer schema version into the storage, or None.

The suite calls it before it creates a state manager. The default returns None, which skips the schema-version test; override it when the implementation records a schema version.

test_new_storage_is_empty

test_new_storage_is_empty(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

A new storage holds no processed id, no failed attempt, and default metadata.

test_processed_ids_persist_across_instances

test_processed_ids_persist_across_instances(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

Marked ids are loaded by the next instance, and marking an id twice keeps it once.

test_concurrent_marks_are_all_kept

test_concurrent_marks_are_all_kept(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

Ids marked concurrently, as the pipeline marks a page's records, are all kept.

test_saved_metadata_resumes_in_a_new_instance

test_saved_metadata_resumes_in_a_new_instance(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

The next instance loads the saved cursor and listing head, stamped with a run time.

test_failed_attempts_are_counted_across_instances

test_failed_attempts_are_counted_across_instances(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

record_failure returns the attempts so far, and the next instance loads them.

test_mark_processed_clears_failed_attempts

test_mark_processed_clears_failed_attempts(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

Marking a record processed clears its failed attempts and leaves other records' attempts.

test_forget_clears_processed_marks_and_failed_attempts

test_forget_clears_processed_marks_and_failed_attempts(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

forget clears the given ids' marks and attempts, ignores unknown ids, and keeps the cursor.

test_a_newer_schema_version_is_refused

test_a_newer_schema_version_is_refused(
    make_state_manager: Callable[[], AsyncStateManager],
    write_newer_schema: Callable[[], None] | None,
) -> None

State from a newer schema version raises a SciEtlError on load and on save.

test_aclose_then_reuse

test_aclose_then_reuse(
    make_state_manager: Callable[[], AsyncStateManager],
) -> None

A closed state manager keeps working, with the state it held before aclose().

sci_etl_core.testing.exporters

The contract suite for exporters.

Subclass :class:ExporterContract in a test module under a name pytest collects, such as TestMyExporter, and give it a make_exporter fixture and a read_exported fixture:

import json

import pytest

from sci_etl_core.testing import ExporterContract

from my_package import MyExporter


class TestMyExporter(ExporterContract):
    @pytest.fixture
    def make_exporter(self, tmp_path):
        return lambda: MyExporter(tmp_path / "out.json")

    @pytest.fixture
    def read_exported(self, tmp_path):
        async def read(record_id):
            exported = json.loads((tmp_path / "out.json").read_text(encoding="utf-8"))
            return exported.get(record_id, [])

        return read

The tests are plain functions that run each scenario with :func:asyncio.run, so pytest-asyncio is not needed.

ExporterContract

Tests every :class:~sci_etl_core.exporters.async_base.AsyncExporter must pass.

A subclass provides two fixtures:

  • make_exporter returns a callable that takes no argument and returns a new exporter over the same destination each time it is called, as each run of a pipeline would build it. The destination starts empty in every test, and the suite calls the callable inside a running event loop.
  • read_exported returns an async callable that takes a record id and returns the entities the destination holds for that record, in a form that compares equal when the content is the same, and an empty sequence when it holds none. The suite awaits it after the exporter has closed, and before, for an exporter with durable_writes = True.

A subclass may also override the make_entities fixture when the exporter takes entities other than dicts, and the write_newer_schema fixture to check that a destination from a newer release is refused.

Each run follows the pipeline's lifecycle: open(), write() for each record, flush(), then aclose(). Some tests check an optional capability and skip when the implementation lacks it: reading a write before flush(), when durable_writes is False, and the schema-version test, when write_newer_schema returns None.

make_entities

make_entities() -> Callable[
    [RawRecord, int], Sequence[Any]
]

Return a callable that builds count distinct entities for record.

The default builds dicts with a name and a value key.

write_newer_schema

write_newer_schema() -> Callable[[], None] | None

Return a callable that writes a destination from a newer schema version, or None.

The suite calls it before it creates an exporter. The default returns None, which skips the schema-version test; override it when the destination records a schema version.

test_written_entities_are_exported

test_written_entities_are_exported(
    make_exporter: Callable[[], AsyncExporter[Any]],
    read_exported: Callable[
        [str], Awaitable[Sequence[Any]]
    ],
    make_entities: Callable[
        [RawRecord, int], Sequence[Any]
    ],
) -> None

After a run, the destination holds every record's entities.

test_a_repeated_write_is_idempotent

test_a_repeated_write_is_idempotent(
    make_exporter: Callable[[], AsyncExporter[Any]],
    read_exported: Callable[
        [str], Awaitable[Sequence[Any]]
    ],
    make_entities: Callable[
        [RawRecord, int], Sequence[Any]
    ],
) -> None

Repeating a write, in the same run or a later one, leaves what a single write left.

test_a_write_with_no_entities_exports_none

test_a_write_with_no_entities_exports_none(
    make_exporter: Callable[[], AsyncExporter[Any]],
    read_exported: Callable[
        [str], Awaitable[Sequence[Any]]
    ],
    make_entities: Callable[
        [RawRecord, int], Sequence[Any]
    ],
) -> None

A record written with no entities holds none, and the other records keep theirs.

test_a_run_without_writes_succeeds

test_a_run_without_writes_succeeds(
    make_exporter: Callable[[], AsyncExporter[Any]],
    read_exported: Callable[
        [str], Awaitable[Sequence[Any]]
    ],
) -> None

A run that writes nothing, flushing twice, completes and exports nothing.

test_aclose_then_reuse

test_aclose_then_reuse(
    make_exporter: Callable[[], AsyncExporter[Any]],
    read_exported: Callable[
        [str], Awaitable[Sequence[Any]]
    ],
    make_entities: Callable[
        [RawRecord, int], Sequence[Any]
    ],
) -> None

An exporter runs again after aclose(), keeping what the earlier run exported.

test_a_durable_write_is_exported_before_flush

test_a_durable_write_is_exported_before_flush(
    make_exporter: Callable[[], AsyncExporter[Any]],
    read_exported: Callable[
        [str], Awaitable[Sequence[Any]]
    ],
    make_entities: Callable[
        [RawRecord, int], Sequence[Any]
    ],
) -> None

With durable_writes = True, write returns only once the entities are exported.

test_a_newer_schema_version_is_refused

test_a_newer_schema_version_is_refused(
    make_exporter: Callable[[], AsyncExporter[Any]],
    make_entities: Callable[
        [RawRecord, int], Sequence[Any]
    ],
    write_newer_schema: Callable[[], None] | None,
) -> None

A destination from a newer schema version raises a SciEtlError from open or write.

sci_etl_core.testing.embeddings

The contract suite for vector stores.

Subclass :class:EmbeddingStoreContract in a test module under a name pytest collects, such as TestMyStore, and give it a make_store fixture:

import pytest

from sci_etl_core.testing import EmbeddingStoreContract

from my_package import MyEmbeddingStore


class TestMyStore(EmbeddingStoreContract):
    @pytest.fixture
    def make_store(self, tmp_path):
        return lambda: MyEmbeddingStore(tmp_path / "vectors.db")

The tests are plain functions that run each scenario with :func:asyncio.run, so pytest-asyncio is not needed.

EmbeddingStoreContract

Tests every :class:~sci_etl_core.embeddings.store_base.AsyncEmbeddingStore must pass.

A subclass provides the make_store fixture. It returns a callable that takes no argument and returns a new store over the same storage each time it is called, and the storage starts empty in every test. The suite calls it inside a running event loop and awaits aclose() on each store it creates. A store that keeps its chunks in memory returns the same instance on every call.

A subclass may also override the write_newer_schema fixture to check that storage from a newer release is refused.

Vectors have three dimensions, and scores are compared to within 1e-5, so a store may keep float32 vectors. Some tests check an optional capability and skip when the implementation lacks it: iter_records, when the store keeps the default that raises NotImplementedError, and the schema-version test, when write_newer_schema returns None.

write_newer_schema

write_newer_schema() -> Callable[[], None] | None

Return a callable that writes storage from a newer schema version, or None.

The suite calls it before it creates a store. The default returns None, which skips the schema-version test; override it when the store records a schema version.

test_new_store_is_empty

test_new_store_is_empty(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

A new store counts no chunk and matches no query.

test_add_replaces_a_chunk_with_the_same_id_and_index

test_add_replaces_a_chunk_with_the_same_id_and_index(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

Adding a chunk again under its record id and index replaces it instead of adding another.

test_chunks_persist_across_instances

test_chunks_persist_across_instances(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

Chunks added by one store are counted and found by the next.

test_query_ranks_by_cosine_similarity

test_query_ranks_by_cosine_similarity(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

Hits come best first with their cosine score, text, and metadata, at most top_k of them.

test_query_honors_min_score_and_exclude_record_id

test_query_honors_min_score_and_exclude_record_id(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

min_score drops weaker hits, and exclude_record_id drops one record's chunks.

test_a_non_finite_vector_never_matches

test_a_non_finite_vector_never_matches(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

A stored vector holding NaN is never returned as a hit.

test_delete_record_removes_every_chunk_of_the_record

test_delete_record_removes_every_chunk_of_the_record(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

delete_record removes all of one record's chunks and ignores an unknown record.

test_replace_record_leaves_only_the_new_chunks

test_replace_record_leaves_only_the_new_chunks(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

replace_record drops a record's old chunks, even those with a higher index.

test_iter_records_yields_passages_in_record_id_order

test_iter_records_yields_passages_in_record_id_order(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

iter_records yields each record's passages in chunk order, records in id order, at any batch size.

test_aclose_then_reuse

test_aclose_then_reuse(
    make_store: Callable[[], AsyncEmbeddingStore],
) -> None

A closed store keeps working, with the chunks it held before aclose().

test_a_newer_schema_version_is_refused

test_a_newer_schema_version_is_refused(
    make_store: Callable[[], AsyncEmbeddingStore],
    write_newer_schema: Callable[[], None] | None,
) -> None

Storage from a newer schema version raises a SciEtlError on read and on write.