Skip to content

Commit

Permalink
Add test for pagination
Browse files Browse the repository at this point in the history
Also rename test file to match class under test
  • Loading branch information
jonathonherbert committed Jul 5, 2024
1 parent 7197375 commit aeddbf2
Showing 1 changed file with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import play.api.mvc.Results.NotFound

import java.time.OffsetDateTime

class DraftRulesSpec extends RuleFixture with Matchers with DBTest {
class DbRuleDraftSpec extends RuleFixture with Matchers with DBTest {

def assertDatesAreWithinRangeMs(date1: OffsetDateTime, date2: OffsetDateTime, range: Int) = {
date1.toInstant().toEpochMilli should be(date2.toInstant().toEpochMilli +- range)
Expand Down Expand Up @@ -65,6 +65,60 @@ class DraftRulesSpec extends RuleFixture with Matchers with DBTest {
published.isPublished should be(true)
}

it should "return pagination results when searching" in { implicit session =>
val totalNoOfRules = 25
val pageSize = 10
(1 to totalNoOfRules).foreach { ruleNo =>
DbRuleDraft
.create(
ruleType = "regex",
pattern = Some(s"Rule $ruleNo"),
user = "test.user",
ignore = false
)
.get
}

val results = DbRuleDraft.searchRules(
page = 1,
maybeWord = Some("Rule"),
sortBy = List("+pattern"),
pageSize = pageSize
)
results.data.size shouldBe pageSize
results.data.flatMap(_.pattern) shouldBe (1 to 10).map(n => s"Rule $n").toList
results.total shouldBe totalNoOfRules
results.page shouldBe 1
results.pageSize shouldBe pageSize
}

it should "return subsequent pages correctly" in { implicit session =>
val totalNoOfRules = 25
val pageSize = 10
(1 to totalNoOfRules).foreach { ruleNo =>
DbRuleDraft
.create(
ruleType = "regex",
pattern = Some(s"Rule $ruleNo"),
user = "test.user",
ignore = false
)
.get
}

val results = DbRuleDraft.searchRules(
page = 2,
maybeWord = Some("Rule"),
sortBy = List("+pattern"),
pageSize = pageSize
)
results.data.size shouldBe pageSize
results.data.flatMap(_.pattern) shouldBe (11 to 20).map(n => s"Rule $n")
results.total shouldBe totalNoOfRules
results.page shouldBe 2
results.pageSize shouldBe pageSize
}

it should "search rules using a partial search phrase – pattern" in { implicit session =>
val rule = DbRuleDraft
.create(
Expand Down

0 comments on commit aeddbf2

Please sign in to comment.