<commit_before>require "test_helper"

class ActiveRecordTest < TraverTest
  attr_reader :subject
  
  def setup
    super
    
    @subject = TraverConstructor.new
  end
  
  def teardown
    super
    
    subject.undefine_all_factories
  end
  
  def test_create_object
    define_model("Blog")

    blog = subject.create(:blog)

    assert_equal true, blog.persisted?
  end

  def test_create_object_with_attributes
    define_model("Blog", title: :string)

    blog = subject.create(blog: { title: "Hello" })

    assert_equal "Hello", blog.title
    assert_equal true,    blog.persisted?
  end

  def test_create_object_using_factory
    define_model("Post", title: :string)
    subject.define_factory(:post, { title: "Hello" })

    post = subject.create(:post)

    assert_equal "Hello", post.title
    assert_equal true,    post.persisted?
  end

  def test_create_object_using_child_factory
    define_model("Post", title: :string, published: :boolean)

    subject.define_factory(:post, { title: "Hello" })
    subject.define_factory(:published_post, :post, { published: true })

    post = subject.create(:published_post)

    assert_equal "Hello", post.title
    assert_equal true,    post.published
    
    assert_equal true,    post.persisted?
  end

  def test_create_associated_object
    define_model("User", name: :string)

    define_model("Blog", user_id: :integer, title: :string) do
      belongs_to :user
    end

    blog = subject.create(blog: {
      title: "Hello",
      user: { name: "Mike" }
    })

    assert_equal "Hello", blog.title
    assert_equal true,    blog.persisted?
    
    assert_equal "Mike",  blog.user.name
    assert_equal true,    blog.user.persisted?
  end

  def test_create_associated_collection
    define_model("Blog", title: :string) do
      has_many :posts
    end

    define_model("Post", blog_id: :integer, title: :string) do
      belongs_to :blog
    end

    blog = subject.create(blog: {
      title: "Hello",
      posts: [
        { title: "Post #1" },
        { title: "Post #2" }
      ]
    })

    assert_equal "Hello", blog.title
    assert_equal true,    blog.persisted?

    assert_equal "Post #1", blog.posts.first.title
    assert_equal true,      blog.posts.first.persisted?

    assert_equal "Post #2", blog.posts.last.title
    assert_equal true,      blog.posts.last.persisted?
  end

  def test_create_collection_using_digit
    define_model("Blog") do
      has_many :posts
    end

    define_model("Post", blog_id: :integer) do
      belongs_to :blog
    end
  
    blog = subject.create(blog: {
      posts: 2
    })
  
    assert_equal 2, blog.posts.count
  end
  
  def test_any_level_of_nesting
    define_model("Blog", title: :string) do
      has_many :posts
    end
     
    define_model("Post", blog_id: :integer, title: :string) do
      belongs_to :blog
      has_many :tags
    end
     
    define_model("Tag", post_id: :integer, name: :string) do
      belongs_to :post
    end
     
    blog = subject.create(blog: {
      title: "Blog",
      posts: [{
        title: "Hello",
        tags: [{ name: "Tag" }]
      }]
    })
     
    assert_equal "Blog", blog.title
    assert_equal true,   blog.persisted?
     <commit_msg>Tiny one<commit_after>require "test_helper"

class ActiveRecordTest < TraverTest
  attr_reader :subject
  
  def setup
    super
    
    @subject = TraverConstructor.new
  end
  
  def teardown
    super
    
    subject.undefine_all_factories
  end
  
  def test_create_object
    define_model("Blog")

    blog = subject.create(:blog)

    assert_equal true, blog.persisted?
  end

  def test_create_object_with_attributes
    define_model("Blog", title: :string)

    blog = subject.create(blog: { title: "Hello" })

    assert_equal "Hello", blog.title
    assert_equal true,    blog.persisted?
  end

  def test_create_object_using_factory
    define_model("Post", title: :string)
    subject.define_factory(:post, { title: "Hello" })

    post = subject.create(:post)

    assert_equal "Hello", post.title
    assert_equal true,    post.persisted?
  end

  def test_create_object_using_child_factory
    define_model("Post", title: :string, published: :boolean)

    subject.define_factory(:post, { title: "Hello" })
    subject.define_factory(:published_post, :post, { published: true })

    post = subject.create(:published_post)

    assert_equal "Hello", post.title
    assert_equal true,    post.published
    
    assert_equal true,    post.persisted?
  end

  def test_create_associated_object
    define_model("User", name: :string)

    define_model("Blog", user_id: :integer, title: :string) do
      belongs_to :user
    end

    blog = subject.create(blog: {
      title: "Hello",
      user: { name: "Mike" }
    })

    assert_equal "Hello", blog.title
    assert_equal true,    blog.persisted?
    
    assert_equal "Mike",  blog.user.name
    assert_equal true,    blog.user.persisted?
  end

  def test_create_associated_collection
    define_model("Blog", title: :string) do
      has_many :posts
    end

    define_model("Post", blog_id: :integer, title: :string) do
      belongs_to :blog
    end

    blog = subject.create(blog: {
      title: "Hello",
      posts: [
        { title: "Post #1" },
        { title: "Post #2" }
      ]
    })

    assert_equal "Hello", blog.title
    assert_equal true,    blog.persisted?

    assert_equal "Post #1", blog.posts.first.title
    assert_equal true,      blog.posts.first.persisted?

    assert_equal "Post #2", blog.posts.last.title
    assert_equal true,      blog.posts.last.persisted?
  end

  def test_create_collection_using_digit
    define_model("Blog") do
      has_many :posts
    end

    define_model("Post", blog_id: :integer) do
      belongs_to :blog
    end
  
    blog = subject.create(blog: {
      posts: 2
    })
  
    assert_equal 2, blog.posts.length
  end
  
  def test_any_level_of_nesting
    define_model("Blog", title: :string) do
      has_many :posts
    end
     
    define_model("Post", blog_id: :integer, title: :string) do
      belongs_to :blog
      has_many :tags
    end
     
    define_model("Tag", post_id: :integer, name: :string) do
      belongs_to :post
    end
     
    blog = subject.create(blog: {
      title: "Blog",
      posts: [{
        title: "Hello",
        tags: [{ name: "Tag" }]
      }]
    })
     
    assert_equal "Blog", blog.title
    assert_equal true,   blog.persisted?
     
{'fraction_non_alphanumeric': 0.10483870967741936, 'fraction_numerical': 0.0018610421836228288, 'mean_word_length': 2.7020665901262917, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6187164', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2080, 'n_words': 475}