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"}) == []
def test_get_invalid_date_is_invalid(controller: layabase.CRUDController): with pytest.raises(layabase.ValidationFailed) as exception_info: controller.get({"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" }
def test_get_is_invalid_with_datetime_and_unknown_as_tuple_in_datetime_column( controller: layabase.CRUDController, ): with pytest.raises(KeyError) as exception_info: controller.get( {"datetime_value": ("test", datetime.datetime(2019, 1, 3, 23, 59, 59))} ) assert str(exception_info.value) == "'test'"
def test_get_all_when_db_down(disconnected_database, controller: layabase.CRUDController): with pytest.raises(Exception) as exception_info: controller.get({}) 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)""" )
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_delete_versioning_is_valid(controller: layabase.CRUDController): controller.post( { "key": "first", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) controller.put({"key": "first", "dict_field.first_key": EnumTest.Value2}) assert controller.delete({"key": "first"}) == 1 assert controller.get_history({}) == [ { "key": "first", "dict_field": {"first_key": "Value2", "second_key": 1}, "valid_since_revision": 2, "valid_until_revision": 3, }, { "key": "first", "dict_field": {"first_key": "Value1", "second_key": 1}, "valid_since_revision": 1, "valid_until_revision": 2, }, ] assert 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_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), ) } )
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"})
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_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_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_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_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_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_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_rollback_unknown_criteria_is_valid(controller: layabase.CRUDController): controller.post( { "key": "first", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) before_update = 1 controller.put({"key": "first", "dict_field.first_key": EnumTest.Value2}) assert controller.rollback_to({"revision": before_update, "key": "unknown"}) == 0 assert controller.get_history({}) == [ { "key": "first", "dict_field": {"first_key": "Value2", "second_key": 1}, "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, }, ] assert controller.get({}) == [ { "key": "first", "dict_field": {"first_key": "Value2", "second_key": 1}, "valid_since_revision": 2, "valid_until_revision": -1, } ]
def test_rollback_already_valid_versioning_is_valid( controller: layabase.CRUDController, ): controller.post( { "key": "first", "dict_field.first_key": EnumTest.Value1, "dict_field.second_key": 1, } ) controller.put({"key": "first", "dict_field.first_key": EnumTest.Value2}) assert controller.rollback_to({"revision": 2}) == 0 assert controller.get_history({}) == [ { "key": "first", "dict_field": {"first_key": "Value2", "second_key": 1}, "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, }, ] assert controller.get({}) == [ { "key": "first", "dict_field": {"first_key": "Value2", "second_key": 1}, "valid_since_revision": 2, "valid_until_revision": -1, } ]
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_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, }, ]
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, } ]
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, ] } )
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_put_is_updating_and_previous_value_cannot_be_used_to_filter( controller: layabase.CRUDController, mock_mongo_audit_datetime): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.put({"key": "my_key1", "optional": "my_value"}) assert controller.get({"optional": "my_value1"}) == [] 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_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"})
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]})
def test_get_with_offset_1_is_retrieving_subset_of_n_minus_1_first_elements( controller: layabase.CRUDController, ): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.post({ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }) controller.post({ "key": "my_key3", "mandatory": 3, "optional": "my_value3" }) assert [ { "key": "my_key2", "mandatory": 2, "optional": "my_value2" }, { "key": "my_key3", "mandatory": 3, "optional": "my_value3" }, ] == controller.get({"offset": 1})
def test_get_with_limit_1_and_offset_1_is_retrieving_middle_element( controller: layabase.CRUDController, ): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.post({ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }) controller.post({ "key": "my_key3", "mandatory": 3, "optional": "my_value3" }) assert [{ "key": "my_key2", "mandatory": 2, "optional": "my_value2" }] == controller.get({ "offset": 1, "limit": 1 })
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_put_is_updating_and_previous_value_cannot_be_used_to_filter( controller: layabase.CRUDController, ): controller.post({ "key": "my_key1", "mandatory": 1, "optional": "my_value1" }) controller.put({"key": "my_key1", "optional": "my_value"}) assert [] == controller.get({"optional": "my_value1"})