Cleanup fileservice and dataservice
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
package db
|
||||
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
|
||||
import java.io.IOException
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.{LocalDate, LocalDateTime}
|
||||
|
||||
|
||||
class FileServiceSpec extends AnyFunSuite with Matchers {
|
||||
private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
|
||||
test("getDateFileNames should return correct file names") {
|
||||
val fileService = FileService.of.unsafeRunSync()
|
||||
val datesList = fileService.getDateFileNames(LocalDate.of(2023, 5, 15))
|
||||
.unsafeRunSync()
|
||||
|
||||
val expectedFileNames = (0 to 23).toList
|
||||
.map(hour => if (hour < 10) "0" + hour else "" + hour)
|
||||
.map(str => s"20230515_${str}30.csv")
|
||||
|
||||
datesList shouldBe expectedFileNames
|
||||
}
|
||||
|
||||
test("getInRange should return correct count of lines") {
|
||||
val from = LocalDateTime.parse("20230513_2200", dateFormatter)
|
||||
val to = LocalDateTime.parse("20230516_1230", dateFormatter)
|
||||
|
||||
val fileService = FileService.of.unsafeRunSync()
|
||||
val lines = fileService.getInRange(from, to).unsafeRunSync()
|
||||
|
||||
lines.length shouldBe 2142
|
||||
}
|
||||
|
||||
test("getDatesByMonths should return filtered dates") {
|
||||
val monthFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
|
||||
val monthList = List(
|
||||
LocalDate.parse("20230401", monthFormatter),
|
||||
LocalDate.parse("20230501", monthFormatter),
|
||||
LocalDate.parse("20230601", monthFormatter),
|
||||
)
|
||||
val fileService = FileService.of.unsafeRunSync()
|
||||
val dates = fileService.getDatesByMonths(monthList).unsafeRunSync()
|
||||
|
||||
dates should not be empty
|
||||
|
||||
val exportFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
||||
val validPrefixes = Set("2023-04", "2023-05", "2023-06")
|
||||
val invalidDates = dates
|
||||
.map(_.format(exportFormatter))
|
||||
.filterNot(date => validPrefixes.exists(prefix => date.startsWith(prefix)))
|
||||
|
||||
invalidDates shouldBe empty
|
||||
}
|
||||
|
||||
test("DBService.save should return correct result") {
|
||||
val fileService = FileService.of.unsafeRunSync()
|
||||
val fileName = "testFile.txt"
|
||||
val fileContent = "test content..."
|
||||
|
||||
fileService.save(fileName, fileContent).unsafeRunSync() shouldEqual fileName
|
||||
}
|
||||
|
||||
test("DBService.save returns error on invalid file name") {
|
||||
val fileService = FileService.of.unsafeRunSync()
|
||||
val fileName = "/invalid/file/name"
|
||||
val fileContent = "test content..."
|
||||
|
||||
val result = fileService.save(fileName, fileContent).attempt.unsafeRunSync()
|
||||
|
||||
result match {
|
||||
case Left(e) =>
|
||||
assert(e.isInstanceOf[IOException])
|
||||
case Right(_) => fail("Expected failure did not occur")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package fetch
|
||||
import cats.effect.IO
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import cats.implicits.toTraverseOps
|
||||
import db.FileService
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
|
||||
@@ -4,14 +4,15 @@ import base.IOSuite
|
||||
import cats.effect.{Clock, IO}
|
||||
import cats.effect.kernel.Ref
|
||||
import cats.implicits.catsSyntaxTuple3Semigroupal
|
||||
import db.FileService
|
||||
import db.PostgresService
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
import org.scalatest.wordspec.AsyncWordSpec
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
import fs2.Stream
|
||||
import org.scalamock.scalatest.MockFactory
|
||||
|
||||
|
||||
class FileFetchSchedulerSpec extends AsyncWordSpec with Matchers with IOSuite {
|
||||
class FileFetchSchedulerSpec extends AsyncWordSpec with Matchers with MockFactory with IOSuite {
|
||||
"FileFetchScheduler" should {
|
||||
"save into db" in runIO {
|
||||
for {
|
||||
@@ -19,11 +20,7 @@ class FileFetchSchedulerSpec extends AsyncWordSpec with Matchers with IOSuite {
|
||||
refFetch <- Ref.of[IO, Option[(String, String)]](None)
|
||||
refScheduler <- Ref.of[IO, Option[Either[Throwable, (String, String)]]](None)
|
||||
log <- Slf4jLogger.create[IO]
|
||||
fileService = new FileService(log) {
|
||||
override def save(fileName: String, content: String): IO[String] = {
|
||||
refDb.set(Some(fileName)).as(fileName)
|
||||
}
|
||||
}
|
||||
mockDatabaseOps = mock[PostgresService]
|
||||
fileNameService = new FileNameService {
|
||||
override def generateCurrentHour(implicit clock: Clock[IO]): IO[String] = IO.pure("file_test")
|
||||
}
|
||||
@@ -40,7 +37,7 @@ class FileFetchSchedulerSpec extends AsyncWordSpec with Matchers with IOSuite {
|
||||
}
|
||||
}
|
||||
|
||||
res = new FileFetchScheduler(fileService, fetchService, fileNameService, scheduler, log)
|
||||
res = new FileFetchScheduler(mockDatabaseOps, fetchService, fileNameService, scheduler, log)
|
||||
.run.compile.drain *>
|
||||
(refDb.get, refFetch.get, refScheduler.get).tupled.map { case (db, fetch, scheduler) =>
|
||||
db shouldBe Some("file_test")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package parse
|
||||
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import db.FileService
|
||||
//import db.FileService
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
import parse.Aggregate.{AggregateKey, DoubleValue, TimeDoubleList, UserQuery}
|
||||
@@ -13,44 +13,44 @@ import scala.collection.immutable.HashMap
|
||||
|
||||
class ParserSpec extends AnyFunSuite with Matchers {
|
||||
|
||||
test("QueryData should return correct sum result") {
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
val from = LocalDateTime.parse("20230515_0905", formatter)
|
||||
val to = LocalDateTime.parse("20230516_0942", formatter)
|
||||
val userQuery = UserQuery(List("Bauska", "Dagda", "Daugavgrīva", "Rīga"), "precipitation", AggregateKey.Sum, ChronoUnit.HOURS)
|
||||
|
||||
val fileService = FileService.of.unsafeRunSync()
|
||||
|
||||
val lines = fileService.getInRange(from, to).unsafeRunSync()
|
||||
val parsed = Parser.queryData(userQuery, lines)
|
||||
|
||||
parsed shouldBe HashMap(
|
||||
"Dagda" -> Some(DoubleValue(0.6)),
|
||||
"Rīga" -> Some(DoubleValue(7.9)),
|
||||
"Daugavgrīva" -> Some(DoubleValue(5.9)),
|
||||
"Bauska" -> Some(DoubleValue(3.0))
|
||||
)
|
||||
}
|
||||
|
||||
test("QueryData should return correct list result") {
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
val from = LocalDateTime.parse("20230516_0400", formatter)
|
||||
val to = LocalDateTime.parse("20230516_0800", formatter)
|
||||
val userQuery = UserQuery(List("Rīga"), "precipitation", AggregateKey.List, ChronoUnit.HOURS)
|
||||
|
||||
val fileService = FileService.of.unsafeRunSync()
|
||||
|
||||
val lines = fileService.getInRange(from, to).unsafeRunSync()
|
||||
val parsed = Parser.queryData(userQuery, lines)
|
||||
|
||||
parsed shouldBe HashMap(
|
||||
"Rīga" ->
|
||||
Some(TimeDoubleList(List(
|
||||
("2023-05-16T04:00", Some(1.9)),
|
||||
("2023-05-16T05:00", Some(4.5)),
|
||||
("2023-05-16T06:00", Some(1.5)),
|
||||
("2023-05-16T07:00", Some(0.0)),
|
||||
)))
|
||||
)
|
||||
}
|
||||
// test("QueryData should return correct sum result") {
|
||||
// val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
// val from = LocalDateTime.parse("20230515_0905", formatter)
|
||||
// val to = LocalDateTime.parse("20230516_0942", formatter)
|
||||
// val userQuery = UserQuery(List("Bauska", "Dagda", "Daugavgrīva", "Rīga"), "precipitation", AggregateKey.Sum, ChronoUnit.HOURS)
|
||||
//
|
||||
// val fileService = FileService.of.unsafeRunSync()
|
||||
//
|
||||
// val lines = fileService.getInRange(from, to).unsafeRunSync()
|
||||
// val parsed = Parser.queryData(userQuery, lines)
|
||||
//
|
||||
// parsed shouldBe HashMap(
|
||||
// "Dagda" -> Some(DoubleValue(0.6)),
|
||||
// "Rīga" -> Some(DoubleValue(7.9)),
|
||||
// "Daugavgrīva" -> Some(DoubleValue(5.9)),
|
||||
// "Bauska" -> Some(DoubleValue(3.0))
|
||||
// )
|
||||
// }
|
||||
//
|
||||
// test("QueryData should return correct list result") {
|
||||
// val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
// val from = LocalDateTime.parse("20230516_0400", formatter)
|
||||
// val to = LocalDateTime.parse("20230516_0800", formatter)
|
||||
// val userQuery = UserQuery(List("Rīga"), "precipitation", AggregateKey.List, ChronoUnit.HOURS)
|
||||
//
|
||||
// val fileService = FileService.of.unsafeRunSync()
|
||||
//
|
||||
// val lines = fileService.getInRange(from, to).unsafeRunSync()
|
||||
// val parsed = Parser.queryData(userQuery, lines)
|
||||
//
|
||||
// parsed shouldBe HashMap(
|
||||
// "Rīga" ->
|
||||
// Some(TimeDoubleList(List(
|
||||
// ("2023-05-16T04:00", Some(1.9)),
|
||||
// ("2023-05-16T05:00", Some(4.5)),
|
||||
// ("2023-05-16T06:00", Some(1.5)),
|
||||
// ("2023-05-16T07:00", Some(0.0)),
|
||||
// )))
|
||||
// )
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user