def test_migrate_all_dbs_with_cherry_picked_samples_mocked(
    config, mongo_database, mock_mysql_connection, mock_update_dart
):
    _, mongo_db = mongo_database

    start_datetime = datetime(year=2020, month=5, day=10, hour=15, minute=10)
    end_datetime = start_datetime + timedelta(days=1)
    # generate and insert sample rows into the mongo database
    test_samples = generate_example_samples(range(0, 6), start_datetime)
    mongo_db.samples.insert_many(test_samples)

    assert mongo_db.samples.count_documents({}) == 8

    cherry_picked_sample = {
        FIELD_ROOT_SAMPLE_ID: [test_samples[0][FIELD_ROOT_SAMPLE_ID]],
        FIELD_PLATE_BARCODE: [test_samples[0][FIELD_PLATE_BARCODE]],
    }

    cherry_picked_df = pd.DataFrame.from_dict(cherry_picked_sample)

    with patch(
        "migrations.helpers.dart_samples_update_helper.get_cherrypicked_samples", side_effect=[cherry_picked_df]
    ):
        with patch(
            "migrations.helpers.dart_samples_update_helper.remove_cherrypicked_samples"
        ) as mock_remove_cherrypicked_samples:

            migrate_all_dbs(config, start_datetime.strftime("%y%m%d_%H%M"), end_datetime.strftime("%y%m%d_%H%M"))

            samples = get_samples(mongo_db.samples, start_datetime, end_datetime)

            mock_remove_cherrypicked_samples.assert_called_once_with(
                samples, [[cherry_picked_sample[FIELD_ROOT_SAMPLE_ID][0], cherry_picked_sample[FIELD_PLATE_BARCODE][0]]]
            )
Beispiel #2
0
def test_migrate_all_dbs_returns_early_failed_mlwh_update(
        config, mock_mysql_connection, mock_update_dart):
    start_datetime = datetime(year=2020, month=5, day=10, hour=15, minute=10)
    end_datetime = start_datetime + timedelta(days=1)
    test_samples = generate_example_samples(range(0, 6), start_datetime)

    def side_effect(_, collection_name):
        mock_collection = MagicMock()
        if collection_name == COLLECTION_SAMPLES:
            mock_collection.aggregate.return_value = test_samples
        elif collection_name == COLLECTION_SOURCE_PLATES:
            mock_collection.find_one.return_value = None
        else:
            raise ValueError(f"{collection_name} is not recognized/expected")
        return mock_collection

    with patch(
            "migrations.helpers.dart_samples_update_helper.get_mongo_collection",
            side_effect=side_effect):
        mock_mysql_connection.side_effect = Exception(
            "Boom!")  # mock creating MLWH mysql connection to fail

        migrate_all_dbs(config, start_datetime.strftime("%y%m%d_%H%M"),
                        end_datetime.strftime("%y%m%d_%H%M"))

        mock_update_dart.assert_not_called()
Beispiel #3
0
def test_migrate_all_dbs_return_early_no_samples(config, mock_mysql_connection,
                                                 mock_update_dart):
    start_datetime = datetime(year=2020, month=5, day=10, hour=15, minute=10)
    end_datetime = start_datetime + timedelta(days=1)

    mock_samples_collection = MagicMock()
    mock_samples_collection.aggregate.return_value = [
    ]  # mock samples collection call
    mock_source_plates_collection = MagicMock()

    def side_effect(_, collection_name):
        if collection_name == COLLECTION_SAMPLES:
            return mock_samples_collection
        elif collection_name == COLLECTION_SOURCE_PLATES:
            return mock_source_plates_collection
        else:
            raise ValueError(f"{collection_name} is not recognized/expected")

    with patch(
            "migrations.helpers.dart_samples_update_helper.get_mongo_collection",
            side_effect=side_effect):
        migrate_all_dbs(config, start_datetime.strftime("%y%m%d_%H%M"),
                        end_datetime.strftime("%y%m%d_%H%M"))

        # ensure no updates are made
        mock_samples_collection.bulk_write.assert_not_called()
        mock_source_plates_collection.insert_many.assert_not_called()
        mock_mysql_connection.assert_not_called()
        mock_update_dart.assert_not_called()
Beispiel #4
0
def test_migrate_all_dbs_returns_early_start_datetime_after_end_datetime(
        config, mock_mongo_client, mock_mysql_connection, mock_update_dart):
    start_datetime = "201017_1200"
    end_datetime = "201016_1600"
    migrate_all_dbs(config, start_datetime, end_datetime)

    # ensure no database connections/updates are made
    mock_mongo_client.assert_not_called()
    mock_mysql_connection.assert_not_called()
    mock_update_dart.assert_not_called()
Beispiel #5
0
def run(config: Config, s_start_datetime: str = "", s_end_datetime: str = "") -> None:
    print("-" * 80)
    print("STARTING LEGACY DART UPDATE")
    print(f"Time start: {datetime.now()}")
    start = time.time()

    dart_samples_update_helper.migrate_all_dbs(config, s_start_datetime, s_end_datetime)

    print(f"Time taken: {round(time.time() - start, 2)}s")
    print(f"Time finished: {datetime.now()}")
    print("=" * 80)
def test_migrate_all_dbs_returns_early_invalid_end_datetime(
    config, mock_mongo_client, mock_mysql_connection, mock_update_dart
):
    start_datetime = "201016_1600"
    end_datetime = "not a real datetime"
    with patch("migrations.helpers.dart_samples_update_helper.valid_datetime_string", side_effect=[True, False]):
        migrate_all_dbs(config, start_datetime, end_datetime)

        # ensure no database connections/updates are made
        mock_mongo_client.assert_not_called()
        mock_mysql_connection.assert_not_called()
        mock_update_dart.assert_not_called()
def test_migrate_all_dbs_remove_cherrypicked_samples(config, mongo_database, mock_update_dart):
    _, mongo_db = mongo_database

    start_datetime = datetime(year=2020, month=5, day=10, hour=15, minute=10)
    end_datetime = start_datetime + timedelta(days=1)
    # generate and insert sample rows into the mongo database
    test_samples = generate_example_samples(range(0, 6), start_datetime)
    mongo_db.samples.insert_many(test_samples)

    assert mongo_db.samples.count_documents({}) == 8

    with patch(
        "migrations.helpers.dart_samples_update_helper.remove_cherrypicked_samples"
    ) as mock_remove_cherrypicked_samples:
        migrate_all_dbs(config, start_datetime.strftime("%y%m%d_%H%M"), end_datetime.strftime("%y%m%d_%H%M"))

        mock_remove_cherrypicked_samples.assert_not_called()