class TestTypeDec(TypeDecorator): impl = NullType() def load_dialect_impl(self, dialect): if dialect.name == "oracle": return dialect.type_descriptor(datatype) else: return self.impl
def _test_setinputsizes(self, datatype, value, sis_value): class TestTypeDec(TypeDecorator): impl = NullType() def load_dialect_impl(self, dialect): if dialect.name == 'oracle': return dialect.type_descriptor(datatype) else: return self.impl m = self.metadata # Oracle can have only one column of type LONG so we make three # tables rather than one table w/ three columns t1 = Table( 't1', m, Column('foo', datatype), ) t2 = Table( 't2', m, Column('foo', NullType().with_variant(datatype, "oracle")), ) t3 = Table('t3', m, Column('foo', TestTypeDec())) m.create_all() class CursorWrapper(object): # cx_oracle cursor can't be modified so we have to # invent a whole wrapping scheme def __init__(self, connection_fairy): self.cursor = connection_fairy.connection.cursor() self.mock = mock.Mock() connection_fairy.info['mock'] = self.mock def setinputsizes(self, *arg, **kw): self.mock.setinputsizes(*arg, **kw) self.cursor.setinputsizes(*arg, **kw) def __getattr__(self, key): return getattr(self.cursor, key) with testing.db.connect() as conn: connection_fairy = conn.connection for tab in [t1, t2, t3]: with mock.patch.object( connection_fairy, "cursor", lambda: CursorWrapper(connection_fairy)): conn.execute(tab.insert(), {"foo": value}) if sis_value: eq_(conn.info['mock'].mock_calls, [mock.call.setinputsizes(foo=sis_value)]) else: eq_(conn.info['mock'].mock_calls, [mock.call.setinputsizes()])
def test_as_json(self): """Tests the jsonable.as_json function""" self.assertEqual(None, jsonable.as_json(None)) self.assertEqual({}, jsonable.as_json({})) import datetime obj = datetime.datetime(2014, 5, 5) self.assertEqual( { '__cls__': 'datetime.datetime', 'value': obj.isoformat() }, jsonable.as_json(obj)) from sqlalchemy.util import KeyedTuple self.assertEqual( { '__cls__': 'sqlalchemy.util.KeyedTuple', '_labels': ['a'], 'a': 1 }, jsonable.as_json(KeyedTuple([1], labels=['a']))) from sqlalchemy.sql.sqltypes import NullType self.assertEqual(None, jsonable.as_json(NullType())) self.assertEqual([1], jsonable.as_json([1])) self.assertEqual(u'a', jsonable.as_json('a')) class TestJsonable(jsonable.Jsonable): def __init__(self): self.a = 1 self.b = jsonable.Jsonable() self.assertEqual( { '__cls__': "<class 'tests.test_jsonable.TestJsonable'>", 'a': 1, 'b': { '__cls__': "<class 'dbmanagr.jsonable.Jsonable'>" } }, jsonable.as_json(TestJsonable()))
def test_setinputsizes( self, datatype, value, sis_value_text, set_nchar_flag ): if isinstance(sis_value_text, str): sis_value = getattr(testing.db.dialect.dbapi, sis_value_text) else: sis_value = sis_value_text class TestTypeDec(TypeDecorator): impl = NullType() def load_dialect_impl(self, dialect): if dialect.name == "oracle": return dialect.type_descriptor(datatype) else: return self.impl m = self.metadata # Oracle can have only one column of type LONG so we make three # tables rather than one table w/ three columns t1 = Table("t1", m, Column("foo", datatype)) t2 = Table( "t2", m, Column("foo", NullType().with_variant(datatype, "oracle")) ) t3 = Table("t3", m, Column("foo", TestTypeDec())) m.create_all() class CursorWrapper(object): # cx_oracle cursor can't be modified so we have to # invent a whole wrapping scheme def __init__(self, connection_fairy): self.cursor = connection_fairy.connection.cursor() self.mock = mock.Mock() connection_fairy.info["mock"] = self.mock def setinputsizes(self, *arg, **kw): self.mock.setinputsizes(*arg, **kw) self.cursor.setinputsizes(*arg, **kw) def __getattr__(self, key): return getattr(self.cursor, key) if set_nchar_flag: engine = testing_engine(options={"use_nchar_for_unicode": True}) else: engine = testing.db with engine.connect() as conn: connection_fairy = conn.connection for tab in [t1, t2, t3]: with mock.patch.object( connection_fairy, "cursor", lambda: CursorWrapper(connection_fairy), ): conn.execute(tab.insert(), {"foo": value}) if sis_value: eq_( conn.info["mock"].mock_calls, [mock.call.setinputsizes(foo=sis_value)], ) else: eq_( conn.info["mock"].mock_calls, [mock.call.setinputsizes()], )