<commit_before>package uk.ac.wellcome.sierra_adapter.utils

import io.circe.Decoder
import org.scalatest.Assertion
import uk.ac.wellcome.messaging.test.fixtures.{MessageInfo, Messaging}
import uk.ac.wellcome.messaging.test.fixtures.SNS.Topic
import uk.ac.wellcome.models.transformable.SierraTransformable
import uk.ac.wellcome.models.transformable.SierraTransformable._
import uk.ac.wellcome.storage.ObjectStore
import uk.ac.wellcome.storage.dynamo._
import uk.ac.wellcome.storage.fixtures.LocalDynamoDb.Table
import uk.ac.wellcome.storage.fixtures.LocalVersionedHybridStore
import uk.ac.wellcome.storage.fixtures.S3.Bucket
import uk.ac.wellcome.storage.vhs.{
  EmptyMetadata,
  HybridRecord,
  VersionedHybridStore
}
import uk.ac.wellcome.json.JsonUtil._
import uk.ac.wellcome.test.fixtures._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait SierraAdapterHelpers extends LocalVersionedHybridStore with Messaging {
  type SierraVHS = VersionedHybridStore[SierraTransformable,
                                        EmptyMetadata,
                                        ObjectStore[SierraTransformable]]

  def withSierraVHS[R](bucket: Bucket, table: Table)(
    testWith: TestWith[SierraVHS, R]): R =
    withTypeVHS[SierraTransformable, EmptyMetadata, R](
      bucket = bucket,
      table = table) { vhs =>
      testWith(vhs)
    }

  def storeInVHS(transformable: SierraTransformable,
                 hybridStore: SierraVHS): Future[Unit] =
    hybridStore
      .updateRecord(id = transformable.sierraId.withoutCheckDigit)(
        ifNotExisting = (transformable, EmptyMetadata()))(
        ifExisting = (t, m) =>
          throw new RuntimeException(
            s"Found record ${transformable.sierraId}, but VHS should be empty")
      )
      .map(_ => ())

  def storeInVHS(transformables: List[SierraTransformable],
                 hybridStore: SierraVHS): Future[List[Unit]] =
    Future.sequence(
      transformables.map { t =>
        storeInVHS(t, hybridStore = hybridStore)
      }
    )

  def assertStored(transformable: SierraTransformable,
                   bucket: Bucket,
                   table: Table): Assertion =
    assertStored[SierraTransformable](
      bucket,
      table,
      id = transformable.sierraId.withoutCheckDigit,
      record = transformable)

  def assertStoredAndSent[T](t: T,
                             id: String,
                             topic: Topic,
                             bucket: Bucket,
                             table: Table)(implicit decoder: Decoder[T]): Assertion = {
    val hybridRecord = getHybridRecord(table, id = id)

    val storedTransformable = getObjectFromS3[T](
      Bucket(hybridRecord.location.namespace),
      hybridRecord.location.key)
    storedTransformable shouldBe t

    listMessagesReceivedFromSNS(topic).map { info: MessageInfo =>
      fromJson[HybridRecord](info.message).get
    } should contain(hybridRecord)
  }

  def assertStoredAndSent(transformable: SierraTransformable,
                          topic: Topic,
                          bucket: Bucket,
                          table: Table): Assertion =
    assertStoredAndSent[SierraTransformable](
      transformable,
      id = transformable.sierraId.withoutCheckDigit,
      topic = topic,
      bucket = bucket,
      table = table
    )
}
<commit_msg>Apply auto-formatting rules<commit_after>package uk.ac.wellcome.sierra_adapter.utils

import io.circe.Decoder
import org.scalatest.Assertion
import uk.ac.wellcome.messaging.test.fixtures.{MessageInfo, Messaging}
import uk.ac.wellcome.messaging.test.fixtures.SNS.Topic
import uk.ac.wellcome.models.transformable.SierraTransformable
import uk.ac.wellcome.models.transformable.SierraTransformable._
import uk.ac.wellcome.storage.ObjectStore
import uk.ac.wellcome.storage.dynamo._
import uk.ac.wellcome.storage.fixtures.LocalDynamoDb.Table
import uk.ac.wellcome.storage.fixtures.LocalVersionedHybridStore
import uk.ac.wellcome.storage.fixtures.S3.Bucket
import uk.ac.wellcome.storage.vhs.{
  EmptyMetadata,
  HybridRecord,
  VersionedHybridStore
}
import uk.ac.wellcome.json.JsonUtil._
import uk.ac.wellcome.test.fixtures._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait SierraAdapterHelpers extends LocalVersionedHybridStore with Messaging {
  type SierraVHS = VersionedHybridStore[SierraTransformable,
                                        EmptyMetadata,
                                        ObjectStore[SierraTransformable]]

  def withSierraVHS[R](bucket: Bucket, table: Table)(
    testWith: TestWith[SierraVHS, R]): R =
    withTypeVHS[SierraTransformable, EmptyMetadata, R](
      bucket = bucket,
      table = table) { vhs =>
      testWith(vhs)
    }

  def storeInVHS(transformable: SierraTransformable,
                 hybridStore: SierraVHS): Future[Unit] =
    hybridStore
      .updateRecord(id = transformable.sierraId.withoutCheckDigit)(
        ifNotExisting = (transformable, EmptyMetadata()))(
        ifExisting = (t, m) =>
          throw new RuntimeException(
            s"Found record ${transformable.sierraId}, but VHS should be empty")
      )
      .map(_ => ())

  def storeInVHS(transformables: List[SierraTransformable],
                 hybridStore: SierraVHS): Future[List[Unit]] =
    Future.sequence(
      transformables.map { t =>
        storeInVHS(t, hybridStore = hybridStore)
      }
    )

  def assertStored(transformable: SierraTransformable,
                   bucket: Bucket,
                   table: Table): Assertion =
    assertStored[SierraTransformable](
      bucket,
      table,
      id = transformable.sierraId.withoutCheckDigit,
      record = transformable)

  def assertStoredAndSent[T](
    t: T,
    id: String,
    topic: Topic,
    bucket: Bucket,
    table: Table)(implicit decoder: Decoder[T]): Assertion = {
    val hybridRecord = getHybridRecord(table, id = id)

    val storedTransformable = getObjectFromS3[T](
      Bucket(hybridRecord.location.namespace),
      hybridRecord.location.key)
    storedTransformable shouldBe t

    listMessagesReceivedFromSNS(topic).map { info: MessageInfo =>
      fromJson[HybridRecord](info.message).get
    } should contain(hybridRecord)
  }

  def assertStoredAndSent(transformable: SierraTransformable,
                          topic: Topic,
                          bucket: Bucket,
                          table: Table): Assertion =
    assertStoredAndSent[SierraTransformable](
      transformable,
      id = transformable.sierraId.withoutCheckDigit,
      topic = topic,
      bucket = bucket,
      table = table
    )
}

{'fraction_non_alphanumeric': 0.09019960978538195, 'fraction_numerical': 0.0006003301815998799, 'mean_word_length': 2.8364997121473805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3321806', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1905, 'n_words': 351}