def test_extract_custom_form_info_samples_not_ascii_characters(self):
        template_path = (os.path.join(
            os.path.split(__file__)[0],
            "extract_custom_form_info_samples_not_ascii_characters.xml"))
        template_path
        with open(template_path) as file:
            req = file.read()
        req_soup = BeautifulSoup(req, "xml")

        form = ua_ilab_tools.extract_custom_form_info("0", "3209463", req_soup)
        samples = form.samples
        for sample in samples:
            assert re.search(r"[^0-9A-Za-z-]", sample.con.name) is None
    def test_extract_from_grid_tgm_data(self):
        template_path = (os.path.join(
            os.path.split(__file__)[0], "extract_from_grid_tgm_data.xml"))
        with open(template_path) as file:
            req = file.read()
        req_soup = BeautifulSoup(req, "xml")

        form = ua_ilab_tools.extract_custom_form_info("0", "3904550", req_soup)
        samples = form.samples

        assert len(samples) == 19
        for sample in samples:
            assert "TGM Assay List" in sample.udf_to_value.keys()
            assert sample.udf_to_value["TGM Assay List"][-1] != ','
    def test_extract_from_grid_lvs_data(self):
        template_path = (os.path.join(
            os.path.split(__file__)[0], "extract_from_grid_lvs_primers.xml"))
        with open(template_path) as file:
            req = file.read()
        req_soup = BeautifulSoup(req, "xml")

        form = ua_ilab_tools.extract_custom_form_info("0", "3923775", req_soup)
        samples = form.samples

        assert len(samples) == 7
        for sample in samples:
            assert "Primer" in sample.udf_to_value.keys()
            assert '*' not in sample.udf_to_value["Primer"]
            assert len(sample.udf_to_value["Primer"].split(',')) == 3
    def test_extract_from_grid_eight_strip_too_many_tubes(self):
        template_path = (os.path.join(
            os.path.split(__file__)[0],
            "extract_from_grid_con_type_eight_strip_too_many_tubes.xml"))
        with open(template_path) as file:
            req = file.read()
        req_soup = BeautifulSoup(req, "xml")

        form = ua_ilab_tools.extract_custom_form_info("0", "3220469", req_soup)
        samples = form.samples
        sample_names = ["A3", "B13", "H11", "F8", "G2", "C3", "C5", "E6"]
        assert len(samples) == len(sample_names)
        for sample in samples:
            assert sample.name in sample_names
            assert re.search(r"2220920-[1-8]", sample.con.name) is not None
            assert sample.con.con_type == "8-well strip"
            assert re.search(r"A:[1-8]", sample.location) is not None
            assert "Volume (uL)" in sample.udf_to_value.keys()
    def test_extract_custom_form_info_samples_con_type_tube(self):
        template_path = (os.path.join(
            os.path.split(__file__)[0],
            "extract_custom_form_info_samples_con_type_tube.xml"))
        with open(template_path) as file:
            req = file.read()
        req_soup = BeautifulSoup(req, "xml")

        samples = ua_ilab_tools.extract_custom_form_info(
            "0", "3271720", req_soup).samples
        sample_names = [
            "UA2001111", "UA2002222", "UA2003333", "UA2004444", "UA2005555"
        ]
        assert len(samples) == len(sample_names)
        for sample in samples:
            assert sample.name in sample_names
            assert sample.con.name in sample_names
            assert sample.con.con_type == "Tube"
            assert sample.location == "1:1"
            assert "TGM Assay List" in sample.udf_to_value.keys()
    def test_extract_from_grid_container_name_in_form(self):
        template_path = (os.path.join(
            os.path.split(__file__)[0],
            "extract_from_grid_container_name_in_form.xml"))
        with open(template_path) as file:
            form = file.read()
        form_soup = BeautifulSoup(form, "xml")

        form = ua_ilab_tools.extract_custom_form_info("0", "3275577",
                                                      form_soup)
        samples = form.samples
        sample_names = [sample.name for sample in samples]

        assert "Empty" not in sample_names
        assert len(samples) == 67
        assert form.field_to_values["container_name"] == "Test-Plate-Name"
        for sample in samples:
            assert "AF" in sample.name
            assert sample.con.name == "Test-Plate-Name"
            assert sample.con.con_type == "96 well plate"
            assert re.search(r"[A-H]:[0-9]{1,2}", sample.location) is not None
            assert "Dilution" in sample.udf_to_value.keys()
    def test_extract_from_grid_container_name_in_grid(self):
        template_path = (os.path.join(
            os.path.split(__file__)[0],
            "extract_from_grid_container_name_in_grid.xml"))
        with open(template_path) as file:
            req = file.read()
        req_soup = BeautifulSoup(req, "xml")

        form = ua_ilab_tools.extract_custom_form_info("0", "3256202", req_soup)
        samples = form.samples
        sample_names = [sample.name for sample in samples]
        container_names = [
            "Plate-1", "Plate-2", "Plate-3", "Plate-4", "Plate-5", "Plate-6"
        ]

        assert "Empty" not in sample_names
        assert len(samples) == 540
        assert "Dilution_each_sample" in form.field_to_values.keys()
        for sample in samples:
            assert '-' in sample.name
            assert sample.con.name in container_names
            assert sample.con.con_type == "96 well plate"
            assert re.search(r"[A-H]:[0-9]{1,2}", sample.location) is not None
            assert "Dilution" in sample.udf_to_value.keys()
