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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
from sqlalchemy import MetaData, Table, Column, String, Unicode, Integer, \
create_engine
from sqlalchemy.testing import fixtures, AssertsExecutionResults, profiling
from sqlalchemy import testing
from sqlalchemy.testing import eq_
from sqlalchemy.util import u
from sqlalchemy.engine.result import RowProxy
import sys
NUM_FIELDS = 10
NUM_RECORDS = 1000
t = t2 = metadata = None
class ResultSetTest(fixtures.TestBase, AssertsExecutionResults):
__backend__ = True
@classmethod
def setup_class(cls):
global t, t2, metadata
metadata = MetaData(testing.db)
t = Table('table', metadata, *[Column('field%d' % fnum, String(50))
for fnum in range(NUM_FIELDS)])
t2 = Table(
'table2', metadata, *
[Column('field%d' % fnum, Unicode(50))
for fnum in range(NUM_FIELDS)])
def setup(self):
metadata.create_all()
t.insert().execute([dict(('field%d' % fnum, u('value%d' % fnum))
for fnum in range(NUM_FIELDS)) for r_num in
range(NUM_RECORDS)])
t2.insert().execute([dict(('field%d' % fnum, u('value%d' % fnum))
for fnum in range(NUM_FIELDS)) for r_num in
range(NUM_RECORDS)])
# warm up type caches
t.select().execute().fetchall()
t2.select().execute().fetchall()
def teardown(self):
metadata.drop_all()
@profiling.function_call_count()
def test_string(self):
[tuple(row) for row in t.select().execute().fetchall()]
@profiling.function_call_count()
def test_unicode(self):
[tuple(row) for row in t2.select().execute().fetchall()]
def test_contains_doesnt_compile(self):
row = t.select().execute().first()
c1 = Column('some column', Integer) + \
Column("some other column", Integer)
@profiling.function_call_count()
def go():
c1 in row
go()
class ExecutionTest(fixtures.TestBase):
__backend__ = True
def test_minimal_connection_execute(self):
# create an engine without any instrumentation.
e = create_engine('sqlite://')
c = e.connect()
# ensure initial connect activities complete
c.execute("select 1")
@profiling.function_call_count()
def go():
c.execute("select 1")
try:
go()
finally:
c.close()
def test_minimal_engine_execute(self, variance=0.10):
# create an engine without any instrumentation.
e = create_engine('sqlite://')
# ensure initial connect activities complete
e.execute("select 1")
@profiling.function_call_count()
def go():
e.execute("select 1")
go()
class RowProxyTest(fixtures.TestBase):
__requires__ = 'cpython',
__backend__ = True
def _rowproxy_fixture(self, keys, processors, row):
class MockMeta(object):
def __init__(self):
pass
metadata = MockMeta()
keymap = {}
for index, (keyobjs, processor, values) in \
enumerate(list(zip(keys, processors, row))):
for key in keyobjs:
keymap[key] = (processor, key, index)
keymap[index] = (processor, key, index)
return RowProxy(metadata, row, processors, keymap)
def _test_getitem_value_refcounts(self, seq_factory):
col1, col2 = object(), object()
def proc1(value):
return value
value1, value2 = "x", "y"
row = self._rowproxy_fixture(
[(col1, "a"), (col2, "b")],
[proc1, None],
seq_factory([value1, value2])
)
v1_refcount = sys.getrefcount(value1)
v2_refcount = sys.getrefcount(value2)
for i in range(10):
row[col1]
row["a"]
row[col2]
row["b"]
row[0]
row[1]
row[0:2]
eq_(sys.getrefcount(value1), v1_refcount)
eq_(sys.getrefcount(value2), v2_refcount)
def test_value_refcounts_pure_tuple(self):
self._test_getitem_value_refcounts(tuple)
def test_value_refcounts_custom_seq(self):
class CustomSeq(object):
def __init__(self, data):
self.data = data
def __getitem__(self, item):
return self.data[item]
def __iter__(self):
return iter(self.data)
self._test_getitem_value_refcounts(CustomSeq)
|