<commit_before>require "cases/helper"
require "models/post"
require "models/comment"
require "models/author"
require "models/rating"

module ActiveRecord
  class RelationTest < ActiveRecord::TestCase
    fixtures :posts, :comments, :authors, :author_addresses, :ratings

    def test_construction
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal FakeKlass, relation.klass
      assert_equal :b, relation.table
      assert !relation.loaded, "relation is not loaded"
    end

    def test_responds_to_model_and_returns_klass
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal FakeKlass, relation.model
    end

    def test_initialize_single_values
      relation = Relation.new(FakeKlass, :b, nil)
      (Relation::SINGLE_VALUE_METHODS - [:create_with, :readonly]).each do |method|
        assert_nil relation.send("#{method}_value"), method.to_s
      end
      assert_equal false, relation.readonly_value
      value = relation.create_with_value
      assert_equal({}, value)
      assert_predicate value, :frozen?
    end

    def test_multi_value_initialize
      relation = Relation.new(FakeKlass, :b, nil)
      Relation::MULTI_VALUE_METHODS.each do |method|
        values = relation.send("#{method}_values")
        assert_equal [], values, method.to_s
        assert_predicate values, :frozen?, method.to_s
      end
    end

    def test_extensions
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal [], relation.extensions
    end

    def test_empty_where_values_hash
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal({}, relation.where_values_hash)
    end

    def test_has_values
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      relation.where! relation.table[:id].eq(10)
      assert_equal({ id: 10 }, relation.where_values_hash)
    end

    def test_values_wrong_table
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      relation.where! Comment.arel_table[:id].eq(10)
      assert_equal({}, relation.where_values_hash)
    end

    def test_tree_is_not_traversed
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      left     = relation.table[:id].eq(10)
      right    = relation.table[:id].eq(10)
      combine  = left.and right
      relation.where! combine
      assert_equal({}, relation.where_values_hash)
    end

    def test_table_name_delegates_to_klass
      relation = Relation.new(FakeKlass, :b, Post.predicate_builder)
      assert_equal "posts", relation.table_name
    end

    def test_scope_for_create
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal({}, relation.scope_for_create)
    end

    def test_create_with_value
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      hash = { hello: "world" }
      relation.create_with_value = hash
      assert_equal hash, relation.scope_for_create
    end

    def test_create_with_value_with_wheres
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      relation.where! relation.table[:id].eq(10)
      relation.create_with_value = { hello: "world" }
      assert_equal({ hello: "world", id: 10 }, relation.scope_for_create)
    end

    # FIXME: is this really wanted or expected behavior?
    def test_scope_for_create_is_cached
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      assert_equal({}, relation.scope_for_create)

      relation.where! relation.table[:id].eq(10)
      assert_equal({}, relation.scope_for_create)

      relation.create_with_value = { hello: "world" }
      assert_equal({}, relation.scope_for_create)
    end

    def test_bad_constants_raise_errors
      assert_raises(NameError) do
        ActiveRecord::Relation::HelloWorld
      end
    end

    def test_empty_eager_loading?
      relation = Relation.new(FakeKlass, :b, nil)
      assert !relation.eager_loading?
    end

    def test_eager_load_values
      relation = Relation.new(FakeKlass, :b, nil)
      relation.eager_load! :b<commit_msg>Use `where(id: 10)` rather than `where(relation.table[:id].eq(10))`<commit_after>require "cases/helper"
require "models/post"
require "models/comment"
require "models/author"
require "models/rating"

module ActiveRecord
  class RelationTest < ActiveRecord::TestCase
    fixtures :posts, :comments, :authors, :author_addresses, :ratings

    def test_construction
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal FakeKlass, relation.klass
      assert_equal :b, relation.table
      assert !relation.loaded, "relation is not loaded"
    end

    def test_responds_to_model_and_returns_klass
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal FakeKlass, relation.model
    end

    def test_initialize_single_values
      relation = Relation.new(FakeKlass, :b, nil)
      (Relation::SINGLE_VALUE_METHODS - [:create_with, :readonly]).each do |method|
        assert_nil relation.send("#{method}_value"), method.to_s
      end
      assert_equal false, relation.readonly_value
      value = relation.create_with_value
      assert_equal({}, value)
      assert_predicate value, :frozen?
    end

    def test_multi_value_initialize
      relation = Relation.new(FakeKlass, :b, nil)
      Relation::MULTI_VALUE_METHODS.each do |method|
        values = relation.send("#{method}_values")
        assert_equal [], values, method.to_s
        assert_predicate values, :frozen?, method.to_s
      end
    end

    def test_extensions
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal [], relation.extensions
    end

    def test_empty_where_values_hash
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal({}, relation.where_values_hash)
    end

    def test_has_values
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      relation.where!(id: 10)
      assert_equal({ "id" => 10 }, relation.where_values_hash)
    end

    def test_values_wrong_table
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      relation.where! Comment.arel_table[:id].eq(10)
      assert_equal({}, relation.where_values_hash)
    end

    def test_tree_is_not_traversed
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      left     = relation.table[:id].eq(10)
      right    = relation.table[:id].eq(10)
      combine  = left.and right
      relation.where! combine
      assert_equal({}, relation.where_values_hash)
    end

    def test_table_name_delegates_to_klass
      relation = Relation.new(FakeKlass, :b, Post.predicate_builder)
      assert_equal "posts", relation.table_name
    end

    def test_scope_for_create
      relation = Relation.new(FakeKlass, :b, nil)
      assert_equal({}, relation.scope_for_create)
    end

    def test_create_with_value
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      hash = { hello: "world" }
      relation.create_with_value = hash
      assert_equal hash, relation.scope_for_create
    end

    def test_create_with_value_with_wheres
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      relation.where!(id: 10)
      relation.create_with_value = { hello: "world" }
      assert_equal({ hello: "world", "id" => 10 }, relation.scope_for_create)
    end

    # FIXME: is this really wanted or expected behavior?
    def test_scope_for_create_is_cached
      relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
      assert_equal({}, relation.scope_for_create)

      relation.where!(id: 10)
      assert_equal({}, relation.scope_for_create)

      relation.create_with_value = { hello: "world" }
      assert_equal({}, relation.scope_for_create)
    end

    def test_bad_constants_raise_errors
      assert_raises(NameError) do
        ActiveRecord::Relation::HelloWorld
      end
    end

    def test_empty_eager_loading?
      relation = Relation.new(FakeKlass, :b, nil)
      assert !relation.eager_loading?
    end

    def test_eager_load_values
      relation = Relation.new(FakeKlass, :b, nil)
      relation.eager_load! :b
{'fraction_non_alphanumeric': 0.09507515581082733, 'fraction_numerical': 0.004399364536233655, 'mean_word_length': 3.4190064794816415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3506291', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2556, 'n_words': 520}