Skip to content

Commit

Permalink
Add test for setVersion(), fix deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
agateau committed Aug 16, 2024
1 parent 011f413 commit 8fbf062
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions yokadi/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ def createTables(self):
Base.metadata.create_all(self.engine)

def getVersion(self):
inspector = inspect(self.engine)
if not inspector.has_table("config"):
if not self._hasConfigTable():
# There was no Config table in v1
return 1

Expand All @@ -357,12 +356,16 @@ def getVersion(self):
raise YokadiException("Configuration key '%s' does not exist. This should not happen!" % DB_VERSION_KEY)

def setVersion(self, version):
assert self.engine.has_table("config")
assert self._hasConfigTable()
instance = self.session.query(Config).filter_by(name=DB_VERSION_KEY).one()
instance.value = str(version)
self.session.add(instance)
self.session.commit()

def _hasConfigTable(self):
inspector = inspect(self.engine)
return inspector.has_table("config")

def checkVersion(self):
"""Check version and exit if it is not suitable"""
version = self.getVersion()
Expand Down
20 changes: 20 additions & 0 deletions yokadi/tests/dbtestcase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
@author: Aurélien Gâteau <mail@agateau.com>
@license: GPL v3 or later
"""

import unittest

from yokadi.core import db


class DbTestCase(unittest.TestCase):
def setUp(self):
db.connectDatabase("", memoryDatabase=True)
self.session = db.getSession()

def testSetVersion(self):
newVersion = db.DB_VERSION + 1
db._database.setVersion(newVersion)
version = db._database.getVersion()
self.assertEqual(version, newVersion)
1 change: 1 addition & 0 deletions yokadi/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from keywordfiltertestcase import KeywordFilterTestCase # noqa: F401, E402
from recurrenceruletestcase import RecurrenceRuleTestCase # noqa: F401, E402
from argstestcase import ArgsTestCase # noqa: F401, E402
from dbtestcase import DbTestCase # noqa: F401, E402


def main():
Expand Down

0 comments on commit 8fbf062

Please sign in to comment.