def test_post_invalid_date_is_invalid(controller: layabase.CRUDController): with pytest.raises(layabase.ValidationFailed) as exception_info: controller.post({ "key": "my_key1", "date_str": "this is not a date", "datetime_str": "2016-09-23T23:59:59", }) assert exception_info.value.errors == {"date_str": ["Not a valid date."]} assert exception_info.value.received_data == { "key": "my_key1", "date_str": "this is not a date", "datetime_str": "2016-09-23T23:59:59", }
def test_get_date_is_handled_for_valid_datetime( 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", }] == controller.get( {"datetime_str": datetime.datetime(2016, 9, 23, 23, 59, 59)})
def test_get_versioned_audit_after_post_put( controllers, controller_versioned: layabase.CRUDController, mock_mongo_audit_datetime, ): controller_versioned.post({"key": "my_key", "enum_fld": EnumTest.Value1}) controller_versioned.put({"key": "my_key", "enum_fld": EnumTest.Value2}) assert controller_versioned.get_one({"key": "my_key"}) == { "enum_fld": "Value2", "key": "my_key", "valid_since_revision": 2, "valid_until_revision": -1, }
def test_get_date_is_handled_for_valid_date( controller: layabase.CRUDController): controller.post({ "key": "my_key1", "date_str": "2017-05-15", "datetime_str": "2016-09-23T23:59:59", }) d = datetime.datetime.strptime("2017-05-15", "%Y-%m-%d").date() assert controller.get({"date_str": d}) == [{ "date_str": "2017-05-15", "datetime_str": "2016-09-23T23:59:59", "key": "my_key1", }]
def test_get_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.get({ "date_str": "2018-12-30", "unknown_field": "value" }) == [{ "key": "my_key1", "date_str": "2018-12-30", "datetime_str": "2016-09-23T23:59:59", }]
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_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, } ]
def test_get_revision_is_valid_when_2(controller: layabase.CRUDController): controller.post( { "key": "first", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) controller.post( { "key": "second", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) assert controller._model.current_revision() == 2
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 })
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_with_dot_notation_as_list_is_valid( controller: layabase.CRUDController): controller.post({ "key": "my_key", "dict_col": { "first_key": EnumTest.Value1, "second_key": 3 } }) assert [{ "dict_col": { "first_key": "Value1", "second_key": 3 }, "key": "my_key" }] == controller.get({"dict_col.first_key": [EnumTest.Value1]})
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_last(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_last({"key": "my_key1"}) == { "key": "my_key1", "mandatory": 1, "optional": "my_value1", }
def test_put_is_updating(controller: layabase.CRUDController, mock_mongo_audit_datetime): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) assert controller.put({ "key": "my_key1", "optional": "my_value" }) == ( { "key": "my_key1", "mandatory": 1, "optional": "my_value1" }, { "key": "my_key1", "mandatory": 1, "optional": "my_value" }, ) assert controller.get({"mandatory": 1}) == [{ "key": "my_key1", "mandatory": 1, "optional": "my_value" }] assert controller.get_audit({}) == [ { "audit_action": "Insert", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "my_key1", "mandatory": 1, "optional": "my_value1", "revision": 1, }, { "audit_action": "Update", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "my_key1", "mandatory": 1, "optional": "my_value", "revision": 2, }, ]
def test_delete_with_filter_is_removing_the_proper_row( controller: layabase.CRUDController, mock_mongo_audit_datetime): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.post({ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }) assert controller.delete({"key": "my_key1"}) == 1 assert controller.get({}) == [{ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }] assert controller.get_audit({}) == [ { "audit_action": "Insert", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "my_key1", "mandatory": 1, "optional": "my_value1", "revision": 1, }, { "audit_action": "Insert", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "my_key2", "mandatory": 2, "optional": "my_value2", "revision": 2, }, { "audit_action": "Delete", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "my_key1", "mandatory": 1, "optional": "my_value1", "revision": 3, }, ]
def test_rollback_to_0(controller: layabase.CRUDController): controller.post( { "key": "first", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) controller.post( { "key": "second", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) assert controller.rollback_to({"revision": 0}) == 2 assert controller.get({}) == []
def test_get_with_filter_is_retrieving_subset_with_multiple_posts( controller: layabase.CRUDController, ): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.post({ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }) assert [{ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }] == controller.get({"optional": "my_value1"})
def test_post_int_instead_of_str_is_valid(controller: layabase.CRUDController): assert controller.post({ "key": 3, "mandatory": 1 }) == { "key": "3", "mandatory": 1, "optional": None, }
def test_post_without_providing_required_nullable_dict_column_is_valid( controller: layabase.CRUDController, ): assert controller.post({"key": "first"}) == { "dict_field": {"first_key": None, "second_key": None}, "key": "first", "valid_since_revision": 1, "valid_until_revision": -1, }
def test_rollback_before_existing_is_valid(controller: layabase.CRUDController): controller.post( { "key": "first", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) before_insert = 1 controller.post( { "key": "second", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) assert controller.rollback_to({"revision": before_insert}) == 1 assert controller.get({"key": "second"}) == []
def test_post_without_optional_is_valid(controller: layabase.CRUDController): assert { "mandatory": 1, "key": "my_key", "optional": None } == controller.post({ "key": "my_key", "mandatory": 1 })
def test_post_with_enum_is_valid(controller: layabase.CRUDController): assert controller.post({ "key": 0, "enum_field": EnumTest.Value1 }) == { "optional_with_default": "Test value", "key": 1, "enum_field": "Value1", }
def test_delete_with_filter_is_removing_the_proper_row( controller: layabase.CRUDController, ): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.post({ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }) assert 1 == controller.delete({"key": "my_key1"}) assert [{ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }] == controller.get({})
def test_get_from_another_thread_than_post( controller: layabase.CRUDController): def save_get_result(): assert controller.get({}) == [{ "mandatory": 1, "optional": "my_value1", "key": "my_key1" }] controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) get_thread = Thread(name="GetInOtherThread", target=save_get_result) get_thread.start() get_thread.join()
def test_get_without_filter_is_retrieving_everything_with_multiple_posts( controller: layabase.CRUDController, mock_mongo_audit_datetime): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.post({ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }) assert controller.get({}) == [ { "key": "my_key1", "mandatory": 1, "optional": "my_value1" }, { "key": "my_key2", "mandatory": 2, "optional": "my_value2" }, ] assert controller.get_audit({}) == [ { "audit_action": "Insert", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "my_key1", "mandatory": 1, "optional": "my_value1", "revision": 1, }, { "audit_action": "Insert", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "my_key2", "mandatory": 2, "optional": "my_value2", "revision": 2, }, ]
def test_put_with_wrong_type_is_invalid(controller: layabase.CRUDController, mock_mongo_audit_datetime): controller.post({"key": "value1", "mandatory": 1}) with pytest.raises(layabase.ValidationFailed) as exception_info: controller.put({"key": "value1", "mandatory": "invalid_value"}) assert exception_info.value.errors == {"mandatory": ["Not a valid int."]} assert exception_info.value.received_data == { "key": "value1", "mandatory": "invalid_value", } assert controller.get_audit({}) == [{ "audit_action": "Insert", "audit_date_utc": "2018-10-11T15:05:05.663000", "audit_user": "", "key": "value1", "mandatory": 1, "optional": None, "revision": 1, }]
def test_post_with_unknown_field_is_valid(controller: layabase.CRUDController): assert {"mandatory": 1, "key": "my_key", "optional": "my_value"} == controller.post( { "key": "my_key", "mandatory": 1, "optional": "my_value", # This field do not exists in schema "unknown": "my_value", } )
def test_post_float_instead_of_str_is_valid( controller: layabase.CRUDController): assert { "key": "1.5", "mandatory": 1, "optional": None } == controller.post({ "key": 1.5, "mandatory": 1 })
def test_post_with_specified_incremented_field_is_ignored_and_valid( controller: layabase.CRUDController): assert controller.post({ "key": "my_key", "enum_field": "Value1" }) == { "optional_with_default": "Test value", "key": 1, "enum_field": "Value1", }
def test_auto_incremented_fields_are_not_incremented_on_post_failure( controller: layabase.CRUDController): assert controller.post({"other": 1}) == { "key": 1, "other": 1, "valid_since_revision": 1, "valid_until_revision": -1, } # Should not increment revision, nor the auto incremented key with pytest.raises(layabase.ValidationFailed): controller.post({"other": "FAILED"}) assert controller.post({"other": 2}) == { "key": 2, "other": 2, "valid_since_revision": 2, "valid_until_revision": -1, }