Testing with Elixir and Phoenix: Models

  • Write tests for User model
  • Write tests for Message model

The tests we're going to write for our models are a little bit more specific than the ones we wrote for our controllers and channels. This is because we may call Model behaviors in our Controllers or Channels but not the other way around.

So we need to test our input data validations and our custom queries.

Channel/Controller tests often indirectly test the behavior of our model, like when we get initial messages on channel.join and expect latest_room_messages/2 to have been called with the most recent 10 messages. But we also want to make sure that our models work independently of our controllers and channels. Fortunately these tests are simpler than the Controller/Channel tests since we can call the functions directly (e.g. User.changeset/2) rather than simulating an HTTP request like we had to do with our Controller.

User model

We start by importing ModelCase, which contains useful functions for Model testing. Check them out at test/support/model_case.ex. Then, just as we did in our other tests, we create a set of valid and invalid attributes to make our tests easier to read.

defmodule PhoenixChat.UserTest do
use PhoenixChat.ModelCase
alias PhoenixChat.User
@valid_attrs %{email: "foo@bar.com", encrypted_password: "some content", username: "some content"}
@invalid_attrs %{}
...
end

Since we don't have any custom queries for our User model yet, all we're going to test is our validations on input data. The first and simplest is testing our changeset with valid inputs. To do this we use changeset.valid?, which is a field in the Ecto.Changeset struct (docs). If you log a valid changeset, you'll get something like the result below (note the valid?: true field).

#Ecto.Changeset<action: nil, changes: %{email: "foo@bar.com", username: "some content"}, errors: [], data: #PhoenixChat.User<>, valid?: true>

This should be the same as the output from the generator.

test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
end

Then we're going to test all the ways in which we can make an invalid changeset. Cure

If you look at our changeset in web/models/user.ex, you'll see that we have a few constraints: namely cast, validate_format, validate_length, and unique_constraint.

def changeset(model, params \\ :empty) do
model
|> cast(params, ~w(email username), [])
|> validate_format(:email, ~r/@/)
|> validate_length(:username, min: 1, max: 20)
|> update_change(:email, &String.downcase/1)
|> unique_constraint(:email)
|> update_change(:username, &String.downcase/1)
|> unique_constraint(:username)
end

In each of the following tests, we're going to create a changeset with invalid attributes of a particular type and check the error message to make sure it's the error we expect.

changeset stores errors in its :errors field, which is populated by a list of two-element tuples in the format {field_name, error_msg}. So in our assertion, we're pulling out the value we want using in to pull the value out of the errors list. We encourage you to put in the occasional IO.puts to make sure you understand which values are coming from where.

test "changeset with blank email and username" do
%{errors: errors} = User.changeset(%User{}, %{})
assert {:email, {"can't be blank", []}} in errors
assert {:username, {"can't be blank", []}} in errors
end
test "changeset with invalid email format" do
%{errors: errors} = User.changeset(%User{}, %{email: "foo"})
assert {:email, {"has invalid format", []}} in errors
end
test "changeset with username invalid length" do
long_username = String.duplicate "f", 21
%{errors: errors} = User.changeset(%User{}, %{username: long_username})
assert {:username, {"should be at most %{count} character(s)", [count: 20]}} in errors
end
test "changeset must have a unique email" do
changeset = User.changeset(%User{}, @valid_attrs)
Repo.insert!(changeset)
changeset = User.changeset(%User{}, @valid_attrs)
{:error, changeset} = Repo.insert(changeset)
assert {:email, {"has already been taken", []}} in changeset.errors
end
test "changeset must have a unique username" do
changeset = User.changeset(%User{}, @valid_attrs)
Repo.insert!(changeset)
attrs = Map.put(@valid_attrs, :email, "test@bar.com")
changeset = User.changeset(%User{}, attrs)
{:error, changeset} = Repo.insert(changeset)
assert {:username, {"has already been taken", []}} in changeset.errors
end

Then we need to test our registration_changeset in the same way. First we pass valid attributes and make sure it works. We're also testing to make sure that an encrypted_password field was created by using the get_change/3 method (docs). We don't need to test put_encrypted_pw/1 directly because it is safe to assume that Comeonin and Bcrypt work properly.

Once we've tested a passing changeset, we send a password that is too short and ensure that our error corresponds to the "password is too short" error.

