Skip to content

Commit

Permalink
Switch to valuesOfType for test code where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmay committed Apr 14, 2021
1 parent b87b7b0 commit 3ff26f7
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CapturePSpec extends AnyWordSpec with TypeCheckedTripleEquals {
import com.rallyhealth.vapors.core.example.CaptureTimeRange._

"find a single fact from a query" in {
val q = factsOfType(FactTypes.WeightMeasurement).exists {
_.get(_.select(_.value).select(_.value)) > 18
val q = valuesOfType(FactTypes.WeightMeasurement).exists {
_.get(_.select(_.value)) > 18
}
val result = eval(JoeSchmoe.factTable)(q)
assert(result.param.value === TimeRange(JoeSchmoe.weight.value.timestamp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class ExprBuilderSpec extends AnyWordSpec {
"ValExprBuilder" should {

"combine lenses from chained .get() methods" in {
val q = factsOfType(FactTypes.GenericMeasurement).exists {
_.get(_.select(_.value)).get(_.select(_.value)) > 0.0
val q = valuesOfType(FactTypes.GenericMeasurement).exists {
_.get(_.select(_.value)) > 0.0
}.returnOutput
inside(q) {
case Expr.ExistsInOutput(_, condExpr, _) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class Snippets(val clock: Clock) {
this(Clock.fixed(fixedLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant, ZoneId.systemDefault()))

val ageFromDateOfBirth: Expr[FactTable, Seq[Int], Unit] = {
factsOfType(FactTypes.DateOfBirth).map { fact =>
valuesOfType(FactTypes.DateOfBirth).map { value =>
val ageInYears = dateDiff(
fact.get(_.select(_.value)),
fact.embedResult(today(clock)),
fact.embedConst(ChronoUnit.YEARS),
value,
value.embedResult(today(clock)),
value.embedConst(ChronoUnit.YEARS),
)
ageInYears.withOutputValue.get(_.select(_.toInt))
}
Expand All @@ -34,15 +34,15 @@ class Snippets(val clock: Clock) {

val isOver18: RootExpr[Boolean, Unit] = {
usingDefinitions(ageFromDateOfBirthDef) {
factsOfType(FactTypes.Age).exists {
_.get(_.select(_.value)) >= 18
valuesOfType(FactTypes.Age).exists {
_ >= 18
}
}
}

lazy val isUser: RootExpr[Boolean, Unit] = {
factsOfType(FactTypes.Role).exists {
_.get(_.select(_.value)) >= Role.User
valuesOfType(FactTypes.Role).exists {
_ >= Role.User
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ConcatOutputSpec extends AnyFreeSpec {
val factTypes = List(FactTypes.TagsUpdate, FactTypes.WeightMeasurement, FactTypes.WeightSelfReported)
val expectedFacts = factTypes.flatMap(JoeSchmoe.factTable.getSortedSeq(_))
val instantQueries = factTypes.map { factType =>
factsOfType(factType).map(_.value.get(_.select(_.timestamp))).returnOutput
valuesOfType(factType).map(_.get(_.select(_.timestamp))).returnOutput
}
val query = concat(instantQueries: _*).toOutputMonoid
val result = eval(JoeSchmoe.factTable)(query)
Expand Down Expand Up @@ -49,7 +49,7 @@ class ConcatOutputSpec extends AnyFreeSpec {
val factTypes = List(FactTypes.TagsUpdate, FactTypes.WeightMeasurement, FactTypes.WeightSelfReported)
val expectedFacts = factTypes.flatMap(JoeSchmoe.factTable.getSortedSeq(_))
val instantQueries = factTypes.map { factType =>
factsOfType(factType).map(_.value.get(_.select(_.timestamp))).returnOutput
valuesOfType(factType).map(_.get(_.select(_.timestamp))).returnOutput
}
val query = concat(instantQueries: _*).toLazyList
val result = eval(JoeSchmoe.factTable)(query)
Expand All @@ -62,7 +62,7 @@ class ConcatOutputSpec extends AnyFreeSpec {
"combined all elements into a LazyList without forcing the values" in {
val factTypes = List(FactTypes.TagsUpdate, FactTypes.WeightMeasurement, FactTypes.WeightSelfReported)
val instantQueries = factTypes.map { factType =>
factsOfType(factType).map(_.value.get(_.select(_.timestamp))).returnOutput
valuesOfType(factType).map(_.get(_.select(_.timestamp))).returnOutput
}
val query = concat(instantQueries: _*).toLazyList
val result = eval(JoeSchmoe.factTable)(query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class DivideOutputsSpec extends AnyFreeSpec {
}

lazy val humanAgeFromDogYears: Expr[FactTable, Seq[Int], Unit] = {
factsOfType(FactTypes.Age).map { fact =>
fact.get(_.select(_.value)) / 7
valuesOfType(FactTypes.Age).map {
_ / 7
}
}

Expand Down Expand Up @@ -92,8 +92,8 @@ class DivideOutputsSpec extends AnyFreeSpec {
}

lazy val celciusFromFahrenheit: Expr[FactTable, Seq[Double], Unit] = {
factsOfType(FactTypes.TempFahrenheit).map { fact =>
(fact.get(_.select(_.value)) - 32.0) / 1.8
valuesOfType(FactTypes.TempFahrenheit).map { tempF =>
(tempF - 32.0) / 1.8
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class EmbedExprSpec extends AnyWordSpec {
"embedding an expression inside a logical operator inside a 'withFactsOfType'" when {

def insideProbOfWeightloss(cond: ValCondExpr[Double, Unit]): RootExpr[Boolean, Unit] = {
factsOfType(FactTypes.ProbabilityToUse)
valuesOfType(FactTypes.ProbabilityToUse)
.flatMap {
_.getFoldable {
_.select(_.value).select(_.scores).at("weightloss").to(Seq)
_.select(_.scores).at("weightloss").to(Seq)
}
}
.exists { _ =>
Expand All @@ -25,8 +25,8 @@ class EmbedExprSpec extends AnyWordSpec {
def weightMeasuredWithin(window: Window[Int]): RootExpr[Boolean, Unit] = {
import cats.syntax.invariant._
val doubleWindow = window.imap(_.toDouble)(_.toInt)
factsOfType(FactTypes.WeightMeasurement).exists {
_.get(_.select(_.value).select(_.value)).within(doubleWindow)
valuesOfType(FactTypes.WeightMeasurement).exists {
_.get(_.select(_.value)).within(doubleWindow)
}
}

Expand Down Expand Up @@ -103,8 +103,8 @@ class EmbedExprSpec extends AnyWordSpec {
}

"disallows embedding an invalid return type" in {
val listOfNumberExpr = factsOfType(FactTypes.ProbabilityToUse).flatMap {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss").to(Seq))
val listOfNumberExpr = valuesOfType(FactTypes.ProbabilityToUse).flatMap {
_.getFoldable(_.select(_.scores).at("weightloss").to(Seq))
}
assertDoesNotCompile {
"""insideProbOfWeightloss(or(trueLiteral, listOfNumberExpr))"""
Expand Down Expand Up @@ -171,8 +171,8 @@ class EmbedExprSpec extends AnyWordSpec {
}

"disallows embedding an invalid return type" in {
val listOfNumberExpr = factsOfType(FactTypes.ProbabilityToUse).flatMap {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss").asIterable.to(Seq))
val listOfNumberExpr = valuesOfType(FactTypes.ProbabilityToUse).flatMap {
_.getFoldable(_.select(_.scores).at("weightloss").asIterable.to(Seq))
}
assertDoesNotCompile {
"""insideProbOfWeightloss(and(trueLiteral, listOfNumberExpr))"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ class GroupOutputSpec extends AnyFreeSpec {
"groupBy should" - {

"create a map from a list using groupBy and mapValues" in {
val query = factsOfType(FactTypes.TagsUpdate)
.groupBy(_.select(_.value.source))
// TODO: This is very verbose and doesn't easily support the common case of mapping over collection values
.mapValues(_.returnOutput.withOutputFoldable.map(_.value).returnOutput)
val query = valuesOfType(FactTypes.TagsUpdate).groupBy(_.select(_.source))
val expected = User1.factTable.getSortedSeq(FactTypes.TagsUpdate).groupMap(_.value.source)(_.value)
val result = eval(User1.factTable)(query)
assertResult(expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class InterpretExprAsResultFnSpec extends AnyWordSpec {
"using no post processing" should {

"find a single fact from a query" in {
val q = factsOfType(FactTypes.Age).exists {
_.get(_.select(_.value)) >= 18
val q = valuesOfType(FactTypes.Age).exists {
_ >= 18
}
val result = eval(JoeSchmoe.factTable)(q)
assert(result.param.value === ())
Expand All @@ -23,8 +23,8 @@ class InterpretExprAsResultFnSpec extends AnyWordSpec {
}

"find a complex fact from a query" in {
val q = factsOfType(FactTypes.ProbabilityToUse).exists {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss")).exists {
val q = valuesOfType(FactTypes.ProbabilityToUse).exists {
_.getFoldable(_.select(_.scores).at("weightloss")).exists {
_ > 0.5
}
}
Expand All @@ -33,8 +33,8 @@ class InterpretExprAsResultFnSpec extends AnyWordSpec {
}

"define a fact expression" in {
val likelyToJoinWeightloss = factsOfType(FactTypes.ProbabilityToUse).exists {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss")).exists {
val likelyToJoinWeightloss = valuesOfType(FactTypes.ProbabilityToUse).exists {
_.getFoldable(_.select(_.scores).at("weightloss")).exists {
_ > 0.5
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class OutputWithinSetExprSpec extends AnyWordSpec {
"Expr.OutputWithinSet" should {

"find an asthma tag in a set that contains it" in {
val q = factsOfType(FactTypes.Tag).exists {
_.value.in(Set("asthma", "diabetes"))
val q = valuesOfType(FactTypes.Tag).exists {
_ in Set("asthma", "diabetes")
}
val result = eval(JoeSchmoe.factTable)(q)
assert(result.output.value)
assertResult(Evidence(JoeSchmoe.asthmaTag))(result.output.evidence)
}

"not find an asthma tag in a set that does not contain it" in {
val q = factsOfType(FactTypes.Tag).exists {
_.value.in(Set("diabetes"))
val q = valuesOfType(FactTypes.Tag).exists {
_ in Set("diabetes")
}
val result = eval(JoeSchmoe.factTable)(q)
assert(!result.output.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import scala.collection.View
class SelectFromOutputSpec extends AnyFreeSpec {

"create a map from a converting a list of facts to tuples" in {
val query = factsOfType(FactTypes.TagsUpdate).map { fact =>
val update = fact.value
val query = valuesOfType(FactTypes.TagsUpdate).map { update =>
wrap(
update.get(_.select(_.source)).returnOutput,
update.get(_.select(_.tags)).returnOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SortOutputSpec extends AnyFreeSpec {
)

"sorted using natural ordering" in {
val query = factsOfType(FactTypes.BloodPressureMeasurement).map(_.value.get(_.select(_.diastolic))).sorted
val query = valuesOfType(FactTypes.BloodPressureMeasurement).map(_.get(_.select(_.diastolic))).sorted
assertResult(Some(highDiastolic)) {
bpFacts.getSortedSeq(FactTypes.BloodPressureMeasurement).headOption.map(_.value)
}
Expand All @@ -38,7 +38,7 @@ class SortOutputSpec extends AnyFreeSpec {
}

"sortBy ordered field" in {
val query = factsOfType(FactTypes.BloodPressureMeasurement).map(_.value).sortBy(_.select(_.diastolic))
val query = valuesOfType(FactTypes.BloodPressureMeasurement).sortBy(_.select(_.diastolic))
assertResult(Some(highDiastolic)) {
bpFacts.getSortedSeq(FactTypes.BloodPressureMeasurement).headOption.map(_.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class UsingDefinitionsExprSpec extends AnyWordSpec {
"add the defined facts to the fact table" in {
val result = eval(JoeSchmoe.factTable) {
usingDefinitions(Snippets.ageFromDateOfBirthDef) {
factsOfType(FactTypes.Age)
valuesOfType(FactTypes.Age)
}
}
val ages = result.output.value.map(_.value)
val ages = result.output.value
ages should contain only JoeSchmoe.age.value
}

Expand All @@ -48,9 +48,7 @@ class UsingDefinitionsExprSpec extends AnyWordSpec {
val definition = Snippets.isEligibleDef
val result = eval(facts) {
usingDefinitions(definition) {
factsOfType(definition.factType).exists {
_.get(_.select(_.value))
}
valuesOfType(definition.factType).exists(_ === true)
}
}
assert(result.output.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class WrapOutputSeqSpec extends AnyFreeSpec {
FactTypes.TagsUpdate,
)
val subExpressions =
factTypes.map(t => factsOfType(t).map(_.value.get(_.select(_.timestamp))).returnOutput)
factTypes.map(t => valuesOfType(t).map(_.get(_.select(_.timestamp))).returnOutput)
val query = sequence(subExpressions)
val factsPerType = factTypes.map(JoeSchmoe.factTable.getSortedSeq(_))
val expectedValues = factsPerType.map(_.map(_.value.timestamp))
Expand Down

0 comments on commit 3ff26f7

Please sign in to comment.