Skip to content

Commit

Permalink
feat:assert approximatively equal, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
scarisey committed Nov 4, 2024
1 parent 1c0ee39 commit 40360a5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
20 changes: 20 additions & 0 deletions src/main/scala/com/lectra/kapoeira/glue/Asserts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ object Asserts extends Matchers with LazyLogging {
}
)

def approxEqual(assertionContext: AssertionContext, alias: String, jsonExpression: String, expected: String, approxRange: String):Assertion =
assertKafkaOutput(
assertionContext,
alias,
jsonExpression,
{ actual => (
for{
x <- actual.double()
expected <- JsonExpr(expected).value.double()
approx <- JsonExpr(approxRange).value.double()
} yield{ x should equal ( expected +- approx ) }
).fold(fail(s"${actual} equal ${expected} +- ${approxRange}"))(identity)
}
)

def matchExactObject(
assertionContext: AssertionContext,
alias: String,
Expand Down Expand Up @@ -150,6 +165,11 @@ object Asserts extends Matchers with LazyLogging {

implicit class JsonNodeOps(val jsonNode: JsonNode) {

def double():Option[Double] ={
val none = (_:Any) => Option.empty[Double]
jsonNode.fold(obj = none, arr = none, number = Some(_), strng = none, bln = none, bin = none, nullOrUndef = none)
}

def fold[T](
obj: JsonNode => T,
arr: JsonNode => T,
Expand Down
23 changes: 3 additions & 20 deletions src/test/resources/features/assertions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,12 @@ Feature: assertions
| topic_out | key1_${uuid} | aliasHeaders2.1 | value2.1 |
| topic_out | key2_${uuid} | aliasHeaders2.2 | value2.2 |
| topic_out | key3_${uuid} | aliasHeaders2.3 | value2.3 |
| topic_out | key4_${uuid} | aliasHeaders2.4 | value2.4 |
And assert value2.1 $.qux == 42
And assert value2.2 $ has size 2
And assert value2.2 $ == [3,4]
And assert value2.3 $ == "value2.3"
And assert value2.4 $.foo == 12.0038209653823934567890123456789
And assert value2.4 $.foo == 12.0038 +- 0.1
And assert aliasHeaders2.1 $ == {"foo":"bar","baz":"42"}
And assert aliasHeaders2.1 $.foo == "bar"

Scenario: Produce a complex record
When records from file with key and value are sent
| topic_alias | separator | file |
| topic_in | # | features/records/keyvalueobjectNarrays.dat |
Then expected records
| topic_alias | key | value |
| topic_out | key1_${uuid} | aValue |
And assert aValue $.foo == "fooString"
And assert aValue $.fooInt == 42
And assert aValue $.foos has size 3
And assert aValue $.foos == ["item1","item2","item3"]
And assert aValue $ match object {"foos":["item1","item2","item3"],"bar":{"baz":["item1","item2","item3"]}}
And assert aValue $ match object {"foos":["item1","item2","item3"]}
And assert aValue $ match object {"bar":{"baz":["item1","item2","item3"]}}
And assert aValue $.bar match object {"baz":["item1","item2","item3"]}
And assert aValue $.bar.baz[0] == "item1"
And assert aValue $ match exact object {"foo":"fooString","fooInt":42,"foos":["item1","item2","item3"],"qux":[{"key1":"toto"},{"key2":"titi"}],"bar":{"baz":["item1","item2","item3"]}}
And assert aValue $.bar match exact object {"baz":["item1","item2","item3"]}
And assert aValue $.qux[?(@.key1!=null)] match object {"key1":"toto"}
1 change: 1 addition & 0 deletions src/test/resources/features/records/keyheadersvalue.dat
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
key1_${uuid}#{"qux":42}#{"foo":"bar","baz":42}
key2_${uuid}#[3,4]#{"foo":"bar","baz":[1,2]}
key3_${uuid}#value2.3#{"foo":"bar"}
key4_${uuid}#{"foo":12.0038209653823934567890123456789}#{"foo":"bar"}
18 changes: 18 additions & 0 deletions src/test/scala/com/lectra/kapoeira/glue/AssertsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ class AssertsTest extends AnyFlatSpec with Matchers with MockFactory {

behavior of "Asserts"

it should "assert approximatively equals on numbers" in {
val backgroundContext = mock[BackgroundContext]
val assertionContext = new AssertionContext(WhenStepsLive(backgroundContext, recordConsume, KapoeiraProducer.run _))
val consumerRecord =
new ConsumerRecord("topic", 0, 0, "key", """{"foo": 12.003820965382393}""".getBytes.asInstanceOf[Any])
val valueAlias = "valueAlias"
val keyValueRecord = KeyValueWithAliasesRecord("topic", "key", valueAlias)
(backgroundContext
.consumeTopic(_: String, _: Map[String, Int])(_: RecordConsumer))
.expects(*, *, *)
.returning(Map("key" -> Seq(consumerRecord)))
val expectedConsumedRecords = List(keyValueRecord)
assertionContext.launchConsumption(expectedConsumedRecords)

Asserts.approxEqual(assertionContext, valueAlias, "$.foo", "12.0038","0.1" )

}

it should "assert equality on literals" in {
val backgroundContext = mock[BackgroundContext]
val assertionContext = new AssertionContext(WhenStepsLive(backgroundContext, recordConsume, KapoeiraProducer.run _))
Expand Down

0 comments on commit 40360a5

Please sign in to comment.