def test_samples_with_cog_uk_id_from_destination_add_to_warehouse(
    app,
    run_id,
    centres,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    mlwh_samples_in_cherrytrack,
    mocked_responses,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
    baracoda_mock_barcodes_group,
    baracoda_mock_responses,
):
    with app.app_context():
        samples, _ = samples_from_cherrytrack_into_mongo
        instance = SamplesWithCogUkId(
            SamplesFromDestination(WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode})))
        )
        message = WarehouseMessage("mytype", "myuuid", "at some point")
        instance.add_to_warehouse_message(message)
        assert message._subjects == [
            {
                "friendly_name": "aRootSampleId1__DS000050001_A01__centre_1__Positive",
                "role_type": "sample",
                "subject_type": "sample",
                "uuid": "aLighthouseUUID1",
            },
            {
                "friendly_name": "aRootSampleId3__DS000050001_A03__centre_1__Positive",
                "role_type": "sample",
                "subject_type": "sample",
                "uuid": "aLighthouseUUID3",
            },
        ]
    def initialize_event(self, params: Dict[str, str]) -> None:
        super().initialize_event(params=params)
        self._event_type = params["event_type"]

        self.properties["plate_barcode"] = PlateBarcode(params)
        self.properties["failure_type"] = FailureType(params)
        self.properties["user_id"] = UserID(params)

        for property_name in ["plate_barcode", "failure_type", "user_id"]:
            self.properties[property_name].is_valid()

        self.properties["destination_plate"] = self.properties["plate_barcode"]
        self.properties["wells"] = WellsFromDestination(
            self.properties["plate_barcode"])
        self.properties["source_plates"] = SourcePlatesFromDestination(
            self.properties["wells"])
        self.properties["samples"] = SamplesFromDestination(
            self.properties["wells"])
        self.properties["samples_with_cog_uk_id"] = SamplesWithCogUkId(
            self.properties["samples"])
        self.properties["controls"] = ControlsFromDestination(
            self.properties["wells"])

        self.properties["run_id"] = RunIDFromWells(self.properties["wells"])
        self.properties["run_info"] = RunInfo(self.properties["run_id"])
        self.properties["automation_system_name"] = AutomationSystemName(
            self.properties["run_info"])
        self.properties["robot_uuid"] = RobotUUID(
            self.properties["automation_system_name"])
def test_source_plates_from_destination_add_to_warehouse(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    mocked_responses,
    source_plates,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
):
    with app.app_context():
        instance = SourcePlatesFromDestination(
            WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        )

        message = WarehouseMessage("mytype", "myuuid", "at some point")
        instance.add_to_warehouse_message(message)
        assert message._subjects == [
            {
                "role_type": "cherrypicking_source_labware",
                "subject_type": "plate",
                "friendly_name": "plate_123",
                "uuid": "a17c38cd-b2df-43a7-9896-582e7855b4cc",
            }
        ]
def test_all_samples_from_destination_value_fails_with_unknown_samples(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    cherrytrack_destination_plate_response,
    mocked_responses,
    cherrytrack_mock_destination_plate,
):
    with app.app_context():
        instance = SamplesFromDestination(
            WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        )
        assert instance.is_valid() is True
        assert instance.errors == []

        with pytest.raises(Exception):
            instance.value

        assert instance.is_valid() is False
        assert instance.errors == [
            (
                "Exception during retrieval: Some samples cannot be obtained because they are not present"
                " in Mongo. Please review: ['unknown']"
            )
        ]
def test_wells_from_destination_value_gets_value(
    app,
    run_id,
    destination_barcode,
    mocked_responses,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
):
    with app.app_context():
        val = WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode})).value
        assert val == cherrytrack_destination_plate_response["data"]["wells"]
def test_wells_from_destination_value_fails_with_duplicated_wells(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    cherrytrack_destination_plate_response,
    mocked_responses,
    cherrytrack_mock_destination_plate,
):
    with app.app_context():
        instance = WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        assert instance.is_valid() is True
        assert instance.errors == []

        with pytest.raises(Exception):
            instance.value

        assert instance.is_valid() is False
        assert instance.errors == [
            "Exception during retrieval: Some coordinates have clashing samples/controls: {'H1'}"
        ]
