2
0
Fork 0

test: add 'nodb' test runner

ensure things work as expected if sqlalchemy is not installed
This commit is contained in:
Lance Edgar 2024-07-04 08:00:42 -05:00
parent 132073177c
commit f5825e964c
5 changed files with 193 additions and 148 deletions

View file

@ -5,16 +5,19 @@ import shutil
import tempfile
from unittest import TestCase
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.engine import Engine
from sqlalchemy.pool import NullPool
from wuttjamaican.db import conf
from wuttjamaican.conf import WuttaConfig
try:
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.engine import Engine
from sqlalchemy.pool import NullPool
from wuttjamaican.db import conf
except ImportError:
pass
else:
class TestGetEngines(TestCase):
class TestGetEngines(TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
@ -35,9 +38,9 @@ class TestGetEngines(TestCase):
def test_default(self):
myfile = self.write_file('my.conf', """\
[wuttadb]
default.url = sqlite://
""")
[wuttadb]
default.url = sqlite://
""")
config = WuttaConfig([myfile])
result = conf.get_engines(config, 'wuttadb')
self.assertEqual(len(result), 1)
@ -47,9 +50,9 @@ default.url = sqlite://
def test_default_fallback(self):
myfile = self.write_file('my.conf', """\
[wuttadb]
sqlalchemy.url = sqlite://
""")
[wuttadb]
sqlalchemy.url = sqlite://
""")
config = WuttaConfig([myfile])
result = conf.get_engines(config, 'wuttadb')
self.assertEqual(len(result), 1)
@ -59,11 +62,11 @@ sqlalchemy.url = sqlite://
def test_other(self):
myfile = self.write_file('my.conf', """\
[otherdb]
keys = first, second
first.url = sqlite://
second.url = sqlite://
""")
[otherdb]
keys = first, second
first.url = sqlite://
second.url = sqlite://
""")
config = WuttaConfig([myfile])
result = conf.get_engines(config, 'otherdb')
self.assertEqual(len(result), 2)
@ -71,7 +74,7 @@ second.url = sqlite://
self.assertIn('second', result)
class TestGetSetting(TestCase):
class TestGetSetting(TestCase):
def setUp(self):
Session = orm.sessionmaker()
@ -97,7 +100,7 @@ class TestGetSetting(TestCase):
self.assertIsNone(value)
class TestMakeEngineFromConfig(TestCase):
class TestMakeEngineFromConfig(TestCase):
def test_basic(self):
engine = conf.make_engine_from_config({

View file

@ -3,14 +3,17 @@
from unittest import TestCase
from unittest.mock import MagicMock
import sqlalchemy as sa
from sqlalchemy import orm
from wuttjamaican.db import sess
from wuttjamaican.conf import WuttaConfig
try:
import sqlalchemy as sa
from sqlalchemy import orm
from wuttjamaican.db import sess
except ImportError:
pass
else:
class TestShortSession(TestCase):
class TestShortSession(TestCase):
def test_none(self):
with sess.short_session() as s:

View file

@ -7,10 +7,9 @@ import warnings
from unittest import TestCase
from unittest.mock import patch, MagicMock
import sqlalchemy as sa
from sqlalchemy import orm
import pytest
from wuttjamaican import app, db
from wuttjamaican import app
from wuttjamaican.conf import WuttaConfig
@ -46,6 +45,11 @@ class TestAppHandler(TestCase):
shutil.rmtree(tempdir)
def test_make_session(self):
try:
from wuttjamaican import db
except ImportError:
pytest.skip("test is not relevant without sqlalchemy")
session = self.app.make_session()
self.assertIsInstance(session, db.Session.class_)
@ -60,6 +64,12 @@ class TestAppHandler(TestCase):
foo='bar', factory=self.app.make_session)
def test_get_setting(self):
try:
import sqlalchemy as sa
from sqlalchemy import orm
except ImportError:
pytest.skip("test is not relevant without sqlalchemy")
Session = orm.sessionmaker()
engine = sa.create_engine('sqlite://')
session = Session(bind=engine)

View file

@ -5,12 +5,10 @@ import os
from unittest import TestCase
from unittest.mock import patch, MagicMock
import sqlalchemy as sa
import pytest
from wuttjamaican import conf
from wuttjamaican.exc import ConfigurationError
from wuttjamaican.db import Session
from wuttjamaican.db.conf import make_engine_from_config
from wuttjamaican.app import AppHandler
from wuttjamaican.testing import FileConfigTestCase
@ -133,6 +131,13 @@ require = %(here)s/first.conf
self.assertEqual(config.get('foo'), 'bar')
def test_constructor_db_flags(self):
try:
# nb. we don't need this import but the test will not
# behave correctly unless the lib is installed
import sqlalchemy
except ImportError:
pytest.skip("test is not relevant without sqlalchemy")
myfile = self.write_file('my.conf', """\
[wutta.config]
usedb = true
@ -155,6 +160,12 @@ preferdb = true
self.assertTrue(config.preferdb)
def test_constructor_db_not_supported(self):
try:
# nb. we don't need this import but the test will not
# behave correctly unless the lib is installed
import sqlalchemy
except ImportError:
pytest.skip("test is not relevant without sqlalchemy")
# flags are off by default
config = conf.WuttaConfig()
@ -269,6 +280,12 @@ configure_logging = true
self.assertEqual(config.get('foo', default='bar'), 'bar')
def test_get_from_db(self):
try:
import sqlalchemy as sa
from wuttjamaican.db import Session
except ImportError:
pytest.skip("test is not relevant without sqlalchemy")
# minimal config, but at least it needs db cxn info
config = conf.WuttaConfig(defaults={'wutta.db.default.url': 'sqlite://'})
@ -317,6 +334,11 @@ configure_logging = true
self.assertIn("makin stuff up", str(error))
def test_get_preferdb(self):
try:
import sqlalchemy as sa
from wuttjamaican.db import Session
except ImportError:
pytest.skip("test is not relevant without sqlalchemy")
# start out with a default value
config = conf.WuttaConfig(defaults={'wutta.db.default.url': 'sqlite://',
@ -403,6 +425,10 @@ configure_logging = true
self.assertIsInstance(app, CustomAppHandler)
def test_get_engine_maker(self):
try:
from wuttjamaican.db.conf import make_engine_from_config
except ImportError:
pytest.skip("test is not relevant without sqlalchemy")
# default func
config = conf.WuttaConfig()
@ -421,7 +447,7 @@ class CustomAppHandler(AppHandler):
pass
def custom_make_engine_from_config(*args, **kwargs):
def custom_make_engine_from_config():
pass

View file

@ -1,6 +1,6 @@
[tox]
envlist = py36, py37, py38, py39, py310, py311
envlist = py36, py37, py38, py39, py310, py311, nodb
# TODO: can remove this when we drop py36 support
# nb. need this for testing older python versions
@ -15,6 +15,9 @@ commands = pytest {posargs}
# nb. newer coverage is causing segfault for this one, so must avoid that
deps = coverage<6.5
[testenv:nodb]
extras = tests
[testenv:coverage]
basepython = python3.11
extras = db,tests