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

Code before:
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_implicit_order_column_is_configurable_with_multiple_values
    old_implicit_order_column = Topic.implicit_order_column
    Topic.implicit_order_column = ["title", "author_name"]

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("topics.title"))} DESC, #{Regexp.escape(quote_table_name("topics.author_name"))} DESC, #{Regexp.escape(quote_table_name("topics.id"))} DESC LIMIT/i) {
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_ordering_does_not_append_primary_keys_or_query_constraints_if_passed_an_implicit_order_column_array_ending_in_nil
    old_implicit_order_column = Topic.implicit_order_column
    Topic.implicit_order_column = ["author_name", nil]

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("topics.author_name"))} DESC LIMIT/i) {
      Topic.last
    }

    assert_no_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("topics.author_name"))} #{Regexp.escape(quote_table_name("topics.id"))} DESC LIMIT/i) {
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_implicit_order_set_to_primary_key
    old_implicit_order_column = Topic.implicit_order_column
    Topic.implicit_order_column = "id"

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("topics.id"))} DESC LIMIT/i) {
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_implicit_order_for_model_without_primary_key
    old_implicit_order_column = NonPrimaryKey.implicit_order_column
    NonPrimaryKey.implicit_order_column = "created_at"

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("non_primary_keys.created_at"))} DESC LIMIT/i) {
      NonPrimaryKey.last
    }
  ensure

Review comment (bug):
This feels fragile (and I think it's currently broken / wouldn't fail on `main`).

Just the positive assertion above is probably sufficient... it's really hard to come up with a good anti-match pattern, `Topic.last` causing multiple separate order-carrying queries seems like Someone Else's Problem IMO.

Code after:
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_implicit_order_column_is_configurable_with_multiple_values
    old_implicit_order_column = Topic.implicit_order_column
    Topic.implicit_order_column = ["title", "author_name"]

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("topics.title"))} DESC, #{Regexp.escape(quote_table_name("topics.author_name"))} DESC, #{Regexp.escape(quote_table_name("topics.id"))} DESC LIMIT/i) {
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_ordering_does_not_append_primary_keys_or_query_constraints_if_passed_an_implicit_order_column_array_ending_in_nil
    old_implicit_order_column = Topic.implicit_order_column
    Topic.implicit_order_column = ["author_name", nil]

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("topics.author_name"))} DESC LIMIT/i) {
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_implicit_order_set_to_primary_key
    old_implicit_order_column = Topic.implicit_order_column
    Topic.implicit_order_column = "id"

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("topics.id"))} DESC LIMIT/i) {
      Topic.last
    }
  ensure
    Topic.implicit_order_column = old_implicit_order_column
  end

  def test_implicit_order_for_model_without_primary_key
    old_implicit_order_column = NonPrimaryKey.implicit_order_column
    NonPrimaryKey.implicit_order_column = "created_at"

    assert_queries_match(/ORDER BY #{Regexp.escape(quote_table_name("non_primary_keys.created_at"))} DESC LIMIT/i) {
      NonPrimaryKey.last
    }
  ensure
    NonPrimaryKey.implicit_order_column = old_implicit_order_column
  end

  def test_implicit_order_column_reorders_query_constraints