def test_index_creation_failure_with_new_index_duplicate(monkeypatch): class TestCollection: __collection_name__ = "test" id = layabase.mongo.Column() controller = layabase.CRUDController(TestCollection) monkeypatch.setattr( mongomock.MongoClient, "server_info", lambda *args: {"version": "3.2.0"} ) base = layabase.load("mongomock", [controller]) controller.post_many([{"id": "1"}, {"id": "1"}]) class TestCollection2: __collection_name__ = "test" id = layabase.mongo.Column(index_type=layabase.mongo.IndexType.Unique) controller2 = layabase.CRUDController(TestCollection2, history=True) monkeypatch.setattr( mongomock.MongoClient, "server_info", lambda *args: {"version": "3.2.0"} ) with pytest.raises(pymongo.errors.DuplicateKeyError): layabase.mongo.link(controller2, base) # Reset this fake value layabase._database_mongo._server_versions = {}
def test_2entities_on_same_collection_with_pk(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) mandatory = layabase.mongo.Column(int, is_nullable=False) optional = layabase.mongo.Column(str) controller = layabase.CRUDController(TestCollection, history=True) mongo_base = layabase.load("mongomock", [controller]) controller.post({"key": "1", "mandatory": 2}) controller.post({"key": "2", "mandatory": 2}) class TestCollection2: __collection_name__ = "test" controller = layabase.CRUDController(TestCollection2, history=True, skip_update_indexes=True) layabase.mongo.link(controller, mongo_base) controller.post({"key": "3", "mandatory": 2}) assert len(controller.get({})) == 3
def controller(): class TestTable: __tablename__ = "test" key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) return layabase.CRUDController(TestTable)
def controller() -> layabase.CRUDController: class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(is_primary_key=True, min_length=3, max_length=4) list_field = layabase.mongo.Column(list, min_length=2, max_length=3, example=["my", "test"]) dict_field = layabase.mongo.Column(dict, min_length=2, max_length=3, example={ "my": 1, "test": 2 }) int_field = layabase.mongo.Column(int, min_value=100, max_value=999) float_field = layabase.mongo.Column(float, min_value=1.25, max_value=1.75) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def test_with_index_fields(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str) dict_col = layabase.mongo.DictColumn( fields={ "field1": layabase.mongo.Column(), "field2": layabase.mongo.Column(), }, index_fields={ "field2": layabase.mongo.Column( index_type=layabase.mongo.IndexType.Unique) }, ) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) controller.post({"key": "my_key1", "dict_col": {"field2": "test"}}) with pytest.raises(layabase.ValidationFailed) as exception_info: controller.post({"key": "my_key2", "dict_col": {"field2": "test"}}) assert exception_info.value.errors == { "": ["This document already exists."] } assert exception_info.value.received_data == { "key": "my_key2", "dict_col": { "field2": "test", "field1": None }, }
def test_get_failure_in_get_fields(): should_fail = False def get_fields_failure(document): if should_fail: raise Exception("Original failure reason") return {} class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) dict_col = layabase.mongo.DictColumn(get_fields=get_fields_failure) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) controller.post({"key": "my_key", "dict_col": {}}) should_fail = True with pytest.raises(layabase.ValidationFailed) as exception_info: controller.get({"key": "my_key", "dict_col": {}}) assert exception_info.value.errors == { "dict_col": ["Original failure reason"] } assert exception_info.value.received_data == { "dict_col": {}, "key": "my_key" }
def test_without_index_fields(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str) dict_col = layabase.mongo.DictColumn(fields={ "field1": layabase.mongo.Column(), "field2": layabase.mongo.Column(), }) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) controller.post({"key": "my_key1", "dict_col": {"field2": "test"}}) assert controller.post({ "key": "my_key2", "dict_col": { "field2": "test" } }) == { "key": "my_key2", "dict_col": { "field2": "test", "field1": None }, }
def controller() -> layabase.CRUDController: class TestTable: __tablename__ = "test" id = sqlalchemy.Column(sqlalchemy.String, primary_key=True) int_value = sqlalchemy.Column( sqlalchemy.Integer, info={"layabase": { "allow_comparison_signs": True }}) float_value = sqlalchemy.Column( sqlalchemy.Float, info={"layabase": { "allow_comparison_signs": True }}) date_value = sqlalchemy.Column( sqlalchemy.Date, info={"layabase": { "allow_comparison_signs": True }}) datetime_value = sqlalchemy.Column( sqlalchemy.DateTime, info={"layabase": { "allow_comparison_signs": True }}) controller = layabase.CRUDController(TestTable) layabase.load("sqlite:///:memory:", [controller]) return controller
def controller() -> layabase.CRUDController: class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(is_primary_key=True) dict_field = layabase.mongo.DictColumn( fields={ "first_key": layabase.mongo.DictColumn( fields={ "inner_key1": layabase.mongo.Column(EnumTest, is_nullable=False), "inner_key2": layabase.mongo.Column(int, is_nullable=False), }, is_required=True, ), "second_key": layabase.mongo.Column(int, is_nullable=False), }, is_required=True, ) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) return layabase.CRUDController(TestCollection)
def database(): class TestCollection: __collection_name__ = "test" id = layabase.mongo.Column() return layabase.load("mongomock", [layabase.CRUDController(TestCollection)])
def controller_retrieve(): class TestCollectionRetrieve: __collection_name__ = "test" key = layabase.mongo.Column(int, is_primary_key=True) my_dict = layabase.mongo.Column(dict, is_required=True) return layabase.CRUDController(TestCollectionRetrieve, skip_name_check=True)
def controller() -> layabase.CRUDController: class TestTable: __tablename__ = "test" key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) mandatory = sqlalchemy.Column(sqlalchemy.Integer, nullable=False) optional = sqlalchemy.Column(sqlalchemy.String) return layabase.CRUDController(TestTable)
def controller(): class TestCollection: __collection_name__ = "test" _id = layabase.mongo.Column(is_primary_key=True) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(int) controller = layabase.CRUDController(TestCollection, audit=True) layabase.load("mongomock", [controller]) return controller
def db(): class TestTable: __tablename__ = "test" key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) mandatory = sqlalchemy.Column(sqlalchemy.Integer, nullable=False) optional = sqlalchemy.Column(sqlalchemy.String) return layabase.load("sqlite:///:memory:", [layabase.CRUDController(TestTable)])
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) mandatory = layabase.mongo.Column(int, is_nullable=False) optional = layabase.mongo.Column(str) return layabase.CRUDController(TestCollection, audit=True)
def controller_unique(): class TestCollectionUnique: __collection_name__ = "test_unique" key = layabase.mongo.Column(int, should_auto_increment=True) unique = layabase.mongo.Column( int, index_type=layabase.mongo.IndexType.Unique) return layabase.CRUDController(TestCollectionUnique)
def test_audit_table_name_is_forbidden(): class TestCollection: __collection_name__ = "audit" key = layabase.mongo.Column(str) with pytest.raises(Exception) as exception_info: layabase.load("mongomock", [layabase.CRUDController(TestCollection)]) assert "audit is a reserved collection name." == str(exception_info.value)
def controller_versioned(): class TestCollectionVersioned: __collection_name__ = "test_versioned" key = layabase.mongo.Column(str, is_primary_key=True) enum_fld = layabase.mongo.Column(EnumTest) return layabase.CRUDController(TestCollectionVersioned, audit=True, history=True)
def controller(): class TestTable: __tablename__ = "test" key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) time_field = sqlalchemy.Column(sqlalchemy.Time) controller = layabase.CRUDController(TestTable) layabase.load("sqlite:///:memory:", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) enum_fld = layabase.mongo.Column(EnumTest) controller = layabase.CRUDController(TestCollection, audit=True) layabase.load("mongomock", [controller]) return controller
def controller() -> layabase.CRUDController: class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(is_primary_key=True, default_value="test") optional = layabase.mongo.Column() controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) controller = layabase.CRUDController(TestCollection, skip_unknown_fields=False) layabase.load("mongomock", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" int_value = layabase.mongo.Column(int) float_value = layabase.mongo.Column(float) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller() -> layabase.CRUDController: class TestTable: __tablename__ = "test" key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) test_field = sqlalchemy.Column(sqlalchemy.Enum("chose1", "chose2")) controller = layabase.CRUDController(TestTable) layabase.load("sqlite:///:memory:", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" id = layabase.mongo.Column(index_type=layabase.mongo.IndexType.Unique) id2 = layabase.mongo.Column(index_type=layabase.mongo.IndexType.Unique) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" int_value = layabase.mongo.Column(int, allow_comparison_signs=True, default_value=3) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller() -> layabase.CRUDController: class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) mandatory = layabase.mongo.Column(int, is_nullable=False) optional = layabase.mongo.Column(str) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) date_str = layabase.mongo.Column(datetime.date) datetime_str = layabase.mongo.Column(datetime.datetime) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller