Skip to content

Commit

Permalink
Merge ab4d50d into 7be86ec
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthchirp committed Apr 16, 2019
2 parents 7be86ec + ab4d50d commit ffd1579
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 48 deletions.
7 changes: 4 additions & 3 deletions ispyb/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ class DBCache(object):
in self._data. Cached data should be accessed as self._data. On first
uncached access reload() is called.'''

def __init__(self):
'''Data has not yet been loaded from the database.'''

def load(self):
'''Ensure data is loaded from the database.'''
if not self.cached:
Expand Down Expand Up @@ -95,6 +92,10 @@ def __nonzero__(self):
'''Python 2: value when used in bool() context.'''
return bool(self._value)

def __hash__(self):
'''Pass on the hash value of the inner object.'''
return hash(self._value)


def add_properties(objectclass, property_list):
'''Generate class properties for a model that provide read-only access
Expand Down
13 changes: 12 additions & 1 deletion ispyb/model/datacollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, dcid, db_area, preload=None):

def reload(self):
'''Load/update information from the database.'''
self._data = self._db.retrieve_data_collection_main(self._dcid)[0]
self._data = self._db.retrieve_data_collection(self._dcid)[0]

@property
def dcid(self):
Expand Down Expand Up @@ -98,15 +98,26 @@ def __str__(self):
))).format(self)

ispyb.model.add_properties(DataCollection, (
('comment', 'comments', 'A free comment field for the data collection.'),
('dcgid', 'groupId', 'Returns the Data Collection Group ID associated with this data collection. '
'You can use .group to get the data collection group model object instead'),
('detector_distance', 'detectorDistance', 'Distance from the sample to the detector in mm'),
('detector_2theta', 'detector2Theta', '2Theta angle between the main beam and the detector normal in degrees'),
('file_template', 'fileTemplate', 'Template for file names with the character \'#\' standing in for image number digits.'),
('file_directory', 'imgDir', 'Fully qualified path to the image files'),
('time_start', 'startTime', None),
('time_exposure', 'exposureTime', 'Exposure time per frame in seconds'),
('time_end', 'endTime', None),
('image_count', 'noImages', None),
('image_start_number', 'startImgNumber', None),
('resolution', 'resolution', 'Inscribed resolution circle in Angstrom. Currently only well-defined for data collections with 2theta=0'),
('status', 'status', 'Returns a string representing the current data collection status.'),
('snapshot1', 'snapshot1', 'One of four possible fields to store file paths to image files relating to the data collection'),
('snapshot2', 'snapshot2', 'One of four possible fields to store file paths to image files relating to the data collection'),
('snapshot3', 'snapshot3', 'One of four possible fields to store file paths to image files relating to the data collection'),
('snapshot4', 'snapshot4', 'One of four possible fields to store file paths to image files relating to the data collection'),
('transmission', 'transmission', 'Beam transmission, in per cent'),
('wavelength', 'wavelength', 'Beam wavelength in Angstrom'),
))


Expand Down
126 changes: 82 additions & 44 deletions tests/model/test_datacollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,110 @@
import mock
import pytest


def test_datacollection_model_retrieves_database_records():
db, record = mock.Mock(), mock.Mock()
db.retrieve_data_collection_main.return_value = [record]
db, record = mock.Mock(), mock.Mock()
db.retrieve_data_collection.return_value = [record]

dc = ispyb.model.datacollection.DataCollection(1234, db)
assert not db.retrieve_data_collection_main.called
assert '1234' in str(dc)
assert '1234' in repr(dc)
assert 'uncached' in repr(dc)
dc = ispyb.model.datacollection.DataCollection(1234, db)
assert not db.retrieve_data_collection.called
assert "1234" in str(dc)
assert "1234" in repr(dc)
assert "uncached" in repr(dc)

dc.load()
db.retrieve_data_collection_main.assert_called_once_with(1234)
assert dc._data == record
assert '1234' in repr(dc)
assert 'cached' in repr(dc) and 'uncached' not in repr(dc)
dc.load()
db.retrieve_data_collection.assert_called_once_with(1234)
assert dc._data == record
assert "1234" in repr(dc)
assert "cached" in repr(dc) and "uncached" not in repr(dc)

