Fix broken test

Hi all, I’m new to elixir and modified a function in a large code base, and as a result a bunch of tests that were previously passing are now failing, and I have to update those tests.

Here’s one of those failing tests:

    test "renders the correct count of active truckloads for a given `%Well{}`",
         %{well: well} do
      _well_truck_load_target = insert(:well_truck_load_target, well: well)
      well = preload_well_truck_load_target(well)

      # Insert some "active" truckloads...
      1..5 |> Enum.to_list() |> Enum.each(fn _number -> insert_active_truck_load(well) end)
      # Insert some truckloads we should disregard...
      1..3 |> Enum.to_list() |> Enum.each(fn _number -> insert_completed_truck_load(well) end)

      counts = Wells.list_active_truck_load_counts()
      html = well |> render_active_truck_loads_badge(counts) |> HTML.safe_to_string()
      assert html =~ "data-stat-content=\"5\""
      refute html =~ "data-stat-content=\"8\""
    end

The issue here is because of list_active_truck_load_counts():

  def list_active_truck_load_counts() do
    Repo.all(query_active_truck_load_counts())
  end

list_active_truck_load_counts() calls this function:

  def query_active_truck_load_counts do
    select(
      query_active_truck_loads(),
      [fulfillment: fulfillment],
      %{well_id: fulfillment.well_id, active_truck_loads: count(fulfillment.truck_load_id)}
    )
  end

Which calls the function I modified, query_active_truck_load_counts:

  def query_active_truck_loads() do
    from(
      fulfillment in Fulfillment,
      as: :fulfillment,
      left_join: trip_breaks in subquery(active_breaks_subquery()),
      on: trip_breaks.truck_load_id == fulfillment.truck_load_id,
      where: is_nil(trip_breaks.id),
      where: is_nil(fulfillment.ended_at),
      group_by: fulfillment.well_id
    )
  end

This is what the failing test says:

1) test TSSWeb.DispatchHome.View.render_active_truck_loads_badge/2 renders the correct count of active truckloads for a given `%Well{}` (TSSWeb.DispatchHome.View.Test)
     test/tss_web/views/dispatch_home_view_test.exs:212
     Assertion with =~ failed
     code:  assert html =~ "data-stat-content=\"5\""
     left:  "<div class=\"WellHealth-badgeContainer\">\n  <a class=\"rev-Badge--circle WellHealth-badge  WellHealth-badge--withNotification\n    \" color=\"#FFCA36\" data-stat-content=\"0\" data-testid=\"active-trucks-badge\" href=\"/wells/5600/dispatch_home?landing=kanban\" stat-content=\"0\" style=\"background-color: #FFCA36\" title=\"Active truckloads\">\n  <i data-feather=\"truck\" style=\"color: #000000\"></i>\n</a>\n\n  \n</div>\n"
     right: "data-stat-content=\"5\""
     stacktrace:
       test/tss_web/views/dispatch_home_view_test.exs:224: (test)

I’m not really sure how to proceed here…any help would be extremely appreciated!!
Thanks!

The result in html has data-stat-content=\"0\", which disagrees with the assertion.

My guess is that something in your change to query_active_truck_loads makes it now return zero results.

3 Likes

looking into this now, thank you!