How can I test this function that queries my database?

Hello, I am new to elixir and ecto and working with a pretty large database. I am trying to modify this function that queries the db:

 def query_active_truck_load_counts do
      well in Well,
      where: is_nil(well.deleted_at),
      where: well.closed == false,
      left_join: truck_load in TruckLoad,
      on: == truck_load.well_id,
      left_join: fulfillment_authorization in assoc(truck_load, :fulfillment_authorization),
      left_join: dispatch_event in TruckLoadEvent,
      on: == dispatch_event.truck_load_id and is_nil(dispatch_event.deleted_at) and
          dispatch_event.status == ^TruckLoadEventStatuses.dispatch(),
      left_join: unloaded_event in TruckLoadEvent,
      on: == unloaded_event.truck_load_id and is_nil(unloaded_event.deleted_at) and
          unloaded_event.status == ^TruckLoadEventStatuses.unloaded(),
      left_join: rerouted_event in TruckLoadEvent,
      on: == rerouted_event.truck_load_id and is_nil(rerouted_event.deleted_at) and
          rerouted_event.status == ^TruckLoadEventStatuses.rerouted(),
      where: not (is_nil(fulfillment_authorization.accepted_at) and is_nil(,
      where: is_nil(,
      where: is_nil(,
      where: is_nil(truck_load.deleted_at),
      select: %{well_id:, active_truck_loads: count(}

My question is, how can I test the function? It is being called by different components on the front end, so I can test it that way, but is there a way I can console log and see different parts of the function?

Thanks!! and sorry if this is a dumb question

It depends what You mean by testing.

You can always print the resulting sql, with Repo.to_sql/2, or Ecto.Adapters.SQL.to_sql/3. For example…

Repo.to_sql(:all, your_query)

If You want to see values, You can pipe them into IO.inspect(label: “My value”), because it returns the given value.

if I pipe a value with IO.inspect(label: “My value”), I should expect to see the value on the front end when I open my console, correct? also thanks for the help!! I really appreciate it

It’s mainly for console debug… not really for frontend.

I’m not sure I understand…where can I see the values after I add the IO.inspect? Is there a way to run that function and see the result in the terminal where I shelled into the database?

You have a powerful console that allows to interact with running node.

If You start with

iex -S mix

# or for Phoenix

iex -S mix phx.server

You can access a console, where all your modules and public functions are available to test and run.

ah gotcha, thank you! one last dumb question: how do I run this function in the terminal, is there a specific syntax/ command to use? I was confused bcs I already did iex -S mix phx.server to start the server and that terminal is non stop running and showing new outputs, even when I don’t input anything into it

You have logs… but hiting enter will give You some prompt.

Most logs are from webpack.

I like to configure webpack with…

module.exports = (_env, options) => {
  const devMode = options.mode !== 'production';

  return {
    stats: "minimal",

stats: minimal does what it is meant to do, reduce logs to a minimum.

I also add a file .iex.exs, where I can setup aliases for the console.

ok, so once I have webpack configured correctly, I should be able to call the query_active_truck_load_counts function within the console and see the results? Is there a special syntax to calling it in the console?

Yes, it’s mfa… Module, Function, Argument.

All my contextes are aliased in, but if You don’t, You can call…


The console is not just nice to have, it’s a huge productivity booster.

And the observer shows You in real time the state of the server. From the console…

iex> :observer.start()

thank you for all this help!!