Skip to content

Commit

Permalink
🐛 Refactor MenuParser and add RestaurantJobTest
Browse files Browse the repository at this point in the history
The MenuParser in both 'quadrilateroParser' and 'centralRibeiraoParser'
functions is updated to improve string cleaning and item finding. A new
test file, RestaurantJobTest.kt, is also introduced to ensure stable and
 reliable retrieval of restaurant menus. This refactor enhances parsing
 logic and data accuracy, and the added test assists in keeping the
 parsing operation error-free.
  • Loading branch information
LeoColman committed Mar 5, 2024
1 parent 1624aad commit d56ce31
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ val centralParser = MenuParser {
val quadrilateroParser = MenuParser {
val items = it.cleanItems()
if (items.size < 3) return@MenuParser closedMenuParser.parse(it)
val veg = items.find("Opção:").single()
val veg = items.find("Opção").single().cleanString()
val main = items.getOrNull(items.findIndex(veg) - 1)?.cleanString()
val dessert = items.getOrNull(items.lastIndex - 3)?.cleanString()
Menu(main, veg, dessert, items.cleanStrings(main, veg, dessert), it)
Expand All @@ -90,9 +90,9 @@ val largoSaoFranciscoParser = MenuParser {
val centralRibeiraoParser = MenuParser {
val items = it.cleanItems()
if (items.size < 3) return@MenuParser closedMenuParser.parse(it)
val veg = items.find("veg:").single()
val main = items.getOrNull(items.findIndex(veg) - 1)?.cleanString()
val des = items.getOrNull(items.lastIndex - 4)?.cleanString()
val main = items.firstOrNull()?.cleanString()
val veg = items.getOrNull(1)?.cleanString()
val des = items.getOrNull(5)?.cleanString()
Menu(main, veg, des, items.cleanStrings(main, veg, des), it)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package app.jopiter.restaurants

import app.jopiter.restaurants.model.Restaurant
import app.jopiter.restaurants.repository.RestaurantItemRepository
import app.jopiter.restaurants.repository.postgres.PostgresRestaurantItemRepository
import app.jopiter.restaurants.repository.usp.USPRestaurantItemRepository
import app.jopiter.restaurants.repository.usp.parsers
import io.kotest.core.extensions.install
import io.kotest.core.spec.style.FunSpec
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.extensions.testcontainers.JdbcTestContainerExtension
import io.kotest.matchers.shouldBe
import org.flywaydb.core.Flyway
import org.ktorm.database.Database
import org.testcontainers.containers.PostgreSQLContainer
import java.time.LocalDate

class RestaurantJobTest : ShouldSpec({

val postgresContainer = PostgreSQLContainer<Nothing>("postgres")
val datasource = install(JdbcTestContainerExtension(postgresContainer))
val flyway = Flyway.configure().cleanDisabled(false).dataSource(datasource).load()

val database = Database.connect(datasource)

beforeSpec {
flyway.clean()
flyway.migrate()
}

val postgresRepository = PostgresRestaurantItemRepository(database)
val uspRepository = USPRestaurantItemRepository("https://uspdigital.usp.br/rucard/servicos", parsers, "596df9effde6f877717b4e81fdb2ca9f")
val restaurantItemRepository = RestaurantItemRepository(uspRepository, postgresRepository)
val target = RestaurantJob(restaurantItemRepository)

context("Attempt to get every parseable restaurant menu for the current day") {
target.run()

Restaurant.entries.sortedBy { it.id }.forEach {
should("Not throw an error when parsing ${it.id}") {
val response = restaurantItemRepository.get(it.id, setOf(LocalDate.now()))
if(response.isEmpty()) {
// Apparently, Crhea was closed back in 2020-03, but still responds and is still listed
it shouldBe Restaurant.Crhea
}
response.take(2).forEach {
println("Example(${it.restaurantId}, \"${it.unparsedMenu.replace("\n", "\\n")}\", \"${it.mainItem}\", \"${it.vegetarianItem}\", \"${it.dessertItem}\", \"mudane\")")
}
}
}
}
})

0 comments on commit d56ce31

Please sign in to comment.