Exemplo n.º 1
0
def test_put_is_updating(controller: layabase.CRUDController):
    controller.post({
        "key": "my_key1",
        "mandatory": 1,
        "optional": "my_value1"
    })
    assert (
        {
            "key": "my_key1",
            "mandatory": 1,
            "optional": "my_value1"
        },
        {
            "key": "my_key1",
            "mandatory": 1,
            "optional": "my_value"
        },
    ) == controller.put({
        "key": "my_key1",
        "optional": "my_value"
    })
    assert [{
        "key": "my_key1",
        "mandatory": 1,
        "optional": "my_value"
    }] == controller.get({"mandatory": 1})
def test_post_many_with_empty_list_is_invalid(
        controller: layabase.CRUDController):
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.post_many([])
    assert exception_info.value.errors == {"": ["No data provided."]}
    assert exception_info.value.received_data == []
    assert controller.get_audit({}) == []
def app(controller: layabase.CRUDController):
    application = flask.Flask(__name__)
    application.testing = True
    api = flask_restplus.Api(application)
    namespace = api.namespace("Test", path="/")

    controller.namespace(namespace)

    @namespace.route("/test")
    class TestResource(flask_restplus.Resource):
        @namespace.expect(controller.query_get_parser)
        @namespace.marshal_with(controller.get_response_model)
        def get(self):
            return []

        @namespace.expect(controller.json_post_model)
        def post(self):
            return []

        @namespace.expect(controller.json_put_model)
        def put(self):
            return []

        @namespace.expect(controller.query_delete_parser)
        def delete(self):
            return []

    @namespace.route("/test/description")
    class TestDescriptionResource(flask_restplus.Resource):
        @namespace.marshal_with(controller.get_model_description_response_model
                                )
        def get(self):
            return {}

    return application
Exemplo n.º 4
0
def test_post_without_key_is_invalid(controller: layabase.CRUDController):
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.post({"mandatory": 1})
    assert exception_info.value.errors == {
        "key": ["Missing data for required field."]
    }
    assert exception_info.value.received_data == {"mandatory": 1}
Exemplo n.º 5
0
def test_post_many_with_wrong_type_is_invalid(controller: layabase.CRUDController):
    with pytest.raises(ValidationFailed) as exception_info:
        controller.post_many([{"key": datetime.date(2007, 12, 5), "mandatory": 1}])
    assert {0: {"key": ["Not a valid str."]}} == exception_info.value.errors
    assert [
        {"key": datetime.date(2007, 12, 5), "mandatory": 1}
    ] == exception_info.value.received_data
Exemplo n.º 6
0
def test_get_without_filter_is_retrieving_the_only_item(
    controller: layabase.CRUDController,
):
    controller.post({"key": "my_key1", "mandatory": 1, "optional": "my_value1"})
    assert controller.get({}) == [
        {"mandatory": 1, "optional": "my_value1", "key": "my_key1"}
    ]
def test_get_is_valid_with_date_and_greater_than_sign_as_tuple_in_date_column(
    controller: layabase.CRUDController,
):
    controller.post_many(
        [
            {"date_value": "2019-01-01"},
            {"date_value": "2019-01-02"},
            {"date_value": "2019-01-03"},
        ]
    )
    assert [
        {
            "int_value": None,
            "float_value": None,
            "date_value": "2019-01-02",
            "datetime_value": None,
        },
        {
            "int_value": None,
            "float_value": None,
            "date_value": "2019-01-03",
            "datetime_value": None,
        },
    ] == controller.get(
        {
            "date_value": (
                layabase.ComparisonSigns.Greater,
                datetime.datetime(2019, 1, 1, 0, 0, 0),
            )
        }
    )
def test_get_is_valid_with_float_and_less_than_sign_as_tuple_in_float_column(
    controller: layabase.CRUDController, ):
    controller.post_many([
        {
            "id": "1",
            "float_value": 0.9
        },
        {
            "id": "2",
            "float_value": 1.0
        },
        {
            "id": "3",
            "float_value": 1.1
        },
    ])
    assert controller.get(
        {"float_value": (layabase.ComparisonSigns.Lower, 1.1)}) == [
            {
                "id": "1",
                "int_value": None,
                "float_value": 0.9,
                "date_value": None,
                "datetime_value": None,
            },
            {
                "id": "2",
                "int_value": None,
                "float_value": 1.0,
                "date_value": None,
                "datetime_value": None,
            },
        ]
def test_rollback_without_revision_is_invalid(controller: layabase.CRUDController):
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.rollback_to({"key": "unknown"})
    assert exception_info.value.errors == {
        "revision": ["Missing data for required field."]
    }
    assert exception_info.value.received_data == {"key": "unknown"}
def test_post_versioning_is_valid(controller: layabase.CRUDController):
    assert controller.post(
        {
            "key": "first",
            "dict_field.first_key": EnumTest.Value1,
            "dict_field.second_key": 1,
        }
    ) == {
        "key": "first",
        "dict_field": {"first_key": "Value1", "second_key": 1},
        "valid_since_revision": 1,
        "valid_until_revision": -1,
    }
    assert controller.get_history({}) == [
        {
            "key": "first",
            "dict_field": {"first_key": "Value1", "second_key": 1},
            "valid_since_revision": 1,
            "valid_until_revision": -1,
        }
    ]
    assert controller.get({}) == [
        {
            "key": "first",
            "dict_field": {"first_key": "Value1", "second_key": 1},
            "valid_since_revision": 1,
            "valid_until_revision": -1,
        }
    ]
Exemplo n.º 11
0
def test_delete_invalid_date_is_invalid(controller: layabase.CRUDController):
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.delete({"date_str": "this is not a date"})
    assert exception_info.value.errors == {"date_str": ["Not a valid date."]}
    assert exception_info.value.received_data == {
        "date_str": "this is not a date"
    }
Exemplo n.º 12
0
def test_put_with_unknown_fields_is_valid(controller: layabase.CRUDController):
    controller.post({
        "key": "my_key1",
        "date_str": "2018-12-30",
        "datetime_str": "2016-09-23T23:59:59",
    })
    assert controller.put({
        "key": "my_key1",
        "date_str": "2018-12-31",
        "unknown_field": "value"
    }) == (
        {
            "key": "my_key1",
            "date_str": "2018-12-30",
            "datetime_str": "2016-09-23T23:59:59",
        },
        {
            "key": "my_key1",
            "date_str": "2018-12-31",
            "datetime_str": "2016-09-23T23:59:59",
        },
    )
    assert controller.get({"date_str": "2018-12-31"}) == [{
        "key":
        "my_key1",
        "date_str":
        "2018-12-31",
        "datetime_str":
        "2016-09-23T23:59:59",
    }]
    assert controller.get({"date_str": "2018-12-30"}) == []
Exemplo n.º 13
0
def test_get_with_multiple_results_dot_notation_as_list_is_valid(
        controller: layabase.CRUDController):
    controller.post_many([
        {
            "key": "my_key",
            "dict_col": {
                "first_key": EnumTest.Value1,
                "second_key": 3
            },
        },
        {
            "key": "my_key2",
            "dict_col": {
                "first_key": EnumTest.Value2,
                "second_key": 4
            },
        },
    ])
    assert [
        {
            "dict_col": {
                "first_key": "Value1",
                "second_key": 3
            },
            "key": "my_key"
        },
        {
            "dict_col": {
                "first_key": "Value2",
                "second_key": 4
            },
            "key": "my_key2"
        },
    ] == controller.get(
        {"dict_col.first_key": [EnumTest.Value1, EnumTest.Value2]})
Exemplo n.º 14
0
def test_update_with_dot_notation_invalid_value_is_invalid(
        controller: layabase.CRUDController):
    assert {
        "dict_col": {
            "first_key": "Value1",
            "second_key": 3
        },
        "key": "my_key",
    } == controller.post({
        "key": "my_key",
        "dict_col": {
            "first_key": "Value1",
            "second_key": 3
        }
    })
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put({
            "key": "my_key",
            "dict_col.second_key": "invalid integer"
        })
    assert {
        "dict_col.second_key": ["Not a valid int."]
    } == exception_info.value.errors
    assert {
        "key": "my_key",
        "dict_col.second_key": "invalid integer",
    } == exception_info.value.received_data
def test_get_is_valid_with_float_and_greater_than_or_equal_sign_as_tuple_in_float_column(
    controller: layabase.CRUDController,
):
    controller.post_many(
        [{"float_value": 0.9}, {"float_value": 1.0}, {"float_value": 1.1}]
    )
    assert [
        {
            "int_value": None,
            "float_value": 0.9,
            "date_value": None,
            "datetime_value": None,
        },
        {
            "int_value": None,
            "float_value": 1.0,
            "date_value": None,
            "datetime_value": None,
        },
        {
            "int_value": None,
            "float_value": 1.1,
            "date_value": None,
            "datetime_value": None,
        },
    ] == controller.get({"float_value": (layabase.ComparisonSigns.GreaterOrEqual, 0.9)})
