You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.2 KiB
84 lines
2.2 KiB
# bsv - Backup, Synchronization, Versioning
|
|
# Copyright (C) 2023 Simon Boyé
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
from __future__ import annotations
|
|
from hashlib import sha256
|
|
from pathlib import Path
|
|
|
|
from tempfile import TemporaryDirectory
|
|
import pytest
|
|
|
|
from bsv.simple_cas.cas import Digest, SimpleCas
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_dir():
|
|
with TemporaryDirectory(prefix="simple_cas_") as tmp_dir:
|
|
yield Path(tmp_dir)
|
|
|
|
|
|
@pytest.fixture
|
|
def cas(tmp_dir):
|
|
cas = SimpleCas(
|
|
tmp_dir,
|
|
sha256, # type: ignore
|
|
)
|
|
with cas:
|
|
yield cas
|
|
|
|
|
|
def test_simple_cas(tmp_dir: Path):
|
|
cas = SimpleCas(
|
|
tmp_dir,
|
|
sha256, # type: ignore
|
|
)
|
|
with cas:
|
|
assert len(cas) == 0
|
|
|
|
data = b"Hello World!"
|
|
digest = cas.write(b"blob", data)
|
|
|
|
assert len(cas) == 1
|
|
assert digest in cas
|
|
|
|
obj = cas.read(digest)
|
|
assert obj is not None
|
|
assert obj.object_type == b"blob"
|
|
assert obj.data == data
|
|
|
|
cas = SimpleCas(
|
|
tmp_dir,
|
|
sha256, # type: ignore
|
|
)
|
|
with cas:
|
|
assert len(cas) == 1
|
|
assert digest in cas
|
|
|
|
obj = cas.read(digest)
|
|
assert obj is not None
|
|
assert obj.object_type == b"blob"
|
|
assert obj.data == data
|
|
|
|
digest2 = cas.write(b"blob", data)
|
|
assert digest2 == digest
|
|
assert len(cas) == 1
|
|
|
|
|
|
def test_refs(cas: SimpleCas):
|
|
digest = Digest(bytes([42] * cas._digest_size))
|
|
assert cas.get_ref("foo/bar") is None
|
|
cas.set_ref("foo/bar", digest)
|
|
assert cas.get_ref("foo/bar") == digest
|
|
assert cas.get_ref("foo") is None
|
|
|