<commit_before># frozen_string_literal: true

require "cases/helper"
require "models/aircraft"
require "models/post"
require "models/comment"
require "models/author"
require "models/topic"
require "models/reply"
require "models/category"
require "models/company"
require "models/developer"
require "models/computer"
require "models/project"
require "models/minimalistic"
require "models/parrot"
require "models/minivan"
require "models/person"
require "models/ship"
require "models/admin"
require "models/admin/user"

class PersistenceTest < ActiveRecord::TestCase
  fixtures :topics, :companies, :developers, :accounts, :minimalistics, :authors, :author_addresses, :posts, :minivans

  def test_update_many
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
    updated = Topic.update(topic_data.keys, topic_data.values)

    assert_equal [1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_duplicated_ids
    updated = Topic.update([1, 1, 2], [
      { "content" => "1 duplicated" }, { "content" => "1 updated" }, { "content" => "2 updated" }
    ])

    assert_equal [1, 1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_invalid_id
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" }, 99999 => {} }

    assert_raise(ActiveRecord::RecordNotFound) do
      Topic.update(topic_data.keys, topic_data.values)
    end

    assert_not_equal "1 updated", Topic.find(1).content
    assert_not_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_active_record_base_object
    error = assert_raises(ArgumentError) do
      Topic.update(Topic.first, "content" => "1 updated")
    end

    assert_equal "You are passing an instance of ActiveRecord::Base to `update`. " \
    "Please pass the id of the object by calling `.id`.", error.message

    assert_not_equal "1 updated", Topic.first.content
  end

  def test_update_many_with_array_of_active_record_base_objects
    error = assert_raise(ArgumentError) do
      Topic.update(Topic.first(2), content: "updated")
    end

    assert_equal "You are passing an array of ActiveRecord::Base instances to `update`. " \
    "Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`.", error.message

    assert_not_equal "updated", Topic.first.content
    assert_not_equal "updated", Topic.second.content
  end

  def test_class_level_update_without_ids
    topics = Topic.all
    assert_equal 5, topics.length
    topics.each do |topic|
      assert_not_equal "updated", topic.content
    end

    updated = Topic.update(content: "updated")
    assert_equal 5, updated.length
    updated.each do |topic|
      assert_equal "updated", topic.content
    end
  end

  def test_class_level_update_is_affected_by_scoping
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }

    assert_raise(ActiveRecord::RecordNotFound) do
      Topic.where("1=0").scoping { Topic.update(topic_data.keys, topic_data.values) }
    end

    assert_not_equal "1 updated", Topic.find(1).content
    assert_not_equal "2 updated", Topic.find(2).content
  end

  def test_returns_object_even_if_validations_failed
    assert_equal Developer.all.to_a, Developer.update(salary: 1_000_000)
  end

  def test_update_many!
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
    updated = Topic.update!(topic_data.keys, topic_data.values)

    assert_equal [1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_duplicated_ids!
    updated = Topic.update!([1, 1, 2], [
      { "content" => "1 duplicated" }, { "content" => "1 updated" }, { "content" => "2 updated" }
    ])

    assert_equal [1, 1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_invalid_id!
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" }, 99999 => {} }

    assert_raise(ActiveRecord::RecordNotFound) do
      Topic.update!(topic_data.keys, topic_data.values)
    end

    assert_not_equal "1 updated", Topic.find(1).content
    assert_not_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_active_record_base_object!
    error = assert_raises(ArgumentError) do
      Topic.update!(Topic.first, "content" => "1 updated")
    end

    assert_equal "You are passing an instance of ActiveRecord::Base to `update`. " \
    "Please pass the id of the object by calling `.id`.", error.message

    assert_not_equal "1 updated", Topic.first.content
  end

  def test_update_many_with_array_of_active_record_base_objects!
    error = assert_raise(ArgumentError) do
      Topic.update!(Topic.first(2), content: "updated")
    end

    assert_equal "You are passing an array of ActiveRecord::Base instances to `update`. " \
    "Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`.", error.message

    assert_not_equal "updated", Topic.first.content
    assert_not_equal "updated", Topic.second.content<commit_msg>Fix the Active Record test suite following fcc83e47384b69<commit_after># frozen_string_literal: true

require "cases/helper"
require "models/aircraft"
require "models/post"
require "models/comment"
require "models/author"
require "models/topic"
require "models/reply"
require "models/category"
require "models/company"
require "models/developer"
require "models/computer"
require "models/project"
require "models/minimalistic"
require "models/parrot"
require "models/minivan"
require "models/person"
require "models/ship"
require "models/admin"
require "models/admin/user"

class PersistenceTest < ActiveRecord::TestCase
  fixtures :topics, :companies, :developers, :accounts, :minimalistics, :authors, :author_addresses, :posts, :minivans

  def test_update_many
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
    updated = Topic.update(topic_data.keys, topic_data.values)

    assert_equal [1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_duplicated_ids
    updated = Topic.update([1, 1, 2], [
      { "content" => "1 duplicated" }, { "content" => "1 updated" }, { "content" => "2 updated" }
    ])

    assert_equal [1, 1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_invalid_id
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" }, 99999 => {} }

    assert_raise(ActiveRecord::RecordNotFound) do
      Topic.update(topic_data.keys, topic_data.values)
    end

    assert_not_equal "1 updated", Topic.find(1).content
    assert_not_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_active_record_base_object
    error = assert_raises(ArgumentError) do
      Topic.update(Topic.first, "content" => "1 updated")
    end

    assert_equal "You are passing an instance of ActiveRecord::Base to `update`. " \
    "Please pass the id of the object by calling `.id`.", error.message

    assert_not_equal "1 updated", Topic.first.content
  end

  def test_update_many_with_array_of_active_record_base_objects
    error = assert_raise(ArgumentError) do
      Topic.update(Topic.first(2), content: "updated")
    end

    assert_equal "You are passing an array of ActiveRecord::Base instances to `update`. " \
    "Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`.", error.message

    assert_not_equal "updated", Topic.first.content
    assert_not_equal "updated", Topic.second.content
  end

  def test_class_level_update_without_ids
    topics = Topic.all
    assert_equal 5, topics.length
    topics.each do |topic|
      assert_not_equal "updated", topic.content
    end

    updated = Topic.update(content: "updated")
    assert_equal 5, updated.length
    updated.each do |topic|
      assert_equal "updated", topic.content
    end
  end

  def test_class_level_update_is_affected_by_scoping
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }

    assert_raise(ActiveRecord::RecordNotFound) do
      Topic.where("1=0").scoping { Topic.update(topic_data.keys, topic_data.values) }
    end

    assert_not_equal "1 updated", Topic.find(1).content
    assert_not_equal "2 updated", Topic.find(2).content
  end

  def test_returns_object_even_if_validations_failed
    assert_equal Developer.all.to_a, Developer.update(salary: 1_000_000)
  end

  def test_update_many!
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
    updated = Topic.update!(topic_data.keys, topic_data.values)

    assert_equal [1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_duplicated_ids!
    updated = Topic.update!([1, 1, 2], [
      { "content" => "1 duplicated" }, { "content" => "1 updated" }, { "content" => "2 updated" }
    ])

    assert_equal [1, 1, 2], updated.map(&:id)
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_invalid_id!
    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" }, 99999 => {} }

    assert_raise(ActiveRecord::RecordNotFound) do
      Topic.update!(topic_data.keys, topic_data.values)
    end

    assert_not_equal "1 updated", Topic.find(1).content
    assert_not_equal "2 updated", Topic.find(2).content
  end

  def test_update_many_with_active_record_base_object!
    error = assert_raises(ArgumentError) do
      Topic.update!(Topic.first, "content" => "1 updated")
    end

    assert_equal "You are passing an instance of ActiveRecord::Base to `update!`. " \
    "Please pass the id of the object by calling `.id`.", error.message

    assert_not_equal "1 updated", Topic.first.content
  end

  def test_update_many_with_array_of_active_record_base_objects!
    error = assert_raise(ArgumentError) do
      Topic.update!(Topic.first(2), content: "updated")
    end

    assert_equal "You are passing an array of ActiveRecord::Base instances to `update!`. " \
    "Please pass the ids of the objects by calling `pluck(:id)` or `map(&:id)`.", error.message

    assert_not_equal "updated", Topic.first.content
    assert_not_equal "updated", Topic.second.content
{'fraction_non_alphanumeric': 0.12608135468433646, 'fraction_numerical': 0.01868212773789803, 'mean_word_length': 4.063839701770736, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708916', 'n_tokens_mistral': 3674, 'n_tokens_neox': 3500, 'n_words': 866}