<commit_before>package uk.ac.wellcome.platform.sierra_bib_merger.merger

import org.scalatest.{FunSpec, Matchers}
import uk.ac.wellcome.models.transformable.SierraTransformable
import uk.ac.wellcome.models.transformable.sierra.test.utils.SierraUtil

class BibMergerTest extends FunSpec with Matchers with SierraUtil {

  describe("merging with a SierraBibRecord") {
    it("merges data from a bibRecord when empty") {
      val bibRecord = createSierraBibRecord
      val transformable = SierraTransformable(sourceId = bibRecord.id)

      val newTransformable = BibMerger.mergeBibRecord(transformable, bibRecord)
      newTransformable.maybeBibData.get shouldEqual bibRecord
    }

    it("only merges bib records with matching ids") {
      val bibRecord = createSierraBibRecord
      val transformable = SierraTransformable(sourceId = createSierraRecordNumberString)

      val caught = intercept[RuntimeException] {
        BibMerger.mergeBibRecord(transformable, bibRecord)
      }
      caught.getMessage shouldEqual s"Non-matching bib ids ${bibRecord.id} != ${transformable.id}"
    }

    it(
      "returns the unmerged transformable when merging bib records with stale data") {
      val oldBibRecord = createSierraBibRecordWith(
        modifiedDate = olderDate
      )

      val newBibRecord = createSierraBibRecordWith(
        id = oldBibRecord.id,
        modifiedDate = newerDate
      )

      val transformable = SierraTransformable(
        sourceId = oldBibRecord.id,
        maybeBibData = Some(newBibRecord)
      )

      val result = BibMerger.mergeBibRecord(transformable, oldBibRecord)
      result shouldBe transformable
    }

    it("updates bibData when merging bib records with newer data") {
      val newBibRecord = createSierraBibRecordWith(
        modifiedDate = newerDate
      )

      val oldBibRecord = createSierraBibRecordWith(
        id = newBibRecord.id,
        modifiedDate = olderDate
      )

      val transformable = SierraTransformable(
        sourceId = "888",
        maybeBibData = Some(oldBibRecord)
      )

      val result = BibMerger.mergeBibRecord(transformable, newBibRecord)
      result.maybeBibData.get shouldBe newBibRecord
    }
  }
}<commit_msg>You need to preserve that ID!<commit_after>package uk.ac.wellcome.platform.sierra_bib_merger.merger

import org.scalatest.{FunSpec, Matchers}
import uk.ac.wellcome.models.transformable.SierraTransformable
import uk.ac.wellcome.models.transformable.sierra.test.utils.SierraUtil

class BibMergerTest extends FunSpec with Matchers with SierraUtil {

  describe("merging with a SierraBibRecord") {
    it("merges data from a bibRecord when empty") {
      val bibRecord = createSierraBibRecord
      val transformable = SierraTransformable(sourceId = bibRecord.id)

      val newTransformable = BibMerger.mergeBibRecord(transformable, bibRecord)
      newTransformable.maybeBibData.get shouldEqual bibRecord
    }

    it("only merges bib records with matching ids") {
      val bibRecord = createSierraBibRecord
      val transformable = SierraTransformable(sourceId = createSierraRecordNumberString)

      val caught = intercept[RuntimeException] {
        BibMerger.mergeBibRecord(transformable, bibRecord)
      }
      caught.getMessage shouldEqual s"Non-matching bib ids ${bibRecord.id} != ${transformable.id}"
    }

    it(
      "returns the unmerged transformable when merging bib records with stale data") {
      val oldBibRecord = createSierraBibRecordWith(
        modifiedDate = olderDate
      )

      val newBibRecord = createSierraBibRecordWith(
        id = oldBibRecord.id,
        modifiedDate = newerDate
      )

      val transformable = SierraTransformable(
        sourceId = oldBibRecord.id,
        maybeBibData = Some(newBibRecord)
      )

      val result = BibMerger.mergeBibRecord(transformable, oldBibRecord)
      result shouldBe transformable
    }

    it("updates bibData when merging bib records with newer data") {
      val newBibRecord = createSierraBibRecordWith(
        modifiedDate = newerDate
      )

      val oldBibRecord = createSierraBibRecordWith(
        id = newBibRecord.id,
        modifiedDate = olderDate
      )

      val transformable = SierraTransformable(
        bibRecord = oldBibRecord
      )

      val result = BibMerger.mergeBibRecord(transformable, newBibRecord)
      result.maybeBibData.get shouldBe newBibRecord
    }
  }
}
{'fraction_non_alphanumeric': 0.06778508274767626, 'fraction_numerical': 0.0006801178871004307, 'mean_word_length': 3.2876579203109815, 'pattern_counts': {'":': 0, '<': 3, '<?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': '146645', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1197, 'n_words': 285}