Beispiel #8
0
def harvest_form(prj_data):
    """Get the correct form from iLab.

    Arguments:
        prj_data (dataclass):
            Object that holds important information for transferring projects.

    Returns:
        custom_form (api_types.CustomForm):
            Object that is populated with all of the information gathered
            from the iLab form.
            None if nothing is found."""
    try:
        forms_uri_to_soup = ILAB_TOOLS.get_custom_forms(prj_data.req_id)
    except ua_ilab_tools.IlabConfigError:
        # Requests without forms are Consultation Requests or Custom Projects
        # that have not yet had a business form attached, and so the request
        # should just be skipped.
        return None
    except requests.exceptions.HTTPError as error:
        # Skip requests that were made with different core ids.
        return None
    # Make sure the current_form is zeroed out between request id's.
    else:
        current_form = None

    # Get all of the custom forms for the current req_id.
    for form_uri, form_soup in forms_uri_to_soup.items():
        # Get the form info (including the sample info) before you post
        # anything, as this is where most of the errors are thrown.
        try:
            current_form = ua_ilab_tools.extract_custom_form_info(
                prj_data.req_id, form_uri, form_soup)

        except TypeError as grid_error:
            grid_error = str(grid_error).replace('"', '\'')
            LOGGER.error({
                "template":
                os.path.join("project_transfer", "error.html"),
                "content":
                (f"The request {prj_data.current_record} has been filled"
                 f" out incorrectly. The error message is:\n{grid_error}")
            })
            break
        except ValueError as form_error:
            LOGGER.error({
                "template":
                os.path.join("project_transfer", "error.html"),
                "content":
                form_error
            })
            continue

        # Add the request_type to the form that was just added.
        current_form.request_type = prj_data.request_type

        # If the request has a non-queryable (NQ) form or Request a Quote A
        # Form, skip it.
        if ("NQ" in current_form.name.strip()
                or "REQUEST A QUOTE" in current_form.name.strip().upper()
                or "DATA ANALYSIS INFO" in current_form.name.strip().upper()):
            current_form = None
            continue

        if not current_form.samples:
            LOGGER.error({
                "template":
                os.path.join("project_transfer", "error.html"),
                "content": (f"The {current_form.name} form in request"
                            f" {current_form.req_id} has no samples.")
            })

    return current_form