def test_transactions_are_rolled_back_if_inserting_fails(mocker): # mock the database access mock_database_service = mocker.patch("ssda.database.ssda.DatabaseService") mock_database_service.return_value.find_observation_id.return_value = None mock_database_service.return_value.find_proposal_id.return_value = None mock_database_service.return_value.insert_artifact.return_value = 713 mock_database_service.return_value.insert_energy.side_effect = ValueError( "This is a fake error.") mock_database_service.return_value.insert_instrument_keyword_value.side_effect = [ 1409, 1410, ] mock_database_service.return_value.insert_instrument_setup.return_value = ( INSTRUMENT_SETUP_ID) mock_database_service.return_value.insert_instrument_specific_content.return_value = ( None) mock_database_service.return_value.insert_observation.return_value = OBSERVATION_ID mock_database_service.return_value.insert_observation_time.return_value = 23011 mock_database_service.return_value.insert_plane.return_value = PLANE_ID mock_database_service.return_value.insert_polarization.return_value = None mock_database_service.return_value.insert_position.return_value = 4943 mock_database_service.return_value.insert_proposal.return_value = PROPOSAL_ID mock_database_service.return_value.insert_target.return_value = 14054 database_config: Any = None observation_properties = ObservationPropertiesStub() try: insert(observation_properties, ssda.database.ssda.DatabaseService(database_config)) except ValueError: pass # the transaction is rolled back mock_database_service.return_value.begin_transaction.assert_called_once() mock_database_service.return_value.commit_transaction.assert_not_called() mock_database_service.return_value.rollback_transaction.assert_called_once( )
def execute_database_insert( fits_path: str, database_services: DatabaseServices, ) -> None: # If the FITS file already exists in the database, do nothing. if database_services.ssda.file_exists(fits_path): return # Get the observation properties. fits_file = StandardFitsFile(fits_path) try: _observation_properties = observation_properties( fits_file, database_services) # Check if the FITS file is to be ignored if _observation_properties.ignore_observation(): clear_warnings() return except Exception as e: propid_header_value = fits_file.header_value("PROPID") proposal_id = (propid_header_value.upper() if propid_header_value else "") # If the FITS file is Junk, Unknown, ENG or CAL_GAIN, do not store the observation. if proposal_id in ("JUNK", "UNKNOWN", "NONE", "ENG", "CAL_GAIN"): return # Do not store engineering data. if "ENG_" in proposal_id or "ENG-" in proposal_id: return raise e # Execute the database insert insert( observation_properties=_observation_properties, ssda_database_service=database_services.ssda, )
def test_observations_are_not_reinserted(mocker): # mock the database access mock_database_service = mocker.patch("ssda.database.ssda.DatabaseService") mock_database_service.return_value.find_observation_id.return_value = OBSERVATION_ID mock_database_service.return_value.find_proposal_id.return_value = None mock_database_service.return_value.insert_artifact.return_value = 713 mock_database_service.return_value.insert_energy.return_value = 92346 mock_database_service.return_value.insert_instrument_keyword_value.side_effect = [ 1409, 1410, ] mock_database_service.return_value.insert_instrument_setup.return_value = ( INSTRUMENT_SETUP_ID) mock_database_service.return_value.insert_instrument_specific_content.return_value = ( None) mock_database_service.return_value.insert_observation.return_value = OBSERVATION_ID mock_database_service.return_value.insert_observation_time.return_value = 23011 mock_database_service.return_value.insert_plane.return_value = PLANE_ID mock_database_service.return_value.insert_polarization.return_value = None mock_database_service.return_value.insert_position.return_value = 4943 mock_database_service.return_value.insert_proposal.return_value = PROPOSAL_ID mock_database_service.return_value.insert_target.return_value = 14054 database_config: Any = None observation_properties = ObservationPropertiesStub() insert(observation_properties, ssda.database.ssda.DatabaseService(database_config)) # a transaction is used mock_database_service.return_value.begin_transaction.assert_called_once() mock_database_service.return_value.commit_transaction.assert_called_once() mock_database_service.return_value.rollback_transaction.assert_not_called() # proposal inserted mock_database_service.return_value.insert_proposal.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_proposal.call_args[0][0], observation_properties.proposal(), ) # proposal investigators inserted assert (mock_database_service.return_value.insert_proposal_investigator. call_count == 3) for i in range(3): assert_equal_properties( mock_database_service.return_value.insert_proposal_investigator. call_args_list[i][0][0], observation_properties.proposal_investigators(PROPOSAL_ID)[i], ) # nothing else is inserted mock_database_service.return_value.insert_artifact.assert_not_called() mock_database_service.return_value.insert_energy.assert_not_called() mock_database_service.return_value.insert_instrument_keyword_value.assert_not_called( ) mock_database_service.return_value.insert_observation.assert_not_called() mock_database_service.return_value.insert_observation_time.assert_not_called( ) mock_database_service.return_value.insert_plane.assert_not_called() mock_database_service.return_value.insert_polarization.assert_not_called() mock_database_service.return_value.insert_position.assert_not_called() mock_database_service.return_value.insert_target.assert_not_called()
def test_observation_groups_are_not_reinserted(mocker): # mock the database access mock_database_service = mocker.patch("ssda.database.ssda.DatabaseService") mock_database_service.return_value.find_observation_id.return_value = None mock_database_service.return_value.find_observation_group_id.return_value = ( OBSERVATION_GROUP_ID) mock_database_service.return_value.find_proposal_id.return_value = PROPOSAL_ID mock_database_service.return_value.insert_artifact.return_value = 713 mock_database_service.return_value.insert_energy.return_value = 92346 mock_database_service.return_value.insert_instrument_keyword_value.side_effect = [ 1409, 1410, ] mock_database_service.return_value.insert_instrument_setup.return_value = ( INSTRUMENT_SETUP_ID) mock_database_service.return_value.insert_instrument_specific_content.return_value = ( None) mock_database_service.return_value.insert_observation.return_value = OBSERVATION_ID mock_database_service.return_value.insert_observation_time.return_value = 23011 mock_database_service.return_value.insert_plane.return_value = PLANE_ID mock_database_service.return_value.insert_polarization.return_value = None mock_database_service.return_value.insert_position.return_value = 4943 mock_database_service.return_value.insert_proposal.return_value = PROPOSAL_ID mock_database_service.return_value.insert_target.return_value = 14054 database_config: Any = None observation_properties = ObservationPropertiesStub() insert(observation_properties, ssda.database.ssda.DatabaseService(database_config)) # a transaction is used mock_database_service.return_value.begin_transaction.assert_called_once() mock_database_service.return_value.commit_transaction.assert_called_once() mock_database_service.return_value.rollback_transaction.assert_not_called() # observation group not reinserted mock_database_service.return_value.insert_observation_group.assert_not_called( ) # proposal investigators not reinserted assert (mock_database_service.return_value.insert_proposal_investigator. asserrt_not_called()) # observation inserted mock_database_service.return_value.insert_observation.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_observation.call_args[0][0], observation_properties.observation(OBSERVATION_GROUP_ID, PROPOSAL_ID), ) # target inserted mock_database_service.return_value.insert_target.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_target.call_args[0][0], observation_properties.target(OBSERVATION_ID), ) # instrument keyword values inserted assert (mock_database_service.return_value.insert_instrument_keyword_value. call_count == 2) for i in range(2): assert_equal_properties( mock_database_service.return_value.insert_instrument_keyword_value. call_args_list[i][0][0], observation_properties.instrument_keyword_values( OBSERVATION_ID)[i], ) # instrument setup inserted mock_database_service.return_value.insert_instrument_setup.assert_called_once( ) assert_equal_properties( mock_database_service.return_value.insert_instrument_setup.call_args[0] [0], observation_properties.instrument_setup(OBSERVATION_ID), ) # instrument-specific content inserted assert (mock_database_service.return_value. insert_instrument_specific_content.call_count == 2) for i in range(2): instrument_specific_content_query = QUERIES[i].sql instrument_specific_content_parameters = { key: value for key, value in QUERIES[i].parameters.items() } instrument_specific_content_parameters[ "instrument_setup_id"] = INSTRUMENT_SETUP_ID assert_equal_properties( mock_database_service.return_value. insert_instrument_specific_content.call_args_list[i][0][0], instrument_specific_content_query, ) assert (mock_database_service.return_value. insert_instrument_specific_content.call_args_list[i][0] [1].items() == instrument_specific_content_parameters.items()) # plane inserted mock_database_service.return_value.insert_plane.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_plane.call_args[0][0], observation_properties.plane(OBSERVATION_ID), ) # energy inserted mock_database_service.return_value.insert_energy.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_energy.call_args[0][0], observation_properties.energy(PLANE_ID), ) # polarization inserted mock_database_service.return_value.insert_polarization.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_polarization.call_args[0][0], observation_properties.polarization(PLANE_ID), ) # observation time inserted mock_database_service.return_value.insert_observation_time.assert_called_once( ) assert_equal_properties( mock_database_service.return_value.insert_observation_time.call_args[0] [0], observation_properties.observation_time(PLANE_ID), ) # position inserted mock_database_service.return_value.insert_position.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_position.call_args[0][0], observation_properties.position(PLANE_ID), ) # artifact inserted mock_database_service.return_value.insert_artifact.assert_called_once() assert_equal_properties( mock_database_service.return_value.insert_artifact.call_args[0][0], observation_properties.artifact(PLANE_ID), )