Hi all,
Just trying out Ecto in a new project, and noticed something unexpected when testing an update method on my model.
The test for update fails intermittently, which I can only presume is due to a race condition between the update and the get, but I'm unsure why that would be, since I didn't think these functions would be operating asynchronously.
Can someone explain why this is happening? I tried to look for a way to get Ecto to log out the SQL it was executing, but couldn't find anything.
Any help would be appreciated.
Here's my test:
test "updating an existing post" do
{:ok, post} = Post.create
Post.update(post, "I went to the park")
post = Repo.get(Post, post.id)
assert post.text == "I went to the park"
end
This is the code for the model:
defmodule MyApp.Models.Post do
use Ecto.Model
alias MyApp.Repo
alias MyApp.Models.Post
schema "posts" do
field :text, :string
field :created_at, :datetime
field :updated_at, :datetime
end
def create(text \\ "") do
now = Ecto.DateTime.from_erl(:calendar.universal_time)
post = %Post{created_at: now, updated_at: now, text: text}
{:ok, Repo.insert(post)}
end
def update(post, text) do
post = %{post | text: text}
Repo.update(post)
end
end
With the following test: