def test_put_many_without_previous(controller: layabase.CRUDController):
    controller.post({"key": "first"})
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put_many([{"key": "unknown"}])
    assert exception_info.value.received_data == {
        "key": "unknown",
        "valid_until_revision": -1,
    }
def test_put_many_without_previous(controller: layabase.CRUDController):
    controller.post({"key": "first"})
    with pytest.raises(ModelCouldNotBeFound) as exception_info:
        controller.put_many([{"key": "unknown"}])
    assert exception_info.value.requested_data == {
        "key": "unknown",
        "valid_until_revision": -1,
    }
Beispiel #3
0
def test_unknown_field_failure_put_many(controller: layabase.CRUDController):
    controller.post({"key": "my_key1"})
    controller.post({"key": "my_key2"})
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put_many([{"key": "my_key1", "unknown": "my_value1"}])
    assert exception_info.value.errors == {0: {"unknown": ["Unknown field"]}}
    assert exception_info.value.received_data == [{
        "key": "my_key1",
        "unknown": "my_value1"
    }]
def test_put_many_is_valid(controller: layabase.CRUDController,
                           mock_mongo_audit_datetime):
    controller.post_many([{
        "key": "my_key",
        "mandatory": 1
    }, {
        "key": "my_key2",
        "mandatory": 2
    }])
    controller.put_many([{
        "key": "my_key",
        "optional": "test"
    }, {
        "key": "my_key2",
        "mandatory": 3
    }])
    assert controller.get_audit({}) == [
        {
            "audit_action": "Insert",
            "audit_date_utc": "2018-10-11T15:05:05.663000",
            "audit_user": "",
            "key": "my_key",
            "mandatory": 1,
            "optional": None,
            "revision": 1,
        },
        {
            "audit_action": "Insert",
            "audit_date_utc": "2018-10-11T15:05:05.663000",
            "audit_user": "",
            "key": "my_key2",
            "mandatory": 2,
            "optional": None,
            "revision": 2,
        },
        {
            "audit_action": "Update",
            "audit_date_utc": "2018-10-11T15:05:05.663000",
            "audit_user": "",
            "key": "my_key",
            "mandatory": 1,
            "optional": "test",
            "revision": 3,
        },
        {
            "audit_action": "Update",
            "audit_date_utc": "2018-10-11T15:05:05.663000",
            "audit_user": "",
            "key": "my_key2",
            "mandatory": 3,
            "optional": None,
            "revision": 4,
        },
    ]
def test_versioned_many(controller: layabase.CRUDController):
    controller.post_many(
        [
            {
                "key": "first",
                "dict_field.first_key": EnumTest.Value1,
                "dict_field.second_key": 1,
            },
            {
                "key": "second",
                "dict_field.first_key": EnumTest.Value2,
                "dict_field.second_key": 2,
            },
        ]
    )
    controller.put_many(
        [
            {"key": "first", "dict_field.first_key": EnumTest.Value2},
            {"key": "second", "dict_field.second_key": 3},
        ]
    )

    assert controller.get_history({}) == [
        {
            "key": "first",
            "dict_field": {"first_key": "Value2", "second_key": 1},
            "valid_since_revision": 2,
            "valid_until_revision": -1,
        },
        {
            "key": "second",
            "dict_field": {"first_key": "Value2", "second_key": 3},
            "valid_since_revision": 2,
            "valid_until_revision": -1,
        },
        {
            "key": "first",
            "dict_field": {"first_key": "Value1", "second_key": 1},
            "valid_since_revision": 1,
            "valid_until_revision": 2,
        },
        {
            "key": "second",
            "dict_field": {"first_key": "Value2", "second_key": 2},
            "valid_since_revision": 1,
            "valid_until_revision": 2,
        },
    ]
