Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sync test linked to network availability & fix Post request response #81

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.flow.firstOrNull
import net.opatry.tasks.data.model.TaskListDataModel
import net.opatry.tasks.data.util.respondNoNetwork
import net.opatry.tasks.data.util.respondWithTaskList
import net.opatry.tasks.data.util.respondWithTaskLists
import net.opatry.tasks.data.util.respondWithTasks
import net.opatry.tasks.data.util.runTaskRepositoryTest
Expand Down Expand Up @@ -105,36 +106,32 @@ class TaskRepositorySyncTest {

@Test
fun `local only tasks are synced at next sync`() {
var requestCount = 0
var isNetworkAvailable = true
MockEngine { request ->
++requestCount
when {
requestCount == 1
&& request.method == HttpMethod.Post
&& request.url.encodedPath == "/tasks/v1/users/@me/lists"
-> respondNoNetwork()
!isNetworkAvailable -> respondNoNetwork()

requestCount == 2
&& request.method == HttpMethod.Get
request.method == HttpMethod.Get
&& request.url.encodedPath == "/tasks/v1/users/@me/lists"
-> respondWithTaskLists()

requestCount == 3
&& request.method == HttpMethod.Post
request.method == HttpMethod.Post
&& request.url.encodedPath == "/tasks/v1/users/@me/lists"
-> respondWithTaskLists("MTAwNDEyMDI1NDY0NDEwNzQ0NDI6MDow" to "Task list")
-> respondWithTaskList("MTAwNDEyMDI1NDY0NDEwNzQ0NDI6MDow", "Task list")

else -> fail("Unexpected request: $request")
}
}.use { mockEngine ->
runTaskRepositoryTest(mockEngine) { repository ->
// for first request, no network
isNetworkAvailable = false
repository.createTaskList("Task list")
val taskList = repository.getTaskLists().firstOrNull()?.firstOrNull()
assertNotNull(taskList)
assertEquals(0, mockEngine.responseHistory.size)

// network is considered back, sync should trigger fetch & push requests
isNetworkAvailable = true
repository.sync()
assertEquals(2, mockEngine.responseHistory.size)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ fun MockRequestHandleScope.respondWithTaskLists(vararg idToTitles: Pair<String,
)
}

fun MockRequestHandleScope.respondWithTaskList(id: String, title: String): HttpResponseData {
return respond(
content = Json.encodeToString(TaskList(id = id, title = title)),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString())
)
}

fun MockRequestHandleScope.respondWithTasks(vararg idToTitles: Pair<String, String>): HttpResponseData {
val tasks = idToTitles.map { Task(id = it.first, title = it.second) }
val tasksResponse = ResourceListResponse(
Expand Down