Skip to content

Commit

Permalink
Write tests for SequenceRecord.id and .comment and fix implementation…
Browse files Browse the repository at this point in the history
… errors
  • Loading branch information
rhpvorderman committed Aug 30, 2023
1 parent 3007f86 commit 3987455
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/dnaio/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class SequenceRecord:
def fastq_bytes(self, two_headers: bool = ...) -> bytes: ...
def is_mate(self, other: SequenceRecord) -> bool: ...
def reverse_complement(self) -> SequenceRecord: ...
@property
def id(self) -> str: ...
@property
def comment(self) -> Optional[str]: ...

# Bytestring = Union[bytes, bytearray, memoryview]. Technically just 'bytes' is
# acceptable as an alias, but even more technically this function supports all
Expand Down
4 changes: 3 additions & 1 deletion src/dnaio/_core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ cdef class SequenceRecord:
cdef char *name
cdef size_t name_length
cdef size_t id_length
cdef char *comment_start
cdef size_t comment_length
# Not yet cached is None
if self._comment is None:
Expand All @@ -186,8 +187,9 @@ cdef class SequenceRecord:
self._comment = ""
else:
comment_length = name_length - (id_length + 1)
comment_start = name + id_length + 1
self._comment = PyUnicode_New(comment_length , 127)
memcpy(PyUnicode_DATA(self._comment), name, comment_length)
memcpy(PyUnicode_DATA(self._comment), comment_start, comment_length)
# Cached but nothing is internally empty string, expose externally as None
if PyUnicode_GET_LENGTH(self._comment) == 0:
return None
Expand Down
36 changes: 36 additions & 0 deletions tests/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,42 @@ def test_set_qualities_none(self):
seq.qualities = None
assert seq.qualities is None

def test_set_id(self):
seq = SequenceRecord("name", "A", "=")
with pytest.raises(AttributeError):
seq.id = "Obi-Wan"

def test_set_comment(self):
seq = SequenceRecord("name", "A", "=")
with pytest.raises(AttributeError):
seq.comment = "Hello there!"

@pytest.mark.parametrize(
["record", "expected"],
[
(SequenceRecord("name", "A", "="), None),
(SequenceRecord("name ", "A", "="), None),
(SequenceRecord("name ", "A", "="), " "),
(SequenceRecord("name", "A", "="), None),
(SequenceRecord("AotC I hate sand!", "A", "="), "I hate sand!"),
],
)
def test_get_comment(self, record, expected):
assert record.comment == expected

@pytest.mark.parametrize(
["record", "expected"],
[
(SequenceRecord("name", "A", "="), "name"),
(SequenceRecord("name ", "A", "="), "name"),
(SequenceRecord("name ", "A", "="), "name"),
(SequenceRecord("name", "A", "="), "name"),
(SequenceRecord("AotC I hate sand!", "A", "="), "AotC"),
],
)
def test_get_id(self, record, expected):
assert record.id == expected


def test_legacy_sequence():
from dnaio import Sequence
Expand Down

0 comments on commit 3987455

Please sign in to comment.