Skip to content

Commit

Permalink
Replace most test modules with testFixtures (cashapp#2793)
Browse files Browse the repository at this point in the history
Replace most test modules with testFixtures
  • Loading branch information
tso authored May 24, 2023
1 parent 9b60615 commit 64c4bed
Show file tree
Hide file tree
Showing 168 changed files with 5,682 additions and 209 deletions.
6 changes: 1 addition & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,10 @@ dependencyAnalysis {
// Due to kotlin 1.8.20 see https://github.com/autonomousapps/dependency-analysis-android-gradle-plugin/issues/884
exclude("() -> java.io.File?")
exclude("org.jetbrains.kotlin:kotlin-test:1.8.21")
exclude(":misk-testing")
}
}
// False positives.
project(":misk-aws2-dynamodb-testing") {
onAny {
exclude("org.antlr:antlr4-runtime")
}
}
project(":misk-gcp") {
onUsedTransitiveDependencies {
// Can be removed once dd-trace-ot uses 0.33.0 of open tracing.
Expand Down
9 changes: 0 additions & 9 deletions misk-aws-dynamodb-testing/api/misk-aws-dynamodb-testing.api
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
public final class misk/aws/dynamodb/testing/DockerDynamoDb : misk/testing/ExternalDependency {
public static final field INSTANCE Lmisk/aws/dynamodb/testing/DockerDynamoDb;
public fun afterEach ()V
public fun beforeEach ()V
public fun getId ()Ljava/lang/String;
public fun shutdown ()V
public fun startup ()V
}

public final class misk/aws/dynamodb/testing/DockerDynamoDbModule : misk/inject/KAbstractModule {
public fun <init> (Ljava/util/List;)V
public fun <init> ([Lkotlin/reflect/KClass;)V
Expand Down
4 changes: 0 additions & 4 deletions misk-aws-dynamodb-testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ dependencies {
api(Dependencies.tempestTestingInternal)
api(project(":misk-aws-dynamodb"))
api(project(":misk-inject"))
api(project(":misk-testing"))
implementation(Dependencies.kotlinReflect)
implementation(Dependencies.tempestTesting)
implementation(Dependencies.tempestTestingDocker)
implementation(Dependencies.tempestTestingJvm)
implementation(project(":misk-core"))
implementation(project(":misk-service"))

testImplementation(Dependencies.assertj)
testImplementation(Dependencies.junitApi)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import misk.inject.KAbstractModule
* Note that this may not be used alongside [InProcessDynamoDbModule]. DynamoDB may execute in Docker or
* in-process, but never both.
*/
@Deprecated("Replace the dependency on misk-aws-dynamodb-testing with testFixtures(misk-aws-dynamodb)")
class DockerDynamoDbModule(
private val tables: List<DynamoDbTable>
) : KAbstractModule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.reflect.full.findAnnotation
* Use [configureTable] to customize the table creation request for testing, such as to configure
* the secondary indexes required by `ProjectionType.ALL`.
*/
@Deprecated("Replace the dependency on misk-aws-dynamodb-testing with testFixtures(misk-aws-dynamodb)")
data class DynamoDbTable(
val tableClass: KClass<*>,
val configureTable: (CreateTableRequest) -> CreateTableRequest = { it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import misk.inject.KAbstractModule
* `@MiskExternalDependency DockerDynamoDb`. DynamoDB may execute in Docker or in-process, but never
* both.
*/
@Deprecated("Replace the dependency on misk-aws-dynamodb-testing with testFixtures(misk-aws-dynamodb)")
class InProcessDynamoDbModule(
private val tables: List<DynamoDbTable>
) : KAbstractModule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.google.inject.Singleton
/**
* Thin wrapper to make `TestDynamoDbService`, which is not a @Singleton, compatible with `ServiceModule`.
*/
@Deprecated("Replace the dependency on misk-aws-dynamodb-testing with testFixtures(misk-aws-dynamodb)")
@Singleton
class TestDynamoDb @Inject constructor(
val service: TestDynamoDbService
Expand Down
50 changes: 50 additions & 0 deletions misk-aws-dynamodb/api/misk-aws-dynamodb.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
public final class misk/aws/dynamodb/testing/DockerDynamoDbModule : misk/inject/KAbstractModule {
public fun <init> (Ljava/util/List;)V
public fun <init> ([Lkotlin/reflect/KClass;)V
public fun <init> ([Lmisk/aws/dynamodb/testing/DynamoDbTable;)V
public final fun provideRequiredTables ()Ljava/util/List;
public final fun providesAmazonDynamoDB (Lmisk/aws/dynamodb/testing/TestDynamoDb;)Lcom/amazonaws/services/dynamodbv2/AmazonDynamoDB;
public final fun providesAmazonDynamoDBStreams (Lmisk/aws/dynamodb/testing/TestDynamoDb;)Lcom/amazonaws/services/dynamodbv2/AmazonDynamoDBStreams;
public final fun providesTestDynamoDb ()Lmisk/aws/dynamodb/testing/TestDynamoDb;
}

public final class misk/aws/dynamodb/testing/DynamoDbTable {
public fun <init> (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Lkotlin/reflect/KClass;
public final fun component2 ()Lkotlin/jvm/functions/Function1;
public final fun copy (Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;)Lmisk/aws/dynamodb/testing/DynamoDbTable;
public static synthetic fun copy$default (Lmisk/aws/dynamodb/testing/DynamoDbTable;Lkotlin/reflect/KClass;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lmisk/aws/dynamodb/testing/DynamoDbTable;
public fun equals (Ljava/lang/Object;)Z
public final fun getConfigureTable ()Lkotlin/jvm/functions/Function1;
public final fun getTableClass ()Lkotlin/reflect/KClass;
public final fun getTableName ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class misk/aws/dynamodb/testing/InProcessDynamoDbModule : misk/inject/KAbstractModule {
public fun <init> (Ljava/util/List;)V
public fun <init> ([Lkotlin/reflect/KClass;)V
public fun <init> ([Lmisk/aws/dynamodb/testing/DynamoDbTable;)V
public final fun provideRequiredTables ()Ljava/util/List;
public final fun providesAmazonDynamoDB (Lmisk/aws/dynamodb/testing/TestDynamoDb;)Lcom/amazonaws/services/dynamodbv2/AmazonDynamoDB;
public final fun providesAmazonDynamoDBStreams (Lmisk/aws/dynamodb/testing/TestDynamoDb;)Lcom/amazonaws/services/dynamodbv2/AmazonDynamoDBStreams;
public final fun providesDynamoDbServiceWrapper ()Lmisk/aws/dynamodb/testing/TestDynamoDb;
}

public final class misk/aws/dynamodb/testing/TestDynamoDb : com/google/common/util/concurrent/Service {
public fun <init> (Lapp/cash/tempest/testing/internal/TestDynamoDbService;)V
public fun addListener (Lcom/google/common/util/concurrent/Service$Listener;Ljava/util/concurrent/Executor;)V
public fun awaitRunning ()V
public fun awaitRunning (JLjava/util/concurrent/TimeUnit;)V
public fun awaitTerminated ()V
public fun awaitTerminated (JLjava/util/concurrent/TimeUnit;)V
public fun failureCause ()Ljava/lang/Throwable;
public final fun getService ()Lapp/cash/tempest/testing/internal/TestDynamoDbService;
public fun isRunning ()Z
public fun startAsync ()Lcom/google/common/util/concurrent/Service;
public fun state ()Lcom/google/common/util/concurrent/Service$State;
public fun stopAsync ()Lcom/google/common/util/concurrent/Service;
}

public abstract interface class misk/dynamodb/DyTimestampedEntity {
public abstract fun getCreated_at ()Ljava/util/Date;
public abstract fun getUpdated_at ()Ljava/util/Date;
Expand Down
27 changes: 23 additions & 4 deletions misk-aws-dynamodb/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
plugins {
kotlin("jvm")
`java-library`
`java-test-fixtures`
}

dependencies {
api("com.amazonaws:aws-java-sdk-core:1.11.960")
api(Dependencies.kotlinLogging)
api(Dependencies.javaxInject)
api(Dependencies.awsDynamodb)
api(Dependencies.awsJavaSdkCore)
api(Dependencies.guice)
api(Dependencies.javaxInject)
api(Dependencies.kotlinLogging)
api(project(":misk-aws"))
api(project(":misk-core"))
api(project(":misk-inject"))
implementation(Dependencies.kotlinReflect)
implementation(Dependencies.wispLogging)
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.0")
implementation(project(":misk-exceptions-dynamodb"))
implementation(project(":misk-service"))

testFixturesApi(Dependencies.awsDynamodb)
testFixturesApi(Dependencies.guice)
testFixturesApi(Dependencies.javaxInject)
testFixturesApi(Dependencies.tempestTestingInternal)
testFixturesApi(project(":misk-aws-dynamodb"))
testFixturesApi(project(":misk-inject"))
testFixturesApi(project(":misk-testing"))
testFixturesImplementation(Dependencies.kotlinReflect)
testFixturesImplementation(Dependencies.tempestTesting)
testFixturesImplementation(Dependencies.tempestTestingDocker)
testFixturesImplementation(Dependencies.tempestTestingJvm)
testFixturesImplementation(project(":misk-core"))
testFixturesImplementation(project(":misk-service"))

testImplementation(Dependencies.assertj)
testImplementation(Dependencies.junitApi)
testImplementation(project(":misk-aws-dynamodb"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,9 @@ import java.time.LocalDate
import javax.inject.Inject

abstract class AbstractDynamoDbTest {

@Inject
lateinit var dynamoDbClient: AmazonDynamoDB

@Inject
lateinit var tables: Set<DynamoDbTable>

@Inject
lateinit var healthCheck: DynamoDbHealthCheck

@Inject
lateinit var serviceManager: ServiceManager
@Inject lateinit var dynamoDbClient: AmazonDynamoDB
@Inject lateinit var healthCheck: DynamoDbHealthCheck
@Inject lateinit var serviceManager: ServiceManager

@Test
fun happyPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package misk.aws.dynamodb.testing
import com.amazonaws.services.dynamodbv2.model.BillingMode
import misk.MiskTestingServiceModule
import misk.inject.KAbstractModule
import misk.testing.MiskExternalDependency
import misk.testing.MiskTest
import misk.testing.MiskTestModule

@MiskTest(startService = true)
class DockerDynamoDbTest : AbstractDynamoDbTest() {
@MiskTestModule val module = TestModule()

@MiskTestModule
val module = TestModule()

class TestModule : KAbstractModule() {
override fun configure() {
install(MiskTestingServiceModule())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import misk.testing.MiskTestModule

@MiskTest(startService = true)
class InProcessDynamoDbTest : AbstractDynamoDbTest() {

@MiskTestModule
val module = TestModule()
@MiskTestModule val module = TestModule()

class TestModule : KAbstractModule() {
override fun configure() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package misk.aws.dynamodb.testing

import app.cash.tempest.testing.DockerDynamoDbServer
import app.cash.tempest.testing.TestTable
import app.cash.tempest.testing.internal.TestDynamoDbService
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams
import com.google.common.util.concurrent.AbstractService
import com.google.inject.Provides
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.reflect.KClass
import misk.ServiceModule
import misk.dynamodb.DynamoDbHealthCheck
import misk.dynamodb.DynamoDbService
import misk.dynamodb.RequiredDynamoDbTable
import misk.healthchecks.HealthCheck
import misk.inject.KAbstractModule

/**
* Spins up a docker container for testing. It clears the table content before each test starts.
*
* Note that this may not be used alongside [InProcessDynamoDbModule]. DynamoDB may execute in Docker or
* in-process, but never both.
*/
class DockerDynamoDbModule(
private val tables: List<DynamoDbTable>
) : KAbstractModule() {

constructor(vararg tables: DynamoDbTable) : this(tables.toList())
constructor(vararg tables: KClass<*>) : this(tables.map { DynamoDbTable(it) })

override fun configure() {
for (table in tables) {
multibind<DynamoDbTable>().toInstance(table)
}
multibind<HealthCheck>().to<DynamoDbHealthCheck>()
bind<DynamoDbService>().to<DockerDynamoDbService>()
install(ServiceModule<DynamoDbService>().dependsOn<TestDynamoDb>())
install(ServiceModule<TestDynamoDb>())
}

@Provides @Singleton
fun provideRequiredTables(): List<RequiredDynamoDbTable> =
tables.map { RequiredDynamoDbTable(it.tableName) }

@Provides @Singleton
fun providesTestDynamoDb(): TestDynamoDb {
return TestDynamoDb(
TestDynamoDbService.create(
serverFactory = DockerDynamoDbServer.Factory,
tables = tables.map { TestTable.create(it.tableClass, it.configureTable) },
port = null
)
)
}

@Provides @Singleton
fun providesAmazonDynamoDB(testDynamoDb: TestDynamoDb): AmazonDynamoDB {
return testDynamoDb.service.client.dynamoDb
}

@Provides @Singleton
fun providesAmazonDynamoDBStreams(testDynamoDb: TestDynamoDb): AmazonDynamoDBStreams {
return testDynamoDb.service.client.dynamoDbStreams
}

/** This service does nothing; depending on Tempest's [TestDynamoDb] is sufficient. */
@Singleton
private class DockerDynamoDbService @Inject constructor() : AbstractService(), DynamoDbService {
override fun doStart() = notifyStarted()
override fun doStop() = notifyStopped()
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package misk.aws.dynamodb.testing

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest
import kotlin.reflect.KClass
import kotlin.reflect.full.findAnnotation

/**
* Use this with [DockerDynamoDbModule] or [InProcessDynamoDbModule] to configure your DynamoDB
* tables for each test execution.
*
* Use [configureTable] to customize the table creation request for testing, such as to configure
* the secondary indexes required by `ProjectionType.ALL`.
*/
data class DynamoDbTable(
val tableClass: KClass<*>,
val configureTable: (CreateTableRequest) -> CreateTableRequest = { it }
) {
val tableName: String
get() {
val annotation = tableClass.findAnnotation<DynamoDBTable>()
?: throw IllegalStateException("Expected @DynamoDBTable on $tableClass")
return annotation.tableName
}
}
Loading

0 comments on commit 64c4bed

Please sign in to comment.