Exemple #1
0
    def test_update_last_runs_with_error_and_teams_url(self, teams_url: Mock, logger_mock: Mock,
                                                       requests_post_mock: Mock):
        """
        Test trying to update last runs but the request to the autoreduce API fails.
        The request to the teams API does not raise an exception in this test, i.e. the success path.
        """
        # Setup test
        with open('test_last_runs.csv', mode='w', encoding="utf-8") as last_runs:
            last_runs.write(CSV_FILE)

        # write out the lastruns.txt file that would usually be on the archive
        with open('lastrun_wish.txt', mode='w', encoding="utf-8") as lastrun_wish:
            lastrun_wish.write(LASTRUN_WISH_TXT)

        # Perform test
        update_last_runs('test_last_runs.csv')
        requests_post_mock.asssert_called_once()

        # Read the CSV and ensure it has been updated
        with open('test_last_runs.csv', encoding="utf-8") as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:
                if row:  # Avoid the empty rows
                    self.assertEqual('44733', row[1])

        assert logger_mock.info.call_count == 2
        assert logger_mock.error.call_count == 2

        assert requests_post_mock.call_count == 2
        assert teams_url in requests_post_mock.call_args[0]
Exemple #2
0
    def test_update_last_runs_not_200_status(self, requests_post_mock: Mock):
        """
        Test when the response is not 200 OK that the error is handled
        """
        mock_response = MockResponse()
        mock_response.status_code = 401
        # write out the local lastruns.csv that is used to track each instrument
        with open('test_last_runs.csv', mode='w', encoding="utf-8") as last_runs:
            last_runs.write(CSV_FILE)

        # write out the lastruns.txt file that would usually be on the archive
        with open('lastrun_wish.txt', mode='w', encoding="utf-8") as lastrun_wish:
            lastrun_wish.write(LASTRUN_WISH_TXT)

        # Perform test
        update_last_runs('test_last_runs.csv')
        requests_post_mock.assert_called_once()
        assert requests_post_mock.call_args[0][0] == AUTOREDUCE_API_URL.format(instrument="WISH")
        assert "json" in requests_post_mock.call_args[1]
        assert "headers" in requests_post_mock.call_args[1]

        assert requests_post_mock.call_args[1]["json"]["runs"] == [44734, 44735]
        assert requests_post_mock.call_args[1]["json"]["user_id"] == 0

        # Read the CSV and ensure it has been updated
        with open('test_last_runs.csv', encoding="utf-8") as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:
                if row:  # Avoid the empty rows
                    # the row value should be UNCHANGED as the submission request failed
                    self.assertEqual('44733', row[1])
Exemple #3
0
    def test_update_last_runs_with_error_and_teams_url_also_fails(self, exception_class, _: Mock, logger_mock: Mock,
                                                                  requests_post_mock: Mock):
        """
        Test trying to update last runs but both the request to the
        autoreduce API and the request to the teams API fail.
        """
        # Setup test
        requests_post_mock.side_effect = exception_class
        with open('test_last_runs.csv', mode='w', encoding="utf-8") as last_runs:
            last_runs.write(CSV_FILE)

        # write out the lastruns.txt file that would usually be on the archive
        with open('lastrun_wish.txt', mode='w', encoding="utf-8") as lastrun_wish:
            lastrun_wish.write(LASTRUN_WISH_TXT)

        # Perform test
        update_last_runs('test_last_runs.csv')
        requests_post_mock.asssert_called_once()

        # Read the CSV and ensure it has been updated
        with open('test_last_runs.csv', encoding="utf-8") as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:
                if row:  # Avoid the empty rows
                    self.assertEqual('44733', row[1])

        assert logger_mock.info.call_count == 2
        assert logger_mock.error.call_count == 3
Exemple #4
0
    def test_update_last_runs(self, requests_post_mock: Mock):
        """
        Test submission with a 200 OK response, everything working OK
        """
        # write out the local lastruns.csv that is used to track each instrument
        with open('test_last_runs.csv', mode='w', encoding="utf-8") as last_runs:
            last_runs.write(CSV_FILE)

        # write out the lastruns.txt file that would usually be on the archive
        with open('lastrun_wish.txt', mode='w', encoding="utf-8") as lastrun_wish:
            lastrun_wish.write(LASTRUN_WISH_TXT)

        # Perform test
        update_last_runs('test_last_runs.csv')
        requests_post_mock.assert_called_once()
        assert requests_post_mock.call_args[0][0] == AUTOREDUCE_API_URL.format(instrument="WISH")
        assert "json" in requests_post_mock.call_args[1]
        assert "headers" in requests_post_mock.call_args[1]

        assert requests_post_mock.call_args[1]["json"]["runs"] == [44734, 44735]
        assert requests_post_mock.call_args[1]["json"]["user_id"] == 0

        # Read the CSV and ensure it has been updated
        with open('test_last_runs.csv', encoding="utf-8") as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:
                if row:  # Avoid the empty rows
                    self.assertEqual('44735', row[1])