Modern Apps git

← Modern-Apps.git

fooddelivery: parse a checked-in stores fixture instead of the live API

Commit
74d285a38ede0a90492542e7fa28704cd6c4654a
Author
Vayun Mathur <[email protected]>
When
2026-11-06 05:27 UTC (1 month ago)
Parents
840a4a8
BitesApiTest hit api.deliverycollective.com directly and failed whenever it returned 5xx.

Diff

fooddelivery/src/test/java/com/vayunmathur/fooddelivery/BitesApiTest.kt
diff --git a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/BitesApiTest.kt b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/BitesApiTest.ktindex bf3b3258c0..5b2014c928 100644--- a/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/BitesApiTest.kt+++ b/fooddelivery/src/test/java/com/vayunmathur/fooddelivery/BitesApiTest.kt@@ -1,43 +1,31 @@ package com.vayunmathur.fooddelivery  import com.vayunmathur.fooddelivery.data.ApiResponse-import com.vayunmathur.fooddelivery.data.Brand import com.vayunmathur.fooddelivery.data.Merchant-import com.vayunmathur.fooddelivery.data.MerchantItem import com.vayunmathur.fooddelivery.data.MerchantsWrapper import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull import kotlin.test.assertTrue-import java.net.HttpURLConnection-import java.net.URL-import java.util.logging.Logger  class BitesApiTest { -    private val logger = Logger.getLogger(BitesApiTest::class.java.name)-     private val json = Json {         ignoreUnknownKeys = true         isLenient = true     } -    private fun httpGet(urlStr: String): String {-        val conn = URL(urlStr).openConnection() as HttpURLConnection-        conn.requestMethod = "GET"-        conn.setRequestProperty("Accept", "application/json")-        conn.connectTimeout = 30000-        conn.readTimeout = 60000-        return conn.inputStream.bufferedReader().readText().also { conn.disconnect() }-    }+    private fun fixture(name: String): String =+        checkNotNull(javaClass.classLoader?.getResource(name)) { "missing fixture $name" }.readText()      @Test     fun testRawResponse() {-        val raw = httpGet("https://api.deliverycollective.com/api/v1/merchants/all/stores")-        println("RAW response length: ${raw.length}")-        println("RAW first 500 chars: ${raw.take(500)}")+        val raw = fixture(STORES_FIXTURE)         assertTrue(raw.isNotEmpty(), "Response was empty")+        assertTrue(json.parseToJsonElement(raw).jsonObject["data"]!!.jsonObject["merchants"]!!.jsonArray.size == 3)     }      @Test@@ -127,40 +115,35 @@ class BitesApiTest {      @Test     fun testParseRealApiResponse() {-        val raw = httpGet("https://api.deliverycollective.com/api/v1/merchants/all/stores")-        println("Got ${raw.length} bytes from API")+        val resp = json.decodeFromString<ApiResponse<MerchantsWrapper>>(fixture(STORES_FIXTURE))+        assertEquals("success", resp.message)+        val merchants = resp.data?.merchants.orEmpty()+        assertEquals(listOf(2615, 3001, 4100), merchants.map { it.id }) -        try {-            val resp = json.decodeFromString<ApiResponse<MerchantsWrapper>>(raw)-            val merchants = resp.data?.merchants ?: emptyList()-            println("SUCCESS: Parsed ${merchants.size} merchants")-            merchants.take(3).forEach { m ->-                println("  - ${m.name} (id=${m.id}, rating=${m.averageRating}, tags=${m.merchantTags})")-            }-            assertTrue(merchants.isNotEmpty(), "No merchants parsed!")-        } catch (e: Exception) {-            logger.warning("PARSE FAILED: ${e::class.simpleName}: ${e.message}\n${e.stackTraceToString()}")-            // Try to parse just the first merchant to isolate the issue-            try {-                val jsonElement = json.parseToJsonElement(raw)-                val dataObj = jsonElement.jsonObject["data"]!!.jsonObject-                val merchantsArr = dataObj["merchants"]!!.jsonArray-                println("JSON has ${merchantsArr.size} merchants in array")+        val slice = merchants[0]+        assertEquals(4.7, slice.displayRating)+        assertEquals(312, slice.displayTotalRatings)+        assertEquals(25.0, slice.freeDeliveryThresholdDollars)+        assertEquals("https://cdn.example.com/brand/slice-hero.jpg", slice.displayImage)+        assertEquals("https://cdn.example.com/brand/slice-logo.png", slice.displayLogo)+        assertEquals("123 Main St, San Mateo, CA", slice.address)+        assertEquals(listOf("Margherita", "Garlic Knots"), slice.items.map { it.name })+        assertNull(slice.items[0].imageUrl) -                // Try parsing one by one to find the bad one-                for (i in 0 until merchantsArr.size) {-                    try {-                        json.decodeFromString<Merchant>(merchantsArr[i].toString())-                    } catch (e2: Exception) {-                        println("MERCHANT[$i] FAILED: ${e2.message?.take(200)}")-                        println("  JSON: ${merchantsArr[i].toString().take(300)}")-                        break-                    }-                }-            } catch (e3: Exception) {-                println("Even basic JSON parse failed: ${e3.message}")-            }-            throw e-        }+        val taqueria = merchants[1]+        assertEquals(0.0, taqueria.displayRating)+        assertEquals(0, taqueria.displayTotalRatings)+        assertNull(taqueria.brand)+        assertEquals("https://cdn.example.com/m/3001.jpg", taqueria.displayImage)+        assertEquals(", Burlingame, CA", taqueria.address)++        val sparse = merchants[2]+        assertEquals("Noodle Bar", sparse.name)+        assertTrue(sparse.isOpen)+        assertEquals("", sparse.displayImage)+    }++    private companion object {+        const val STORES_FIXTURE = "merchants_all_stores.json"     } }
fooddelivery/src/test/resources/merchants_all_stores.json
diff --git a/fooddelivery/src/test/resources/merchants_all_stores.json b/fooddelivery/src/test/resources/merchants_all_stores.jsonnew file mode 100644index 0000000000..51acdac0a3--- /dev/null+++ b/fooddelivery/src/test/resources/merchants_all_stores.json@@ -0,0 +1,80 @@+{+  "message": "success",+  "data": {+    "merchants": [+      {+        "id": 2615,+        "name": "Slice House",+        "addressStreet": "123 Main St",+        "addressCity": "San Mateo",+        "addressState": "CA",+        "addressZip": "94401",+        "latitude": 37.5630,+        "longitude": -122.3255,+        "logoUrl": "",+        "imageUrl": "",+        "isOpen": true,+        "isDeliveryEnabled": true,+        "isPickupEnabled": false,+        "closingTime": "Closes at 9:00PM",+        "nextOpenWindow": "ASAP",+        "averageRating": 4.7,+        "totalRatings": 312,+        "rewardsPercentage": 5.0,+        "brandColor": "#d32f2f",+        "brand": {+          "logoUrl": "https://cdn.example.com/brand/slice-logo.png",+          "imageUrl": "https://cdn.example.com/brand/slice-hero.jpg",+          "showOnApp": true,+          "unknownBrandField": 1+        },+        "merchantTags": ["Pizza", "Italian"],+        "items": [+          {"id": 1, "name": "Margherita", "price": 1499, "imageUrl": null, "thumbnailUrl": "https://cdn.example.com/i/1.jpg"},+          {"id": 2, "name": "Garlic Knots", "price": 699}+        ],+        "sortOrder": 1,+        "distance": 0.8,+        "freeDeliveryThreshold": 2500,+        "doordashMarkup": 15,+        "doordashMarkupComparison": 20,+        "storefrontAlias": "slice-house",+        "operatingHours": [{"day": "MON", "open": "11:00", "close": "21:00"}]+      },+      {+        "id": 3001,+        "name": "Taqueria Sol",+        "addressStreet": "",+        "addressCity": "Burlingame",+        "addressState": "CA",+        "addressZip": "94010",+        "latitude": 37.5779,+        "longitude": -122.3481,+        "logoUrl": "https://cdn.example.com/m/3001-logo.png",+        "imageUrl": "https://cdn.example.com/m/3001.jpg",+        "isOpen": false,+        "closingTime": "",+        "nextOpenWindow": "Opens at 10:00AM",+        "averageRating": null,+        "totalRatings": null,+        "rewardsPercentage": null,+        "brandColor": null,+        "brand": null,+        "merchantTags": [],+        "items": [],+        "sortOrder": null,+        "distance": null,+        "freeDeliveryThreshold": 0,+        "doordashMarkup": null,+        "doordashMarkupComparison": null,+        "storefrontAlias": "taqueria-sol"+      },+      {+        "id": 4100,+        "name": "Noodle Bar"+      }+    ],+    "total": 3,+    "tags": ["Pizza", "Italian", "Mexican", "Noodles"]+  }+}