Ejemplo n.º 1
0
class TestRunSummaryPageNoArchive(NavbarTestMixin, BaseTestCase,
                                  FooterTestMixin, AccessibilityTestMixin):
    fixtures = BaseTestCase.fixtures + ["run_with_one_variable"]

    @classmethod
    def setUpClass(cls):
        """Set the instrument for all test cases"""
        super().setUpClass()
        cls.instrument_name = "TestInstrument"

    def setUp(self) -> None:
        """Set up RunSummaryPage before each test case"""
        super().setUp()
        self.page = RunSummaryPage(self.driver, self.instrument_name, 99999, 0)
        self.page.launch()

    def test_opening_run_summary_without_reduce_vars(self):
        """
        Test that opening the run summary without a reduce_vars present for the instrument
        will not show the "Reset to current" buttons as there is no current values!
        """
        # the reset to current values should not be visible
        assert self.page.warning_message.is_displayed()
        assert self.page.warning_message.text == (
            "The reduce_vars.py script is missing for this instrument."
            " Please create it before being able to submit re-runs.")

    def test_opening_run_summary_without_run_variables(self):
        """
        Test that opening the run summary without a reduce_vars present for the instrument
        will not show the "Reset to current" buttons as there is no current values!
        """
        # Delete the variables, and re-open the page
        ReductionRun.objects.get(pk=1).run_variables.all().delete()
        self.page.launch()
        # the reset to current values should not be visible
        assert self.page.warning_message.is_displayed()
        assert self.page.warning_message.text == "No variables found for this run."
        assert self.page.run_description_text(
        ) == "Run description: This is the test run_description"
Ejemplo n.º 2
0
class TestRunSummaryPageIntegration(BaseTestCase, FooterTestMixin,
                                    NavbarTestMixin, AccessibilityTestMixin):
    """
    Test cases for the InstrumentSummary page when the Rerun form is NOT visible
    """

    fixtures = BaseTestCase.fixtures + ["run_with_one_variable"]

    accessibility_test_ignore_rules = {
        # https://github.com/ISISScientificComputing/autoreduce/issues/1267
        # https://github.com/ISISScientificComputing/autoreduce/issues/1268
        "duplicate-id-aria": "input, #run_description",
    }

    @classmethod
    def setUpClass(cls):
        """ Start all external services """
        super().setUpClass()
        cls.instrument_name = "TestInstrument"
        cls.rb_number = 1234567
        cls.run_number = 99999
        cls.data_archive, cls.queue_client, cls.listener = setup_external_services(
            cls.instrument_name, 21, 21)
        cls.data_archive.add_reduction_script(cls.instrument_name,
                                              """print('some text')""")
        cls.data_archive.add_reduce_vars_script(
            cls.instrument_name,
            """standard_vars={"variable1":"test_variable_value_123"}""")

    @classmethod
    def tearDownClass(cls) -> None:
        """Stop all external services"""
        cls.queue_client.disconnect()
        cls.data_archive.delete()
        super().tearDownClass()

    def setUp(self) -> None:
        """Sets up the RunSummaryPage and shows the rerun panel before each test case"""
        super().setUp()
        self.page = RunSummaryPage(self.driver, self.instrument_name, 99999, 0)
        self.page.launch()
        # clicks the toggle to show the rerun panel, otherwise the buttons in the form are non interactive
        self.page.toggle_button.click()

    def test_submit_rerun_same_variables(self):
        """
        Test: Just opening the submit page and clicking rerun
        """
        result = submit_and_wait_for_result(self)
        assert len(result) == 2

        assert result[0].run_version == 0
        assert result[1].run_version == 1

        for run0_var, run1_var in zip(result[0].run_variables.all(),
                                      result[1].run_variables.all()):
            assert run0_var.variable == run1_var.variable

    def test_submit_rerun_changed_variable_arbitrary_value(self):
        """
        Test: Open submit page, change a variable, submit the run
        """
        # change the value of the variable field
        self.page.variable1_field = "the new value in the field"

        result = submit_and_wait_for_result(self)
        assert len(result) == 2

        assert result[0].run_version == 0
        assert result[1].run_version == 1

        for run0_var, run1_var in zip(result[0].run_variables.all(),
                                      result[1].run_variables.all()):
            # the value of the variable has been overwritten because it's the same run number
            assert run0_var.variable == run1_var.variable

        assert result[1].run_variables.first(
        ).variable.value == "the new value in the field"

    def test_submit_rerun_after_clicking_reset_initial(self):
        """
        Test: Submitting a run after changing the value and then clicking reset to initial values
        will correctly use the initial values
        """
        # change the value of the variable field
        self.page.variable1_field = "the new value in the field"

        self.page.reset_to_initial_values.click()
        result = submit_and_wait_for_result(self)
        assert len(result) == 2

        assert result[0].run_version == 0
        assert result[1].run_version == 1

        for run0_var, run1_var in zip(result[0].run_variables.all(),
                                      result[1].run_variables.all()):
            # the value of the variable has been overwritten because it's the same run number
            assert run0_var.variable == run1_var.variable

        assert result[1].run_variables.first().variable.value == "value1"

    def test_submit_rerun_after_clicking_reset_current_script(self):
        """
        Test: Submitting a run after clicking the reset to current script uses the values saved in the current script
        """
        self.page.reset_to_current_values.click()
        result = submit_and_wait_for_result(self)

        assert len(result) == 2

        assert result[0].run_version == 0
        assert result[1].run_version == 1

        for run0_var, run1_var in zip(result[0].run_variables.all(),
                                      result[1].run_variables.all()):
            # the value of the variable has been overwritten because it's the same run number
            assert run0_var.variable == run1_var.variable

        assert result[1].run_variables.first(
        ).variable.value == "test_variable_value_123"

    def test_submit_confirm_page(self):
        """
        Test: Submitting a run leads to the correct page
        """
        result = submit_and_wait_for_result(self)
        expected_url = reverse("run_confirmation",
                               kwargs={"instrument": self.instrument_name})
        assert expected_url in self.driver.current_url
        # wait until the message processing is complete before ending the test
        # otherwise the message handling can pollute the DB state for the next test
        assert len(result) == 2
        # check that the error is because of missing Mantid
        # if this fails then something else in the reduction caused an error instead!
        assert "Mantid" in result[1].admin_log
