lycantropos / hypothesis_sqlalchemy

hypothesis strategies for generating SQLAlchemy objects
MIT License
28 stars 8 forks source link

UUID not supported #21

Closed bmabey closed 4 years ago

bmabey commented 5 years ago

UUID is not a supported inferred strategy but I believe it should be.. maybe it as simple as adding a mapping of UUID to the UUID strategy in this dictionary: https://github.com/lycantropos/hypothesis_sqlalchemy/blob/master/hypothesis_sqlalchemy/columnar/values.py#L51-L63

Here is how you reproduce the issue:

import sqlalchemy as sa
import sqlalchemy.dialects.postgresql as pg
from hypothesis_sqlalchemy import tabular

metadata = sa.MetaData()

user = sa.Table(
    "users",
    metadata,
    sa.Column("user_id", pg.UUID(), primary_key=True, unique=True),
    sa.Column("login", pg.VARCHAR(255), nullable=True),
)

tabular.records.factory(user) # throws NotImplementedError

The end of the stack trace looks like:


.../python3.6/site-packages/hypothesis_sqlalchemy/columnar/values.py in from_type(type_)
     66 @singledispatch
     67 def from_type(type_: TypeEngine) -> Strategy[Any]:
---> 68     return values_by_python_types[type_.python_type]
     69
     70

.../python3.6/site-packages/sqlalchemy/sql/type_api.py in python_type(self)
    394
    395         """
--> 396         raise NotImplementedError()
    397
    398     def with_variant(self, type_, dialect_name):

NotImplementedError:
lycantropos commented 4 years ago

does this help?

from typing import Union
from uuid import UUID

from hypothesis import strategies
from sqlalchemy.dialects import postgresql

from hypothesis_sqlalchemy.columnar.values import from_type
from hypothesis_sqlalchemy.hints import Strategy

# somewhere during project/tests start, e.g. in `conftest.py` for `pytest`
@from_type.register(postgresql.UUID)
def uuid_type_values_factory(type_: postgresql.UUID
                             ) -> Strategy[Union[str, UUID]]:
    result = strategies.uuids()
    if not type_.as_uuid:
        result = result.map(str)
    return result
lycantropos commented 4 years ago

since this type is Postgres-specific I'm thinking about the way for specifying a dialect for generating tables with this kind of column