コード例 #1
0
 def test_connect(self):
     """Test we can use connect() method to set a ctx"""
     connect(
         host = config.get('db', 'host'),
         port = config.get('db', 'port'),
         database = config.get('db', 'database'),
         user = config.get('db', 'user'),
         password = config.get('db', 'password'),
     )
     get_cursor() #Passes when no error
コード例 #2
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())
コード例 #3
0
ファイル: meta.py プロジェクト: pombredanne/Elephantoplasty
    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 test_get_cursor_with_invalid(self):
     """Test passing invalid type to get_cursor raises exc"""
     self.assertRaises(TypeError, lambda: get_cursor(1))
コード例 #5
0
 def test_get_cursor_with_cursor(self):
     """Test if passing a cursor to get_cursor returns itself"""
     cur = get_test_conn().cursor()
     self.assertEqual(get_cursor(cur), cur)
コード例 #6
0
 def test_get_cursor_with_conn(self):
     """Test getting cursor with a connection"""
     self.assertEqual(type(get_cursor(get_test_conn())), EPCursor)
コード例 #7
0
 def test_set_with_cursor(self):
     """Test we can use cursor to set ctx."""
     set_context(get_test_conn().cursor())
     get_cursor() #Passes when no error