Repository: rails/rails
File: activerecord/test/cases/finder_test.rb (Ruby)

Code before:
    assert_nothing_raised do
      ordered_edge.all.first
    end
  end

  def first_with_at_least_query_constraints
    ordered_edge = Class.new(Edge) do
      query_constraints "source_id"
    end
    assert_nothing_raised do
      ordered_edge.all.first
    end
  end

  def test_last_without_order_columns
    assert_nil Edge.primary_key
    assert_nil Edge.implicit_order_column
    assert_nil Edge.query_constraints_list
    error = assert_raises(ActiveRecord::MissingRequiredOrderError) do
      Edge.all.last
    end
    assert_equal <<~MSG.squish, error.message
      Relation has no current order, and Edge has no order columns to use as a default.
      Set at least one of `implicit_order_column`, `query_constraints` or `primary_key` on
      the model when no `order `is specified on the relation.
    MSG
  end

  def test_last_with_at_least_primary_key
    ordered_edge = Class.new(Edge) do
      self.primary_key = "source_id"
    end
    assert_nothing_raised do
      ordered_edge.all.last
    end
  end

  def test_last_with_at_least_implict_order_column
    ordered_edge = Class.new(Edge) do
      self.implicit_order_column = "source_id"
    end
    assert_nothing_raised do
      ordered_edge.all.last
    end
  end

  def last_with_at_least_query_constraints
    ordered_edge = Class.new(Edge) do
      query_constraints "source_id"
    end
    assert_nothing_raised do

Review comment (suggestion):
I'm not sure it's worth matching the whole message. Something like `assert_match "Relation has no current order"` is enough and less things to change if we decide to reword the error message.

Code after:
  def test_first_with_at_least_primary_key
    ordered_edge = Class.new(Edge) do
      self.primary_key = "source_id"
    end
    assert_nothing_raised do
      ordered_edge.all.first
    end
  end

  def test_first_with_at_least_implict_order_column
    ordered_edge = Class.new(Edge) do
      self.implicit_order_column = "source_id"
    end
    assert_nothing_raised do
      ordered_edge.all.first
    end
  end

  def first_with_at_least_query_constraints
    ordered_edge = Class.new(Edge) do
      query_constraints "source_id"
    end
    assert_nothing_raised do
      ordered_edge.all.first
    end
  end

  def test_last_without_order_columns
    assert_nil Edge.primary_key
    assert_nil Edge.implicit_order_column
    assert_nil Edge.query_constraints_list
    error = assert_raises(ActiveRecord::MissingRequiredOrderError) do
      Edge.all.last
    end
    assert_match(/Relation has no order values/, error.message)
  end

  # TODO: Remove this test when we remove `raise_on_missing_required_finder_order_columns`
  def test_last_without_order_columns_and_raise_on_missing_required_finder_order_columns_disabled
    raise_on_missing_required_finder_order_columns_before = ActiveRecord.raise_on_missing_required_finder_order_columns
    ActiveRecord.raise_on_missing_required_finder_order_columns = false

    assert_nil Edge.primary_key
    assert_nil Edge.implicit_order_column
    assert_nil Edge.query_constraints_list
    error = assert_raises(ActiveRecord::IrreversibleOrderError) do
      assert_deprecated(/Calling order dependent finder methods/, ActiveRecord.deprecator) do
        Edge.all.last
      end
    end
    assert_match(/Relation has no order values/, error.message)