Skip to content

Commit

Permalink
Initial start on cpython interface testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromekelleher committed Jul 14, 2024
1 parent 660bd65 commit 5afff95
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
53 changes: 53 additions & 0 deletions tests/test_cpython_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest
import numpy as np

from vcztools import _vcztools

FIXED_FIELD_NAMES = ["chrom", "pos", "id", "qual", "ref", "alt", "filter"]


def example_fixed_data(num_variants, num_samples=0):
chrom = np.array(["X"] * num_variants, dtype="S")
pos = np.arange(num_variants, dtype=np.int32)
id = np.array(["."] * num_variants, dtype="S").reshape((num_variants, 1))
ref = np.array(["A"] * num_variants, dtype="S")
alt = np.array(["T"] * num_variants, dtype="S").reshape((num_variants, 1))
qual = np.arange(num_variants, dtype=np.float32)
filter_ = np.ones(num_variants, dtype=bool).reshape((num_variants, 1))
filter_id = np.array(["PASS"], dtype="S")
return {
"chrom": chrom,
"pos": pos,
"id": id,
"qual": qual,
"ref": ref,
"alt": alt,
"filter": filter_,
"filter_ids": filter_id,
}


class TestTypeChecking:
@pytest.mark.parametrize("name", FIXED_FIELD_NAMES)
def test_field_incorrect_length(self, name):
num_variants = 5
data = example_fixed_data(num_variants)
data[name] = data[name][1:]
with pytest.raises(ValueError, match=f"Array {name.upper()} must have "):
_vcztools.VcfEncoder(num_variants, 0, **data)

@pytest.mark.parametrize("name", FIXED_FIELD_NAMES)
def test_field_incorrect_dtype(self, name):
num_variants = 5
data = example_fixed_data(num_variants)
data[name] = np.zeros(data[name].shape, dtype=np.int64)
with pytest.raises(ValueError, match=f"Wrong dtype for {name.upper()}"):
_vcztools.VcfEncoder(num_variants, 0, **data)

@pytest.mark.parametrize("name", FIXED_FIELD_NAMES)
def test_field_incorrect_type(self, name):
num_variants = 5
data = example_fixed_data(num_variants)
data[name] = "A Python string"
with pytest.raises(TypeError, match=f"must be numpy.ndarray"):
_vcztools.VcfEncoder(num_variants, 0, **data)
6 changes: 3 additions & 3 deletions vcztools/_vcztoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ VcfEncoder_store_fixed_array(VcfEncoder *self, PyArrayObject *array, const char
goto out;
}
if (PyArray_DTYPE(array)->type_num != type) {
PyErr_Format(PyExc_ValueError, "Array %s is not of the correct type", name);
PyErr_Format(PyExc_ValueError, "Wrong dtype for %s", name);
goto out;
}

Expand Down Expand Up @@ -189,7 +189,7 @@ VcfEncoder_init(VcfEncoder *self, PyObject *args, PyObject *kwds)
goto out;
}

/* NOTE: we generalise this pattern for CHROM also to save a bit of time
/* NOTE: we could generalise this pattern for CHROM also to save a bit of time
* in building numpy String arrays */
assert(PyArray_CheckExact(filter_ids));
if (!PyArray_CHKFLAGS(filter_ids, NPY_ARRAY_IN_ARRAY)) {
Expand All @@ -209,7 +209,7 @@ VcfEncoder_init(VcfEncoder *self, PyObject *args, PyObject *kwds)
if (VcfEncoder_add_array(self, "IDS/", "FILTER", filter_ids) != 0) {
goto out;
}
if (VcfEncoder_store_fixed_array(self, filter, "filter", NPY_BOOL, 2, num_variants)
if (VcfEncoder_store_fixed_array(self, filter, "FILTER", NPY_BOOL, 2, num_variants)
!= 0) {
goto out;
}
Expand Down

0 comments on commit 5afff95

Please sign in to comment.