Skip to content

Commit bc5b4b1

Browse files
committed
more tests
1 parent 1427574 commit bc5b4b1

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

dictdatabase/io_bytes.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66

7-
def read(db_name: str, start=None, end=None) -> bytes:
7+
def read(db_name: str, start: int = None, end: int = None) -> bytes:
88
"""
99
Read the content of a file as bytes. Reading works even when the config
1010
changes, so a compressed ddb file can also be read if compression is
@@ -13,10 +13,11 @@ def read(db_name: str, start=None, end=None) -> bytes:
1313
Note: Only specify either both start and end, or none of them.
1414
1515
Args:
16-
- `db_name`: The name of the database to read from.
17-
- `start`: The start index to read from.
18-
- `end`: The end index to read up to (not included).
16+
- `db_name`: The name of the database file to read from.
17+
- `start`: The start byte index to read from.
18+
- `end`: The end byte index to read up to (not included).
1919
"""
20+
2021
json_path, json_exists, ddb_path, ddb_exists = utils.file_info(db_name)
2122

2223
if json_exists:
@@ -39,7 +40,7 @@ def read(db_name: str, start=None, end=None) -> bytes:
3940

4041

4142

42-
def write(db_name: str, dump: bytes, start=None):
43+
def write(db_name: str, dump: bytes, start: int = None):
4344
"""
4445
Write the bytes to the file of the db_path. If the db was compressed but no
4546
compression is enabled, remove the compressed file, and vice versa.
@@ -48,6 +49,8 @@ def write(db_name: str, dump: bytes, start=None):
4849
- `db_name`: The name of the database to write to.
4950
- `dump`: The bytes to write to the file, representing correct JSON when
5051
decoded.
52+
- `start`: The start byte index to write to. If None, the whole file is overwritten.
53+
If the original content was longer, the rest truncated.
5154
"""
5255

5356
json_path, json_exists, ddb_path, ddb_exists = utils.file_info(db_name)

tests/test_io_bytes.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
from dictdatabase import io_bytes
3+
4+
5+
6+
def test_write_bytes(use_test_dir, name_of_test):
7+
# Write shorter content at index
8+
io_bytes.write(name_of_test, b"0123456789")
9+
io_bytes.write(name_of_test, b"abc", 2)
10+
assert io_bytes.read(name_of_test) == b"01abc"
11+
# Overwrite with shorter content
12+
io_bytes.write(name_of_test, b"xy")
13+
assert io_bytes.read(name_of_test) == b"xy"
14+
# Overwrite with longer content
15+
io_bytes.write(name_of_test, b"0123456789")
16+
io_bytes.write(name_of_test, b"abcdef", 8)
17+
assert io_bytes.read(name_of_test) == b"01234567abcdef"
18+
# Write at index out of range
19+
io_bytes.write(name_of_test, b"01")
20+
io_bytes.write(name_of_test, b"ab", 4)
21+
assert io_bytes.read(name_of_test) == b'01\x00\x00ab'
22+
23+
24+
25+
def test_read_bytes(use_test_dir, name_of_test, use_compression):
26+
io_bytes.write(name_of_test, b"0123456789")
27+
# In range
28+
assert io_bytes.read(name_of_test, 2, 5) == b"234"
29+
# Complete range
30+
assert io_bytes.read(name_of_test, 0, 10) == b"0123456789"
31+
assert io_bytes.read(name_of_test, 0, None) == b"0123456789"
32+
assert io_bytes.read(name_of_test) == b"0123456789"
33+
# End out of range
34+
assert io_bytes.read(name_of_test, 9, 20) == b"9"
35+
# Completely out of range
36+
assert io_bytes.read(name_of_test, 25, 30) == b""
37+
# Start negative
38+
if use_compression:
39+
assert io_bytes.read(name_of_test, -5, 3) == b""
40+
else:
41+
with pytest.raises(OSError):
42+
io_bytes.read(name_of_test, -5, 3)

0 commit comments

Comments
 (0)