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

Code before:
      Topic.order("title nulls last").reverse_order
    end
    assert_raises(ActiveRecord::IrreversibleOrderError) do
      Topic.order("title NULLS FIRST, author_name").reverse_order
    end
    assert_raises(ActiveRecord::IrreversibleOrderError) do
      Topic.order("author_name, title nulls last").reverse_order
    end
  end if current_adapter?(:PostgreSQLAdapter)

  def test_default_reverse_order_on_table_without_primary_key
    assert_raises(ActiveRecord::IrreversibleOrderError) do
      Edge.all.reverse_order
    end
  end

  def test_default_reverse_order_on_table_without_primary_key_with_implicit_order_column
    implicit_order_column_before = Edge.implicit_order_column
    Edge.implicit_order_column = "source_id"

    edge_1 = Edge.create!(source_id: 1, sink_id: 2)
    edge_2 = Edge.create!(source_id: 2, sink_id: 3)
    assert_equal edge_1.source_id, Edge.all.first.source_id
    assert_equal edge_2.source_id, Edge.all.reverse_order.first.source_id
  ensure
    Edge.implicit_order_column = implicit_order_column_before
  end

  def test_order_with_hash_and_symbol_generates_the_same_sql
    assert_equal Topic.order(:id).to_sql, Topic.order(id: :asc).to_sql
  end

  def test_finding_with_desc_order_with_string
    topics = Topic.order(id: "desc")
    assert_equal 5, topics.to_a.size
    assert_equal [topics(:fifth), topics(:fourth), topics(:third), topics(:second), topics(:first)], topics.to_a
  end

  def test_finding_with_asc_order_with_string
    topics = Topic.order(id: "asc")
    assert_equal 5, topics.to_a.size
    assert_equal [topics(:first), topics(:second), topics(:third), topics(:fourth), topics(:fifth)], topics.to_a
  end

  def test_support_upper_and_lower_case_directions
    assert_includes Topic.order(id: "ASC").to_sql, "ASC"
    assert_includes Topic.order(id: "asc").to_sql, "ASC"
    assert_includes Topic.order(id: :ASC).to_sql, "ASC"
    assert_includes Topic.order(id: :asc).to_sql, "ASC"

    assert_includes Topic.order(id: "DESC").to_sql, "DESC"

Review comment (suggestion):
Without patch:

![Screenshot 2025-02-24 at 8 56 51 pm](https://github.com/user-attachments/assets/ec7a3245-38c4-4692-9824-4b4a01fe9a68)


Code after:
      Topic.order("title nulls last").reverse_order
    end
    assert_raises(ActiveRecord::IrreversibleOrderError) do
      Topic.order("title NULLS FIRST, author_name").reverse_order
    end
    assert_raises(ActiveRecord::IrreversibleOrderError) do
      Topic.order("author_name, title nulls last").reverse_order
    end
  end if current_adapter?(:PostgreSQLAdapter)

  def test_default_reverse_order_on_table_without_primary_key
    assert_raises(ActiveRecord::IrreversibleOrderError) do
      Edge.all.reverse_order
    end
  end

  def test_default_reverse_order_on_table_without_primary_key_with_implicit_order_column
    ordered_edge = Class.new(Edge) do
      self.implicit_order_column = "source_id"
    end

    ordered_edge.delete_all

    edge_1 = ordered_edge.create!(source_id: 1, sink_id: 2)
    edge_2 = ordered_edge.create!(source_id: 2, sink_id: 3)
    assert_equal edge_1.source_id, ordered_edge.all.first.source_id
    assert_equal edge_2.source_id, ordered_edge.all.reverse_order.first.source_id
  end

  def test_order_with_hash_and_symbol_generates_the_same_sql
    assert_equal Topic.order(:id).to_sql, Topic.order(id: :asc).to_sql
  end

  def test_finding_with_desc_order_with_string
    topics = Topic.order(id: "desc")
    assert_equal 5, topics.to_a.size
    assert_equal [topics(:fifth), topics(:fourth), topics(:third), topics(:second), topics(:first)], topics.to_a
  end

  def test_finding_with_asc_order_with_string
    topics = Topic.order(id: "asc")
    assert_equal 5, topics.to_a.size
    assert_equal [topics(:first), topics(:second), topics(:third), topics(:fourth), topics(:fifth)], topics.to_a
  end

  def test_support_upper_and_lower_case_directions
    assert_includes Topic.order(id: "ASC").to_sql, "ASC"
    assert_includes Topic.order(id: "asc").to_sql, "ASC"
    assert_includes Topic.order(id: :ASC).to_sql, "ASC"
    assert_includes Topic.order(id: :asc).to_sql, "ASC"