Skip to content

Commit

Permalink
Add test for inserting cards of different types to ensure type inform…
Browse files Browse the repository at this point in the history
…ation is persisted
  • Loading branch information
jonathonherbert committed Apr 29, 2024
1 parent b02a873 commit 793613c
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 393 deletions.
5 changes: 4 additions & 1 deletion app/controllers/EditionsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ class EditionsController(db: EditionsDB,
(LocalDate.parse(dateFrom), LocalDate.parse(dateTo))
}.map {
case (localDateFrom, localDateTo) =>
Ok(Json.toJson(db.listIssues(edition, localDateFrom, localDateTo).flatMap(_.toOption)))
db.listIssues(edition, localDateFrom, localDateTo) match {
case Right(issues) => Ok(Json.toJson(issues))
case Left(errors) => InternalServerError(s"Error listing issues: ${errors.mkString(", ")}")
}
}.getOrElse(BadRequest("Invalid or missing date"))
}

Expand Down
3 changes: 2 additions & 1 deletion app/services/editions/db/CollectionsQueries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ trait CollectionsQueries {
INSERT INTO cards (
collection_id,
id,
card_type,
index,
added_on,
metadata
) VALUES (${collection.id}, ${card.id}, $index, $addedOn, ${card.metadata.map(m => Json.toJson(m).toString)}::JSONB)
) VALUES (${collection.id}, ${card.id}, ${card.cardType.entryName}, $index, $addedOn, ${card.metadata.map(m => Json.toJson(m).toString)}::JSONB)
""".execute().apply()
}

Expand Down
35 changes: 20 additions & 15 deletions app/services/editions/db/IssueQueries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,26 @@ trait IssueQueries extends Logging {
}

def listIssues(edition: Edition, dateFrom: LocalDate, dateTo: LocalDate) = DB readOnly { implicit session =>
sql"""
SELECT
id,
name,
issue_date,
timezone_id,
created_on,
created_by,
created_email,
launched_on,
launched_by,
launched_email
FROM edition_issues
WHERE issue_date BETWEEN $dateFrom AND $dateTo AND name = ${edition.entryName}
""".map(EditionsIssue.fromRow(_)).list().apply()
val maybeIssues = sql"""
SELECT
id,
name,
issue_date,
timezone_id,
created_on,
created_by,
created_email,
launched_on,
launched_by,
launched_email
FROM edition_issues
WHERE issue_date BETWEEN $dateFrom AND $dateTo AND name = ${edition.entryName}
""".map(EditionsIssue.fromRow(_)).list().apply()

maybeIssues.partitionMap(identity) match {
case (Nil, rights) => Right(rights)
case (lefts, _) => Left(lefts)
}
}

def getIssueIdFromCollectionId(collectionId: String): Option[String] = DB readOnly { implicit session =>
Expand Down
4 changes: 2 additions & 2 deletions test/logic/EditionsCheckerTest.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package logic

import java.time.LocalDate

import model.editions.Edition.DailyEdition
import model.editions.{EditionsCard, EditionsCollection, EditionsFront, EditionsFrontMetadata, EditionsIssue}
import model.editions.{CuratedPlatform, EditionsCard, EditionsCollection, EditionsFront, EditionsFrontMetadata, EditionsIssue}
import org.scalatest.{FreeSpec, Matchers}

class EditionsCheckerTest extends FreeSpec with Matchers {
Expand Down Expand Up @@ -96,6 +95,7 @@ class EditionsCheckerTest extends FreeSpec with Matchers {
EditionsIssue(
"id",
DailyEdition,
CuratedPlatform.Editions,
"timezoneId",
LocalDate.now(),
LocalDate.now().toEpochDay,
Expand Down
1 change: 1 addition & 0 deletions test/model/editions/PublishedIssueSerialisationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PublishedIssueSerialisationTest extends FreeSpec with Matchers {
val issue: EditionsIssue = EditionsIssue(
id = "4290573248905743296789524389623",
edition = Edition.DailyEdition,
platform = CuratedPlatform.Editions,
timezoneId = "Europe/London",
issueDate = issueDate,
createdOn = 0,
Expand Down
1 change: 1 addition & 0 deletions test/model/editions/PublishedIssueTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PublishedIssueTest extends FreeSpec with Matchers with OptionValues {
EditionsIssue(
"test-edition",
Edition.DailyEdition,
CuratedPlatform.Editions,
LondonZone.toString,
date,
dateTimeMilli,
Expand Down
Loading

0 comments on commit 793613c

Please sign in to comment.