mrmicahcooper/ecto_factory

Better Document Attribute Casting

Closed this issue · 1 comments

This project rocks! Thank you for building it.

One thing I like is the casting of string and integer attributes, which I believe happens here:

https://github.com/mrmicahcooper/ecto_factory/blob/master/lib/ecto_factory.ex#L92-L93

This lets you ignore irrelevant attributes in your test setup. So we can build a channel record without specifying a not-null attribute, twitter_hashtag:

iex(1)> EctoFactory.insert(:channel, name: "vim")
%Tilex.Channel{__meta__: #Ecto.Schema.Metadata<:loaded, "channels">, id: 5,
 inserted_at: #Ecto.DateTime<2016-12-29 18:26:09>, name: "vim", posts: [],
 twitter_hashtag: "twitter_hashtag",
 updated_at: #Ecto.DateTime<2016-12-29 18:26:09>}

I.e. The twitter_hashtag column gets set to a string version of its column name when no options are supplied.

This is a really unique feature, but it surprised me after experience with Ruby's FactoryGirl. That library would have expected a default value for twitter_hashtag, raising an error if it wasn't set. The situation gets trickier if twitter_hashtag has a uniqueness constraint, because there can only be one "twitter_hashtag" value– the feature only works once in a transaction.

I'd like to clarify this in the README and inline docs.

Now leaving an attribute blank will generate a random value based on the datatype for that field. So, you'll have to explicitly set a field to nil if you want it to be nil.