<commit_before>require "cases/helper"
require 'active_support/core_ext/object/instance_variables'

class SerializationTest < ActiveModel::TestCase
  class User
    include ActiveModel::Serialization

    attr_accessor :name, :email, :gender, :address, :friends

    def initialize(name, email, gender)
      @name, @email, @gender = name, email, gender
      @friends = []
    end

    def attributes
      instance_values.except("address", "friends")
    end

    def foo
      'i_am_foo'
    end
  end

  class Address
    include ActiveModel::Serialization

    attr_accessor :street, :city, :state, :zip

    def attributes
      instance_values
    end
  end

  setup do
    @user = User.new('David', '<EMAIL>', 'male')
    @user.address = Address.new
    @user.address.street = "123 Lane"
    @user.address.city = "Springfield"
    @user.address.state = "CA"
    @user.address.zip = 11111
    @user.friends = [User.new('Joe', '<EMAIL>', 'male'),
                     User.new('Sue', '<EMAIL>', 'female')]
  end

  def test_method_serializable_hash_should_work
    expected =  {"name"=>"David", "gender"=>"male", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash
  end

  def test_method_serializable_hash_should_work_with_only_option
    expected =  {"name"=>"David"}
    assert_equal expected , @user.serializable_hash(:only => [:name])
  end

  def test_method_serializable_hash_should_work_with_except_option
    expected =  {"gender"=>"male", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash(:except => [:name])
  end

  def test_method_serializable_hash_should_work_with_methods_option
    expected =  {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash(:methods => [:foo])
  end

  def test_method_serializable_hash_should_work_with_only_and_methods
    expected =  {:foo=>"i_am_foo"}
    assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo])
  end

  def test_method_serializable_hash_should_work_with_except_and_methods
    expected =  {"gender"=>"male", :foo=>"i_am_foo"}
    assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo])
  end

  def test_should_not_call_methods_that_dont_respond
    expected =  {"name"=>"David", "gender"=>"male", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash(:methods => [:bar])
  end

  def test_include_option_with_singular_association
    expected =  {"name"=>"David", "gender"=>"male", "email"=>"<EMAIL>",
                 :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}}
    assert_equal expected , @user.serializable_hash(:include => :address)
  end

  def test_include_option_with_plural_association
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :friends=>[{"name"=>'Joe', "email"=>'<EMAIL>', "gender"=>'male'},
                            {"name"=>'Sue', "email"=>'<EMAIL>', "gender"=>'female'}]}
    assert_equal expected , @user.serializable_hash(:include => :friends)
  end

  def test_include_option_with_empty_association
    @user.friends = []
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David", :friends=>[]}
    assert_equal expected , @user.serializable_hash(:include => :friends)
  end

  def test_multiple_includes
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111},
                 :friends=>[{"name"=>'Joe', "email"=>'<EMAIL>', "gender"=>'male'},
                            {"name"=>'Sue', "email"=>'<EMAIL>', "gender"=>'female'}]}
    assert_equal expected , @user.serializable_hash(:include => [:address, :friends])
  end

  def test_include_with_options
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :address=>{"street"=>"123 Lane"}}
    assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}})
  end

  def test_nested_include
    @user.friends.first.friends = [@user]
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :friends=>[{"name"=>'Joe', "email"=>'<EMAIL>', "gender"=>'male',
                             :friends => ["email"=>"<EMAIL>", "gender"=>"male", "name"=>"David"]},
                            {"name"=>'Sue', "email"=>'<EMAIL>', "gender"=>'female', :friends => []}]}
    assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}})
  end
end
<commit_msg>added more tests for only-include and except-include options in serialization<commit_after>require "cases/helper"
require 'active_support/core_ext/object/instance_variables'

