Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for cli and small code refactor #323

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 20 additions & 39 deletions modules/ingestor/src/main/scala/cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,21 @@ object cli
case opts: IndexOpts => index(opts)
case opts: WatchOpts => watch(opts)

import opts.{ All, Single }
def index(opts: IndexOpts): IO[Unit] =
opts.index match
case All =>
case Index.Forum =>
forum.run(opts.since, opts.until, opts.dry).compile.drain
case Index.Study =>
study.run(opts.since, opts.until, opts.dry).compile.drain
case Index.Game =>
game.run(opts.since, opts.until, opts.dry).compile.drain
case Index.Team =>
team.run(opts.since, opts.until, opts.dry).compile.drain
case _ =>
forum.run(opts.since, opts.until, opts.dry).compile.drain *>
study.run(opts.since, opts.until, opts.dry).compile.drain *>
game.run(opts.since, opts.until, opts.dry).compile.drain *>
team.run(opts.since, opts.until, opts.dry).compile.drain
case Single(index) =>
index match
case Index.Forum =>
forum.run(opts.since, opts.until, opts.dry).compile.drain
case Index.Study =>
study.run(opts.since, opts.until, opts.dry).compile.drain
case Index.Game =>
game.run(opts.since, opts.until, opts.dry).compile.drain
case Index.Team =>
team.run(opts.since, opts.until, opts.dry).compile.drain

def watch(opts: WatchOpts): IO[Unit] =
opts.index match
Expand All @@ -79,9 +76,7 @@ object cli
case _ => IO.println("We only support game watch for now")

object opts:
case class Single(index: Index)
object All
case class IndexOpts(index: Single | All.type, since: Instant, until: Instant, dry: Boolean)
case class IndexOpts(index: Index | Unit, since: Instant, until: Instant, dry: Boolean)
case class WatchOpts(index: Index, since: Instant, dry: Boolean)

def parse = Opts.subcommand("index", "index documents")(indexOpt) <+>
Expand All @@ -95,20 +90,20 @@ object opts:
short = "i",
metavar = "forum|team|study|game"
)
.map(Single.apply)

val allIndexOpt =
Opts
.flag(
long = "all",
help = "All indexes"
)
.as(All)
.flag(long = "all", help = "All indexes")
.void

val inputOpts = singleIndexOpt orElse allIndexOpt
val dryOpt =
Opts
.flag(long = "dry", help = "Dry run", short = "d")
.orNone
.map(_.isDefined)

val indexOpt = (
inputOpts,
singleIndexOpt orElse allIndexOpt,
Opts.option[Instant](
long = "since",
help = "Index all documents since",
Expand All @@ -123,14 +118,7 @@ object opts:
metavar = "time in epoch seconds"
)
.orElse(Instant.now.pure[Opts]),
Opts
.flag(
long = "dry",
help = "Dry run",
short = "d"
)
.orNone
.map(_.isDefined)
dryOpt
).mapN(IndexOpts.apply)
.mapValidated(x =>
if x.until.isAfter(x.since) then Validated.valid(x)
Expand All @@ -152,14 +140,7 @@ object opts:
metavar = "time in epoch seconds"
)
.orElse(Instant.now.pure[Opts]),
Opts
.flag(
long = "dry",
help = "Dry run",
short = "d"
)
.orNone
.map(_.isDefined)
dryOpt
).mapN(WatchOpts.apply)

given Argument[Index] =
Expand Down
27 changes: 27 additions & 0 deletions modules/ingestor/src/test/scala/ClITest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package lila.search
package ingestor

import cats.syntax.all.*
import com.monovore.decline
import com.monovore.decline.Help

import java.time.Instant

import opts.*

object CLITest extends weaver.FunSuite:

def testCommand(args: String*): Either[Help, IndexOpts | WatchOpts] =
decline.Command("test", "Test Command")(opts.parse).parse(args)

test("index command"):
expect(
testCommand("index", "--index", "team", "--since", "0", "--until", "1", "--dry") ==
IndexOpts(Index.Team, Instant.ofEpochSecond(0), Instant.ofEpochSecond(1), true).asRight
)

test("watch command"):
expect(
testCommand("watch", "--index", "team", "--since", "0", "--dry") ==
WatchOpts(Index.Team, Instant.ofEpochSecond(0), true).asRight
)
Loading