Exemplo n.º 1
0
async def test_store_write_and_read_with_migrations(
    real_store_path: PurePosixPath, ) -> None:
    """A store should be able to write and read a file."""
    old_store = Store.create(real_store_path, schema=OldModel)

    item_key = "some-key"
    item = OldModel(foo="hello")

    await old_store.put(item, item_key)

    new_store = Store.create(
        real_store_path,
        schema=CoolModel,
        migrations=(
            lambda obj: {
                "foo": obj["foo"],
                "bar": "world"
            },
            lambda obj: {
                "foo": obj["foo"],
                "bar": obj["bar"],
                "baz": 0
            },
        ),
    )

    result = await new_store.get(item_key)

    assert result == CoolModel(foo="hello", bar="world", baz=0)
Exemplo n.º 2
0
def test_store_put_sync_bad_primary_key_asserts(
        store_path: PurePosixPath, mock_filesystem: AsyncMock) -> None:
    """store.put_sync should assert if primary_key isn't in the model."""
    store = Store(
        directory=store_path,
        schema=CoolModel,
        primary_key="not-a-field",
        migrations=(),
        ignore_errors=False,
        filesystem=mock_filesystem,
    )
    item = CoolModel(foo="hello", bar=0)

    with pytest.raises(AssertionError, match="key.+required"):
        store.put_sync(item)
Exemplo n.º 3
0
def store(store_path: PurePosixPath, mock_filesystem: AsyncMock) -> Store[CoolModel]:
    """Create a fresh Store with a mock filesystem."""
    return Store(
        directory=store_path,
        schema=CoolModel,
        primary_key=None,
        migrations=(),
        ignore_errors=False,
        filesystem=mock_filesystem,
    )
Exemplo n.º 4
0
def ignore_errors_store(
    store_path: PurePosixPath, mock_filesystem: AsyncMock
) -> Store[CoolModel]:
    """Create a Store with errors ignored."""
    return Store(
        directory=store_path,
        schema=CoolModel,
        primary_key=None,
        migrations=(),
        ignore_errors=True,
        filesystem=mock_filesystem,
    )
Exemplo n.º 5
0
async def test_store_write_and_read(real_store_path: PurePosixPath) -> None:
    """A store should be able to write and read a file."""
    store = Store.create(real_store_path, schema=CoolModel)
    item = CoolModel(foo="hello", bar="world")

    key = await store.put(item, "foo")

    assert key == "foo"

    result = await store.get(key)

    assert result == CoolModel(foo="hello", bar="world")
Exemplo n.º 6
0
async def test_store_write_and_ensure(real_store_path: PurePosixPath) -> None:
    """A store should be able to write and read a file."""
    store = Store.create(real_store_path, schema=CoolModel)
    default_item = CoolModel(foo="default", bar="value")
    item = CoolModel(foo="hello", bar="world")

    result = await store.ensure(default_item, "foo")
    assert result == default_item

    await store.put(item, "foo")
    result = await store.ensure(default_item, "foo")

    assert result == item
Exemplo n.º 7
0
async def test_keyed_store_write_and_read_dir(
        real_store_path: PurePosixPath) -> None:
    """A store should be able to write and read the whole store."""
    store = Store.create(real_store_path, schema=CoolModel, primary_key="foo")

    # TODO(mc, 2020-10-03): replace with store.add
    await store.put(CoolModel(foo="hello", bar="world"))
    await store.put(CoolModel(foo="oh", bar="hai"))

    result = await store.get_all_entries()

    assert len(result) == 2
    assert ("hello", CoolModel(foo="hello", bar="world")) in result
    assert ("oh", CoolModel(foo="oh", bar="hai")) in result
Exemplo n.º 8
0
async def test_store_write_and_read_with_nested_keys(
    real_store_path: PurePosixPath, ) -> None:
    """A store should be able to write and read a file synchronously."""
    store = Store.create(real_store_path, schema=CoolModel)
    item = CoolModel(foo="hello", bar="world")

    key = await store.put(item, "foo/bar/baz")

    assert key == "foo/bar/baz"

    result = await store.get(key)

    assert result == CoolModel(foo="hello", bar="world")
    assert Path(real_store_path / "foo" / "bar" / "baz.json").exists()
Exemplo n.º 9
0
async def test_keyed_store_write_and_ensure(
        real_store_path: PurePosixPath) -> None:
    """A store should be able to write and read a file."""
    store = Store.create(real_store_path, schema=CoolModel, primary_key="foo")
    default_item = CoolModel(foo="some-key", bar="hello world")
    item = CoolModel(foo="some-key", bar="oh hai")

    result = await store.ensure(default_item)
    assert result == default_item

    await store.put(item)
    result = await store.ensure(default_item)

    assert result == item