Refactor main files to test files
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
package db
|
||||
|
||||
import cats.effect.IO
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import cats.implicits.toTraverseOps
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
|
||||
import java.time.{LocalDate, LocalDateTime}
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
object Main {
|
||||
private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
|
||||
private def testGetInRange: IO[Unit] = {
|
||||
val from = LocalDateTime.parse("20230414_2200", dateFormatter)
|
||||
val to = LocalDateTime.parse("20230501_1230", dateFormatter)
|
||||
|
||||
for {
|
||||
log <- Slf4jLogger.create[IO]
|
||||
dbService <- DBService.of
|
||||
lines <- dbService.getInRange(from, to)
|
||||
_ <- lines.traverse(log.info(_))
|
||||
} yield ()
|
||||
}
|
||||
|
||||
private def testGetDates: IO[Unit] = {
|
||||
for {
|
||||
log <- Slf4jLogger.create[IO]
|
||||
dbService <- DBService.of
|
||||
dates <- dbService.getDates
|
||||
_ <- log.info(s"$dates")
|
||||
} yield ()
|
||||
}
|
||||
|
||||
private def testGetDate: IO[Unit] = {
|
||||
for {
|
||||
log <- Slf4jLogger.create[IO]
|
||||
dbService <- DBService.of
|
||||
dates <- dbService.getDateFileNames(LocalDate.of(2023, 5, 15))
|
||||
_ <- log.info(s"$dates")
|
||||
} yield ()
|
||||
}
|
||||
|
||||
private def testDataService: IO[Unit] = {
|
||||
for {
|
||||
dbService <- DBService.of
|
||||
dataService <- DataService.of(dbService)
|
||||
} yield ()
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
// testGetInRange.unsafeRunSync()
|
||||
// testGetDates.unsafeRunSync()
|
||||
// testGetDate.unsafeRunSync()
|
||||
testDataService.unsafeRunSync()
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package fetch
|
||||
|
||||
import cats.effect.IO
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import cats.implicits.toTraverseOps
|
||||
import db.DBService
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
|
||||
import java.time.LocalDateTime
|
||||
|
||||
object Main {
|
||||
// def run: IO[Unit] = {
|
||||
// val from = LocalDateTime.of(2023, 4, 28, 10, 0)
|
||||
// val to = LocalDateTime.of(2023, 4, 28, 13, 30)
|
||||
// for {
|
||||
// log <- Slf4jLogger.create[IO]
|
||||
// fetch <- FetchService.of
|
||||
// // fetchResultEither <- fetch.fetchFromDate(LocalDate.of(2023, 4, 28)).attempt
|
||||
// fetchResultEither <- fetch.fetchInRange(from, to).attempt
|
||||
// fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
|
||||
// fetchResult = fetchResultEither.getOrElse(List.empty)
|
||||
// (fetchErrors, successDownloads) = fetchResult.partitionMap(identity)
|
||||
// dbService <- DBService.of
|
||||
// saveResults <- successDownloads.traverse { case (name, content) => dbService.save(name, content) }
|
||||
// (saveErrors, successSaves) = saveResults.partitionMap(identity)
|
||||
// successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
|
||||
// errors = fetchServiceError ++ fetchErrors.map(e => s"FetchError: ${e.getMessage}") ++ saveErrors.map(e => s"SaveError: ${e.getMessage}")
|
||||
// _ <- log.info(s"errors: $errors")
|
||||
// _ <- log.info(s"successes: $successes")
|
||||
// } yield (successes, errors)
|
||||
// }
|
||||
|
||||
def run: IO[Unit] = {
|
||||
for {
|
||||
fetch <- FetchService.of
|
||||
fetchResultEither <- fetch.fetchSingleFile("20230524_0030.csv").attempt
|
||||
fetchResultEither <- fetch.fetchSingleFile("20230522_0130.csv").attempt
|
||||
fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
|
||||
fetchResult = fetchResultEither.flatMap(res => res.flatMap(aaa => {
|
||||
println(s"fffffff: ${aaa._1}")
|
||||
Right(aaa._1)
|
||||
}))
|
||||
// _ = println(s"${fetchResult.map()}")
|
||||
} yield ()
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
run.unsafeRunSync()
|
||||
}
|
||||
}
|
||||
@@ -53,8 +53,6 @@ object Aggregate {
|
||||
def getKeys: List[String] = {
|
||||
val runtimeMirror = scala.reflect.runtime.currentMirror
|
||||
val classSymbol = runtimeMirror.classSymbol(classOf[AggregateValue])
|
||||
|
||||
// Get all case classes that extend the AggregateValue trait
|
||||
val keys = classSymbol.knownDirectSubclasses.map(_.name.toString).toList
|
||||
|
||||
keys
|
||||
@@ -97,7 +95,6 @@ object Aggregate {
|
||||
case "dewPoint" => weatherData.map(_.dewPoint)
|
||||
case "humidity" => weatherData.map(_.humidity)
|
||||
case "sunDuration" => weatherData.map(_.sunDuration)
|
||||
// case "phenomena" => weatherData.map(_..phenomena)
|
||||
case _ => List.empty
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package parse
|
||||
|
||||
import cats.effect.IO
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import db.DBService
|
||||
import io.circe.syntax.EncoderOps
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
import parse.Aggregate.AggregateValueImplicits.aggregateValueEncoder
|
||||
import parse.Aggregate.{AggregateKey, UserQuery}
|
||||
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
object Main {
|
||||
private def run: IO[Unit] = {
|
||||
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)
|
||||
// val userQuery = UserQuery(List("Daugavgrīva"), "precipitation", AggregateKey.List)
|
||||
|
||||
for {
|
||||
log <- Slf4jLogger.create[IO]
|
||||
dbService <- DBService.of
|
||||
lines <- dbService.getInRange(from, to)
|
||||
parsed <- IO(Parser.queryData(userQuery, lines))
|
||||
_ <- log.info(parsed.asJson.toString)
|
||||
} yield ()
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
run.unsafeRunSync()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package db
|
||||
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.{LocalDate, LocalDateTime}
|
||||
|
||||
|
||||
class DBServiceSpec extends AnyFunSuite with Matchers {
|
||||
private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
|
||||
test("getDateFileNames should return correct file names") {
|
||||
val dbService = DBService.of.unsafeRunSync()
|
||||
val datesList = dbService.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 dbService = DBService.of.unsafeRunSync()
|
||||
val lines = dbService.getInRange(from, to).unsafeRunSync()
|
||||
|
||||
lines.length shouldBe 2142
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package fetch
|
||||
|
||||
import cats.effect.IO
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import cats.implicits.toTraverseOps
|
||||
import db.DBService
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
|
||||
import java.time.LocalDateTime
|
||||
|
||||
class FetchServiceSpec extends AnyFunSuite with Matchers {
|
||||
// def fetchInRange: IO[Unit] = {
|
||||
// val from = LocalDateTime.of(2023, 4, 28, 10, 0)
|
||||
// val to = LocalDateTime.of(2023, 4, 28, 13, 30)
|
||||
// for {
|
||||
// log <- Slf4jLogger.create[IO]
|
||||
// fetch <- FetchService.of
|
||||
// // fetchResultEither <- fetch.fetchFromDate(LocalDate.of(2023, 4, 28)).attempt
|
||||
// fetchResultEither <- fetch.fetchInRange(from, to).attempt
|
||||
// fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
|
||||
// fetchResult = fetchResultEither.getOrElse(List.empty)
|
||||
// (fetchErrors, successDownloads) = fetchResult.partitionMap(identity)
|
||||
// dbService <- DBService.of
|
||||
// saveResults <- successDownloads.traverse { case (name, content) => dbService.save(name, content) }
|
||||
// (saveErrors, successSaves) = saveResults.partitionMap(identity)
|
||||
// successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
|
||||
// errors = fetchServiceError ++ fetchErrors.map(e => s"FetchError: ${e.getMessage}") ++ saveErrors.map(e => s"SaveError: ${e.getMessage}")
|
||||
// _ <- log.info(s"errors: $errors")
|
||||
// _ <- log.info(s"successes: $successes")
|
||||
// } yield (successes, errors)
|
||||
// }
|
||||
//
|
||||
// def fetchSingleFile: IO[Unit] = {
|
||||
// for {
|
||||
// fetch <- FetchService.of
|
||||
// fetchResultEither <- fetch.fetchSingleFile("20230524_0030.csv").attempt
|
||||
// fetchResultEither <- fetch.fetchSingleFile("20230522_0130.csv").attempt
|
||||
// fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
|
||||
// fetchResult = fetchResultEither.flatMap(res => res.flatMap(aaa => {
|
||||
// println(s"fffffff: ${aaa._1}")
|
||||
// Right(aaa._1)
|
||||
// }))
|
||||
// // _ = println(s"${fetchResult.map()}")
|
||||
// } yield ()
|
||||
// }
|
||||
//
|
||||
// def main(args: Array[String]): Unit = {
|
||||
// run.unsafeRunSync()
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package parse
|
||||
|
||||
import cats.effect.unsafe.implicits.global
|
||||
import db.DBService
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
import parse.Aggregate.{AggregateKey, DoubleValue, TimeDoubleList, UserQuery}
|
||||
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
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)
|
||||
|
||||
val dbService = DBService.of.unsafeRunSync()
|
||||
|
||||
val lines = dbService.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)
|
||||
|
||||
val dbService = DBService.of.unsafeRunSync()
|
||||
|
||||
val lines = dbService.getInRange(from, to).unsafeRunSync()
|
||||
val parsed = Parser.queryData(userQuery, lines)
|
||||
|
||||
parsed shouldBe HashMap(
|
||||
"Rīga" ->
|
||||
Some(TimeDoubleList(List(
|
||||
(LocalDateTime.parse("2023-05-16T04:00"), Some(1.9)),
|
||||
(LocalDateTime.parse("2023-05-16T05:00"), Some(4.5)),
|
||||
(LocalDateTime.parse("2023-05-16T06:00"), Some(1.5)),
|
||||
(LocalDateTime.parse("2023-05-16T07:00"), Some(0.0)),
|
||||
)))
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user