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

Code before:
      Edge.all.reverse_order
    end
  end

  def test_default_reverse_order_on_table_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_default_reverse_order_on_table_with_query_constraints
    ClothingItem.delete_all

    ClothingItem.create!(clothing_type: "shirt")
    ClothingItem.create!(clothing_type: "pant")
    ClothingItem.create!(clothing_type: "tie")

    assert_equal ["tie", "shirt", "pant"], ClothingItem.all.reverse_order.pluck(:clothing_type)
  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"
    assert_includes Topic.order(id: "desc").to_sql, "DESC"

Review comment (suggestion):
Without patch:

![Screenshot 2025-02-24 at 9 35 56 pm](https://github.com/user-attachments/assets/397109ba-8171-4a0c-9a60-0d468db442f1)

Code after:
    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"

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

  def test_raising_exception_on_invalid_hash_params
    e = assert_raise(ArgumentError) { Topic.order(:name, "id DESC", id: :asfsdf) }
    assert_equal 'Direction "asfsdf" is invalid. Valid directions are: [:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"]', e.message
  end

  def test_finding_last_with_arel_order
    topics = Topic.order(Topic.arel_table[:id].asc)