test "registration changeset with valid attributes" do
valid_attrs = Map.put(@valid_attrs, :password, "password")
changeset = User.registration_changeset(%User{}, valid_attrs)
assert changeset.valid?
assert get_change(changeset, :encrypted_password)
end
test "registration changeset with invalid password length" do
long_password = String.duplicate "p", 101
%{errors: errors} = User.registration_changeset(%User{}, %{password: long_password})
assert {:password, {"should be at most %{count} character(s)", [count: 100]}} in errors
end

Message model

Our message model is a little bit more complicated since we need to test our custom query that fetches the latest 10 posts.

First we set up the valid and invalid attributes.

defmodule PhoenixChat.MessageTest do
use PhoenixChat.ModelCase
alias PhoenixChat.Message
@valid_attrs %{body: "some content", room: "some content", timestamp: "2010-04-17 14:00:00"}
@invalid_attrs %{}
...
end

Then we test our changeset with those attributes, just as we did with the User model.

test "changeset with valid attributes" do
changeset = Message.changeset(%Message{}, @valid_attrs)
assert changeset.valid?
end
test "changeset with invalid attributes" do
%{errors: errors} = Message.changeset(%Message{}, @invalid_attrs)
assert {:body, {"can't be blank", []}} in errors
assert {:timestamp, {"can't be blank", []}} in errors
assert {:room, {"can't be blank", []}} in errors
end

And now we test our query. We can test queries by running them and then checking the records returned by the query and matching them with the results that we expect.

In some circumstances, we may choose to run an assertion on the query struct returned by Ecto. We're going to do this in the last assertion of this test because all we're testing is that the default limit is set to 10. You could also test this by creating 11 messages, running the query, then assert that we only got 10 back.

We prepare the test by inserting multiple messages to the database with different timestamps, since we need to know which are the most recent to ensure they are properly ordered.

Then we create our query by calling latest_room_messages/2 with 1 as the room (note that we added three messages to room 1) and 2 as the override for the default value of 10, so we will only get the two most recent messages back. Now that we've created the query we want, we use Repo.all/1 to get the result of the query.

Now we run our first assertion, which makes sure that we only received 2 messages. If we hadn't specified 2 in our latest_room_messages/2, we should have received all 3 messages we added to the database because the default limit is 10 messages.

From there we use the head-tail pattern matching to separate out the first message from the list (the head) from the rest of the message (the tail) and assign the head to msg1 and the tail to msg2.

Then in our assertion, we are checking to make sure that the second message (msg2) has the same timestamp value as the second message we added to our database (second). We do this with Ecto.DateTime.compare/2. Keep in mind that comparing Ecto.DateTime structs with things like > or other comparison operators won't work as expected because they're treated as structs rather than normal values.

Our next assertion checks to make sure that the values we received are ordered properly. We expect that our most recent (by timestamp) message should be first, so we use compare/2 again to assert that msg1.timestamp is greater than (:gt) msg2.timestamp.

The last assertion, as mentioned earlier, tests to make sure that the default value for our limit is 10 if we do not specify anything.

test "query that returns latest messages of a given room" do
first = Ecto.DateTime.from_erl({{2016, 5, 23}, {12, 30, 12}})
second = Ecto.DateTime.from_erl({{2016, 5, 24}, {12, 30, 12}})
third = Ecto.DateTime.from_erl({{2016, 5, 25}, {12, 30, 12}})
Repo.insert!(%Message{room: "1", body: "test", timestamp: first})
Repo.insert!(%Message{room: "1", body: "test", timestamp: second})
Repo.insert!(%Message{room: "1", body: "test", timestamp: third})
Repo.insert!(%Message{room: "2", body: "test", timestamp: Ecto.DateTime.utc()})
messages = Message.latest_room_messages("1", 2) |> Repo.all
assert length(messages) == 2
[msg1 | [msg2]] = messages
assert Ecto.DateTime.compare(msg2.timestamp, second) == :eq
assert Ecto.DateTime.compare(msg1.timestamp, msg2.timestamp) == :gt
assert {10, :integer} in Message.latest_room_messages("1").limit.params
end

If you run your tests and you're getting an error along the lines of the code below, you are most likely running the wrong version of Ecto. You need to upgrade to Ecto 2.0.

3) test changeset with invalid attributes (PhoenixChat.MessageTest)
test/models/message_test.exs:14
Assertion with in failed
code: {:body, "can't be blank"} in errors
lhs: {:body, "can't be blank"}
rhs: [body: {"can't be blank", []}, timestamp: {"can't be blank", []},
room: {"can't be blank", []}]
stacktrace:
test/models/message_test.exs:16: (test)
Previous: Testing with Elixir and Phoenix: ChannelsNext: Associate Organizations and Users