Assert the Size of the List in Elixir

I would like to assert list's size. Currently I do it as follows:

assert devices = Repo.all from d in device, where d.uuid == ^attrs.uuid
assert devices.first == devices.last

Is there a better way to do that?

0

1 Answer

will return the size of a list:

length([1,2,3]) #3

You can do this from an Ecto query using:

query = from d in Device, where: d.uuid == ^uuid, select: fragment("count(?)", d.id)
assert  Repo.all(query)== 3

In Ecto 2 you can use

query = from d in Device, where: d.uuid == ^uuid)
assert Repo.aggregate(query, :count, :id) == 3
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest