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
 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())
Beispiel #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()
 def test_get_cursor_with_invalid(self):
     """Test passing invalid type to get_cursor raises exc"""
     self.assertRaises(TypeError, lambda: get_cursor(1))
 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)
 def test_get_cursor_with_conn(self):
     """Test getting cursor with a connection"""
     self.assertEqual(type(get_cursor(get_test_conn())), EPCursor)
 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