Esempio n. 1
0
def test_restricted_eval():
    forbidden_string = "len([1, 2, 3])"
    with raises(ValidationError):
        CriterionField._restricted_eval(forbidden_string)
    forbidden_string = "''.__class__"
    with raises(ValidationError):
        CriterionField._restricted_eval(forbidden_string)
    accepted_string = "Greater(5)"
    assert Greater(5) == CriterionField._restricted_eval(accepted_string)
Esempio n. 2
0
def test_shrink() -> None:

    assert shrink(-5, 1) == -4
    assert shrink(-5, 5) == 0
    assert shrink(0, 0) == 0
    assert shrink(5, 1) == 4
    assert shrink(5, 0) == 5

    with raises(AssertionError):
        shrink(-5, -1)
    with raises(AssertionError):
        shrink(10, 11)
def test_comparisons(q: Tensor):
    q_ = q / 2 * (2 * second)
    qm = q * meter
    q = q * second

    assert (q == q_).all()
    assert torch.allclose(q, q_)
    assert torch.isclose(q, q_).all()

    with raises(UnitError):
        _ = q == qm
    with raises(UnitError):
        torch.allclose(q, qm)
    with raises(UnitError):
        torch.isclose(q, qm)
Esempio n. 4
0
def test_call_events(storage):
    """It should call the appropriate functions on every successful/failed call.
    """
    class Listener(CircuitBreakerListener):
        def __init__(self):
            self.out = []

        def before_call(self, breaker, func, *args, **kwargs):
            assert breaker
            self.out.append("CALL")

        def success(self, breaker):
            assert breaker
            self.out.append("SUCCESS")

        def failure(self, breaker, exception):
            assert breaker
            assert isinstance(exception, DummyException)
            self.out.append("FAILURE")

    listener = Listener()
    breaker = CircuitBreaker(listeners=(listener, ), state_storage=storage)

    assert breaker.call(func_succeed)

    with raises(DummyException):
        breaker.call(func_exception)

    assert ["CALL", "SUCCESS", "CALL", "FAILURE"] == listener.out
Esempio n. 5
0
def should_fail_if_unknown_sqs_message_type() -> None:
    with raises(UnhandledSQSMessageException):
        lambda_handler(
            {
                RECORDS_KEY: [{
                    BODY_KEY: any_dataset_version_id(),
                    MESSAGE_ATTRIBUTES_KEY: {
                        MESSAGE_ATTRIBUTE_TYPE_KEY: {
                            STRING_VALUE_KEY_LOWER: "test",
                            DATA_TYPE_KEY: DATA_TYPE_STRING,
                        }
                    },
                }]
            },
            any_lambda_context(),
        )
Esempio n. 6
0
def test_recursive_conversion_without_ref():
    tmp = None
    conversion = Conversion(rec_converter, sub_conversion=LazyConversion(lambda: tmp))
    tmp = conversion
    with raises(TypeError, match=r"Recursive type <.*> need a ref"):
        serialization_schema(RecConv, conversion=conversion)
Esempio n. 7
0
    def test_response(self):
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.IRRD,
            response_type=WhoisQueryResponseType.SUCCESS,
            result='test').generate_response()
        assert response == 'A5\ntest\nC\n'
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.IRRD,
            response_type=WhoisQueryResponseType.SUCCESS,
            result='').generate_response()
        assert response == 'C\n'
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.IRRD,
            response_type=WhoisQueryResponseType.KEY_NOT_FOUND,
            result='test').generate_response()
        assert response == 'D\n'
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.IRRD,
            response_type=WhoisQueryResponseType.ERROR,
            result='test').generate_response()
        assert response == 'F test\n'
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.RIPE,
            response_type=WhoisQueryResponseType.SUCCESS,
            result='test').generate_response()
        assert response == 'test\n\n'
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.RIPE,
            response_type=WhoisQueryResponseType.SUCCESS,
            result='').generate_response()
        assert response == '%  No entries found for the selected source(s).\n'
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.RIPE,
            response_type=WhoisQueryResponseType.KEY_NOT_FOUND,
            result='test').generate_response()
        assert response == '%  No entries found for the selected source(s).\n'
        response = WhoisQueryResponse(
            mode=WhoisQueryResponseMode.RIPE,
            response_type=WhoisQueryResponseType.ERROR,
            result='test').generate_response()
        assert response == '%% test\n'

        with raises(RuntimeError) as ve:
            # noinspection PyTypeChecker
            WhoisQueryResponse(mode='bar',
                               response_type=WhoisQueryResponseType.ERROR,
                               result='foo').generate_response()  # type:ignore
        assert 'foo' in str(ve)

        with raises(RuntimeError) as ve:
            # noinspection PyTypeChecker
            WhoisQueryResponse(mode=WhoisQueryResponseMode.IRRD,
                               response_type='foo',
                               result='foo').generate_response()  # type:ignore
        assert 'foo' in str(ve)

        with raises(RuntimeError) as ve:
            # noinspection PyTypeChecker
            WhoisQueryResponse(mode=WhoisQueryResponseMode.RIPE,
                               response_type='foo',
                               result='foo').generate_response()  # type:ignore
        assert 'foo' in str(ve)
Esempio n. 8
0
def test_relative_by_positive_two_out_of_range_raises_value_error():
    with raises(ValueError):
        relative_to([2, 4, 6, 9, 7], 9, offset=+2)
Esempio n. 9
0
 def test_node_symmetric_difference_in_place(self):
     n = Node()
     with raises(TypeError):
         n ^= alice
Esempio n. 10
0
 def test_node_difference_in_place(self):
     n = Node()
     with raises(TypeError):
         n -= alice
Esempio n. 11
0
 def test_node_intersection_in_place(self):
     n = Node()
     with raises(TypeError):
         n &= alice
Esempio n. 12
0
 def test_node_union_in_place(self):
     n = Node()
     with raises(TypeError):
         n |= alice
Esempio n. 13
0
def test_generic_ref_error(cls):
    with raises(TypeError):
        type_name("Data")(cls)
Esempio n. 14
0
    ASubclass,
    NestingClass,
    Parameters,
)


@mark.parametrize(
    "name,expected",
    [
        ("tests.instantiate.Adam", Adam),
        ("tests.instantiate.Parameters", Parameters),
        ("tests.instantiate.AClass", AClass),
        ("tests.instantiate.ASubclass", ASubclass),
        ("tests.instantiate.NestingClass", NestingClass),
        ("tests.instantiate.AnotherClass", AnotherClass),
        ("", raises(ImportError, match=re.escape("Empty path"))),
        [
            "not_found",
            raises(ImportError,
                   match=re.escape("Error loading module 'not_found'")),
        ],
        (
            "tests.instantiate.b.c.Door",
            raises(ImportError,
                   match=re.escape("No module named 'tests.instantiate.b'")),
        ),
    ],
)
def test_locate(name: str, expected: Any) -> None:
    if isinstance(expected, RaisesContext):
        with expected:
Esempio n. 15
0
def test_generic_ref_error(cls):
    with raises(TypeError):
        schema_ref(...)(cls)
Esempio n. 16
0
def assertRaises(func, exception, match):

    with raises(exception, match=match):
        func()
Esempio n. 17
0
def test_relative_to_missing_item_raises_value_error():
    with raises(ValueError):
        relative_to([4, 6, 9], 12, offset=1)