Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
* for explanations see the comments in the code
  • Loading branch information
utnapischtim committed Oct 11, 2024
1 parent 25898fe commit 9d87e8b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions invenio_db/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion invenio_db/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions invenio_db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/mocks.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 4 additions & 2 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions tests/test_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"))
Expand All @@ -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()
)
Expand All @@ -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
Expand Down

0 comments on commit 9d87e8b

Please sign in to comment.