Ejemplo n.º 3
0
class TestRunSummaryPagePlots(BaseTestCase):
    """
    Test cases for the InstrumentSummary page when the Rerun form is NOT visible
    """

    fixtures = BaseTestCase.fixtures + ["one_run_plot"]

    def setUp(self) -> None:
        """
        Set up the instrument name and page
        """
        super().setUp()
        self.instrument_name = "TestInstrument"

        self.page = RunSummaryPage(self.driver, self.instrument_name, 99999, 0)
        self.run = ReductionRun.objects.first()

    def test_plot_files_png(self):
        """
        Test: PNG plot files are fetched and shown
        """
        # the plot files are expected to be in the reduction location, so we write them there for the test to work
        plot_files = [
            tempfile.NamedTemporaryFile(
                prefix="data_",
                suffix=".png",
                dir=self.run.reduction_location.first().file_path),
            tempfile.NamedTemporaryFile(
                prefix="data_",
                suffix=".png",
                dir=self.run.reduction_location.first().file_path)
        ]
        self.page.launch()

        # 1 is the logo, the other 2 are the plots
        images = self.page.images()
        assert len(images) == 3
        for img in images[1:]:
            alt_text = img.get_attribute("alt")
            assert "Plot image stored at" in alt_text
            assert any(
                os.path.basename(f.name) in alt_text for f in plot_files)

    def test_plot_files_json(self):
        """
        Test: JSON plot files are fetched and rendered by plotly
        """
        # the plot files are expected to be in the reduction location, so we write them there for the test to work
        plot_files = []

        for _ in range(2):
            # pylint:disable=consider-using-with
            tfile = tempfile.NamedTemporaryFile(
                'w',
                prefix="data_",
                suffix=".json",
                dir=self.run.reduction_location.first().file_path)
            tfile.write(
                """{"data": [{"type": "bar","x": [1,2,3],"y": [1,3,2]}]}""")
            tfile.flush()
            plot_files.append(tfile)

        self.page.launch()

        plots = self.page.plotly_plots()
        assert len(plots) == 2
        for plot in plots:
            assert any(
                plot.get_attribute("id") in file.name for file in plot_files)

    def test_plot_files_mix(self):
        """Test that both static and interactive plots are rendered"""
        plot_files = [
            tempfile.NamedTemporaryFile(
                prefix="data_",
                suffix=".png",
                dir=self.run.reduction_location.first().file_path),
            tempfile.NamedTemporaryFile(
                'w',
                prefix="data_",
                suffix=".json",
                dir=self.run.reduction_location.first().file_path)
        ]
        # write the interactive plot data
        plot_files[1].write(
            """{"data": [{"type": "bar","x": [1,2,3],"y": [1,3,2]}]}""")
        plot_files[1].flush()

        self.page.launch()

        images = self.page.images()
        # 1 is the logo, the other is the static plot
        assert len(images) == 2

        img = images[1]
        alt_text = img.get_attribute("alt")
        assert "Plot image stored at" in alt_text
        assert any(os.path.basename(f.name) in alt_text for f in plot_files)

        plots = self.page.plotly_plots()
        assert len(plots) == 1
        assert plots[0].get_attribute("id") in plot_files[1].name
