Testing my Ecto Repo

On that note: Comparing ecto schema structs for equality (==/2) is hardly ever somethings you actually want to test for. E.g. the data coming out of the db will have an id, the input might not. The data coming out of the db have inserted-/updated_at timestamps set, the input might not. There might be defaults for db columns that your runtime doesn’t know of. So total equality is not really to be expected.

Tests are rather concerned with:

  • “are those the same entities” a.k.a. do they hold the same identity.
    The identity of those structs is described by the primary key alone (id field in most cases), so assert on them matching.
  • “are the fields of a struct saved correcty”
    Test just those fields like assert %{…} = Map.take(entity, [:name, :body, :link]) or even one assert per field.

I can see that those might require more elaborate tests than Module.func() == [entity], but that’s the nature of the beast.

5 Likes