-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds MenuStack test to showcase parameters backfill.
- Loading branch information
1 parent
1641dbf
commit 2575c9a
Showing
12 changed files
with
196 additions
and
6 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
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
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
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
80 changes: 80 additions & 0 deletions
80
...k-hibernate/src/test/kotlin/app/cash/backfila/client/misk/menustack/MenuStackBackfills.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,80 @@ | ||
package app.cash.backfila.client.misk.menustack | ||
|
||
import app.cash.backfila.client.BackfillConfig | ||
import app.cash.backfila.client.misk.ClientMiskService | ||
import app.cash.backfila.client.misk.DbMenu | ||
import app.cash.backfila.client.misk.MenuItem | ||
import app.cash.backfila.client.misk.MenuQuery | ||
import app.cash.backfila.client.misk.hibernate.HibernateBackfill | ||
import app.cash.backfila.client.misk.hibernate.UnshardedPartitionProvider | ||
import app.cash.backfila.client.stat.parameters.DatasourceParameters | ||
import app.cash.backfila.client.stat.parameters.ParametersDatasourceBackfill | ||
import java.util.concurrent.BlockingDeque | ||
import javax.inject.Inject | ||
import misk.hibernate.Id | ||
import misk.hibernate.Query | ||
import misk.hibernate.Transacter | ||
import misk.hibernate.newQuery | ||
|
||
/** | ||
* Backfills all menus of a specific type to the singleton menuStack. | ||
*/ | ||
class MenuStackDbBackfill @Inject constructor( | ||
@ClientMiskService private val transacter: Transacter, | ||
private val queryFactory: Query.Factory, | ||
private val menuProcessor: MenuProcessor, | ||
) : HibernateBackfill<DbMenu, Id<DbMenu>, MenuStackDbParameters>() { | ||
override fun backfillCriteria(config: BackfillConfig<MenuStackDbParameters>): Query<DbMenu> { | ||
return queryFactory.newQuery<MenuQuery>().name(config.parameters.type) | ||
} | ||
|
||
override fun runOne(pkey: Id<DbMenu>, config: BackfillConfig<MenuStackDbParameters>) { | ||
menuProcessor.addToStack(pkey, config) | ||
} | ||
|
||
override fun partitionProvider() = UnshardedPartitionProvider(transacter) | ||
} | ||
|
||
/** | ||
* Backfills all menus specified by ID in the backfill parameters to the singleton menuStack. | ||
*/ | ||
class MenuStackParametersBackfill @Inject constructor( | ||
private val menuProcessor: MenuProcessor, | ||
) : ParametersDatasourceBackfill<Id<DbMenu>, MenuStackIdParameters>() { | ||
override fun runOne(item: Id<DbMenu>, config: BackfillConfig<MenuStackIdParameters>) { | ||
menuProcessor.addToStack(item, config) | ||
} | ||
} | ||
|
||
/** | ||
* Processes a menu and places it onto the menuStack. | ||
*/ | ||
class MenuProcessor @Inject constructor( | ||
@ClientMiskService private val transacter: Transacter, | ||
private val queryFactory: Query.Factory, | ||
@MenuStack private val menuStack: BlockingDeque<MenuItem>, | ||
) { | ||
fun addToStack(item: Id<DbMenu>, config: BackfillConfig<*>) { | ||
val menuItem = transacter.transaction { session -> | ||
queryFactory.newQuery<MenuQuery>().id(item).list(session).single().menuItem() | ||
} | ||
if (!config.dryRun) { | ||
menuStack.add(menuItem) | ||
} | ||
} | ||
} | ||
|
||
data class MenuStackDbParameters( | ||
/** | ||
* beef, chicken, duck .... | ||
*/ | ||
val type: String, | ||
) | ||
|
||
data class MenuStackIdParameters( | ||
val menuIds: String, | ||
) : DatasourceParameters<Id<DbMenu>> { | ||
override fun getBackfillData(): List<Id<DbMenu>> { | ||
return menuIds.split(',').map { Id(it.toLong()) } | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...misk-hibernate/src/test/kotlin/app/cash/backfila/client/misk/menustack/MenuStackModule.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,22 @@ | ||
package app.cash.backfila.client.misk.menustack | ||
|
||
import app.cash.backfila.client.misk.MenuItem | ||
import app.cash.backfila.client.misk.hibernate.HibernateBackfillModule | ||
import app.cash.backfila.client.stat.StaticDatasourceBackfillModule | ||
import com.google.inject.Provides | ||
import java.util.concurrent.BlockingDeque | ||
import java.util.concurrent.LinkedBlockingDeque | ||
import javax.inject.Singleton | ||
import misk.inject.KAbstractModule | ||
|
||
class MenuStackModule : KAbstractModule() { | ||
override fun configure() { | ||
install(HibernateBackfillModule.create<MenuStackDbBackfill>()) | ||
install(StaticDatasourceBackfillModule.create<MenuStackParametersBackfill>()) | ||
} | ||
|
||
@Provides @MenuStack @Singleton | ||
fun provideMenuStack(): BlockingDeque<MenuItem> = LinkedBlockingDeque() | ||
} | ||
|
||
annotation class MenuStack |
71 changes: 71 additions & 0 deletions
71
...t-misk-hibernate/src/test/kotlin/app/cash/backfila/client/misk/menustack/MenuStackTest.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,71 @@ | ||
package app.cash.backfila.client.misk.menustack | ||
|
||
import app.cash.backfila.client.misk.ClientMiskService | ||
import app.cash.backfila.client.misk.ClientMiskTestingModule | ||
import app.cash.backfila.client.misk.DbMenu | ||
import app.cash.backfila.client.misk.MenuItem | ||
import app.cash.backfila.client.misk.MenuQuery | ||
import app.cash.backfila.embedded.Backfila | ||
import app.cash.backfila.embedded.createDryRun | ||
import app.cash.backfila.embedded.createWetRun | ||
import com.google.inject.Module | ||
import java.util.concurrent.BlockingDeque | ||
import javax.inject.Inject | ||
import misk.hibernate.Query | ||
import misk.hibernate.Session | ||
import misk.hibernate.Transacter | ||
import misk.hibernate.newQuery | ||
import misk.testing.MiskTest | ||
import misk.testing.MiskTestModule | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
@MiskTest(startService = true) | ||
class MenuStackTest { | ||
@Suppress("unused") | ||
@MiskTestModule | ||
val module: Module = ClientMiskTestingModule(false) | ||
|
||
@Inject @ClientMiskService | ||
lateinit var transacter: Transacter | ||
|
||
@Inject lateinit var queryFactory: Query.Factory | ||
|
||
@Inject lateinit var backfila: Backfila | ||
|
||
@MenuStack @Inject | ||
lateinit var menuStack: BlockingDeque<MenuItem> | ||
|
||
/** | ||
* This test helps give customers of Backfila some idea on how different backfills built on different clients | ||
* that perform the same underlying action can be used together. | ||
* | ||
* Gives customers an alternative to adding `in` clauses to their DB backfills that perform poorly. | ||
*/ | ||
@Test | ||
fun `shows how using a hibernate backfill and a parameter backfill a common stack of menus can be created`() { | ||
// Fill the database with chicken, beef and duck. | ||
transacter.transaction { session: Session -> | ||
repeat(10) { session.save(DbMenu("chicken")) } | ||
repeat(5) { session.save(DbMenu("beef")) } | ||
repeat(8) { session.save(DbMenu("duck")) } | ||
repeat(10) { session.save(DbMenu("chicken")) } | ||
} | ||
|
||
// Dry run all the beef. | ||
backfila.createDryRun<MenuStackDbBackfill>(parameters = MenuStackDbParameters(type = "beef")).execute() | ||
|
||
// Wet run all the chicken. | ||
backfila.createWetRun<MenuStackDbBackfill>(parameters = MenuStackDbParameters(type = "chicken")).execute() | ||
|
||
// Pick 4 duck menu ids and backfill those using a comma separated parameter. | ||
val pickedString = transacter.transaction { session: Session -> | ||
queryFactory.newQuery<MenuQuery>().name("duck").list(session).map { it.id }.take(4).joinToString(separator = ",") | ||
} | ||
backfila.createWetRun<MenuStackParametersBackfill>(parameters = MenuStackIdParameters(menuIds = pickedString)).execute() | ||
|
||
// Check that the singleton stack of menus is exactly 20 chicken followed by 4 duck. | ||
val expected = List(20) { "chicken" } + List(4) { "duck" } | ||
assertThat(menuStack.map { it.name }).containsExactlyElementsOf(expected.toList()) | ||
} | ||
} |
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