|
| 1 | +package fide |
| 2 | +package cli |
| 3 | +package test |
| 4 | + |
| 5 | +import cats.effect.IO |
| 6 | +import cats.effect.kernel.Resource |
| 7 | +import cats.syntax.all.* |
| 8 | +import com.comcast.ip4s.* |
| 9 | +import fide.db.test.Containers |
| 10 | +import fide.db.{ Db, HistoryDb, PostgresConfig } |
| 11 | +import fide.domain.Models.* |
| 12 | +import fide.types.* |
| 13 | +import io.github.iltotore.iron.* |
| 14 | +import org.typelevel.log4cats.Logger |
| 15 | +import org.typelevel.log4cats.noop.NoOpLogger |
| 16 | +import weaver.* |
| 17 | + |
| 18 | +import java.nio.file.{ Files as JFiles, Path } |
| 19 | + |
| 20 | +object HistoryIngestorSuite extends SimpleIOSuite: |
| 21 | + |
| 22 | + given Logger[IO] = NoOpLogger[IO] |
| 23 | + |
| 24 | + private val defaultPage = Pagination(PageNumber(1), PageSize(100)) |
| 25 | + |
| 26 | + // Dummy postgres config — never used since HistoryIngestor receives db/historyDb directly |
| 27 | + private val dummyPg = PostgresConfig(ip"0.0.0.0", port"5432", "", "", "", 1, "fide", false, false) |
| 28 | + |
| 29 | + private def mkConfig( |
| 30 | + dir: Path, |
| 31 | + startMonth: Option[YearMonth] = none, |
| 32 | + endMonth: Option[YearMonth] = none |
| 33 | + ): IngestConfig = |
| 34 | + IngestConfig(dir, startMonth, endMonth, dummyPg, 100) |
| 35 | + |
| 36 | + private def resourceWithFeds: Resource[IO, (HistoryDb, Db)] = |
| 37 | + Containers.createResource.map(x => (HistoryDb(x.postgres, 100), Db(x.postgres))) |
| 38 | + |
| 39 | + private def writeCsv(dir: java.nio.file.Path, filename: String, content: String): IO[Unit] = |
| 40 | + IO: |
| 41 | + JFiles.writeString(dir.resolve(filename), content) |
| 42 | + () |
| 43 | + |
| 44 | + private val csvHeader = |
| 45 | + "id,name,title,womenTitle,otherTitles,standard,standardK,rapid,rapidK,blitz,blitzK,gender,birthYear,active,federationId" |
| 46 | + |
| 47 | + private def playerRow( |
| 48 | + id: Int, |
| 49 | + name: String, |
| 50 | + fedId: String = "USA", |
| 51 | + standard: Int = 2700, |
| 52 | + active: Boolean = true |
| 53 | + ) = |
| 54 | + s"$id,$name,GM,,,${standard},40,2600,40,2500,40,M,1990,$active,$fedId" |
| 55 | + |
| 56 | + // fide-ced: CSV discovery tests |
| 57 | + test("discovers only yyyy-MM.csv files, ignores others"): |
| 58 | + resourceWithFeds.use: (historyDb, db) => |
| 59 | + IO(JFiles.createTempDirectory("fide-test")).flatMap: tmpDir => |
| 60 | + for |
| 61 | + _ <- writeCsv(tmpDir, "2024-01.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 62 | + _ <- writeCsv(tmpDir, "notes.csv", s"$csvHeader\n${playerRow(2, "Bob")}") |
| 63 | + _ <- writeCsv(tmpDir, "backup.csv", s"$csvHeader\n${playerRow(3, "Charlie")}") |
| 64 | + config = mkConfig(tmpDir) |
| 65 | + ingestor = HistoryIngestor(historyDb, db, config) |
| 66 | + _ <- ingestor.ingest |
| 67 | + months <- historyDb.availableMonths |
| 68 | + yield expect(months.size == 1) and |
| 69 | + expect(months.head == YearMonth(2024, 1)) |
| 70 | + |
| 71 | + test("startMonth filter only ingests files >= startMonth"): |
| 72 | + resourceWithFeds.use: (historyDb, db) => |
| 73 | + IO(JFiles.createTempDirectory("fide-test")).flatMap: tmpDir => |
| 74 | + for |
| 75 | + _ <- writeCsv(tmpDir, "2024-01.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 76 | + _ <- writeCsv(tmpDir, "2024-02.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 77 | + _ <- writeCsv(tmpDir, "2024-03.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 78 | + config = mkConfig(tmpDir, startMonth = YearMonth(2024, 2).some) |
| 79 | + ingestor = HistoryIngestor(historyDb, db, config) |
| 80 | + _ <- ingestor.ingest |
| 81 | + months <- historyDb.availableMonths |
| 82 | + yield expect(months.size == 2) and |
| 83 | + expect(!months.contains(YearMonth(2024, 1))) |
| 84 | + |
| 85 | + test("endMonth filter only ingests files <= endMonth"): |
| 86 | + resourceWithFeds.use: (historyDb, db) => |
| 87 | + IO(JFiles.createTempDirectory("fide-test")).flatMap: tmpDir => |
| 88 | + for |
| 89 | + _ <- writeCsv(tmpDir, "2024-01.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 90 | + _ <- writeCsv(tmpDir, "2024-02.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 91 | + _ <- writeCsv(tmpDir, "2024-03.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 92 | + config = mkConfig(tmpDir, endMonth = YearMonth(2024, 2).some) |
| 93 | + ingestor = HistoryIngestor(historyDb, db, config) |
| 94 | + _ <- ingestor.ingest |
| 95 | + months <- historyDb.availableMonths |
| 96 | + yield expect(months.size == 2) and |
| 97 | + expect(!months.contains(YearMonth(2024, 3))) |
| 98 | + |
| 99 | + // fide-u4h: end-to-end integration test |
| 100 | + test("full pipeline: CSV to queryable history"): |
| 101 | + resourceWithFeds.use: (historyDb, db) => |
| 102 | + IO(JFiles.createTempDirectory("fide-test")).flatMap: tmpDir => |
| 103 | + val jan2024 = YearMonth(2024, 1) |
| 104 | + val feb2024 = YearMonth(2024, 2) |
| 105 | + for |
| 106 | + _ <- writeCsv( |
| 107 | + tmpDir, |
| 108 | + "2024-01.csv", |
| 109 | + s"$csvHeader\n${playerRow(1, "Alice")}\n${playerRow(2, "Bob")}" |
| 110 | + ) |
| 111 | + _ <- writeCsv(tmpDir, "2024-02.csv", s"$csvHeader\n${playerRow(1, "Alice", standard = 2750)}") |
| 112 | + config = mkConfig(tmpDir) |
| 113 | + ingestor = HistoryIngestor(historyDb, db, config) |
| 114 | + _ <- ingestor.ingest |
| 115 | + // Verify available months |
| 116 | + months <- historyDb.availableMonths |
| 117 | + // Verify player_info populated |
| 118 | + janPlayers <- historyDb.allPlayers( |
| 119 | + jan2024, |
| 120 | + Sorting(SortBy.Name, Order.Asc), |
| 121 | + defaultPage, |
| 122 | + PlayerFilter.default |
| 123 | + ) |
| 124 | + // Verify per-month snapshots |
| 125 | + febPlayers <- historyDb.allPlayers( |
| 126 | + feb2024, |
| 127 | + Sorting(SortBy.Name, Order.Asc), |
| 128 | + defaultPage, |
| 129 | + PlayerFilter.default |
| 130 | + ) |
| 131 | + // Verify playerById |
| 132 | + alice <- historyDb.playerById(PlayerId(1), feb2024) |
| 133 | + // Verify federation populated |
| 134 | + feds <- db.allFederations |
| 135 | + yield expect(months.size == 2) and |
| 136 | + expect(janPlayers.size == 2) and |
| 137 | + expect(janPlayers.head.name == "Alice") and |
| 138 | + expect(febPlayers.size == 1) and |
| 139 | + expect(alice.isDefined) and |
| 140 | + expect(alice.get.standard.contains(Rating(2750))) and |
| 141 | + expect(feds.exists(_.id == FederationId("USA"))) |
| 142 | + |
| 143 | + test("re-running ingest is idempotent"): |
| 144 | + resourceWithFeds.use: (historyDb, db) => |
| 145 | + IO(JFiles.createTempDirectory("fide-test")).flatMap: tmpDir => |
| 146 | + val jan2024 = YearMonth(2024, 1) |
| 147 | + for |
| 148 | + _ <- writeCsv(tmpDir, "2024-01.csv", s"$csvHeader\n${playerRow(1, "Alice")}") |
| 149 | + config = mkConfig(tmpDir) |
| 150 | + ingestor = HistoryIngestor(historyDb, db, config) |
| 151 | + _ <- ingestor.ingest |
| 152 | + _ <- ingestor.ingest // second run |
| 153 | + players <- historyDb.allPlayers( |
| 154 | + jan2024, |
| 155 | + Sorting(SortBy.Name, Order.Asc), |
| 156 | + defaultPage, |
| 157 | + PlayerFilter.default |
| 158 | + ) |
| 159 | + yield expect(players.size == 1) // no duplicates |
0 commit comments