mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
- Add `photoUris` field to `MealEntry` and update Firestore rules. - Implement `UiEvent` handling in `MainActivity` for navigation/links. - Remove sensitive email data from error logs and clean up manifest.
104 lines
4.1 KiB
Kotlin
104 lines
4.1 KiB
Kotlin
package com.example
|
|
|
|
import com.example.data.model.FoodUnit
|
|
import com.example.data.model.MealEntry
|
|
import com.example.data.model.MealFoodItem
|
|
import com.example.data.model.MealType
|
|
import com.example.util.CsvImporter
|
|
import org.junit.Assert.assertEquals
|
|
import org.junit.Test
|
|
import java.io.ByteArrayInputStream
|
|
import java.time.LocalDateTime
|
|
import java.time.ZoneId
|
|
import java.time.format.DateTimeFormatter
|
|
import java.util.Locale
|
|
|
|
class CsvImporterTest {
|
|
|
|
@Test
|
|
fun testParseFoodItemsFormatted() {
|
|
val raw = "Saumon grillé (150 g) ; Riz basmati (180 g) ; Pomme (1 portion)"
|
|
val parsed = CsvImporter.parseFoodItems(raw)
|
|
|
|
assertEquals(3, parsed.size)
|
|
assertEquals("Saumon grillé", parsed[0].name)
|
|
assertEquals(150.0, parsed[0].weight ?: 0.0, 0.001)
|
|
assertEquals(FoodUnit.G, parsed[0].unit)
|
|
|
|
assertEquals("Riz basmati", parsed[1].name)
|
|
assertEquals(180.0, parsed[1].weight ?: 0.0, 0.001)
|
|
assertEquals(FoodUnit.G, parsed[1].unit)
|
|
|
|
assertEquals("Pomme", parsed[2].name)
|
|
assertEquals(1.0, parsed[2].weight ?: 0.0, 0.001)
|
|
assertEquals(FoodUnit.PORTION, parsed[2].unit)
|
|
}
|
|
|
|
@Test
|
|
fun testParseCsvStreamStandard() {
|
|
val csv = """
|
|
Date,Heure,Type de repas,Faim avant (/10),Ressenti faim,Satiété après (/10),Ressenti satiété,Aliments & Poids,Note globale,Photos jointes
|
|
08/11/2026,12:30,Déjeuner,6,"Faim présente",7,"Idéale","Riz basmati (150 g) ; Poulet (120 g)","Déjeuner au calme",0
|
|
08/11/2026,20:00,Dîner,4,"Petite faim",6,"Légère","Soupe de légumes (250 ml)","Repas léger",0
|
|
""".trimIndent()
|
|
|
|
val stream = ByteArrayInputStream(csv.toByteArray(Charsets.UTF_8))
|
|
val result = CsvImporter.parseCsvStream(stream, emptyList()).getOrThrow()
|
|
|
|
assertEquals(2, result.importedMeals.size)
|
|
assertEquals(0, result.skippedDuplicatesCount)
|
|
assertEquals(3, result.newFoodItems.size) // Riz basmati, Poulet, Soupe de légumes
|
|
|
|
val firstMeal = result.importedMeals[0]
|
|
assertEquals(MealType.LUNCH.name, firstMeal.mealType)
|
|
assertEquals(6, firstMeal.hungerBefore)
|
|
assertEquals(7, firstMeal.satietyAfter)
|
|
assertEquals(2, firstMeal.parsedFoodItems().size)
|
|
}
|
|
|
|
@Test
|
|
fun testDeduplicationIgnoresExistingMeal() {
|
|
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm", Locale.FRENCH)
|
|
val timestamp = LocalDateTime.parse("08/11/2026 12:30", formatter)
|
|
.atZone(ZoneId.systemDefault())
|
|
.toInstant()
|
|
.toEpochMilli()
|
|
|
|
val existingMeal = MealEntry(
|
|
id = "existing_1",
|
|
mealType = MealType.LUNCH.name,
|
|
mealTimestamp = timestamp
|
|
)
|
|
|
|
val csv = """
|
|
Date,Heure,Type de repas,Faim avant (/10),Satiété après (/10),Aliments & Poids,Note globale
|
|
08/11/2026,12:30,Déjeuner,6,7,"Riz basmati (150 g)","Déjeuner",0
|
|
08/11/2026,20:00,Dîner,4,6,"Soupe (250 ml)","Repas",0
|
|
""".trimIndent()
|
|
|
|
val stream = ByteArrayInputStream(csv.toByteArray(Charsets.UTF_8))
|
|
val result = CsvImporter.parseCsvStream(stream, listOf(existingMeal)).getOrThrow()
|
|
|
|
// 1 duplicate skipped, 1 imported
|
|
assertEquals(1, result.importedMeals.size)
|
|
assertEquals(1, result.skippedDuplicatesCount)
|
|
assertEquals(MealType.DINNER.name, result.importedMeals[0].mealType)
|
|
}
|
|
|
|
@Test
|
|
fun testFrenchSemicolonCsvDelimiter() {
|
|
val csv = """
|
|
Date;Heure;Type de repas;Faim avant (/10);Satiété après (/10);Aliments & Poids;Note globale
|
|
08/11/2026;12:30;Déjeuner;7;8;Pavé de saumon (150 g);Très bon
|
|
""".trimIndent()
|
|
|
|
val stream = ByteArrayInputStream(csv.toByteArray(Charsets.UTF_8))
|
|
val result = CsvImporter.parseCsvStream(stream, emptyList()).getOrThrow()
|
|
|
|
assertEquals(1, result.importedMeals.size)
|
|
assertEquals("Très bon", result.importedMeals[0].note)
|
|
assertEquals(1, result.importedMeals[0].parsedFoodItems().size)
|
|
assertEquals("Pavé de saumon", result.importedMeals[0].parsedFoodItems()[0].name)
|
|
}
|
|
}
|