Exemple #1
0
def test_raises_multierror_0_errors():
    """
    raises_multierror_with_one_exception fails if MultiError has 0 exceptions
    """
    with pytest.raises(BaseException):
        with raises_multierror_with_one_exception(AssertionError):
            raise MultiError([])
Exemple #2
0
def test_freezeinfo_add_404_url(reason, expected_reasons):
    hook_called = False

    def start_hook(freezeinfo):
        nonlocal hook_called
        hook_called = True

        freezeinfo.add_url(
            'http://example.com/404-page.html',
            reason=reason,
        )

    with context_for_test('app_with_extra_page') as module:
        config = {
            'output': {
                'type': 'dict'
            },
            'prefix': 'http://example.com/',
            'hooks': {
                'start': [start_hook]
            },
        }

        with raises_multierror_with_one_exception(UnexpectedStatus) as e:
            freeze(module.app, config)
        assert e.freezeyt_task.reasons == expected_reasons
Exemple #3
0
def test_raises_multierror_nonmulti_error():
    """raises_multierror_with_one_exception fails if non-MultiError if raised
    """

    with pytest.raises(BaseException):
        with raises_multierror_with_one_exception(TypeError):
            raise TypeError()
Exemple #4
0
def test_let_incomplete_dir_intact(tmp_path):
    output_dir = tmp_path / "output"
    config = {"cleanup": False, "output": str(output_dir)}
    with raises_multierror_with_one_exception(UnexpectedStatus):
        freeze(app, config)
    assert output_dir.exists()  # the output dir has to exist
    assert (output_dir / "index.html"
            ).exists()  # the index.html file inside output dir has to exist
Exemple #5
0
def test_cleanup_empty_app(tmp_path):
    """Test that cleanup succeeds even when no page is frozen"""

    output_dir = tmp_path / "output2"
    config = {"cleanup": True, "output": str(output_dir)}
    with raises_multierror_with_one_exception(UnexpectedStatus):
        freeze(empty_app, config)
    assert not output_dir.exists()  # the output dir has to be gone
Exemple #6
0
def test_raises_multierror_different_exception():
    """raises_multierror_with_one_exception fails if MultiError has bad error
    """
    dummy_task = asyncio_run(create_failing_task())

    with pytest.raises(BaseException):
        with raises_multierror_with_one_exception(TypeError):
            raise MultiError([dummy_task])
Exemple #7
0
def test_raises_multierror_2_errors():
    """
    raises_multierror_with_one_exception fails if MultiError has too many excs
    """
    dummy_task1 = asyncio_run(create_failing_task())
    dummy_task2 = asyncio_run(create_failing_task())

    with pytest.raises(BaseException):
        with raises_multierror_with_one_exception(AssertionError):
            raise MultiError([dummy_task1, dummy_task2])
Exemple #8
0
def test_raises_multierror():
    """raises_multierror_with_one_exception exposes correct exception info
    """
    dummy_task = asyncio_run(create_failing_task())

    with raises_multierror_with_one_exception(AssertionError) as e:
        raise MultiError([dummy_task])
    assert e.type == AssertionError
    assert isinstance(e.value, AssertionError)
    assert isinstance(e.freezeyt_task, TaskInfo)
    assert e.freezeyt_task._task == dummy_task
Exemple #9
0
def test_reason_homepage():
    app = Flask(__name__)
    config = {
        'prefix': 'http://localhost/',
        'output': {
            'type': 'dict'
        },
    }

    with raises_multierror_with_one_exception(UnexpectedStatus) as e:
        freeze(app, config)
    assert str(e.value.url) == 'http://localhost:80/'
    assert e.value.status[:3] == '404'
    assert e.freezeyt_task.reasons == ['site root (homepage)']
def test_default_handlers(response_status):
    app = Flask(__name__)
    config = {
        'output': {
            'type': 'dict'
        },
    }

    @app.route('/')
    def index():
        return Response(response='Hello world!', status=response_status)

    with raises_multierror_with_one_exception(UnexpectedStatus) as e:
        freeze(app, config)

    assert e.value.status[:3] == f'{response_status}'
def test_error_custom_handler():
    app = Flask(__name__)
    config = {
        'output': {
            'type': 'dict'
        },
        'status_handlers': {
            '200': custom_handler
        }
    }

    @app.route('/')
    def index():
        return 'Hello world!'

    with raises_multierror_with_one_exception(UnexpectedStatus):
        freeze(app, config)
Exemple #12
0
def test_exc_info():
    def simple_app(environ, start_response):
        try:
            # regular application code here
            status = "200 OK"
            response_headers = [("content-type", "text/html")]
            start_response(status, response_headers)
            raise InternalServerError('something went wrong')
            return ["normal body goes here"]
        except Exception:
            status = "500 Internal Server Error"
            response_headers = [("content-type", "text/html")]
            start_response(status, response_headers, sys.exc_info())
            return [b"error body goes here"]

    config = {'output': {'type': 'dict'}}

    with raises_multierror_with_one_exception(InternalServerError):
        freeze(simple_app, config)
Exemple #13
0
def test_reason_link():
    app = Flask(__name__)

    @app.route('/')
    def index():
        return '<a href="404.html">link to 404</a>'

    config = {
        'prefix': 'http://localhost/',
        'output': {
            'type': 'dict'
        },
    }

    with raises_multierror_with_one_exception(UnexpectedStatus) as e:
        freeze(app, config)
    print(e)
    assert str(e.value.url) == 'http://localhost:80/404.html'
    assert e.value.status[:3] == '404'
    assert e.freezeyt_task.reasons == ['linked from: index.html']
Exemple #14
0
def test_reason_extra():
    app = Flask(__name__)

    @app.route('/')
    def index():
        return 'OK'

    config = {
        'prefix': 'http://localhost/',
        'output': {
            'type': 'dict'
        },
        'extra_pages': ['404.html'],
    }

    with raises_multierror_with_one_exception(UnexpectedStatus) as e:
        freeze(app, config)
    print(e)
    assert str(e.value.url) == 'http://localhost:80/404.html'
    assert e.value.status[:3] == '404'
    assert e.freezeyt_task.reasons == ['extra page']
Exemple #15
0
def test_reason_redirect():
    app = Flask(__name__)

    @app.route('/')
    def index():
        return redirect('http://localhost/404')

    config = {
        'prefix': 'http://localhost/',
        'output': {
            'type': 'dict'
        },
        'status_handlers': {
            '3xx': 'follow'
        },
    }

    with raises_multierror_with_one_exception(UnexpectedStatus) as e:
        freeze(app, config)

    assert str(e.value.url) == 'http://localhost:80/404'
    assert e.value.status[:3] == '404'
    assert e.freezeyt_task.reasons == ['target of redirect from: index.html']
Exemple #16
0
def test_remove_incomplete_dir_by_default(tmp_path):
    output_dir = tmp_path / "output3"
    config = {"output": str(output_dir)}
    with raises_multierror_with_one_exception(UnexpectedStatus):
        freeze(app, config)
    assert not output_dir.exists()  # the output dir has to be gone
Exemple #17
0
def test_raises_multierror_no_exception():
    """raises_multierror_with_one_exception fails if no error occurred"""
    with pytest.raises(BaseException):
        with raises_multierror_with_one_exception(AssertionError):
            pass