<commit_before>require "bundler/setup"
Bundler.require

require "minitest/autorun"

class DefaultViewModel < ViewModel
  attributes :foo, :bar
end

class TestViewModel < ViewModel
  attributes :val
  def serialize_view(json, **options)
    json.name val
  end
end

class ViewModel::ActiveRecordTest < ActiveSupport::TestCase
  def test_serialize
    s = TestViewModel.new("a")
    assert_equal(TestViewModel.serialize_to_hash(s),
                 { "name" => "a" })
  end

  def test_default_serialize
    s = DefaultViewModel.new("a", 1)
    assert_equal(TestViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => 1 })
  end

  def test_default_serialize_array
    s = DefaultViewModel.new("a", [1,2])
    assert_equal(TestViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => [1,2] })
  end

  def test_default_serialize_hash
    s = DefaultViewModel.new("a", { "x" => "y" })
    assert_equal(DefaultViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => { "x" => "y" } })
  end

  def test_default_serialize_viewmodel
    s = DefaultViewModel.new("a", DefaultViewModel.new(1, 2))
    assert_equal(DefaultViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => { "foo" => 1, "bar" => 2 } })
  end

  def test_default_serialize_array_of_viewmodel
    s = DefaultViewModel.new("a", [TestViewModel.new("x"), TestViewModel.new("y")])
    assert_equal(DefaultViewModel.serialize_to_hash(s),<commit_msg>Add test for empty hash attribute<commit_after>require "bundler/setup"
Bundler.require

require "minitest/autorun"

class DefaultViewModel < ViewModel
  attributes :foo, :bar
end

class TestViewModel < ViewModel
  attributes :val
  def serialize_view(json, **_options)
    json.name val
  end
end

class ViewModel::ActiveRecordTest < ActiveSupport::TestCase
  def test_serialize
    s = TestViewModel.new("a")
    assert_equal(TestViewModel.serialize_to_hash(s),
                 { "name" => "a" })
  end

  def test_default_serialize
    s = DefaultViewModel.new("a", 1)
    assert_equal(TestViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => 1 })
  end

  def test_default_serialize_array
    s = DefaultViewModel.new("a", [1,2])
    assert_equal(TestViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => [1,2] })
  end

  def test_default_serialize_hash
    s = DefaultViewModel.new("a", { "x" => "y" })
    assert_equal(DefaultViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => { "x" => "y" } })
  end

  def test_default_serialize_empty_hash
    s = DefaultViewModel.new("a", {})
    assert_equal(DefaultViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => {} })
  end

  def test_default_serialize_viewmodel
    s = DefaultViewModel.new("a", DefaultViewModel.new(1, 2))
    assert_equal(DefaultViewModel.serialize_to_hash(s),
                 { "foo" => "a", "bar" => { "foo" => 1, "bar" => 2 } })
  end

  def test_default_serialize_array_of_viewmodel
    s = DefaultViewModel.new("a", [TestViewModel.new("x"), TestViewModel.new("y")])
    assert_equal(DefaultViewModel.serialize_to_hash(s),
{'fraction_non_alphanumeric': 0.14430379746835442, 'fraction_numerical': 0.006329113924050633, 'mean_word_length': 3.5028490028490027, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, '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': '6484819', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1036, 'n_words': 197}