Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

Field objects can have testdata generators #121

Closed Jaymon closed 1 year ago

Jaymon commented 3 years ago

So the idea is that you could add an Orm.testdata() method that would look at each field and generate an instance that satisfies the constraints of that field, but you can also override field values. And you could also pass in a testdata generator into Field so you can customize it.

So basically, by default the Field.testdata() would check for choice values, defaults, and things like that and generate a value for that field. The Orm.testdata method would call all the field testdata methods to get a set of field values and then return an instance, something like this:

import testdata

class Foo(Orm):
  bar = Field(str, True, testdata=lambda: testdata.get_str())
  che = Field(int, True)

  def testdata(self, **kwargs):
     for name, f in self.schema.fields.items():
       kwargs.setdefault(name, f.testdata())

     return type(self)(**kwargs)
Jaymon commented 3 years ago

The Orm.testdata method can go through all the fields, if one of the fields is a foreign key, it can then call that Orm's testdata method so it can layer building the objects and handle the dependencies and return a fully populated object, creating all the dependent objects along the way

Jaymon commented 1 year ago

I think https://github.com/Jaymon/prom/blob/master/prom/extras/testdata.py handles this. It allows fields to have a testdata method that it will use.