First commit, basic config (with db) and app handler
this has 100% test coverage and i intend to keep it that way. api docs have a good start but still need narrative. several more things must be added before i can seriously consider incorporating into rattail but this seemed a good save point
This commit is contained in:
commit
5c3c42d6b3
36 changed files with 3322 additions and 0 deletions
0
tests/db/__init__.py
Normal file
0
tests/db/__init__.py
Normal file
132
tests/db/test_conf.py
Normal file
132
tests/db/test_conf.py
Normal file
|
@ -0,0 +1,132 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
import os
|
||||
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
|
||||
|
||||
|
||||
class TestEngineFromConfig(TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
engine = conf.engine_from_config({
|
||||
'sqlalchemy.url': 'sqlite://',
|
||||
})
|
||||
self.assertIsInstance(engine, Engine)
|
||||
|
||||
def test_poolclass(self):
|
||||
|
||||
engine = conf.engine_from_config({
|
||||
'sqlalchemy.url': 'sqlite://',
|
||||
})
|
||||
self.assertNotIsInstance(engine.pool, NullPool)
|
||||
|
||||
engine = conf.engine_from_config({
|
||||
'sqlalchemy.url': 'sqlite://',
|
||||
'sqlalchemy.poolclass': 'sqlalchemy.pool:NullPool',
|
||||
})
|
||||
self.assertIsInstance(engine.pool, NullPool)
|
||||
|
||||
def test_pool_pre_ping(self):
|
||||
|
||||
engine = conf.engine_from_config({
|
||||
'sqlalchemy.url': 'sqlite://',
|
||||
})
|
||||
self.assertFalse(engine.pool._pre_ping)
|
||||
|
||||
engine = conf.engine_from_config({
|
||||
'sqlalchemy.url': 'sqlite://',
|
||||
'sqlalchemy.pool_pre_ping': 'true',
|
||||
})
|
||||
self.assertTrue(engine.pool._pre_ping)
|
||||
|
||||
|
||||
class TestGetEngines(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.tempdir = tempfile.mkdtemp()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tempdir)
|
||||
|
||||
def write_file(self, filename, content):
|
||||
path = os.path.join(self.tempdir, filename)
|
||||
with open(path, 'wt') as f:
|
||||
f.write(content)
|
||||
return path
|
||||
|
||||
def test_no_default(self):
|
||||
myfile = self.write_file('my.conf', '')
|
||||
config = WuttaConfig([myfile])
|
||||
self.assertEqual(conf.get_engines(config, 'wuttadb'), {})
|
||||
|
||||
def test_default(self):
|
||||
myfile = self.write_file('my.conf', """\
|
||||
[wuttadb]
|
||||
default.url = sqlite://
|
||||
""")
|
||||
config = WuttaConfig([myfile])
|
||||
result = conf.get_engines(config, 'wuttadb')
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertIn('default', result)
|
||||
engine = result['default']
|
||||
self.assertEqual(engine.dialect.name, 'sqlite')
|
||||
|
||||
def test_default_fallback(self):
|
||||
myfile = self.write_file('my.conf', """\
|
||||
[wuttadb]
|
||||
sqlalchemy.url = sqlite://
|
||||
""")
|
||||
config = WuttaConfig([myfile])
|
||||
result = conf.get_engines(config, 'wuttadb')
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertIn('default', result)
|
||||
engine = result['default']
|
||||
self.assertEqual(engine.dialect.name, 'sqlite')
|
||||
|
||||
def test_other(self):
|
||||
myfile = self.write_file('my.conf', """\
|
||||
[otherdb]
|
||||
keys = first, second
|
||||
first.url = sqlite://
|
||||
second.url = sqlite://
|
||||
""")
|
||||
config = WuttaConfig([myfile])
|
||||
result = conf.get_engines(config, 'otherdb')
|
||||
self.assertEqual(len(result), 2)
|
||||
self.assertIn('first', result)
|
||||
self.assertIn('second', result)
|
||||
|
||||
|
||||
class TestGetSetting(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
Session = orm.sessionmaker()
|
||||
engine = sa.create_engine('sqlite://')
|
||||
self.session = Session(bind=engine)
|
||||
self.session.execute(sa.text("""
|
||||
create table setting (
|
||||
name varchar(255) primary key,
|
||||
value text
|
||||
);
|
||||
"""))
|
||||
|
||||
def tearDown(self):
|
||||
self.session.close()
|
||||
|
||||
def test_basic_value(self):
|
||||
self.session.execute(sa.text("insert into setting values ('foo', 'bar');"))
|
||||
value = conf.get_setting(self.session, 'foo')
|
||||
self.assertEqual(value, 'bar')
|
||||
|
||||
def test_missing_value(self):
|
||||
value = conf.get_setting(self.session, 'foo')
|
||||
self.assertIsNone(value)
|
54
tests/db/test_sess.py
Normal file
54
tests/db/test_sess.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
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
|
||||
|
||||
|
||||
class TestShortSession(TestCase):
|
||||
|
||||
def test_none(self):
|
||||
with sess.short_session() as s:
|
||||
self.assertIsInstance(s, sess.Session.class_)
|
||||
|
||||
def test_factory(self):
|
||||
TestSession = orm.sessionmaker()
|
||||
with sess.short_session(factory=TestSession) as s:
|
||||
self.assertIsInstance(s, TestSession.class_)
|
||||
|
||||
def test_instance(self):
|
||||
# nb. nothing really happens if we provide the session instance
|
||||
session = MagicMock()
|
||||
with sess.short_session(session=session) as s:
|
||||
pass
|
||||
session.commit.assert_not_called()
|
||||
session.close.assert_not_called()
|
||||
|
||||
def test_config(self):
|
||||
config = MagicMock()
|
||||
TestSession = orm.sessionmaker()
|
||||
config.get_app.return_value.make_session = TestSession
|
||||
# nb. config may be first arg (or kwarg)
|
||||
with sess.short_session(config) as s:
|
||||
self.assertIsInstance(s, TestSession.class_)
|
||||
|
||||
def test_without_commit(self):
|
||||
session = MagicMock()
|
||||
TestSession = MagicMock(return_value=session)
|
||||
with sess.short_session(factory=TestSession, commit=False) as s:
|
||||
pass
|
||||
session.commit.assert_not_called()
|
||||
session.close.assert_called_once_with()
|
||||
|
||||
def test_with_commit(self):
|
||||
session = MagicMock()
|
||||
TestSession = MagicMock(return_value=session)
|
||||
with sess.short_session(factory=TestSession, commit=True) as s:
|
||||
pass
|
||||
session.commit.assert_called_once_with()
|
||||
session.close.assert_called_once_with()
|
Loading…
Add table
Add a link
Reference in a new issue