Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjben committed Feb 23, 2024
1 parent b875b9b commit fc4b389
Show file tree
Hide file tree
Showing 4 changed files with 666 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ object EnrichmentManager {
)
.leftMap(NonEmptyList.one)
.possiblyExitingEarly(emitIncomplete)
// Next 2 lines remove the invalid contexts and the invalid unstructured event from the event.
// This should be done after the bad row was created and only if emitIncomplete is enabled.
_ = {
enriched.contexts = ME.formatContexts(extractResult.contexts).orNull
enriched.unstruct_event = ME.formatUnstructEvent(extractResult.unstructEvent).orNull
}
enrichmentsContexts <- runEnrichments(
registry,
processor,
Expand Down Expand Up @@ -109,10 +115,10 @@ object EnrichmentManager {
.possiblyExitingEarly(emitIncomplete)
} yield enriched

iorT.leftMap(_.last)
iorT.leftMap(_.head)
}

def mapAndValidateInput[F[_]: Clock: Monad](
private def mapAndValidateInput[F[_]: Clock: Monad](
raw: RawEvent,
enrichedEvent: EnrichedEvent,
etlTstamp: DateTime,
Expand All @@ -123,10 +129,6 @@ object EnrichmentManager {
val iorT = for {
_ <- IorT.fromIor[F](setupEnrichedEvent(raw, enrichedEvent, etlTstamp, processor))
extract <- IgluUtils.extractAndValidateInputJsons(enrichedEvent, client, registryLookup)
_ = {
enrichedEvent.contexts = ME.formatContexts(extract.contexts).orNull
enrichedEvent.unstruct_event = ME.formatUnstructEvent(extract.unstructEvent).orNull
}
} yield extract

iorT.leftMap { violations =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.snowplowanalytics.iglu.client.{IgluCirceClient, Resolver}
import com.snowplowanalytics.iglu.client.resolver.registries.Registry
import com.snowplowanalytics.iglu.client.resolver.registries.JavaNetRegistryLookup

import com.snowplowanalytics.iglu.core.SelfDescribingData
import com.snowplowanalytics.iglu.core.{SchemaKey, SelfDescribingData}
import com.snowplowanalytics.iglu.core.circe.implicits._

import com.snowplowanalytics.lrumap.CreateLruMap._
Expand Down Expand Up @@ -129,6 +129,25 @@ object SpecHelpers extends CatsEffect {
.flatMap(SelfDescribingData.parse[Json])
.leftMap(err => s"Can't parse Json [$rawJson] as as SelfDescribingData, error: [$err]")

def listContextsSchemas(rawContexts: String): List[SchemaKey] =
jsonStringToSDJ(rawContexts)
.map(_.data.asArray.get.toList)
.flatMap(contexts => contexts.traverse(c => SelfDescribingData.parse[Json](c).map(_.schema))) match {
case Left(err) =>
throw new IllegalArgumentException(s"Couldn't list contexts schemas. Error: [$err]")
case Right(schemas) => schemas
}

def getUnstructSchema(rawUnstruct: String): SchemaKey =
jsonStringToSDJ(rawUnstruct)
.map(_.data)
.flatMap(SelfDescribingData.parse[Json])
.map(_.schema) match {
case Left(err) =>
throw new IllegalArgumentException(s"Couldn't get unstruct event schema. Error: [$err]")
case Right(schema) => schema
}

implicit class MapOps[A, B](underlying: Map[A, B]) {
def toOpt: Map[A, Option[B]] = underlying.map { case (a, b) => (a, Option(b)) }
}
Expand Down
Loading

0 comments on commit fc4b389

Please sign in to comment.