How To Locally Test Arc Ecto Schema

Image for post
Image for post

TL;DR

In our Phoenix project, we are using an excellent Arc and Arc Ecto library that enables us to integrate S3 file storage into Ecto Schemas. This post is how to locally test Changesets for Arc Ecto schemas.

If your application handles file uploads, the question is where to store those files. The right architecture decision is to use Amazon S3 storage. If you develop your application in Phoenix Framework, Arc and Arc Ecto is an excellent choice to integrate S3 into your solution. As we write unit tests, the simple question arose, how to test our code without hitting the S3 Amazon storage. The reason is straightforward, and that is a fast test suite.

We need to use mocking, but the question was, what to mock in Arc Ecto?

Let’s define Arc Definition module, we have this in testivator_web/lib/uploaders/file.ex:

And we use this uploader type in the following schema:

We use espec and espec phoenix for testing. Here is espec for datafile schema where we test datafile Changeset but we do not set up connection parameters for Amazon S3:

When we run the example connection timeout the result is the following exception:

18:36:25.589 [error] GenServer ExAws.Config.AuthCache terminating
** (RuntimeError) Instance Meta Error: {:error, %{reason: :connect_timeout}}

We need to figure out which function of Arc Ecto we should mock. The heuristic that we used is to investigate how Arc Ecto was tested. We found the following function of interest.

We need to mock store method and this is done in example valid attributes. As we are using Mock library, do not forget to set async: false.

Conclusion

Arc Ecto library helps us to integrate file upload in an ecto schema. The result is that when we create valid changeset, we need a valid connection for AWS S3. We showed how to use the Elixir Mock library in order to mock the correct Arc Ecto function to be able to test the Arc Schema locally.

Originally published at https://blog.tentamen.eu on May 21, 2020.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store