def test_get_is_valid_with_int_and_greater_than_sign_as_tuple_in_int_column(
        controller: layabase.CRUDController):
    controller.post_many([
        {
            "id": "1",
            "int_value": 122
        },
        {
            "id": "2",
            "int_value": 123
        },
        {
            "id": "3",
            "int_value": 124
        },
    ])
    assert controller.get(
        {"int_value": (layabase.ComparisonSigns.Greater, 122)}) == [
            {
                "id": "2",
                "int_value": 123,
                "float_value": None,
                "date_value": None,
                "datetime_value": None,
            },
            {
                "id": "3",
                "int_value": 124,
                "float_value": None,
                "date_value": None,
                "datetime_value": None,
            },
        ]
def test_get_is_valid_with_float_range_using_comparison_signs_as_tuple_in_float_column(
    controller: layabase.CRUDController,
):
    controller.post_many(
        [{"float_value": 0.9}, {"float_value": 1.0}, {"float_value": 1.1}]
    )
    assert controller.get(
        {
            "float_value": [
                (layabase.ComparisonSigns.Greater, 0.9),
                (layabase.ComparisonSigns.LowerOrEqual, 1.1),
            ]
        }
    ) == [
        {
            "int_value": None,
            "float_value": 1.0,
            "date_value": None,
            "datetime_value": None,
        },
        {
            "int_value": None,
            "float_value": 1.1,
            "date_value": None,
            "datetime_value": None,
        },
    ]
Exemplo n.º 18
0
def test_get_history(controller: layabase.CRUDController):
    controller.post({"key": "my_key1", "mandatory": 1, "optional": "my_value1"})
    controller.post({"key": "my_key2", "mandatory": 2, "optional": "my_value2"})
    assert controller.get_history({}) == [
        {"key": "my_key1", "mandatory": 1, "optional": "my_value1"},
        {"key": "my_key2", "mandatory": 2, "optional": "my_value2"},
    ]
def test_get_is_valid_with_date_range_using_comparison_signs_as_tuple_in_date_column(
    controller: layabase.CRUDController,
):
    controller.post_many(
        [
            {"date_value": "2019-01-01"},
            {"date_value": "2019-01-02"},
            {"date_value": "2019-01-03"},
        ]
    )
    assert controller.get(
        {
            "date_value": [
                (
                    layabase.ComparisonSigns.Greater,
                    datetime.datetime(2019, 1, 1, 0, 0, 0),
                ),
                (
                    layabase.ComparisonSigns.Lower,
                    datetime.datetime(2019, 1, 3, 0, 0, 0),
                ),
            ]
        }
    ) == [
        {
            "int_value": None,
            "float_value": None,
            "date_value": "2019-01-02",
            "datetime_value": None,
        }
    ]
Exemplo n.º 20
0
def test_get_with_non_nullable_none_is_valid(controller: layabase.CRUDController):
    assert {"mandatory": 1, "key": "my_key", "optional": None} == controller.post(
        {"key": "my_key", "mandatory": 1}
    )
    assert [{"mandatory": 1, "key": "my_key", "optional": None}] == controller.get(
        {"key": "my_key", "mandatory": None}
    )
def test_get_is_valid_with_int_range_and_value_out_of_range_using_comparison_signs_as_tuple_in_int_column(
    controller: layabase.CRUDController,
):
    controller.post_many(
        [{"int_value": 122}, {"int_value": 123}, {"int_value": 124}, {"int_value": 125}]
    )
    assert [
        {
            "int_value": 122,
            "float_value": None,
            "date_value": None,
            "datetime_value": None,
        },
        {
            "int_value": 123,
            "float_value": None,
            "date_value": None,
            "datetime_value": None,
        },
        {
            "int_value": 125,
            "float_value": None,
            "date_value": None,
            "datetime_value": None,
        },
    ] == controller.get(
        {
            "int_value": [
                (layabase.ComparisonSigns.GreaterOrEqual, 122),
                (layabase.ComparisonSigns.Lower, 124),
                125,
            ]
        }
    )
Exemplo n.º 22
0
def test_delete_without_filter_is_removing_everything(
    controller: layabase.CRUDController,
):
    controller.post({"key": "my_key1", "mandatory": 1, "optional": "my_value1"})
    controller.post({"key": "my_key2", "mandatory": 2, "optional": "my_value2"})
    assert 2 == controller.delete({})
    assert [] == controller.get({})
