StatefulFetchService to save last 24h of data

This commit is contained in:
Guntis Smaukstelis
2023-05-25 00:06:20 +03:00
parent 77026a9cb0
commit d40aa9ec9b
8 changed files with 159 additions and 33 deletions
+37 -2
View File
@@ -1,10 +1,16 @@
import cats.Applicative
import cats.effect.unsafe.implicits.global
import fetch.FileNameService
import org.scalatest.funsuite.AnyFunSuite
import cats.effect.{Clock, IO}
import java.util.concurrent.TimeUnit
import java.time.LocalDateTime
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.duration._
class FileNameServiceSpec extends AnyFunSuite {
test("FileNameService.generate returns correct file names") {
val startTime = LocalDateTime.of(2023, 5, 17, 0, 0)
val endTime = LocalDateTime.of(2023, 5, 17, 4, 0)
@@ -25,11 +31,40 @@ class FileNameServiceSpec extends AnyFunSuite {
val date = LocalDateTime.of(2023, 5, 17, 0, 0).toLocalDate
val expectedFileNames = (0 to 23).toList
.map(hour => if(hour < 10) "0"+hour else ""+hour)
.map(hour => if (hour < 10) "0" + hour else "" + hour)
.map(str => s"20230517_${str}30.csv")
val actualFileNames = new FileNameService().generateFromDate(date)
assert(actualFileNames == expectedFileNames)
}
val fixedClock: Clock[IO] = new Clock[IO] {
val timeInMillis = 1618862447000L
override def realTime: IO[FiniteDuration] =
IO.pure(Duration.fromNanos(TimeUnit.MILLISECONDS.toNanos(timeInMillis)))
override def applicative: Applicative[IO] =
Applicative.apply
override def monotonic: IO[FiniteDuration] =
IO.pure(Duration.fromNanos(TimeUnit.MILLISECONDS.toNanos(timeInMillis)))
override def timed[A](fa: IO[A]): IO[(FiniteDuration, A)] =
fa.map(a => (Duration.Zero, a))
}
test("FileNameService.generateLast24Hours returns correct file names for a fixed time") {
val fileNameService = new FileNameService()
val filenames = fileNameService.generateLast24Hours(fixedClock).unsafeRunSync()
assert(filenames.size == 24)
val expectedFileNames = (0 to 23).toList
.map(hour => if (hour < 10) "0" + hour else "" + hour)
.map(str => s"20210419_${str}30.csv")
assert(filenames.sorted == expectedFileNames.sorted)
}
}