found my fault.

i have used elixir in a deprecated way, so the error has been masked.

reposting correct code. it may still work the older way too.

thanks,
alex

On Tue, Oct 21, 2008 at 10:25, alex bodnaru <[EMAIL PROTECTED]> wrote:
> hello friends,
>
> i'm trying to use a custom type, as illustrated in
> http://www.sqlalchemy.org/docs/05/types.html,
> but neither the old convert_bind_param and convert_result_value, nor
> the newer process_bind_param and
> process_result_value to not appear to be invoked when i manipulate the type.
>
> the original code was working for me, in the old method. it was
> downloaded from:
> http://www.mail-archive.com/[EMAIL PROTECTED]/msg00299.html.
>
> the code and test are attached.
>
> best regards,
> alex
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

#############
"""enum.py"""

from sqlalchemy.types import TypeEngine, TypeDecorator, String, Unicode

class Enum(TypeDecorator):
    """This class adds support for ENUM fields, similar in 
    functionality to the
    ENUM column type in MySQL"""
    
    impl = TypeEngine

    def __init__(self, values, empty_to_none=False):
        '''
        contruct an Enum type

        values : a list of values that are valid for this column
        empty_to_none : treat the empty string '' as None
        '''
        if values is None or len(values) is 0:
            raise exceptions.AssertionError('Enum requires a list of values')
        self.empty_to_none = empty_to_none
        self.values = values
        # the length of the string/unicode column should be the longest string
        # in values
        size = max([len(v) for v in values if v is not None])
        super(Enum, self).__init__(size)

    def test_range(self, value):
        if value not in self.values:
            raise AssertionError('"%s" not in Enum.values' % value)
        return value

    def convert_bind_param(self, value, engine):
        if self.empty_to_none and value is '':
            value = None
        self.test_range(value)
        return super(Enum, self).convert_bind_param(value, engine)

    def convert_result_value(self, value, engine):
        self.test_range(value)
        return super(Enum, self).convert_result_value(value, engine)

    def process_bind_param(self, value, dialect):     
        return self.test_range(value)
        
    def process_result_value(self, value, dialect):
        return self.test_range(value)

    def copy(self):
        return type(self)(self.values, self.empty_to_none)

class EnumUnicode(Enum):
    impl = Unicode

class EnumString(Enum):
    impl = String
#############
"""test_enum.py"""

from enum import *
from elixir import *

metadata.bind = 'sqlite:///'
#metadata.bind.echo = True

def test_enum():
    class TestPerson(Entity):
        gender = Field(EnumString(["m","f"]))

    setup_all(True)

    t = TestPerson()
    t.gender = "m"
    session.flush()
    t.gender = "f"
    session.flush()

    t.gender = "W"  #this should fail
#    session.flush()
    try:
        session.flush()
    except AssertionError:
#        print "'W' failed"
        pass
    else:
        assert False  #make sure this fails if it doesn't raise the exception above

    drop_all()
    session.clear()

Reply via email to