<commit_before>package db

import com.gilt.apidoc.api.v0.models.{Application, Change, Diff, DiffBreaking, DiffNonBreaking, Organization, Version}
import org.scalatest.{FunSpec, Matchers}
import java.util.UUID

class ChangesDaoSpec extends FunSpec with Matchers {

  new play.core.StaticApplication(new java.io.File("."))

  private def getApplication(version: Version): Application = {
    ApplicationsDao.findByGuid(Authorization.All, version.application.guid).getOrElse {
      sys.error("Could not find application for version: " + version)
    }
  }

  private def createChange(
    description: String = "Breaking difference - " + UUID.randomUUID.toString,
    org: Organization = Util.createOrganization()
  ): Change = {
    val app = Util.createApplication(org = org)
    val fromVersion = Util.createVersion(application = app, version = "1.0.0")
    val toVersion = Util.createVersion(application = app, version = "1.0.1")
    val diff = DiffBreaking(description)
    ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Seq(diff))

    ChangesDao.findAll(
      Authorization.All,
      fromVersionGuid = Some(fromVersion.guid)
    ).head
  }

  describe("with a valid from and to version") {

    it("upsert is a no-op with no differences") {
      val fromVersion = Util.createVersion(version = "1.0.0")
      val toVersion = Util.createVersion(version = "1.0.1", application = getApplication(fromVersion))

      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Nil)
      ChangesDao.findAll(
        Authorization.All,
        fromVersionGuid = Some(fromVersion.guid)
      ) should be(Nil)
    }

    it("upsert can record multiple changes") {
      val fromVersion = Util.createVersion(version = "1.0.0")
      val toVersion = Util.createVersion(version = "1.0.1", application = getApplication(fromVersion))

      val diffs = Seq(
        DiffBreaking("Breaking difference - " + UUID.randomUUID.toString),
        DiffNonBreaking("Non breaking difference - " + UUID.randomUUID.toString)
      )

      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, diffs)

      ChangesDao.findAll(
        Authorization.All,
        fromVersionGuid = Some(fromVersion.guid)
      ).map(_.diff) should be(diffs)
    }

    it("upsert does not throw error on duplicate") {
      val fromVersion = Util.createVersion(version = "1.0.0")
      val toVersion = Util.createVersion(version = "1.0.1", application = getApplication(fromVersion))

      val diff = DiffBreaking("Breaking difference - " + UUID.randomUUID.toString)
      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Seq(diff, diff))
      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Seq(diff))

      ChangesDao.findAll(
        Authorization.All,
        fromVersionGuid = Some(fromVersion.guid)<commit_msg>Update sort order in test<commit_after>package db

import com.gilt.apidoc.api.v0.models.{Application, Change, Diff, DiffBreaking, DiffNonBreaking, Organization, Version}
import org.scalatest.{FunSpec, Matchers}
import java.util.UUID

class ChangesDaoSpec extends FunSpec with Matchers {

  new play.core.StaticApplication(new java.io.File("."))

  private def getApplication(version: Version): Application = {
    ApplicationsDao.findByGuid(Authorization.All, version.application.guid).getOrElse {
      sys.error("Could not find application for version: " + version)
    }
  }

  private def createChange(
    description: String = "Breaking difference - " + UUID.randomUUID.toString,
    org: Organization = Util.createOrganization()
  ): Change = {
    val app = Util.createApplication(org = org)
    val fromVersion = Util.createVersion(application = app, version = "1.0.0")
    val toVersion = Util.createVersion(application = app, version = "1.0.1")
    val diff = DiffBreaking(description)
    ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Seq(diff))

    ChangesDao.findAll(
      Authorization.All,
      fromVersionGuid = Some(fromVersion.guid)
    ).head
  }

  describe("with a valid from and to version") {

    it("upsert is a no-op with no differences") {
      val fromVersion = Util.createVersion(version = "1.0.0")
      val toVersion = Util.createVersion(version = "1.0.1", application = getApplication(fromVersion))

      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Nil)
      ChangesDao.findAll(
        Authorization.All,
        fromVersionGuid = Some(fromVersion.guid)
      ) should be(Nil)
    }

    it("upsert can record multiple changes") {
      val fromVersion = Util.createVersion(version = "1.0.0")
      val toVersion = Util.createVersion(version = "1.0.1", application = getApplication(fromVersion))

      val diffs = Seq(
        DiffBreaking("Breaking difference - " + UUID.randomUUID.toString),
        DiffNonBreaking("Non breaking difference - " + UUID.randomUUID.toString)
      )

      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, diffs)

      ChangesDao.findAll(
        Authorization.All,
        fromVersionGuid = Some(fromVersion.guid)
      ).map(_.diff) should be(diffs.reverse)
    }

    it("upsert does not throw error on duplicate") {
      val fromVersion = Util.createVersion(version = "1.0.0")
      val toVersion = Util.createVersion(version = "1.0.1", application = getApplication(fromVersion))

      val diff = DiffBreaking("Breaking difference - " + UUID.randomUUID.toString)
      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Seq(diff, diff))
      ChangesDao.upsert(Util.createdBy, fromVersion, toVersion, Seq(diff))

      ChangesDao.findAll(
        Authorization.All,
        fromVersionGuid = Some(fromVersion.guid)
{'fraction_non_alphanumeric': 0.11124867350548284, 'fraction_numerical': 0.008843296781039971, 'mean_word_length': 3.575242718446602, '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': '3375237', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1679, 'n_words': 369}