-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Ramotar <[email protected]>
- Loading branch information
1 parent
2d7033d
commit 47890fa
Showing
5 changed files
with
112 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 5 additions & 45 deletions
50
...ndation/trails/xplat/lib/repositories/post/impl/store/database/PostSourceOfTruthWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ion/trails/xplat/lib/repositories/post/impl/store/database/RealPostSourceOfTruthWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...trails/xplat/lib/repositories/post/impl/store/database/RealPostSourceOfTruthWriterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) } | ||
} | ||
} |