whatyouhide / stream_data

Data generation and property-based testing for Elixir. 🔮

Home Page:https://hexdocs.pm/stream_data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generator for Ecto schemas

timgent opened this issue · comments

It'd be great if we could have a generator for Ecto schemas - they contain information on the fields and the types of those fields, so in theory we should be able to have a generator for that. Something like:

defmodule MySchema
  use Ecto.Schema

  schema "my_schema" do
    field name, :string
    field age, :integer
  end
end

# Then you could do something like
StreamData.schema(MySchema)

In terms of implementation you can pull out the fields and types for an Ecto schema with something like this:

MySchema.__schema__(:fields) 
|> Enum.map(fn field -> 
    {field, MySchema.__schema__(:type, field)} 
  end)

You could either have it just do this for the top level fields, or if you wanted to generate nested fields for relationships too I guess there would need to be a way to pass in custom generators.

Appreciate it may be nice to have this as a separate library that builds on this one if you want to avoid the dependency on Ecto. Anyway thoughts appreciated!

Appreciate it may be nice to have this as a separate library that builds on this one if you want to avoid the dependency on Ecto. Anyway thoughts appreciated!

This is absolutely the way to go IMO! StreamData doesn't depend on Ecto and not all StreamData users care about Ecto generation. I think trying this out as a separate library would be a great idea.

There are some details and behaviors to iron out when it comes to things like custom Ecto types and such, but this is something to try out. Good luck and let me know if I can help in any way! ❤️