def test_is_running_checks_status_on_first_call_and_returns_correct_value(
            self):
        self.conn.send_get_request.return_value = self.status_with(
            {'running': True})
        r = Resource('ID', self.conn)

        running = r.is_running()

        self.assertTrue(running)
        self.conn.send_get_request.assert_called_once_with('/ID/status')
    def test_is_running_checks_status_on_first_call_and_returns_correct_value(self):
        self.conn.send_get_request.return_value = self.status_with({
            'running': True
        })
        r = Resource('ID', self.conn)

        running = r.is_running()

        self.assertTrue(running)
        self.conn.send_get_request.assert_called_once_with('/ID/status')
    def test_has_failed_checks_status_on_first_call_and_returns_correct_value(self):
        self.conn.send_get_request.return_value = self.status_with({
            'finished': True,
            'failed': True
        })
        r = Resource('ID', self.conn)

        failed = r.has_failed()

        self.assertTrue(failed)
        self.conn.send_get_request.assert_called_once_with('/ID/status')
    def test_has_failed_checks_status_on_first_call_and_returns_correct_value(self):
        self.conn.send_get_request.return_value = self.status_with({
            'finished': True,
            'failed': True
        })
        r = Resource('ID', self.conn)

        failed = r.has_failed()

        self.assertTrue(failed)
        self.conn.send_get_request.assert_called_once_with('/ID/status')
    def test_get_error_checks_status_on_first_call_and_returns_correct_value(self):
        self.conn.send_get_request.return_value = self.status_with({
            'finished': True,
            'failed': True,
            'error': 'Error message.'
        })
        r = Resource('ID', self.conn)

        error = r.get_error()

        self.assertEqual(error, 'Error message.')
        self.conn.send_get_request.assert_called_once_with('/ID/status')
    def test_get_error_checks_status_on_first_call_and_returns_correct_value(self):
        self.conn.send_get_request.return_value = self.status_with({
            'finished': True,
            'failed': True,
            'error': 'Error message.'
        })
        r = Resource('ID', self.conn)

        error = r.get_error()

        self.assertEqual(error, 'Error message.')
        self.conn.send_get_request.assert_called_once_with('/ID/status')
    def test_two_successive_state_queries_do_not_result_into_two_status_checks(
            self):
        # A certain time interval has to pass between successive checks for the
        # resource to query its status.
        self.conn.send_get_request.side_effect = [
            self.status_with({'pending': True}),
            self.status_with({'pending': False})
        ]
        r = Resource('ID', self.conn)

        r.is_pending()
        pending = r.is_pending()

        self.assertEqual(len(self.conn.send_get_request.mock_calls), 1)
        self.assertTrue(
            pending)  # Still True because there was only one query.
    def test_two_successive_state_queries_do_not_result_into_two_status_checks(self):
        # A certain time interval has to pass between successive checks for the
        # resource to query its status.
        self.conn.send_get_request.side_effect = [
            self.status_with({
                'pending': True
            }), self.status_with({
                'pending': False
            })
        ]
        r = Resource('ID', self.conn)

        r.is_pending()
        pending = r.is_pending()

        self.assertEqual(len(self.conn.send_get_request.mock_calls), 1)
        self.assertTrue(pending)  # Still True because there was only one query.
    def test_id_returns_passed_id(self):
        r = Resource('ID', self.conn)

        self.assertEqual(r.id, 'ID')