I'm trying to unit test a raw sql query but not getting any results. My test does a number of inserts like:
Repo.insert!(%Inquiry{<fields>})
My inquiries select part does truncating of a datetime to a date and some other stuff which is why I have it setup as a raw inquiry but in attempting to get this to work, I've just simplified it to something like the following:
query = "SELECT * FROM inquiries"
results = Ecto.Adapters.SQL.query(Repo, query, [])
In my test this does not return any rows but a command such as the following does:
Repo.all(Inquiry)
I've tried to setup to share the connection but it doesn't work. Any secret to getting this to work?
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end
You can issue raw SQL queries through the database adapter (most commonly
Ecto.Adapters.SQLfor PostGreSQL). For example:If you don't have any positional bind variables (e.g.
$1), the 3rd argument can be an empty list[].To test these queries, you should be able to execute them manually in your SQL editor.
Note that the shape of the result is different than what you would get via regular Ecto query. Sample results might look like