tests: restructure some tests for objects/data

This commit is contained in:
Saugat Pachhai (सौगात)
2022-05-19 18:38:21 +05:45
committed by skshetry
parent 455260b29c
commit 95c6cea36d
11 changed files with 38 additions and 36 deletions
-16
View File
@@ -1,16 +0,0 @@
from dvc.fs import LocalFileSystem
from dvc.objects.hash import file_md5
def test_file_md5(tmp_dir):
tmp_dir.gen("foo", "foo content")
fs = LocalFileSystem()
assert file_md5("foo", fs) == file_md5("foo", fs)
def test_file_md5_crlf(tmp_dir):
fs = LocalFileSystem()
tmp_dir.gen("cr", b"a\nb\nc")
tmp_dir.gen("crlf", b"a\r\nb\r\nc")
assert file_md5("cr", fs) == file_md5("crlf", fs)
View File
+6 -6
View File
@@ -12,10 +12,10 @@ def set_value(cache: Cache, key: str, value: Any) -> Any:
@pytest.mark.parametrize("disk_type", [None, "test"])
def test_pickle_protocol_error(tmp_dir, disk_type):
directory = tmp_dir / "test"
def test_pickle_protocol_error(tmp_path, disk_type):
directory = tmp_path / "test"
cache = Cache(
directory,
str(directory),
disk_pickle_protocol=pickle.HIGHEST_PROTOCOL + 1,
type=disk_type,
)
@@ -33,14 +33,14 @@ def test_pickle_protocol_error(tmp_dir, disk_type):
(pickle.HIGHEST_PROTOCOL, pickle.HIGHEST_PROTOCOL - 1),
],
)
def test_pickle_backwards_compat(tmp_dir, proto_a, proto_b):
def test_pickle_backwards_compat(tmp_path, proto_a, proto_b):
with Cache(
directory=(tmp_dir / "test"),
directory=str(tmp_path / "test"),
disk_pickle_protocol=proto_a,
) as cache:
set_value(cache, "key", ("value1", "value2"))
with Cache(
directory=(tmp_dir / "test"),
directory=str(tmp_path / "test"),
disk_pickle_protocol=proto_b,
) as cache:
assert ("value1", "value2") == cache["key"]
+19
View File
@@ -0,0 +1,19 @@
from dvc.objects.fs import LocalFileSystem
from dvc.objects.hash import file_md5
def test_file_md5(tmp_path):
foo = tmp_path / "foo"
foo.write_text("foo content")
fs = LocalFileSystem()
assert file_md5(str(foo), fs) == file_md5(str(foo), fs)
def test_file_md5_crlf(tmp_path):
fs = LocalFileSystem()
cr = tmp_path / "cr"
crlf = tmp_path / "crlf"
cr.write_bytes(b"a\nb\nc")
crlf.write_bytes(b"a\r\nb\r\nc")
assert file_md5(str(cr), fs) == file_md5(str(crlf), fs)
+13 -14
View File
@@ -5,10 +5,10 @@ from dvc.objects.hash import HashStreamFile, file_md5
from dvc.objects.istextfile import DEFAULT_CHUNK_SIZE, istextfile
def test_hashed_stream_reader(tmp_dir):
tmp_dir.gen({"foo": "foo"})
def test_hashed_stream_reader(tmp_path):
foo = tmp_path / "foo"
foo.write_bytes(b"foo")
foo = tmp_dir / "foo"
with open(foo, "rb") as fobj:
stream_reader = HashStreamFile(fobj)
@@ -21,15 +21,14 @@ def test_hashed_stream_reader(tmp_dir):
assert stream_reader.read(1) == b"o"
assert stream_reader.tell() == 3
hex_digest = file_md5(foo, LocalFileSystem())
hex_digest = file_md5(str(foo), LocalFileSystem())
assert stream_reader.is_text
assert hex_digest == stream_reader.hash_value
def test_hashed_stream_reader_as_chunks(tmp_dir):
tmp_dir.gen({"foo": b"foo \x00" * 16})
foo = tmp_dir / "foo"
def test_hashed_stream_reader_as_chunks(tmp_path):
foo = tmp_path / "foo"
foo.write_bytes(b"foo \x00" * 16)
actual_size = len(foo.read_bytes())
with open(foo, "rb") as fobj:
@@ -45,7 +44,7 @@ def test_hashed_stream_reader_as_chunks(tmp_dir):
assert stream_reader.tell() == actual_size == total_read
hex_digest = file_md5(foo, LocalFileSystem())
hex_digest = file_md5(str(foo), LocalFileSystem())
assert not stream_reader.is_text
assert hex_digest == stream_reader.hash_value
@@ -54,20 +53,20 @@ def test_hashed_stream_reader_as_chunks(tmp_dir):
"contents",
[b"x" * DEFAULT_CHUNK_SIZE + b"\x00", b"clean", b"not clean \x00"],
)
def test_hashed_stream_reader_compatibility(tmp_dir, contents):
def test_hashed_stream_reader_compatibility(tmp_path, contents):
# Always read more than the DEFAULT_CHUNK_SIZE (512 bytes).
# This imitates the read actions performed by upload_fobj.
chunk_size = DEFAULT_CHUNK_SIZE * 2
tmp_dir.gen("data", contents)
data = tmp_dir / "data"
data = tmp_path / "data"
data.write_bytes(contents)
with open(data, "rb") as fobj:
stream_reader = HashStreamFile(fobj)
stream_reader.read(chunk_size)
local_fs = LocalFileSystem()
hex_digest = file_md5(data, local_fs)
hex_digest = file_md5(str(data), local_fs)
assert stream_reader.is_text is istextfile(data, local_fs)
assert stream_reader.is_text is istextfile(str(data), local_fs)
assert stream_reader.hash_value == hex_digest