# Test caching behaviour
dc.load()
db.retrieve_data_collection_main.assert_called_once()
# Test caching behaviour
dc.load()
db.retrieve_data_collection.assert_called_once()


def test_datacollection_model_accepts_preloading():
db, record = mock.Mock(), mock.Mock()
db, record = mock.Mock(), mock.Mock()

dc = ispyb.model.datacollection.DataCollection(1234, db, preload=record)
assert dc._data == record
dc = ispyb.model.datacollection.DataCollection(1234, db, preload=record)
assert dc._data == record

dc.load()
assert not db.retrieve_data_collection_main.called
dc.load()
assert not db.retrieve_data_collection.called


database_column_to_attribute_name = {
"groupId": None,
"detectorId": None,
"apertureSizeX": None,
"axisEnd": None,
"axisRange": None,
"axisStart": None,
"beamSizeAtSampleX": None,
"beamSizeAtSampleY": None,
"bestWilsonPlotPath": None,
"blSubSampleId": None,
"chiStart": None,
"comments": "comment",
"dcNumber": None,
"startTime": "time_start",
"detector2Theta": "detector_2theta",
"detectorDistance": "detector_distance",
"detectorId": None,
"endTime": "time_end",
"status": None,
"noImages": "image_count",
"startImgNumber": "image_start_number",
"noPasses": None,
"exposureTime": "time_exposure",
"fileTemplate": "file_template",
"flux": None,
"fluxEnd": None,
"focalSpotSizeAtSampleX": None,
"focalSpotSizeAtSampleY": None,
"groupId": None,
"imgContainerSubPath": None,
"imgDir": None,
"imgPrefix": None,
"imgSuffix": None,
"fileTemplate": None,
"snapshot1": None,
"snapshot2": None,
"snapshot3": None,
"snapshot4": None,
"comments": None,
}
record = {
k: getattr(mock.sentinel, k)
for k in database_column_to_attribute_name
"kappaStart": None,
"noImages": "image_count",
"noPasses": None,
"omegaStart": None,
"overlap": None,
"phiStart": None,
"resolution": "resolution",
"resolutionAtCorner": None,
"rotationAxis": None,
"slitGapHorizontal": None,
"slitGapVertical": None,
"snapshot1": "snapshot1",
"snapshot2": "snapshot2",
"snapshot3": "snapshot3",
"snapshot4": "snapshot4",
"startImgNumber": "image_start_number",
"startTime": "time_start",
"status": "status",
"synchrotronMode": None,
"transmission": "transmission",
"undulatorGap1": None,
"undulatorGap2": None,
"undulatorGap3": None,
"wavelength": "wavelength",
"xBeam": None,
"yBeam": None,
}
record = {k: getattr(mock.sentinel, k) for k in database_column_to_attribute_name}
record["imgDir"] = "/path/to/some/images/"
record["fileTemplate"] = "file_####.cbf"

@pytest.mark.parametrize('column,attribute', filter(lambda ca: ca[1], database_column_to_attribute_name.items()))

@pytest.mark.parametrize(
"column,attribute",
filter(lambda ca: ca[1], database_column_to_attribute_name.items()),
)
def test_datacollection_model_attributes_return_correct_values(column, attribute):
dc = ispyb.model.datacollection.DataCollection(1234, None, preload=record)
assert getattr(dc, attribute) == record[column]
dc = ispyb.model.datacollection.DataCollection(1234, None, preload=record)
assert getattr(dc, attribute) == record[column]


@pytest.mark.parametrize('printed_attribute', ('startTime', 'endTime', 'imgDir', 'fileTemplate'))
@pytest.mark.parametrize(
"printed_attribute", ("startTime", "endTime", "imgDir", "fileTemplate")
)
def test_pretty_printing_datacollection_shows_attribute(printed_attribute):
dc_str = str(ispyb.model.datacollection.DataCollection(1234, None, preload=record))
assert "1234" in dc_str
assert str(record[printed_attribute]) in dc_str
dc_str = str(ispyb.model.datacollection.DataCollection(1234, None, preload=record))
assert "1234" in dc_str
assert str(record[printed_attribute]) in dc_str

0 comments on commit ffd1579

Please sign in to comment.