<commit_before>require 'test_helper'
require 'active_model/serializer'

module ActiveModel
  class Serializer
    class HasOneTest < ActiveModel::TestCase
      def setup
        @user = User.new({ name: '<NAME>', email: '<EMAIL>', gender: 'M' })
        @user_serializer = UserSerializer.new(@user)
        @user_serializer.class._associations[0].include = false
        @user_serializer.class._associations[0].embed = :ids
      end

      def test_associations_definition
        associations = @user_serializer.class._associations

        assert_equal 1, associations.length
        assert_kind_of Association::HasOne, associations[0]
        assert_equal 'profile', associations[0].name
      end

      def test_associations_embedding_ids_serialization_using_serializable_hash
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id
        }, @user_serializer.serializable_hash)
      end

      def test_associations_embedding_ids_serialization_using_as_json
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id
        }, @user_serializer.as_json)
      end

      def test_associations_embedding_objects_serialization_using_serializable_hash
        @user_serializer.class._associations[0].embed = :objects
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.serializable_hash)
      end

      def test_associations_embedding_objects_serialization_using_as_json
        @user_serializer.class._associations[0].embed = :objects
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.as_json)
      end

      def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
        @user_serializer.class._associations[0].include = true
        assert_equal({
          'name' => 'Name 1', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.serializable_hash)
      end

      def test_associations_embedding_ids_including_objects_serialization_using_as_json
        @user_serializer.class._associations[0].include = true
        assert_equal({
          'name' => 'Name 1', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.as_json)
      end
    end
  end
end<commit_msg>Assign the association in the setup method<commit_after>require 'test_helper'
require 'active_model/serializer'

module ActiveModel
  class Serializer
    class HasOneTest < ActiveModel::TestCase
      def setup
        @user = User.new({ name: '<NAME>', email: '<EMAIL>', gender: 'M' })
        @user_serializer = UserSerializer.new(@user)
        @association = UserSerializer._associations[0]
        @association.include = false
        @association.embed = :ids
      end

      def test_associations_definition
        assert_equal 1, UserSerializer._associations.length
        assert_kind_of Association::HasOne, @association
        assert_equal 'profile', @association.name
      end

      def test_associations_embedding_ids_serialization_using_serializable_hash
        assert_equal({
          'name' => 'Name 1', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id
        }, @user_serializer.serializable_hash)
      end

      def test_associations_embedding_ids_serialization_using_as_json
        assert_equal({
          'name' => 'Name 1', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id
        }, @user_serializer.as_json)
      end

      def test_associations_embedding_objects_serialization_using_serializable_hash
        @association.embed = :objects
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.serializable_hash)
      end

      def test_associations_embedding_objects_serialization_using_as_json
        @association.embed = :objects
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.as_json)
      end

      def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
        @association.include = true
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.serializable_hash)
      end

      def test_associations_embedding_ids_including_objects_serialization_using_as_json
        @association.include = true
        assert_equal({
          'name' => '<NAME>', 'email' => '<EMAIL>', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
        }, @user_serializer.as_json)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13281402142161636, 'fraction_numerical': 0.006037000973709835, 'mean_word_length': 3.323232323232323, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 83, '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': '4146859', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1578, 'n_words': 279}