示例#1
0
 def test_no_connection(self):
     """Test trying to use nonexistent ctx raises exc"""
     self.assertRaises(CtxError, lambda: get_connection())
     self.assertRaises(CtxError, lambda: get_cursor())
     self.assertRaises(CtxError, lambda: get_session())
     self.assertRaises(CtxError, lambda: commit())
     self.assertRaises(CtxError, lambda: add())
示例#2
0
 def test_custom_name(self):
     """Creating a table with custom table_name"""
     class Spam(Object):
         __table_name__ = 'eggs'
         meal = f.CharacterVarying(length = 10)
         
     add(Spam(meal = 'bacon'))
     commit()
     c = get_connection()
     cur = c.cursor()
     cur.execute('SELECT id, meal FROM eggs WHERE 1 = 1;')
     self.assertEqual(cur.fetchone(), (1, 'bacon'))
示例#3
0
    def create_table(cls, ctx = None): #@NoSelf
        cursor = get_cursor(ctx)
        connection = get_connection(ctx)
        column_decls = []
        columns = it.chain(*[field.columns for field in cls.fields])
        constraints = sum([field.constraints for field in cls.fields], [])
        for column in columns:
            if column.declaration:
                column_decls.append(column.declaration) 
        constraints.append('PRIMARY KEY ({0})'.format(','.join(cls.__pk__)))
        command = """CREATE TABLE {tname}
        (
        {columns}
        )
        {inh_clause};""".format(
            tname = cls.__table_name__, columns = ',\n'.join(
                [d[0] for d in column_decls] + constraints
            ), inh_clause = (
                'INHERITS ({0})'.format(', '.join((
                    column.__table_name__ for column in cls.parent_classes)
                )) if cls.parent_classes else ''
            )
        )
        args = sum([declaration[1] for declaration in column_decls], [])
        retried = False
        while True:
            try:
                cursor.execute(command, args)
                break
            except ProgrammingError as err:
                if err.pgcode == UNDEFINED_TABLE and not retried:
                    cursor.connection.rollback_clean()
                    for col in cls.columns:
                        if col.references is not None:
                            col.references.create_table()
                            retried = True
                            break
                else:
                    cursor.connection.rollback()
                    raise
        for index in cls.indexes:
            cursor.execute(index_cmd(cls.__table_name__, index), [])

        cursor.connection.save()
示例#4
0
 def tearDown(self):
     c = get_connection()
     c.cursor().execute('DROP TABLE knights;')
     c.commit()
示例#5
0
 def test_get_connection_simple(self):
     """Passing a connection to get_connection should return itself"""
     conn = get_test_conn()
     self.assertEqual(conn, get_connection(conn))