<commit_before>require 'test_helper'

class BulkUploadTest < ActiveSupport::TestCase

  def fixture_file(filename)
    File.open(File.join(Rails.root, 'test', 'fixtures', filename))
  end

  def attachments_params(*pairs)
    {}.tap do |params|
      pairs.each_with_index do |pair, i|
        attachment, data = pair
        params[i.to_s] = attachment.merge(attachment_data_attributes: data)
      end
    end
  end

  # Parameters suitable for posting to #create for attachments with new filenames
  def new_attachments_params
    attachments_params(
      [{ title: 'Title 1' }, { file: fixture_file('whitepaper.pdf') }],
      [{ title: 'Title 2' }, { file: fixture_file('greenpaper.pdf') }]
    )
  end

  def invalid_new_attachments_params
    new_attachments_params.tap { |params| params['0'][:title] = '' }
  end

  test '.from_files builds Attachment instances for an array of file paths' do
    paths = %w(greenpaper.pdf whitepaper.pdf).map { |f| fixture_file(f).path }
    bulk_upload = BulkUpload.from_files(create(:news_article), paths)
    assert_equal 2, bulk_upload.attachments.size
    assert_equal 'greenpaper.pdf', bulk_upload.attachments[0].filename
    assert_equal 'whitepaper.pdf', bulk_upload.attachments[1].filename
  end

  test '.from_files loads attachments from the edition if filenames match' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    paths = ['whitepaper.pdf', existing.filename].map { |name| fixture_file(name).path }
    bulk_upload = BulkUpload.from_files(edition, paths)
    assert bulk_upload.attachments.first.new_record?, 'Attachment should be new record'
    refute bulk_upload.attachments.last.new_record?, "Attachment shouldn't be new record"
  end

  test '.from_files always builds new AttachmentData instances' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    paths = ['whitepaper.pdf', existing.filename].map { |name| fixture_file(name).path }
    bulk_upload = BulkUpload.from_files(edition, paths)
    assert bulk_upload.attachments.all? { |a| a.attachment_data.new_record? }
  end

  test '.from_files sets replaced_by on existing AttachmentData when file re-attached' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    paths = ['whitepaper.pdf', existing.filename].map { |name| fixture_file(name).path }
    bulk_upload = BulkUpload.from_files(edition, paths)
    new_attachment_data = bulk_upload.attachments.last.attachment_data
    new_attachment_data.save!
    assert_equal new_attachment_data, existing.attachment_data.reload.replaced_by
  end

  test '#attachments_attributes builds new AttachmentData when file attached' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    bulk_upload = BulkUpload.new(edition)
    bulk_upload.attachments_attributes = attachments_params(
      [{ id: existing.id, title: 'Title' }, {}]
    )
    attachment = bulk_upload.attachments.first
    assert attachment.attachment_data.new_record?, 'AttachmentData should be new record'
  end

  test '#attachments_attributes sets replaced_by on existing AttachmentData when file re-attached' do<commit_msg>Tweak BulkUpload unit test descriptions<commit_after>require 'test_helper'

class BulkUploadTest < ActiveSupport::TestCase

  def fixture_file(filename)
    File.open(File.join(Rails.root, 'test', 'fixtures', filename))
  end

  def attachments_params(*pairs)
    {}.tap do |params|
      pairs.each_with_index do |pair, i|
        attachment, data = pair
        params[i.to_s] = attachment.merge(attachment_data_attributes: data)
      end
    end
  end

  # Parameters suitable for posting to #create for attachments with new filenames
  def new_attachments_params
    attachments_params(
      [{ title: 'Title 1' }, { file: fixture_file('whitepaper.pdf') }],
      [{ title: 'Title 2' }, { file: fixture_file('greenpaper.pdf') }]
    )
  end

  def invalid_new_attachments_params
    new_attachments_params.tap { |params| params['0'][:title] = '' }
  end

  test '.from_files builds Attachment instances for an array of file paths' do
    paths = %w(greenpaper.pdf whitepaper.pdf).map { |f| fixture_file(f).path }
    bulk_upload = BulkUpload.from_files(create(:news_article), paths)
    assert_equal 2, bulk_upload.attachments.size
    assert_equal 'greenpaper.pdf', bulk_upload.attachments[0].filename
    assert_equal 'whitepaper.pdf', bulk_upload.attachments[1].filename
  end

  test '.from_files loads attachments from the edition if filenames match' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    paths = ['whitepaper.pdf', existing.filename].map { |name| fixture_file(name).path }
    bulk_upload = BulkUpload.from_files(edition, paths)
    assert bulk_upload.attachments.first.new_record?, 'Attachment should be new record'
    refute bulk_upload.attachments.last.new_record?, "Attachment shouldn't be new record"
  end

  test '.from_files always builds new AttachmentData instances' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    paths = ['whitepaper.pdf', existing.filename].map { |name| fixture_file(name).path }
    bulk_upload = BulkUpload.from_files(edition, paths)
    assert bulk_upload.attachments.all? { |a| a.attachment_data.new_record? }
  end

  test '.from_files sets replaced_by on existing AttachmentData when file re-attached' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    paths = ['whitepaper.pdf', existing.filename].map { |name| fixture_file(name).path }
    bulk_upload = BulkUpload.from_files(edition, paths)
    new_attachment_data = bulk_upload.attachments.last.attachment_data
    new_attachment_data.save!
    assert_equal new_attachment_data, existing.attachment_data.reload.replaced_by
  end

  test '#attachments_attributes= builds new AttachmentData when existing file re-attached' do
    edition = create(:news_article, :with_attachment)
    existing = edition.attachments.first
    bulk_upload = BulkUpload.new(edition)
    bulk_upload.attachments_attributes = attachments_params(
      [{ id: existing.id, title: 'Title' }, {}]
    )
    attachment = bulk_upload.attachments.first
    assert attachment.attachment_data.new_record?, 'AttachmentData should be new record'
  end

  test '#attachments_attributes= sets replaced_by on existing AttachmentData when file re-attached' do
{'fraction_non_alphanumeric': 0.09914451573479988, 'fraction_numerical': 0.0018331805682859762, 'mean_word_length': 4.451290591174022, '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': '6061450', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1969, 'n_words': 464}