def test_no_strings(self): conn = dbapi.connect(self._simple_fixture()) curs = conn.cursor() assert_raises_message( dbapi.Error, "Only Pandas callable functions accepted for execute()", curs.execute, "select * from table")
def test_description(self): def stmt(trace, namespace, params): return namespace["df1"][["col1", "col2", "col3"]] conn = dbapi.connect(self._simple_fixture()) curs = conn.cursor() curs.execute(stmt) eq_(curs.description, [('col1', None, None, None, None, None, None), ('col2', None, None, None, None, None, None), ('col3', None, None, None, None, None, None)])
def test_dataframe(self): def stmt(trace, namespace, params): return namespace["df1"][["col1", "col2", "col3"]] conn = dbapi.connect(self._simple_fixture()) curs = conn.cursor() curs.execute(stmt) eq_(list(curs.dataframe.itertuples(index=False)), [(0, 'string: 0', 0.0), (1, 'string: 1', 0.0054729999999999996), (2, 'string: 2', 0.010945999999999999), (3, 'string: 3', 0.016419), (4, 'string: 4', 0.021891999999999998)])
def test_simple_statement(self): def stmt(trace, namespace, params): return namespace["df1"][["col1", "col2", "col3"]] conn = dbapi.connect(self._simple_fixture()) curs = conn.cursor() curs.execute(stmt) eq_(curs.fetchall(), [(0, 'string: 0', 0.0), (1, 'string: 1', 0.005473), (2, 'string: 2', 0.010946), (3, 'string: 3', 0.016419), (4, 'string: 4', 0.021892)])
def _numbers_fixture(self): numbers = pd.DataFrame(np.array( ((0.01, 0.01, 0.02, 0.04, 0.03), (0.00, 0.02, 0.02, 0.03, 0.02), (0.01, 0.02, 0.02, 0.03, 0.02), (0.01, 0.00, 0.01, 0.05, 0.03))), columns=['a', 'b', 'c', 'd', 'e']) m = MetaData() n_t = Table('numbers', m, Column('a', Float), Column('b', Float), Column('c', Float), Column('d', Float), Column('e', Float)) conn = dbapi.connect({"numbers": numbers}) return n_t, conn
def _emp_d_fixture(self): emp_df = pd.DataFrame([ { "emp_id": 1, "name": "ed", "fullname": "Ed Jones", "dep_id": 1 }, { "emp_id": 2, "name": "wendy", "fullname": "Wendy Wharton", "dep_id": 1 }, { "emp_id": 3, "name": "jack", "fullname": "Jack Smith", "dep_id": 2 }, ]) dept_df = pd.DataFrame([ { "dep_id": 1, "name": "Engineering" }, { "dep_id": 2, "name": "Accounting" }, { "dep_id": 3, "name": "Sales" }, ]) m = MetaData() emp = Table('employee', m, Column('emp_id', Integer, primary_key=True), Column('name', String), Column('fullname', String), Column('dep_id', Integer, ForeignKey('department.dep_id'))) dep = Table( 'department', m, Column('dep_id', Integer, primary_key=True), Column('name', String), ) conn = dbapi.connect({"employee": emp_df, "department": dept_df}) return emp, dep, conn
def _autoinc_fixture(self): dept_df = pd.DataFrame([ { "name": "Engineering" }, { "name": "Accounting" }, { "name": "Sales" }, ]) m = MetaData() dep = Table('department', m, Column('dep_id', Integer, primary_key=True), Column('name', String), pandas_index_pk=True) conn = dbapi.connect({"department": dept_df}) return dep, conn
def _emp_d_fixture(self, pandas_index_pk=False): if pandas_index_pk: emp_df = pd.DataFrame(columns=["name", "fullname", "dep_id"]) dept_df = pd.DataFrame(columns=["name"]) else: emp_df = pd.DataFrame( columns=["emp_id", "name", "fullname", "dep_id"]) dept_df = pd.DataFrame(columns=["dep_id", "name"]) m = MetaData() emp = Table('employee', m, Column('emp_id', Integer, primary_key=True), Column('name', String), Column('fullname', String), Column('dep_id', Integer, ForeignKey('department.dep_id')), pandas_index_pk=pandas_index_pk) dep = Table('department', m, Column('dep_id', Integer, primary_key=True), Column('name', String), pandas_index_pk=pandas_index_pk) conn = dbapi.connect({"employee": emp_df, "department": dept_df}) return emp, dep, conn
def _conn_fixture(self): conn = dbapi.connect(None) return conn