Skip to content

Commit

Permalink
Integrated DateRangeConstraints into app. Added example config to `ex…
Browse files Browse the repository at this point in the history
…amples/complete/accounts.conf`
  • Loading branch information
Entea committed Sep 7, 2016
1 parent 6976e7b commit e45b772
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
26 changes: 21 additions & 5 deletions base/src/main/scala/co/uproot/abandon/Config.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package co.uproot.abandon

import java.time.LocalDate
import java.time.format.DateTimeFormatter

import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory

import collection.JavaConverters._
import com.typesafe.config.ConfigObject
import org.rogach.scallop.ScallopConf
import SettingsHelper._
import com.typesafe.config.ConfigException
import scala.util.Try

class AbandonCLIConf(arguments: Seq[String]) extends ScallopConf(arguments) {
val inputs = opt[List[String]]("input", short = 'i')
Expand Down Expand Up @@ -81,7 +82,8 @@ object SettingsHelper {
val accountConfigs = config.optConfigList("accounts").getOrElse(Nil)
val accounts = accountConfigs.map(makeAccountSettings)
val eodConstraints = config.optConfigList("eodConstraints").getOrElse(Nil).map(makeEodConstraints(_))
Right(Settings(inputs, eodConstraints, accounts, reports, ReportOptions(isRight), exports, Some(file)))
val dateConstraints = config.optConfigList("dateConstraints").getOrElse(Nil).map(makeDateConstraints(_))
Right(Settings(inputs, eodConstraints ++ dateConstraints, accounts, reports, ReportOptions(isRight), exports, Some(file)))
} catch {
case e: ConfigException => Left(e.getMessage)
}
Expand All @@ -99,6 +101,14 @@ object SettingsHelper {
}
}

def makeDateConstraints(config: Config): DateConstraint = {
val format = DateTimeFormatter.ISO_LOCAL_DATE

val from = Try(LocalDate.parse(config.getString("from"), format)).map(Some(_)).getOrElse(None)
val to = Try(LocalDate.parse(config.getString("to"), format)).map(Some(_)).getOrElse(None)
DateConstraint(from, to)
}

def makeReportSettings(config: Config) = {
val title = config.getString("title")
val reportType = config.getString("type")
Expand Down Expand Up @@ -209,21 +219,27 @@ case class NegativeConstraint(val accName: String) extends Constraint with SignC

case class DateConstraint(dateFrom: Option[LocalDate], dateTo: Option[LocalDate]) extends Constraint {
override def check(appState: AppState): Boolean = {
val badPost = appState.accState.posts.find(post => {
appState.accState.posts.find(post => {
val from = dateFrom.getOrElse(LocalDate.MIN)
val to = dateTo.getOrElse(LocalDate.MAX)
val date = LocalDate.of(post.date.year, post.date.month, post.date.day)

from.isAfter(date) || to.isBefore(date)
}).foreach(post => {
throw new ConstraintError(
s"${post.name} is not in date range of " +
s"[${dateFrom.getOrElse("None")}, ${dateTo.getOrElse("None")}]. " +
s"Date is ${post.date.formatISO8601Ext}"
)
})

badPost.isEmpty
true
}
}

case class Settings(
inputs: Seq[String],
eodConstraints: Seq[Constraint],
constraints: Seq[Constraint],
accounts: Seq[AccountSettings],
reports: Seq[ReportSettings],
reportOptions: ReportOptions,
Expand Down
12 changes: 6 additions & 6 deletions base/src/test/scala/co/uproot/abandon/DateConstraintTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ class DateConstraintTest extends FlatSpec with Matchers with BeforeAndAfterEach
constraint.check(appState) should be(true)
}

it should "return false on end date violation" in {
it should "throw exception on end date violation" in {
val constraint = new DateConstraint(
Some(LocalDate.of(2013, Month.JANUARY, 1)),
Some(LocalDate.of(2013, Month.NOVEMBER, 1))
)
constraint.check(appState) should be(false)
an [ConstraintError] should be thrownBy constraint.check(appState)

val constraintWithoutFrom = new DateConstraint(None, Some(LocalDate.of(2013, Month.NOVEMBER, 1)))
constraintWithoutFrom.check(appState) should be(false)
an [ConstraintError] should be thrownBy constraintWithoutFrom.check(appState)
}

it should "return false on start date violation" in {
it should "throw exception on start date violation" in {
val constraint = new DateConstraint(
Some(LocalDate.of(2013, Month.JUNE, 1)),
Some(LocalDate.of(2013, Month.NOVEMBER, 1))
)
constraint.check(appState) should be(false)
an [ConstraintError] should be thrownBy constraint.check(appState)

val constraintWithoutTo = new DateConstraint(Some(LocalDate.of(2013, Month.NOVEMBER, 1)), None)
constraintWithoutTo.check(appState) should be(false)
an [ConstraintError] should be thrownBy constraintWithoutTo.check(appState)
}

it should "return true on empty dates" in {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/scala/co/uproot/abandon/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ object CLIMain {
val (parseError, astEntries, processedFiles) = Processor.parseAll(settings.inputs)
if (!parseError) {
val appState = Processor.process(astEntries,settings.accounts)
Processor.checkConstaints(appState, settings.eodConstraints)
Processor.checkConstaints(appState, settings.constraints)
settings.exports.foreach { exportSettings =>
val reportWriter = new ReportWriter(settings, exportSettings.outFiles)
println()
Expand Down
5 changes: 5 additions & 0 deletions examples/complete/accounts.conf
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ exports += {
}]

}

dateConstraints += {
from = "2012-01-01"
to = "2013-12-31"
}
5 changes: 0 additions & 5 deletions examples/simple/accounts.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ reports += {
type = balance
accountMatch = ["^Income.*", "^Expenses.*"]
}

dateConstraints += {
from = "2013-1-1"
to = "2013-12-31"
}

0 comments on commit e45b772

Please sign in to comment.