def test_run_id_from_wells_value(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    mocked_responses,
    source_plates,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
):
    with app.app_context():
        val = RunIDFromWells(WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))).value

        assert val == 5
def test_all_controls_from_destination_value_gets_value(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    mocked_responses,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
):
    with app.app_context():
        wells = cherrytrack_destination_plate_response["data"]["wells"]
        val = ControlsFromDestination(
            WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        ).value

        assert val == {"E10": wells[2], "E11": wells[3]}
def test_all_controls_from_destination_value_fails_with_missing_controls(
    app,
    destination_barcode,
    mocked_responses,
    cherrytrack_mock_destination_plate,
):
    with app.app_context():
        instance = ControlsFromDestination(
            WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        )
        assert instance.is_valid() is True
        assert instance.errors == []

        with pytest.raises(Exception):
            instance.value

        assert instance.is_valid() is False
        assert instance.errors == [
            ("Exception during retrieval: We were expecting one positive and one negative control to be present.")
        ]
def test_source_plates_from_destination_value_gets_value(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    mocked_responses,
    source_plates,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
):
    with app.app_context():
        val = SourcePlatesFromDestination(
            WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        ).value

        for elem in val:
            del elem["_id"]

        assert val == [
            {"Lab ID": "lab_1", "barcode": "plate_123", "lh_source_plate_uuid": "a17c38cd-b2df-43a7-9896-582e7855b4cc"}
        ]
def test_samples_with_cog_uk_ids_from_destination_add_to_sequencescape(
    app,
    run_id,
    centres,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    mlwh_samples_in_cherrytrack,
    mocked_responses,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
    baracoda_mock_barcodes_group,
    baracoda_mock_responses,
):
    with app.app_context():
        samples, _ = samples_from_cherrytrack_into_mongo
        instance = SamplesWithCogUkId(
            SamplesFromDestination(WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode})))
        )
        message = SequencescapeMessage()
        instance.add_to_sequencescape_message(message)
        assert message._contents == {
            "H08": {
                "content": {
                    "name": "DS000050001_A01",
                    "phenotype": "positive",
                    "sample_description": "aRootSampleId1",
                    "supplier_name": "COGUK1",
                    "uuid": "aLighthouseUUID1",
                }
            },
            "H12": {
                "content": {
                    "name": "DS000050001_A03",
                    "phenotype": "positive",
                    "sample_description": "aRootSampleId3",
                    "supplier_name": "COGUK2",
                    "uuid": "aLighthouseUUID3",
                }
            },
        }
def test_all_samples_from_destination_value_gets_value(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    mocked_responses,
    cherrytrack_mock_destination_plate,
    cherrytrack_destination_plate_response,
):
    with app.app_context():
        val = SamplesFromDestination(
            WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        ).value
        samples, _ = samples_from_cherrytrack_into_mongo

        for sample in val.values():
            del sample["_id"]
            del sample["Date Tested"]
        for sample in samples:
            del sample["_id"]
            del sample["Date Tested"]
        assert val == {"H08": samples[0], "H12": samples[2]}
def test_all_samples_from_destination_value_fails_with_duplicated_samples(
    app,
    run_id,
    destination_barcode,
    samples_from_cherrytrack_into_mongo,
    cherrytrack_destination_plate_response,
    mocked_responses,
    cherrytrack_mock_destination_plate,
):
    with app.app_context():
        instance = SamplesFromDestination(
            WellsFromDestination(PlateBarcode({FIELD_EVENT_BARCODE: destination_barcode}))
        )
        assert instance.is_valid() is True
        assert instance.errors == []

        with pytest.raises(Exception):
            instance.value

        assert instance.is_valid() is False
        assert instance.errors == [
            ("Exception during retrieval: There is duplication in the lh sample uuids provided: ['uuid1']")
        ]