lycantropos / hypothesis_sqlalchemy

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

Records lists_factory unique_by with multiple columns #15

Closed rubenhelsloot closed 5 years ago

rubenhelsloot commented 5 years ago

Hi,

First of all, I wanted to thank you for all the work, this project has really helped me in setting up an easily testable codebase! I've found an issue when you have a model with multiple unique columns. Specifically, the lists_factory returns records that are unique by the combination of multiple columns, but not as individual columns. I have, for example, the following model:

class Country(BaseEntity):
  """
  A country is an overarching entity that can hold information about all
  locations assigned to it.
  """
  __tablename__ = "country"
  id = db.Column(db.Integer, primary_key=True)
  title = db.Column(db.String, unique=True, nullable=False)
  vat = db.Column(db.Integer)
  currency = db.Column(db.String(3), default='EUR', nullable=False)

Passing this model to the lists_factory returns a lists strategy with unique_by function lambda row: (row[0], row[1]), which means that the combination of id and title must be unique, but not the individual id and title. This results in errors when I try to generate multiple, as those fields may not be unique, especially when hypothesis is shrinking.

I would propose to change the unique_by-generating function to return a tuple of functions, one for each column. (lambda row: row[0], lambda row: row[1]) would only result in records that differ on both columns. I wouldn't mind opening a PR for this, if you agree that it would be nice to have this functionality.

Thanks in advance!

Ruben

lycantropos commented 5 years ago

Glad to read that and thanks, this sounds reasonable, feel free to send a PR.