Skip to content

Commit

Permalink
Fixes #18: Improved type-checking for cell setter method
Browse files Browse the repository at this point in the history
  • Loading branch information
JBGreisman committed Nov 20, 2020
1 parent 29c2897 commit 8c4fc7a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions reciprocalspaceship/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,15 @@ def cell(self):

@cell.setter
def cell(self, val):
self._cell = val

# GH#18: Type-checking for supported input types
if isinstance(val, gemmi.UnitCell) or (val is None):
self._cell = val
elif isinstance(val, (list, tuple, np.ndarray)) and len(val) == 6:
self._cell = gemmi.UnitCell(*val)
else:
raise ValueError(f"Cannot construct gemmi.UnitCell from value: {val}")


@property
def merged(self):
"""Whether DataSet contains merged reflection data (boolean)"""
Expand Down
29 changes: 29 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ def test_spacegroup(data_fmodel, spacegroup):
data_fmodel.spacegroup = spacegroup


@pytest.mark.parametrize("cell", [None,
gemmi.UnitCell(10, 20, 30, 40, 50, 60),
[10, 20, 30, 40, 50, 60],
(10, 20, 30, 40, 50, 60),
np.array([10, 20, 30, 40, 50, 60]),
[10, 20, 30],
[]
])
def test_cell(data_fmodel, cell):
if cell is None:
data_fmodel.cell = cell
assert data_fmodel.cell is None
elif not isinstance(cell, np.ndarray) and (cell == [] or cell == [10, 20, 30]):
with pytest.raises(ValueError):
data_fmodel.cell = cell
else:
data_fmodel.cell = cell
if isinstance(cell, gemmi.UnitCell):
expected = cell
else:
expected = gemmi.UnitCell(*cell)
assert expected.a == data_fmodel.cell.a
assert expected.b == data_fmodel.cell.b
assert expected.c == data_fmodel.cell.c
assert expected.alpha == data_fmodel.cell.alpha
assert expected.beta == data_fmodel.cell.beta
assert expected.gamma == data_fmodel.cell.gamma


@pytest.mark.parametrize("spacegroup", [None, gemmi.SpaceGroup(1)])
@pytest.mark.parametrize("cell", [None, gemmi.UnitCell(1, 1, 1, 90, 90, 90)])
def test_constructor_gemmi(data_gemmi, spacegroup, cell):
Expand Down

0 comments on commit 8c4fc7a

Please sign in to comment.