Skip to content

Commit

Permalink
add all exported fields to the dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthchirp committed Apr 16, 2019
1 parent 0ca45bc commit 6fb7554
Showing 1 changed file with 78 additions and 40 deletions.
118 changes: 78 additions & 40 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.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.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.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.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.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": None,
"detectorDistance": None,
"detectorId": None,
"endTime": "time_end",
"status": "status",
"noImages": "image_count",
"startImgNumber": "image_start_number",
"noPasses": None,
"exposureTime": None,
"fileTemplate": "file_template",
"flux": None,
"fluxEnd": None,
"focalSpotSizeAtSampleX": None,
"focalSpotSizeAtSampleY": None,
"groupId": None,
"imgContainerSubPath": None,
"imgDir": None,
"imgPrefix": None,
"imgSuffix": None,
"fileTemplate": "file_template",
"kappaStart": None,
"noImages": "image_count",
"noPasses": None,
"omegaStart": None,
"overlap": None,
"phiStart": None,
"resolution": None,
"resolutionAtCorner": None,
"rotationAxis": None,
"slitGapHorizontal": None,
"slitGapVertical": None,
"snapshot1": "snapshot1",
"snapshot2": "snapshot2",
"snapshot3": "snapshot3",
"snapshot4": "snapshot4",
"comments": "comment",
}
record = {
k: getattr(mock.sentinel, k)
for k in database_column_to_attribute_name
"startImgNumber": "image_start_number",
"startTime": "time_start",
"status": "status",
"synchrotronMode": None,
"transmission": None,
"undulatorGap1": None,
"undulatorGap2": None,
"undulatorGap3": None,
"wavelength": None,
"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 6fb7554

Please sign in to comment.