@@ -16,6 +16,7 @@ import org.wordpress.android.fluxc.model.SiteModel
1616import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.UNKNOWN
1717import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError
1818import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType.GENERIC_ERROR
19+ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
1920import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult
2021import org.wordpress.android.fluxc.persistence.dao.BookingsDao
2122import org.wordpress.android.fluxc.persistence.entity.BookingEntity
@@ -58,7 +59,7 @@ class BookingsStoreTest {
5859 val storedBooking = sampleBookingEntity(order = BookingOrderInfo (productInfo = null ))
5960 whenever(bookingsDao.getBooking(TEST_LOCAL_SITE_ID , dto.id)).thenReturn(storedBooking)
6061 whenever(bookingsRestClient.updateBooking(site, dto.id, BookingUpdatePayload (note = " n" )))
61- .thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc. WooPayload (dto))
62+ .thenReturn(WooPayload (dto))
6263 whenever(bookingsDao.insertOrReplace(any<BookingEntity >())).thenReturn(1L )
6364
6465 // when
@@ -85,7 +86,7 @@ class BookingsStoreTest {
8586 val fetchedOrder = OrderEntity (orderId = dto.orderId, localSiteId = TEST_LOCAL_SITE_ID )
8687 whenever(orderStore.fetchSingleOrderSync(site, dto.orderId)).thenReturn(WooResult (fetchedOrder))
8788 whenever(bookingsRestClient.updateBooking(site, dto.id, BookingUpdatePayload (status = Status .Confirmed )))
88- .thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc. WooPayload (dto))
89+ .thenReturn(WooPayload (dto))
8990 whenever(bookingsDao.insertOrReplace(any<BookingEntity >())).thenReturn(1L )
9091
9192 // when
@@ -101,6 +102,7 @@ class BookingsStoreTest {
101102 val expected = with (bookingDtoMapper) { dto.toEntity(TEST_LOCAL_SITE_ID , fetchedOrder) }
102103 assertThat(result.model).isEqualTo(expected)
103104 verify(bookingsDao).insertOrReplace(expected)
105+ verify(bookingsDao, never()).getBooking(TEST_LOCAL_SITE_ID , dto.id)
104106 }
105107
106108 @Test
@@ -109,7 +111,7 @@ class BookingsStoreTest {
109111 val site = SiteModel ().apply { id = TEST_LOCAL_SITE_ID .value }
110112 val error = WooError (GENERIC_ERROR , UNKNOWN )
111113 whenever(bookingsRestClient.updateBooking(site, TEST_BOOKING_ID , BookingUpdatePayload (note = " n" )))
112- .thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc. WooPayload (error))
114+ .thenReturn(WooPayload (error))
113115
114116 // when
115117 val result = sut.updateBooking(
@@ -125,38 +127,47 @@ class BookingsStoreTest {
125127 }
126128
127129 @Test
128- fun `given order refresh fails, when updateBooking with refreshOrder true, then returns error and does not insert` (): Unit = runBlocking {
129- // given
130- val site = SiteModel ().apply { id = TEST_LOCAL_SITE_ID .value }
131- val dto = sampleBookingDto()
132- whenever(
133- bookingsRestClient.updateBooking(
134- site,
135- dto.id,
136- BookingUpdatePayload (attendanceStatus = AttendanceStatus .CheckedIn )
130+ fun `given order refresh fails, when updateBooking with refreshOrder true, then fallbacks to local entity and inserts it` (): Unit =
131+ runBlocking {
132+ // given
133+ val site = SiteModel ().apply { id = TEST_LOCAL_SITE_ID .value }
134+ val dto = sampleBookingDto()
135+ val storedBooking = sampleBookingEntity(
136+ order = BookingOrderInfo (
137+ productInfo = null ,
138+ status = " paid" ,
139+ )
137140 )
138- )
139- .thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload (dto))
140-
141- whenever(
142- orderStore.fetchSingleOrderSync(
143- site,
144- dto.orderId
141+ whenever(bookingsDao.getBooking(TEST_LOCAL_SITE_ID , dto.id)).thenReturn(storedBooking)
142+ whenever(
143+ bookingsRestClient.updateBooking(
144+ site,
145+ dto.id,
146+ BookingUpdatePayload (attendanceStatus = AttendanceStatus .CheckedIn )
147+ )
148+ ).thenReturn(WooPayload (dto))
149+
150+ whenever(
151+ orderStore.fetchSingleOrderSync(
152+ site,
153+ dto.orderId
154+ )
155+ ).thenReturn(WooResult (error = WooError (GENERIC_ERROR , UNKNOWN )))
156+
157+ // when
158+ val result = sut.updateBooking(
159+ site = site,
160+ bookingId = dto.id,
161+ bookingUpdatePayload = BookingUpdatePayload (attendanceStatus = AttendanceStatus .CheckedIn ),
162+ refreshOrder = true
145163 )
146- ).thenReturn(WooResult (error = WooError (GENERIC_ERROR , UNKNOWN )))
147-
148- // when
149- val result = sut.updateBooking(
150- site = site,
151- bookingId = dto.id,
152- bookingUpdatePayload = BookingUpdatePayload (attendanceStatus = AttendanceStatus .CheckedIn ),
153- refreshOrder = true
154- )
155164
156- // then
157- assertThat(result.isError).isTrue()
158- verify(bookingsDao, never()).insertOrReplace(any<BookingEntity >())
159- }
165+ // then
166+ assertThat(result.isError).isFalse
167+ verify(bookingsDao).getBooking(TEST_LOCAL_SITE_ID , dto.id)
168+ // The store preserves the stored order on the mapped entity
169+ verify(bookingsDao).insertOrReplace(argThat<BookingEntity > { this .order == storedBooking.order })
170+ }
160171
161172 private fun sampleBookingDto (): BookingDto = BookingDto (
162173 id = TEST_BOOKING_ID ,
0 commit comments