Skip to content

Commit

Permalink
Cover RealPostSourceOfTruthWriter
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Ramotar <[email protected]>
  • Loading branch information
matt-ramotar committed Nov 28, 2024
1 parent 2d7033d commit 47890fa
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class PostStoreFactory(
comparer: PostComparer
) {

private val sourceOfTruthReader = RealPostSourceOfTruthReader(postDAO, predicateEvaluator, comparer, coroutineDispatcher)
private val sourceOfTruthWriter = PostSourceOfTruthWriter(trailsDatabase, coroutineDispatcher)
private val sourceOfTruthReader =
RealPostSourceOfTruthReader(postDAO, predicateEvaluator, comparer, coroutineDispatcher)
private val sourceOfTruthWriter = RealPostSourceOfTruthWriter(postDAO)
private val sourceOfTruthFactory = PostSourceOfTruthFactory(sourceOfTruthReader, sourceOfTruthWriter)
private val fetcherFactory = PostFetcherFactory(postFetcherServices)
private val updaterFactory = PostUpdaterFactory(client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import org.mobilenativefoundation.store.store5.SourceOfTruth
import org.mobilenativefoundation.trails.xplat.lib.models.post.Post
import org.mobilenativefoundation.trails.xplat.lib.models.post.PostOutput
import org.mobilenativefoundation.trails.xplat.lib.operations.io.Operation
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.PostOperation
import org.mobilenativefoundation.trails.xplat.lib.models.post.PostOutput

class PostSourceOfTruthFactory(
private val reader: PostSourceOfTruthReader,
Expand All @@ -34,7 +34,7 @@ class PostSourceOfTruthFactory(
mutableSharedFlow.asSharedFlow()
},
writer = { operation, output ->
writer.handleWrite(operation, output)
handleWrite(operation, output)
},
delete = { operation ->
TODO()
Expand Down Expand Up @@ -62,4 +62,37 @@ class PostSourceOfTruthFactory(
is Operation.Query.QueryManyComposite -> reader.queryManyComposite(operation.query, emit)
}
}

// We write to the SOT on mutations and queries
// So we need to handle all cases

private suspend fun handleWrite(
operation: PostOperation,
value: PostOutput
) {

when (operation) {
is Operation.Mutation.Create.InsertOne -> TODO()
Operation.Mutation.Delete.DeleteAll -> TODO()
is Operation.Mutation.Delete.DeleteMany -> TODO()
is Operation.Mutation.Delete.DeleteOne -> TODO()
is Operation.Mutation.Update.ReplaceOne -> TODO()
is Operation.Mutation.Update.UpdateOne -> TODO()
is Operation.Mutation.Upsert.UpsertOne -> TODO()
is Operation.Query.FindAll -> TODO()
is Operation.Query.FindMany -> TODO()
is Operation.Query.FindOne -> TODO()
is Operation.Query.FindOneComposite -> TODO()
is Operation.Query.ObserveMany -> TODO()
is Operation.Query.ObserveOne -> TODO()
is Operation.Query.ObserveOneComposite -> TODO()
is Operation.Query.QueryMany -> TODO()
is Operation.Query.QueryManyComposite -> {
writer.queryManyComposite(operation, value)
}

is Operation.Query.QueryOne -> TODO()
}

}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,14 @@
package org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import org.mobilenativefoundation.trails.xplat.lib.db.TrailsDatabase
import org.mobilenativefoundation.trails.xplat.lib.models.post.Post
import org.mobilenativefoundation.trails.xplat.lib.models.post.PostOutput
import org.mobilenativefoundation.trails.xplat.lib.operations.io.Operation
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.extensions.PostQueriesExtensions.saveComposite
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.PostOperation

class PostSourceOfTruthWriter(
private val trailsDatabase: TrailsDatabase,
private val coroutineDispatcher: CoroutineDispatcher
) {
private val coroutineScope = CoroutineScope(coroutineDispatcher)

// We write to the SOT on mutations and queries
// So we need to handle all cases

suspend fun handleWrite(
operation: PostOperation,
interface PostSourceOfTruthWriter {
suspend fun queryManyComposite(
operation: Operation.Query.QueryManyComposite<Post.Key, Post.Properties, Post.Edges, Post.Node>,
value: PostOutput
) {

when (operation) {
is Operation.Mutation.Create.InsertOne -> TODO()
Operation.Mutation.Delete.DeleteAll -> TODO()
is Operation.Mutation.Delete.DeleteMany -> TODO()
is Operation.Mutation.Delete.DeleteOne -> TODO()
is Operation.Mutation.Update.ReplaceOne -> TODO()
is Operation.Mutation.Update.UpdateOne -> TODO()
is Operation.Mutation.Upsert.UpsertOne -> TODO()
is Operation.Query.FindAll -> TODO()
is Operation.Query.FindMany -> TODO()
is Operation.Query.FindOne -> TODO()
is Operation.Query.FindOneComposite -> TODO()
is Operation.Query.ObserveMany -> TODO()
is Operation.Query.ObserveOne -> TODO()
is Operation.Query.ObserveOneComposite -> TODO()
is Operation.Query.QueryMany -> TODO()
is Operation.Query.QueryManyComposite -> {
require(value is PostOutput.Collection)

value.values.filterIsInstance<Post.Composite>().forEach { composite ->
trailsDatabase.postQueries.saveComposite(composite)
}
}
)
}

is Operation.Query.QueryOne -> TODO()
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database

import org.mobilenativefoundation.trails.xplat.lib.models.post.Post
import org.mobilenativefoundation.trails.xplat.lib.models.post.PostOutput
import org.mobilenativefoundation.trails.xplat.lib.operations.io.Operation

class RealPostSourceOfTruthWriter(
private val postDAO: PostDAO
) : PostSourceOfTruthWriter {

override suspend fun queryManyComposite(
operation: Operation.Query.QueryManyComposite<Post.Key, Post.Properties, Post.Edges, Post.Node>,
value: PostOutput
) {
require(value is PostOutput.Collection)

value.values.filterIsInstance<Post.Composite>().forEach { composite ->
postDAO.insertOneComposite(composite)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database

import dev.mokkery.MockMode
import dev.mokkery.matcher.eq
import dev.mokkery.mock
import dev.mokkery.verifySuspend
import kotlinx.coroutines.test.runTest
import org.mobilenativefoundation.trails.xplat.lib.models.post.Post
import org.mobilenativefoundation.trails.xplat.lib.models.post.PostOutput
import org.mobilenativefoundation.trails.xplat.lib.operations.io.Operation.Query.QueryManyComposite
import org.mobilenativefoundation.trails.xplat.lib.operations.query.DataSources
import org.mobilenativefoundation.trails.xplat.lib.operations.query.Query
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.test_utils.FakePostFactory.createCompositePost
import kotlin.test.Test

class RealPostSourceOfTruthWriterTest {

private val postDAO = mock<PostDAO>(MockMode.autoUnit)
private val writer = RealPostSourceOfTruthWriter(postDAO)


@Test
fun queryManyComposite_givenNormalDAO_whenCalledWithValidValue_thenShouldSaveUsingDAO() = runTest {
// Given
val composite1 = createCompositePost(1)
val composite2 = createCompositePost(2)
val value = PostOutput.Collection(
values = listOf(composite1, composite2)
)

val operation = QueryManyComposite(
query = Query.Many<Post.Node>(
DataSources.all,
null,
null,
null
),
dataSources = DataSources.all
)

// When
writer.queryManyComposite(operation, value)

// Then
verifySuspend { postDAO.insertOneComposite(eq(composite1)) }
verifySuspend { postDAO.insertOneComposite(eq(composite2)) }
}
}

0 comments on commit 47890fa

Please sign in to comment.