def test_post_and_put_many(
    controllers,
    controller_versioned: layabase.CRUDController,
    mock_mongo_audit_datetime,
):
    controller_versioned.post_many([
        {
            "key": "my_key1",
            "enum_fld": EnumTest.Value1
        },
        {
            "key": "my_key2",
            "enum_fld": EnumTest.Value1
        },
    ])
    assert controller_versioned.put_many([
        {
            "key": "my_key1",
            "enum_fld": EnumTest.Value2
        },
        {
            "key": "my_key2",
            "enum_fld": EnumTest.Value2
        },
    ]) == (
        [
            {
                "enum_fld": "Value1",
                "key": "my_key1",
                "valid_since_revision": 1,
                "valid_until_revision": -1,
            },
            {
                "enum_fld": "Value1",
                "key": "my_key2",
                "valid_since_revision": 1,
                "valid_until_revision": -1,
            },
        ],
        [
            {
                "enum_fld": "Value2",
                "key": "my_key1",
                "valid_since_revision": 2,
                "valid_until_revision": -1,
            },
            {
                "enum_fld": "Value2",
                "key": "my_key2",
                "valid_since_revision": 2,
                "valid_until_revision": -1,
            },
        ],
    )
def test_put_many_without_primary_key_and_unique_index_update(
    controller: layabase.CRUDController, ):
    assert controller.post_many([{
        "id": "test1",
        "id2": "test1"
    }, {
        "id": "test1",
        "id2": "test2"
    }]) == [{
        "id": "test1",
        "id2": "test1"
    }, {
        "id": "test1",
        "id2": "test2"
    }]
    # It should never be declared without a PK in this case but as there is no PK, the first document is updated.
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put_many([{"id2": "test2"}])
    assert exception_info.value.errors == {
        "": ["One document already exists."]
    }
    assert exception_info.value.received_data == [{"id": None, "id2": "test2"}]
def test_auto_incremented_fields_are_not_incremented_on_multi_put_failure(
        controller: layabase.CRUDController):
    assert controller.post_many([{
        "other": 1
    }]) == [{
        "key": 1,
        "other": 1,
        "valid_since_revision": 1,
        "valid_until_revision": -1
    }]

    # Should not increment revision
    with pytest.raises(layabase.ValidationFailed):
        controller.put_many([{"other": 1}, {"other": "FAILED"}, {"other": 1}])

    assert controller.post_many([{
        "other": 5
    }]) == [{
        "key": 2,
        "other": 5,
        "valid_since_revision": 2,
        "valid_until_revision": -1
    }]
Beispiel #9
0
def test_put_many_is_valid(controller: layabase.CRUDController):
    controller.post_many(
        [{"key": "my_key", "mandatory": 1}, {"key": "my_key2", "mandatory": 2}]
    )
    assert (
        [
            {"mandatory": 1, "key": "my_key", "optional": None},
            {"mandatory": 2, "key": "my_key2", "optional": None},
        ],
        [
            {"mandatory": 1, "key": "my_key", "optional": "test"},
            {"mandatory": 3, "key": "my_key2", "optional": None},
        ],
    ) == controller.put_many(
        [{"key": "my_key", "optional": "test"}, {"key": "my_key2", "mandatory": 3}]
    )
Beispiel #10
0
def test_put_many_unexisting(controller: layabase.CRUDController):
    controller.post({"key": "my_key1"})
    controller.post({"key": "my_key2"})
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put_many([{"key": "my_key3"}])
    assert exception_info.value.received_data == {"key": "my_key3"}
Beispiel #11
0
def test_put_many_with_empty_list_is_invalid(
        controller: layabase.CRUDController):
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put_many([])
    assert exception_info.value.errors == {"": ["No data provided."]}
    assert exception_info.value.received_data == []
Beispiel #12
0
def test_put_many_with_dict_is_invalid(controller: layabase.CRUDController):
    with pytest.raises(layabase.ValidationFailed) as exception_info:
        controller.put_many({""})
    assert exception_info.value.errors == {"": ["Must be a list."]}
    assert exception_info.value.received_data == {""}
Beispiel #13
0
def test_put_many_unexisting(controller: layabase.CRUDController):
    controller.post({"key": "my_key1"})
    controller.post({"key": "my_key2"})
    with pytest.raises(ModelCouldNotBeFound) as exception_info:
        controller.put_many([{"key": "my_key3"}])
    assert exception_info.value.requested_data == {"key": "my_key3"}