class SerializationTest < ActiveModel::TestCase
  class User
    include ActiveModel::Serialization

    attr_accessor :name, :email, :gender, :address, :friends

    def initialize(name, email, gender)
      @name, @email, @gender = name, email, gender
      @friends = []
    end

    def attributes
      instance_values.except("address", "friends")
    end

    def foo
      'i_am_foo'
    end
  end

  class Address
    include ActiveModel::Serialization

    attr_accessor :street, :city, :state, :zip

    def attributes
      instance_values
    end
  end

  setup do
    @user = User.new('David', '<EMAIL>', 'male')
    @user.address = Address.new
    @user.address.street = "123 Lane"
    @user.address.city = "Springfield"
    @user.address.state = "CA"
    @user.address.zip = 11111
    @user.friends = [User.new('Joe', '<EMAIL>', 'male'),
                     User.new('Sue', '<EMAIL>', 'female')]
  end

  def test_method_serializable_hash_should_work
    expected =  {"name"=>"David", "gender"=>"male", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash
  end

  def test_method_serializable_hash_should_work_with_only_option
    expected =  {"name"=>"David"}
    assert_equal expected , @user.serializable_hash(:only => [:name])
  end

  def test_method_serializable_hash_should_work_with_except_option
    expected =  {"gender"=>"male", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash(:except => [:name])
  end

  def test_method_serializable_hash_should_work_with_methods_option
    expected =  {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash(:methods => [:foo])
  end

  def test_method_serializable_hash_should_work_with_only_and_methods
    expected =  {:foo=>"i_am_foo"}
    assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo])
  end

  def test_method_serializable_hash_should_work_with_except_and_methods
    expected =  {"gender"=>"male", :foo=>"i_am_foo"}
    assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo])
  end

  def test_should_not_call_methods_that_dont_respond
    expected =  {"name"=>"David", "gender"=>"male", "email"=>"<EMAIL>"}
    assert_equal expected , @user.serializable_hash(:methods => [:bar])
  end

  def test_include_option_with_singular_association
    expected =  {"name"=>"David", "gender"=>"male", "email"=>"<EMAIL>",
                 :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}}
    assert_equal expected , @user.serializable_hash(:include => :address)
  end

  def test_include_option_with_plural_association
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :friends=>[{"name"=>'Joe', "email"=>'<EMAIL>', "gender"=>'male'},
                            {"name"=>'Sue', "email"=>'<EMAIL>', "gender"=>'female'}]}
    assert_equal expected , @user.serializable_hash(:include => :friends)
  end

  def test_include_option_with_empty_association
    @user.friends = []
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David", :friends=>[]}
    assert_equal expected , @user.serializable_hash(:include => :friends)
  end

  def test_multiple_includes
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111},
                 :friends=>[{"name"=>'Joe', "email"=>'<EMAIL>', "gender"=>'male'},
                            {"name"=>'Sue', "email"=>'<EMAIL>', "gender"=>'female'}]}
    assert_equal expected , @user.serializable_hash(:include => [:address, :friends])
  end

  def test_include_with_options
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :address=>{"street"=>"123 Lane"}}
    assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}})
  end

  def test_nested_include
    @user.friends.first.friends = [@user]
    expected =  {"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David",
                 :friends=>[{"name"=>'Joe', "email"=>'<EMAIL>', "gender"=>'male',
                             :friends => [{"email"=>"<EMAIL>", "gender"=>"male", "name"=>"David"}]},
                            {"name"=>'Sue', "email"=>'<EMAIL>', "gender"=>'female', :friends => []}]}
    assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}})
  end

  def test_only_include
    expected = {"name"=>"David", :friends => [{"name" => "Joe"}, {"name" => "Sue"}]}
    assert_equal expected , @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}})
  end

  def test_except_include
    expected = {"name"=>"David", "email"=>"<EMAIL>",
                                :friends => [{"name" => 'Joe', "email" => '<EMAIL>'},
                                             {"name" => "Sue", "email" => '<EMAIL>'}]}
    assert_equal expected , @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}})
  end

end

{'fraction_non_alphanumeric': 0.1890832328106152, 'fraction_numerical': 0.005428226779252111, 'mean_word_length': 3.952215032354405, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 247, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2391600', 'n_tokens_mistral': 3430, 'n_tokens_neox': 3252, 'n_words': 578}