def test_update_mlwh_filtered_positive_fields_raises_with_error_updating_mlwh(config, mlwh_connection): with patch( "migrations.helpers.update_filtered_positives_helper.run_mysql_executemany_query", side_effect=NotImplementedError("Boom!"), ): with pytest.raises(NotImplementedError): update_mlwh_filtered_positive_fields(config, [])
def test_update_mlwh_filtered_positive_fields_return_false_with_no_connection( config): with patch( "migrations.helpers.update_filtered_positives_helper.create_mysql_connection" ) as mock_connection: mock_connection().is_connected.return_value = False result = update_mlwh_filtered_positive_fields(config, []) assert result is False
filtered_positive_identifier, non_cp_pos_pending_samples, version, update_timestamp, ) logger.info("Updated filtered positives") logger.info("Updating Mongo...") mongo_updated = update_mongo_filtered_positive_fields( config, non_cp_pos_pending_samples, version, update_timestamp) logger.info("Finished updating Mongo") if mongo_updated: logger.info("Updating MLWH...") mlwh_updated = update_mlwh_filtered_positive_fields( config, non_cp_pos_pending_samples) logger.info("Finished updating MLWH") if not omit_dart and mlwh_updated: logger.info("Updating DART...") dart_updated = update_dart_fields( config, non_cp_pos_pending_samples) logger.info("Finished updating DART") else: logger.warning( "No non-cherrypicked matching positive samples found, not updating any database" ) else: logger.warning( "No matching positive samples found in Mongo, not updating any database" )
def test_update_mlwh_filtered_positive_fields_calls_to_update_samples(config, mlwh_connection): # populate the mlwh database with existing entries mlwh_samples = [ { MLWH_MONGODB_ID: "1", MLWH_COORDINATE: "A1", MLWH_PLATE_BARCODE: "123", MLWH_ROOT_SAMPLE_ID: "MCM001", MLWH_RNA_ID: "AAA123", MLWH_RESULT: RESULT_VALUE_POSITIVE, MLWH_FILTERED_POSITIVE: None, MLWH_FILTERED_POSITIVE_VERSION: None, MLWH_FILTERED_POSITIVE_TIMESTAMP: None, }, { MLWH_MONGODB_ID: "2", MLWH_COORDINATE: "B1", MLWH_PLATE_BARCODE: "123", MLWH_ROOT_SAMPLE_ID: "MCM002", MLWH_RNA_ID: "BBB123", MLWH_RESULT: RESULT_VALUE_POSITIVE, MLWH_FILTERED_POSITIVE: True, MLWH_FILTERED_POSITIVE_VERSION: "v1.0", MLWH_FILTERED_POSITIVE_TIMESTAMP: datetime(2020, 4, 23, 14, 40, 8), }, ] insert_sql = """\ INSERT INTO lighthouse_sample (mongodb_id, root_sample_id, rna_id, plate_barcode, coordinate, result, filtered_positive, filtered_positive_version, filtered_positive_timestamp) VALUES (%(mongodb_id)s, %(root_sample_id)s, %(rna_id)s, %(plate_barcode)s, %(coordinate)s, %(result)s, %(filtered_positive)s, %(filtered_positive_version)s, %(filtered_positive_timestamp)s) """ cursor = mlwh_connection.cursor() cursor.executemany(insert_sql, mlwh_samples) cursor.close() mlwh_connection.commit() # call to update the database with newly filtered positive entries update_timestamp = datetime(2020, 6, 23, 14, 40, 8) mongo_samples = [ { FIELD_MONGODB_ID: "1", FIELD_COORDINATE: "A01", FIELD_PLATE_BARCODE: "123", FIELD_ROOT_SAMPLE_ID: "MCM001", FIELD_RNA_ID: "AAA123", FIELD_FILTERED_POSITIVE: True, FIELD_FILTERED_POSITIVE_VERSION: "v2.3", FIELD_FILTERED_POSITIVE_TIMESTAMP: update_timestamp, }, { FIELD_MONGODB_ID: "2", FIELD_COORDINATE: "B01", FIELD_PLATE_BARCODE: "123", FIELD_ROOT_SAMPLE_ID: "MCM002", FIELD_RNA_ID: "BBB123", FIELD_FILTERED_POSITIVE: False, FIELD_FILTERED_POSITIVE_VERSION: "v2.3", FIELD_FILTERED_POSITIVE_TIMESTAMP: update_timestamp, }, ] result = update_mlwh_filtered_positive_fields(config, mongo_samples) # type: ignore assert result is True cursor = mlwh_connection.cursor() cursor.execute("SELECT COUNT(*) FROM lighthouse_sample") sample_count = cursor.fetchone()[0] cursor.execute( "SELECT filtered_positive, filtered_positive_version, filtered_positive_timestamp FROM " "lighthouse_sample WHERE mongodb_id = '1'" ) filtered_positive_sample = cursor.fetchone() cursor.execute( "SELECT filtered_positive, filtered_positive_version, filtered_positive_timestamp FROM " "lighthouse_sample WHERE mongodb_id = '2'" ) filtered_negative_sample = cursor.fetchone() cursor.close() assert sample_count == 2 assert filtered_positive_sample[0] assert filtered_positive_sample[1] == "v2.3" assert filtered_positive_sample[2] == update_timestamp assert not filtered_negative_sample[0] assert filtered_negative_sample[1] == "v2.3" assert filtered_negative_sample[2] == update_timestamp