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

Code before:
    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.delete_all

    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"
    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

Review comment (suggestion):
Rather than to mutate the model, you can do an anonymous subclass of it:

```suggestion
  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
```

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)