Files
WeatherTool/src/test/scala/db/FileServiceSpec.scala
T

79 lines
2.6 KiB
Scala
Raw Normal View History

2023-05-25 13:06:39 +03:00
package db
import cats.effect.unsafe.implicits.global
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
2023-06-22 13:59:44 +03:00
import java.io.IOException
2023-05-25 13:06:39 +03:00
import java.time.format.DateTimeFormatter
import java.time.{LocalDate, LocalDateTime}
2023-10-22 23:51:59 +03:00
class FileServiceSpec extends AnyFunSuite with Matchers {
2023-05-25 13:06:39 +03:00
private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
test("getDateFileNames should return correct file names") {
2023-10-22 23:51:59 +03:00
val fileService = FileService.of.unsafeRunSync()
val datesList = fileService.getDateFileNames(LocalDate.of(2023, 5, 15))
2023-05-25 13:06:39 +03:00
.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)
2023-10-22 23:51:59 +03:00
val fileService = FileService.of.unsafeRunSync()
val lines = fileService.getInRange(from, to).unsafeRunSync()
2023-05-25 13:06:39 +03:00
lines.length shouldBe 2142
}
2023-06-22 13:59:44 +03:00
2023-08-11 16:22:15 +03:00
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),
)
2023-10-22 23:51:59 +03:00
val fileService = FileService.of.unsafeRunSync()
val dates = fileService.getDatesByMonths(monthList).unsafeRunSync()
2023-08-11 16:22:15 +03:00
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
}
2023-06-22 13:59:44 +03:00
test("DBService.save should return correct result") {
2023-10-22 23:51:59 +03:00
val fileService = FileService.of.unsafeRunSync()
2023-06-22 13:59:44 +03:00
val fileName = "testFile.txt"
val fileContent = "test content..."
2023-10-22 23:51:59 +03:00
fileService.save(fileName, fileContent).unsafeRunSync() shouldEqual fileName
2023-06-22 13:59:44 +03:00
}
test("DBService.save returns error on invalid file name") {
2023-10-22 23:51:59 +03:00
val fileService = FileService.of.unsafeRunSync()
2023-06-22 13:59:44 +03:00
val fileName = "/invalid/file/name"
val fileContent = "test content..."
2023-10-22 23:51:59 +03:00
val result = fileService.save(fileName, fileContent).attempt.unsafeRunSync()
2023-06-22 13:59:44 +03:00
result match {
case Left(e) =>
assert(e.isInstanceOf[IOException])
case Right(_) => fail("Expected failure did not occur")
}
}
2023-05-25 13:06:39 +03:00
}