forked from pymssql/pymssql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sqlalchemy.py
62 lines (50 loc) · 1.53 KB
/
test_sqlalchemy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import unittest
from .helpers import config, eq_, skip_test
try:
import sqlalchemy as sa
except ImportError:
skip_test('SQLAlchemy is not available')
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = sa.create_engine(
'mssql+pymssql://%s:%s@%s:%s/%s' % (
config.user,
config.password,
config.server,
config.port,
config.database
),
echo=False
)
meta = sa.MetaData()
Base = declarative_base(metadata=meta)
Session = sessionmaker(bind=engine)
sess = Session()
class SAObj(Base):
__tablename__ = 'sa_test_objs'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(50))
data = sa.Column(sa.PickleType)
saotbl = SAObj.__table__
saotbl.drop(engine, checkfirst=True)
saotbl.create(engine)
class TestSA(unittest.TestCase):
def tearDown(self):
# issue rollback first, otherwise clearing the table might give us
# an error that the session is in a bad state
sess.rollback()
sess.execute(SAObj.__table__.delete())
sess.commit()
def test_basic_usage(self):
s = SAObj(name='foobar')
sess.add(s)
sess.commit()
assert s.id
assert sess.query(SAObj).count() == 1
def test_pickle_type(self):
s = SAObj(name='foobar', data=['one'])
sess.add(s)
sess.commit()
res = sess.execute(sa.select([saotbl.c.data]))
row = res.fetchone()
eq_(row['data'], ['one'])