<commit_before>require "test_helper"

class PublicationTest < ActiveSupport::TestCase

  test "should be valid when built from the factory" do
    publication = build(:publication)
    assert publication.valid?
  end

  test 'should be invalid without a publication date' do
    publication = build(:publication, publication_date: nil)
    refute publication.valid?
  end

  test 'should be valid without ISBN' do
    publication = build(:publication, isbn: nil)
    assert publication.valid?
  end

  test 'should be valid with blank ISBN' do
    publication = build(:publication, isbn: "")
    assert publication.valid?
  end

  test 'should be invalid with ISBN but not in ISBN-10 or ISBN-13 format' do
    publication = build(:publication, isbn: "invalid-isbn")
    refute publication.valid?
  end

  test 'should be valid with ISBN in ISBN-10 format' do
    publication = build(:publication, isbn: "0261102737")
    assert publication.valid?
  end

  test 'should be valid with ISBN in ISBN-13 format' do
    publication = build(:publication, isbn: "978-0261103207")
    assert publication.valid?
  end

  test 'should be invalid with malformed order url' do
    publication = build(:publication, order_url: "invalid-url")
    refute publication.valid?
  end

  test 'should be valid with order url with HTTP protocol' do
    publication = build(:publication, order_url: "http://example.com")
    assert publication.valid?
  end

  test 'should be valid with order url with HTTPS protocol' do
    publication = build(:publication, order_url: "https://example.com")
    assert publication.valid?
  end

  test 'should be valid without order url' do
    publication = build(:publication, order_url: nil)
    assert publication.valid?
  end

  test 'should be valid with blank order url' do
    publication = build(:publication, order_url: nil)
    assert publication.valid?
  end

  test "should provide writers and readers for metadatum attributes" do
    publication = build(:publication,
      publication_date: Date.parse("1900-01-01"),
      unique_reference: "ABC-123",
      isbn: "0140621431",
      research: true,
      order_url: "http://example.com/order-url"
    )
    assert_equal Date.parse("1900-01-01"), publication.publication_date
    assert_equal "ABC-123", publication.unique_reference
    assert_equal "0140621431", publication.isbn
    assert publication.research?
    assert_equal "http://example.com/order-url", publication.order_url
  end

  test "should save metadatum attributes on create" do
    publication = create(:publication,
      publication_date: Date.parse("1900-01-01"),
      unique_reference: "ABC-123",
      isbn: "0140621431",
      research: true,
      order_url: "http://example.com/order-url"
    )
    publication.reload
    assert_equal Date.parse("1900-01-01"), publication.publication_date
    assert_equal "ABC-123", publication.unique_reference
    assert_equal "0140621431", publication.isbn
    assert publication.research?
    assert_equal "http://example.com/order-url", publication.order_url
  end

  test "should save metadatum attributes on update" do
    publication = create(:publication)
    publication.update_attributes(
      publication_date: Date.parse("1900-01-01"),
      unique_reference: "ABC-123",
      isbn: "0140621431",
      research: true,
      order_url: "http://example.com/order-url"
    )
    publication.reload
    assert_equal Date.parse("1900-01-01"), publication.publication_date
    assert_equal "ABC-123", publication.unique_reference
    assert_equal "0140621431", publication.isbn
    assert publication.research?
    assert_equal "http://example.com/order-url", publication.order_url
  end

  test "should build a draft copy of the existing publication" do
    attachment = create(:attachment)
    published_publication = create(:published_publication,
      publication_date: Date.parse("2010-01-01"),
      unique_reference: "ABC-123",
      isbn: "0099532816",
      research: true,
      order_url: "http://example.com/order-url",
      attachments: [attachment]
    )

    draft_publication = published_publication.create_draft(create(:policy_writer))

    assert_kind_of Attachment, published_publication.attachments.first
    assert_equal published_publication.attachments, draft_publication.attachments
    assert_equal published_publication.publication_date, draft_publication.publication_date<commit_msg>Remove redundant tests.<commit_after>require "test_helper"

class PublicationTest < ActiveSupport::TestCase

  test "should be valid when built from the factory" do
    publication = build(:publication)
    assert publication.valid?
  end

  test 'should be invalid without a publication date' do
    publication = build(:publication, publication_date: nil)
    refute publication.valid?
  end

  test 'should be valid without ISBN' do
    publication = build(:publication, isbn: nil)
    assert publication.valid?
  end

  test 'should be valid with blank ISBN' do
    publication = build(:publication, isbn: "")
    assert publication.valid?
  end

  test 'should be invalid with ISBN but not in ISBN-10 or ISBN-13 format' do
    publication = build(:publication, isbn: "invalid-isbn")
    refute publication.valid?
  end

  test 'should be valid with ISBN in ISBN-10 format' do
    publication = build(:publication, isbn: "0261102737")
    assert publication.valid?
  end

  test 'should be valid with ISBN in ISBN-13 format' do
    publication = build(:publication, isbn: "978-0261103207")
    assert publication.valid?
  end

  test 'should be invalid with malformed order url' do
    publication = build(:publication, order_url: "invalid-url")
    refute publication.valid?
  end

  test 'should be valid with order url with HTTP protocol' do
    publication = build(:publication, order_url: "http://example.com")
    assert publication.valid?
  end

  test 'should be valid with order url with HTTPS protocol' do
    publication = build(:publication, order_url: "https://example.com")
    assert publication.valid?
  end

  test 'should be valid without order url' do
    publication = build(:publication, order_url: nil)
    assert publication.valid?
  end

  test 'should be valid with blank order url' do
    publication = build(:publication, order_url: nil)
    assert publication.valid?
  end

  test "should build a draft copy of the existing publication" do
    attachment = create(:attachment)
    published_publication = create(:published_publication,
      publication_date: Date.parse("2010-01-01"),
      unique_reference: "ABC-123",
      isbn: "0099532816",
      research: true,
      order_url: "http://example.com/order-url",
      attachments: [attachment]
    )

    draft_publication = published_publication.create_draft(create(:policy_writer))

    assert_kind_of Attachment, published_publication.attachments.first
    assert_equal published_publication.attachments, draft_publication.attachments
    assert_equal published_publication.publication_date, draft_publication.publication_date
{'fraction_non_alphanumeric': 0.09006829823562891, 'fraction_numerical': 0.032726237905520775, 'mean_word_length': 3.877862595419847, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4913218', 'n_tokens_mistral': 2141, 'n_tokens_neox': 2006, 'n_words': 591}