littlegraph-python 0.2.1

Python extension module for the littlegraph embedded graph database
Documentation
from __future__ import annotations

import os
from pathlib import Path

import pytest

import littlegraph


def test_import_exports_version() -> None:
    assert littlegraph.__version__ == "0.2.0"
    assert littlegraph.Database.__module__ == "littlegraph._littlegraph"


def test_create_and_close_database(tmp_path: Path) -> None:
    database_path = tmp_path / "database"

    with littlegraph.open(database_path, read_only=False, create=True) as database:
        assert database.path == str(database_path)
        assert database.read_only is False
        assert database.closed is False

    assert database.closed is True
    database.close()


def test_read_only_is_default_and_never_creates(tmp_path: Path) -> None:
    database_path = tmp_path / "missing"

    with pytest.raises(littlegraph.DatabaseError) as raised:
        littlegraph.open(database_path)

    assert raised.value.code == "Database.NotFound"
    assert isinstance(raised.value.message, str)
    assert raised.value.path == str(database_path)
    assert not database_path.exists()


def test_create_requires_writable_mode(tmp_path: Path) -> None:
    with pytest.raises(ValueError, match="create=True requires read_only=False"):
        littlegraph.open(tmp_path / "database", create=True)


def test_bytes_paths_are_rejected(tmp_path: Path) -> None:
    with pytest.raises(TypeError, match="not bytes"):
        littlegraph.open(os.fsencode(tmp_path))


def test_limits_reject_bool_and_invalid_spill_configuration() -> None:
    with pytest.raises(TypeError, match="max_result_rows must be an integer"):
        littlegraph.Limits(max_result_rows=True)

    with pytest.raises(ValueError, match="cannot exceed"):
        littlegraph.Limits(
            default_spill_threshold_bytes=2,
            default_max_spill_bytes=1,
        )