class TestRunSummaryPage(NavbarTestMixin, BaseTestCase, FooterTestMixin):
    """
    Test cases for the InstrumentSummary page when the Rerun form is NOT visible
    """

    fixtures = BaseTestCase.fixtures + ["run_with_one_variable"]

    @classmethod
    def setUpClass(cls):
        """Sets up Dataarchive with scripts and sets instrument for all test cases"""
        super().setUpClass()
        cls.instrument_name = "TestInstrument"
        cls.data_archive = DataArchive([cls.instrument_name], 21, 21)
        cls.data_archive.create()
        cls.data_archive.add_reduce_vars_script(
            cls.instrument_name,
            """standard_vars={"variable1":"test_variable_value_123"}""")

    @classmethod
    def tearDownClass(cls) -> None:
        """Destroys created data archive"""
        cls.data_archive.delete()
        super().tearDownClass()

    def setUp(self) -> None:
        """Sets up RunSummaryPage before each test case"""
        super().setUp()
        self.page = RunSummaryPage(self.driver, self.instrument_name, 99999, 0)
        self.page.launch()

    def test_reduction_job_panel_displayed(self):
        """Tests that the reduction job panel is showing the right things"""
        # only one run in the fixture, get it for assertions
        run = ReductionRun.objects.first()
        assert self.page.reduction_job_panel.is_displayed()
        assert self.page.run_description_text(
        ) == f"Run description: {run.run_description}"
        # because it's started_by: -1, determined in `started_by_id_to_name`
        assert self.page.started_by_text() == "Started by: Development team"
        assert self.page.status_text() == "Status: Processing"
        assert self.page.instrument_text(
        ) == f"Instrument: {run.instrument.name}"
        assert self.page.rb_number_text(
        ) == f"RB Number: {run.experiment.reference_number}"
        assert self.page.last_updated_text(
        ) == "Last Updated: 19 Oct 2020, 6:35 p.m."
        assert self.page.reduction_host_text() == "Host: test-host-123"

    def test_reduction_job_panel_reset_to_values_first_used_for_run(self):
        """Test that the button to reset the variables to the values first used for the run works"""
        self.page.toggle_button.click()
        self.page.variable1_field = "the new value in the field"

        self.page.reset_to_initial_values.click()

        # need to re-query the driver because resetting replaces the elements
        assert self.page.variable1_field.get_attribute("value") == "value1"

    def test_reduction_job_panel_reset_to_current_reduce_vars(self):
        """Test that the button to reset the variables to the values from the reduce_vars script works"""
        self.page.toggle_button.click()
        self.page.variable1_field = "the new value in the field"

        self.page.reset_to_current_values.click()

        # need to re-query the driver because resetting replaces the elements
        assert self.page.variable1_field.get_attribute(
            "value") == "test_variable_value_123"

    def test_rerun_form(self):
        """
        Test: Rerun form shows contents from Variable in database (from the fixture) and not reduce_vars.py
        """
        rerun_form = self.page.rerun_form
        assert not rerun_form.is_displayed()
        self.page.toggle_button.click()
        assert rerun_form.is_displayed()
        assert rerun_form.find_element_by_id(
            "var-standard-variable1").get_attribute("value") == "value1"
        labels = rerun_form.find_elements_by_tag_name("label")

        WebDriverWait(
            self.driver,
            10).until(lambda _: labels[0].text == "Re-run description")
        WebDriverWait(self.driver,
                      10).until(lambda _: labels[1].text == "variable1")

    def test_back_to_instruments_goes_back(self):
        """
        Test: Clicking back goes back to the instrument
        """
        back = self.page.cancel_button
        assert back.is_displayed()
        assert back.text == f"Back to {self.instrument_name} runs"
        back.click()
        assert reverse("runs:list",
                       kwargs={"instrument": self.instrument_name
                               }) in self.driver.current_url

    def test_reset_single_to_initial(self):
        """
        Tests changing the value of a variable field and resetting to the initial value, by using the inline button
        """
        self.page.toggle_button.click()
        initial_value = self.page.variable1_field_val
        self.page.variable1_field = "the new value in the field"
        assert self.page.variable1_field_val != initial_value

        self.page.variable1_field_reset_buttons.to_initial.click()
        assert self.page.variable1_field_val == initial_value

    def test_reset_single_to_script(self):
        """
        Tests changing the value of a variable field and resetting to the script value, by using the inline button
        """
        self.page.toggle_button.click()
        initial_value = "test_variable_value_123"
        self.page.variable1_field = "the new value in the field"
        assert self.page.variable1_field_val != initial_value

        self.page.variable1_field_reset_buttons.to_script.click()
        assert self.page.variable1_field_val == initial_value