def test_get_is_valid_with_datetime_and_less_than_sign_as_tuple_in_datetime_column(
    controller: layabase.CRUDController,
):
    controller.post_many(
        [
            {"datetime_value": "2019-01-01T23:59:59"},
            {"datetime_value": "2019-01-02T23:59:59"},
            {"datetime_value": "2019-01-03T23:59:59"},
        ]
    )
    assert [
        {
            "int_value": None,
            "float_value": None,
            "date_value": None,
            "datetime_value": "2019-01-01T23:59:59",
        },
        {
            "int_value": None,
            "float_value": None,
            "date_value": None,
            "datetime_value": "2019-01-02T23:59:59",
        },
    ] == controller.get(
        {
            "datetime_value": (
                layabase.ComparisonSigns.Lower,
                datetime.datetime(2019, 1, 3, 23, 59, 59),
            )
        }
    )
Exemplo n.º 24
0
def test_put_with_empty_dict_is_invalid(controller: layabase.CRUDController):
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put({})
    assert exception_info.value.errors == {
        "key": ["Missing data for required field."]
    }
    assert exception_info.value.received_data == {}
Exemplo n.º 25
0
def test_put_is_updating_date(controller: layabase.CRUDController):
    controller.post({
        "key": "my_key1",
        "date_str": "2017-05-15",
        "datetime_str": "2016-09-23T23:59:59",
    })
    assert (
        {
            "date_str": "2017-05-15",
            "datetime_str": "2016-09-23T23:59:59",
            "key": "my_key1",
        },
        {
            "date_str": "2018-06-01",
            "datetime_str": "1989-12-31T01:00:00",
            "key": "my_key1",
        },
    ) == controller.put({
        "key": "my_key1",
        "date_str": "2018-06-01",
        "datetime_str": "1989-12-31T01:00:00",
    })
    assert [{
        "date_str": "2018-06-01",
        "datetime_str": "1989-12-31T01:00:00",
        "key": "my_key1",
    }] == controller.get({"date_str": "2018-06-01"})
Exemplo n.º 26
0
def test_get_without_filter_is_retrieving_everything(
    controller: layabase.CRUDController, ):
    controller.post_many([
        {
            "key": "my_key1",
            "mandatory": 1,
            "optional": "my_value1"
        },
        {
            "key": "my_key2",
            "mandatory": 2,
            "optional": "my_value2"
        },
    ])
    assert [
        {
            "key": "my_key1",
            "mandatory": 1,
            "optional": "my_value1"
        },
        {
            "key": "my_key2",
            "mandatory": 2,
            "optional": "my_value2"
        },
    ] == controller.get({})
def test_get_is_valid_with_date_and_less_than_sign_as_tuple_in_date_column(
        controller: layabase.CRUDController):
    controller.post_many([
        {
            "id": "1",
            "date_value": "2019-01-01"
        },
        {
            "id": "2",
            "date_value": "2019-01-02"
        },
        {
            "id": "3",
            "date_value": "2019-01-03"
        },
    ])
    assert controller.get({
        "date_value":
        (layabase.ComparisonSigns.Lower, datetime.date(2019, 1, 3))
    }) == [
        {
            "id": "1",
            "int_value": None,
            "float_value": None,
            "date_value": "2019-01-01",
            "datetime_value": None,
        },
        {
            "id": "2",
            "int_value": None,
            "float_value": None,
            "date_value": "2019-01-02",
            "datetime_value": None,
        },
    ]
def test_get_on_default_value_is_valid(controller: layabase.CRUDController):
    controller.post({"optional": "test"})
    controller.post({"key": "test2", "optional": "test2"})
    assert [{
        "key": "test",
        "optional": "test"
    }] == controller.get({"key": "test"})
Exemplo n.º 29
0
def test_update_with_dot_notation_is_valid(
        controller: layabase.CRUDController):
    assert {
        "dict_col": {
            "first_key": "Value1",
            "second_key": 3
        },
        "key": "my_key",
    } == controller.post({
        "key": "my_key",
        "dict_col": {
            "first_key": "Value1",
            "second_key": 3
        }
    })
    assert (
        {
            "dict_col": {
                "first_key": "Value1",
                "second_key": 3
            },
            "key": "my_key"
        },
        {
            "dict_col": {
                "first_key": "Value1",
                "second_key": 4
            },
            "key": "my_key"
        },
    ) == controller.put({
        "key": "my_key",
        "dict_col.second_key": 4
    })
Exemplo n.º 30
0
def test_remove_when_db_down(disconnected_database,
                             controller: layabase.CRUDController):
    with pytest.raises(Exception) as exception_info:
        controller.delete({})
    assert (
        str(exception_info.value) ==
        """A error occurred while querying database: (sqlite3.OperationalError) no such table: test\n[SQL: SELECT test."key" AS test_key \nFROM test]\n(Background on this error at: http://sqlalche.me/e/13/e3q8)"""
    )