From 9d87e8b0fb0e8753e6e81ff5d96d54f7d4c21b66 Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Tue, 1 Oct 2024 22:29:57 +0200 Subject: [PATCH] fix: tests * for explanations see the comments in the code --- invenio_db/cli.py | 1 + invenio_db/ext.py | 3 ++- invenio_db/utils.py | 1 + setup.cfg | 4 ++-- tests/mocks.py | 2 +- tests/test_db.py | 6 ++++-- tests/test_versioning.py | 19 +++++++++---------- 7 files changed, 20 insertions(+), 16 deletions(-) diff --git a/invenio_db/cli.py b/invenio_db/cli.py index aab0229..a94977a 100644 --- a/invenio_db/cli.py +++ b/invenio_db/cli.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2018 CERN. +# Copyright (C) 2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. diff --git a/invenio_db/ext.py b/invenio_db/ext.py index fda1a0f..fa3eb35 100644 --- a/invenio_db/ext.py +++ b/invenio_db/ext.py @@ -3,7 +3,7 @@ # This file is part of Invenio. # Copyright (C) 2015-2018 CERN. # Copyright (C) 2022 RERO. -# Copyright (C) 2022 Graz University of Technology. +# Copyright (C) 2022-2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -91,6 +91,7 @@ def init_db(self, app, entry_point_group="invenio_db.models", **kwargs): # All models should be loaded by now. sa.orm.configure_mappers() + # Ensure that versioning classes have been built. if app.config["DB_VERSIONING"]: manager = self.versioning_manager diff --git a/invenio_db/utils.py b/invenio_db/utils.py index c2383f6..3e6b3d4 100644 --- a/invenio_db/utils.py +++ b/invenio_db/utils.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2017-2018 CERN. +# Copyright (C) 2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. diff --git a/setup.cfg b/setup.cfg index d09beef..2bd5a68 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,7 +4,7 @@ # Copyright (C) 2015-2022 CERN. # Copyright (C) 2021 Northwestern University. # Copyright (C) 2022 RERO. -# Copyright (C) 2022-2023 Graz University of Technology. +# Copyright (C) 2022-2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -30,7 +30,7 @@ python_requires = >=3.7 zip_safe = False install_requires = alembic>=1.10.0 - Flask-Alembic>=2.0.1 + Flask-Alembic>=3.0.0 Flask-SQLAlchemy>=3.0 invenio-base>=1.2.10 SQLAlchemy-Continuum>=1.3.12 diff --git a/tests/mocks.py b/tests/mocks.py index fb83176..8a12c78 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2023 Graz University of Technology. +# Copyright (C) 2023-2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. diff --git a/tests/test_db.py b/tests/test_db.py index 8fb385e..259e33b 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -56,14 +56,15 @@ class Demo2(db.Model): with app.app_context(): # Fails fk check + d3 = Demo2(fk=10) db.session.add(d3) pytest.raises(IntegrityError, db.session.commit) db.session.rollback() with app.app_context(): - Demo2.query.delete() - Demo.query.delete() + db.session.query(Demo2).delete() + db.session.query(Demo).delete() db.session.commit() db.drop_all() @@ -367,6 +368,7 @@ def test_db_create_alembic_upgrade(app, db): try: if db.engine.name == "sqlite": raise pytest.skip("Upgrades are not supported on SQLite.") + db.drop_all() runner = app.test_cli_runner() # Check that 'db create' creates the same schema as diff --git a/tests/test_versioning.py b/tests/test_versioning.py index 67a59c1..4727d79 100644 --- a/tests/test_versioning.py +++ b/tests/test_versioning.py @@ -3,7 +3,7 @@ # This file is part of Invenio. # Copyright (C) 2015-2018 CERN. # Copyright (C) 2022 RERO. -# Copyright (C) 2023 Graz University of Technology. +# Copyright (C) 2023-2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -17,13 +17,6 @@ from sqlalchemy_continuum import VersioningManager, remove_versioning from invenio_db import InvenioDB -from invenio_db.shared import db as _db - - -class EarlyClass(_db.Model): - __versioned__ = {} - - pk = _db.Column(_db.Integer, primary_key=True) @patch("importlib_metadata.entry_points", _mock_entry_points("invenio_db.models_a")) @@ -35,12 +28,18 @@ def test_disabled_versioning(db, app): assert 2 == len(db.metadata.tables) -@pytest.mark.skip(reason="it seems that is not more possible") @pytest.mark.parametrize("versioning,tables", [(False, 1), (True, 3)]) def test_disabled_versioning_with_custom_table(db, app, versioning, tables): """Test SQLAlchemy-Continuum table loading.""" app.config["DB_VERSIONING"] = versioning + # this class has to be defined here, because the the db has to be the db + # from the fixture. using it "from invenio_db import db" is not working + class EarlyClass(db.Model): + __versioned__ = {} + + pk = db.Column(db.Integer, primary_key=True) + idb = InvenioDB( app, entry_point_group=None, db=db, versioning_manager=VersioningManager() ) @@ -65,7 +64,7 @@ def test_disabled_versioning_with_custom_table(db, app, versioning, tables): @patch("importlib_metadata.entry_points", _mock_entry_points("invenio_db.models_b")) def test_versioning(db, app): """Test SQLAlchemy-Continuum enabled versioning.""" - # it is essential that it is imported HERE, otherwise it fails + # they have to imported inside of the tests, otherwise it doesn't work from demo.versioned_b import UnversionedArticle, VersionedArticle app.config["DB_VERSIONING"] = True