Reputation: 358
I am trying to test a view created in Postgres, but it is returning an empty result set. However, when testing out the view in an Elixir interactive shell, I get back the expected data. Here are the steps I have taken to create and test the view:
def up do
execute """
CREATE VIEW example_view AS
...
import Ecto.Changeset
schema "test_view" do
field(:user_id, :string)
describe "example loads" do
setup [
:with_example_data
]
test "view" do
query = from(ev in Schema.ExampleView)
IO.inspect Repo.all(query)
end
end
The response back is an empty array []
Is there a setting that I am missing to allow for views to be tested in test?
Upvotes: 0
Views: 449
Reputation: 3204
As pointed out in one of the comments:
mix phx.server
... run on the :dev
environment and the dev DB:test
environment and runs on a separate DBIt actually makes a lot of sense because you want your test suite to be reproducible and independent of whatever records that you might create/edit in your dev env.
You can open iex in the :test
environment to confirm that your query returns the empty array here too:
MIX_ENV=test iex -S mix
What you'll need is to populate your test DB with some known records before querying. There are at least 2 ways to achieve that: fixtures and seeds.
test/support/test_helpers.ex
(typically: takes some attrs
, adds some defaults and calls some create_
function from your context) def foo_fixture(attrs \\ %{}) do
{:ok, foo} =
attrs
|> Enum.into(%{name: "foo", bar: " default bar"})
|> MyContext.create_foo()
foo
end
setup
function or test case before queryingDataCase
, each test is wrapped in its own transaction and any fixture that you created will be rollback-ed at the end of the test, so tests are isolated and independent from each other.priv/repo/seeds.exs
.Repo.insert!/1
)mix ecto.setup
or mix ecto.reset
just after your migrations (whatever the env used)seeds.exs
, you can run the following:# reset dev DB
mix ecto.reset
# reset test DB
MIX_ENV=test mix ecto.reset
dev_seeds.exs
) and modify your mix.exs
to configure ecto.setup
.I usually find myself using a mix of both approaches.
Upvotes: 1