Skip to content

Commit

Permalink
Cover RealPostSourceOfTruthReader
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 adde145 commit 2d7033d
Show file tree
Hide file tree
Showing 18 changed files with 458 additions and 301 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: chmod +x gradlew

- name: Build and Test with Coverage
run: ./gradlew clean build koverXmlReport --stacktrace
run: ./gradlew build koverXmlReport --stacktrace

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import org.mobilenativefoundation.trails.xplat.lib.repositories.post.api.PostCom
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.api.PostRepository
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.PostStore
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.PostStoreFactory
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database.PostDAO
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database.RealPostDAO
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database.*
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.fetcher.PostFetcherServices
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.fetcher.RealPostFetcherServices
import org.mobilenativefoundation.trails.xplat.lib.rest.api.TrailsClientComponent
Expand All @@ -35,16 +34,32 @@ abstract class RealPostComponent(
)
}

@Provides
fun providePostPredicateEvaluator(): PostPredicateEvaluator {
return RealPostPredicateEvaluator()
}

@Provides
fun providePostComparer(): PostComparer {
return RealPostComparer()
}

@Provides
fun providePostStore(
postFetcherServices: PostFetcherServices
postFetcherServices: PostFetcherServices,
postDAO: PostDAO,
postPredicateEvaluator: PostPredicateEvaluator,
comparer: PostComparer
): PostStore {
return PostStoreFactory(
client = trailsClientComponent.trailsClient,
trailsDatabase = database,
coroutineDispatcher = TrailsDispatchers.io,
postFetcherServices = postFetcherServices
postDAO = postDAO,
postFetcherServices = postFetcherServices,
predicateEvaluator = postPredicateEvaluator,
comparer = comparer

).create()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.extensions

import org.mobilenativefoundation.trails.xplat.lib.operations.query.Order


object OrderExtensions {
fun Order<*>?.toDomain(): org.mobilenativefoundation.trails.xplat.lib.models.query.Order? {
return this?.let {
org.mobilenativefoundation.trails.xplat.lib.models.query.Order(
propertyName = it.property.name,
direction = it.direction,
propertyType = it.propertyType
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ 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.bookkeeper.PostBookkeeperFactory
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database.PostSourceOfTruthFactory
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database.PostSourceOfTruthReader
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database.PostSourceOfTruthWriter
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database.*
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.fetcher.PostFetcherFactory
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.fetcher.PostFetcherServices
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.updater.PostUpdaterFactory
Expand All @@ -29,10 +27,13 @@ class PostStoreFactory(
client: PostOperations,
trailsDatabase: TrailsDatabase,
coroutineDispatcher: CoroutineDispatcher,
postFetcherServices: PostFetcherServices
postFetcherServices: PostFetcherServices,
postDAO: PostDAO,
predicateEvaluator: PostPredicateEvaluator,
comparer: PostComparer
) {

private val sourceOfTruthReader = PostSourceOfTruthReader(trailsDatabase, coroutineDispatcher)
private val sourceOfTruthReader = RealPostSourceOfTruthReader(postDAO, predicateEvaluator, comparer, coroutineDispatcher)
private val sourceOfTruthWriter = PostSourceOfTruthWriter(trailsDatabase, coroutineDispatcher)
private val sourceOfTruthFactory = PostSourceOfTruthFactory(sourceOfTruthReader, sourceOfTruthWriter)
private val fetcherFactory = PostFetcherFactory(postFetcherServices)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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.operations.query.Order

interface PostComparer {
fun compare(a: Post.Node, b: Post.Node, order: Order<Post.Node>?): Int
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.database

import kotlinx.coroutines.launch
import org.mobilenativefoundation.trails.xplat.lib.db.PostEntity
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.PostExtensions.asNode

interface PostDAO {
suspend fun insertOne(entity: PostEntity)
suspend fun insertOneComposite(composite: Post.Composite)
suspend fun findOne(id: Int): PostEntity?
suspend fun findAll(): List<PostEntity>
fun findOneAndAssembleComposite(id: Int): Post.Composite?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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.operations.query.Predicate

interface PostPredicateEvaluator {
fun evaluate(predicate: Predicate<Post.Node, *>, item: Post.Node): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.channels.BufferOverflow
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.operations.io.Operation
import org.mobilenativefoundation.trails.xplat.lib.repositories.post.impl.store.PostOperation
import org.mobilenativefoundation.trails.xplat.lib.models.post.PostOutput
Expand All @@ -26,7 +27,7 @@ class PostSourceOfTruthFactory(
// We only invoke the SOT on reads
require(operation is Operation.Query)

reader.handleRead(operation) {
handleRead(operation) {
mutableSharedFlow.emit(it)
}

Expand All @@ -42,54 +43,23 @@ class PostSourceOfTruthFactory(
TODO()
}
)
}

// SourceOfTruth.of(
// reader = { operation ->
//
// val mutableSharedFlow = MutableSharedFlow<Post?>(
// replay = 8,
// extraBufferCapacity = 20,
// onBufferOverflow = BufferOverflow.DROP_OLDEST
// )
//
// // We only invoke the SOT on reads
// require(operation is Operation.Query)
//
// when (operation) {
// is PostOperation.Query.FindOne -> {
// val id = operation.key.id
// coroutineScope.launch {
// mutableSharedFlow.emit(trailsDatabase.postQueries.assemblePostModel(id.toLong()))
// }
// }
//
// is PostOperation.Query.FindOneComposite -> {
// val id = operation.key.id
// coroutineScope.launch {
// mutableSharedFlow.emit(trailsDatabase.postQueries.assembleCompositePost(id.toLong()))
// }
// }
//
// is PostOperation.Query.ObserveOne -> {
// val id = operation.key.id
// coroutineScope.launch {
// trailsDatabase.postQueries.observePostModel(id.toLong()).collect {
// mutableSharedFlow.emit(it)
// }
// }
// }
//
// is PostOperation.Query.ObserveOneComposite -> {
// val id = operation.key.id
// coroutineScope.launch {
// trailsDatabase.postQueries.observeCompositePost(id.toLong()).collect {
// mutableSharedFlow.emit(it)
// }
// }
// }
//
// is PostOperation.Query.QueryOne -> TODO()
// }
//
// mutableSharedFlow.asSharedFlow()
private fun handleRead(
operation: Operation.Query<Post.Key, Post.Properties, Post.Edges, Post.Node>,
emit: suspend (PostOutput?) -> Unit
) {

when (operation) {
is Operation.Query.FindAll -> TODO()
is Operation.Query.FindMany -> TODO()
is Operation.Query.FindOne -> reader.findOne(operation, emit)
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.QueryOne -> TODO()
is Operation.Query.QueryManyComposite -> reader.queryManyComposite(operation.query, emit)
}
}
}
Loading

0 comments on commit 2d7033d

Please sign in to comment.