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_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_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_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_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_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_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
def test_post_many_without_key_is_invalid(controller: layabase.CRUDController): with pytest.raises(ValidationFailed) as exception_info: controller.post_many([{"mandatory": 1}]) assert exception_info.value.errors == { 0: {"key": ["Missing data for required field."]} } assert exception_info.value.received_data == [{"mandatory": 1}]
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_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_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 test_get_with_filter_is_retrieving_subset(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"} ] == controller.get({"optional": "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_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_get_with_default_and_equality(controller: layabase.CRUDController): controller.post_many( [ {"int_value": -10}, {"int_value": 0}, {"int_value": None}, # Consider as default: 3 {"int_value": 4}, {"int_value": 5}, {"int_value": 6}, ] ) assert controller.get( {"int_value": [3, 5]} # Default value # Non default value (equality) ) == [{"int_value": 3}, {"int_value": 5}]
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_many_with_unknown_field_is_valid( controller: layabase.CRUDController): assert controller.post_many([ { "key": "my_key", "mandatory": 1, "optional": "my_value", # This field do not exists in schema "unknown": "my_value", }, { "key": "my_key2", "mandatory": 2, "optional": "my_value2", # This field do not exists in schema "unknown": "my_value2", }, ]) == [ { "mandatory": 1, "key": "my_key", "optional": "my_value" }, { "mandatory": 2, "key": "my_key2", "optional": "my_value2" }, ]
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}] )
def test_post_many_without_optional_is_valid(controller: layabase.CRUDController): assert [ {"mandatory": 1, "key": "my_key", "optional": None}, {"mandatory": 2, "key": "my_key2", "optional": None}, ] == controller.post_many( [{"key": "my_key", "mandatory": 1}, {"key": "my_key2", "mandatory": 2}] )
def test_post_many_when_db_down(disconnected_database, controller: layabase.CRUDController): with pytest.raises(Exception) as exception_info: controller.post_many([ { "key": "my_key1", "mandatory": 1, "optional": "my_value1" }, { "key": "my_key2", "mandatory": 1, "optional": "my_value1" }, ]) assert str(exception_info.value) == "Database could not be reached."
def test_get_is_valid_with_int_and_less_than_sign_as_tuple_in_int_column(controller: layabase.CRUDController): controller.post_many([{"int_value": 122}, {"int_value": 123}, {"int_value": 124}]) 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, }, ] == controller.get({"int_value": (layabase.ComparisonSigns.Lower, 124)})
def test_get_without_filter_is_retrieving_everything( controller: layabase.CRUDController, mock_mongo_audit_datetime): controller.post_many([ { "key": "my_key1", "mandatory": 1, "optional": "my_value1" }, { "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_post_many_when_db_down(disconnected_database, controller: layabase.CRUDController): with pytest.raises(Exception) as exception_info: controller.post_many([ { "key": "my_key1", "mandatory": 1, "optional": "my_value1" }, { "key": "my_key2", "mandatory": 1, "optional": "my_value1" }, ]) 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 \nWHERE test."key" = ?\n LIMIT ? OFFSET ?]\n[parameters: ('my_key1', 1, 0)]\n(Background on this error at: http://sqlalche.me/e/13/e3q8)""" )
def test_get_with_interval_and_default_and_equality(controller: layabase.CRUDController): controller.post_many( [ {"int_value": -10}, {"int_value": 0}, {"int_value": None}, # Consider as default: 3 {"int_value": 4}, {"int_value": 5}, {"int_value": 6}, ] ) assert controller.get( { "int_value": [ (layabase.ComparisonSigns.Lower, 2), (layabase.ComparisonSigns.GreaterOrEqual, -5), 3, # Default value 5, # Non default value (equality) ] } ) == [{"int_value": 0}, {"int_value": 3}, {"int_value": 5}]
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 }]
def test_auto_incremented_fields_are_not_incremented_on_multi_post_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, nor the auto incremented key with pytest.raises(layabase.ValidationFailed): controller.post_many([{"other": 2}, {"other": "FAILED"}, {"other": 4}]) assert controller.post_many([{ "other": 5 }]) == [{ "key": 3, # For performance reasons, deserialization is performed before checks on other doc (so first valid document incremented the counter) "other": 5, "valid_since_revision": 2, "valid_until_revision": -1, }]
def test_post_many_without_optional_is_valid( controller: layabase.CRUDController, mock_mongo_audit_datetime): assert controller.post_many([{ "key": "my_key", "mandatory": 1 }]) == [{ "optional": None, "mandatory": 1, "key": "my_key" }] 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, }]