mirror of
https://github.com/google/nomulus
synced 2026-09-06 16:17:13 +00:00
Add loadOnlyOf method to tm() (#1162)
* Add loadOnlyOf method to tm() In addition there's a bit of a refator of SqlReplayCheckpoint to make it more in line with the other singletons. This method is useful for the singleton classes where we expect at most one entity to exist, e.g. ServerSecret.
This commit is contained in:
+23
@@ -17,7 +17,9 @@ package google.registry.persistence.transaction;
|
||||
import static com.google.common.collect.ImmutableList.toImmutableList;
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth8.assertThat;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
@@ -372,6 +374,27 @@ public class TransactionManagerTest {
|
||||
() -> tm().transact(() -> tm().loadAllOf(TestEntity.class)));
|
||||
}
|
||||
|
||||
@TestOfyAndSql
|
||||
void loadSingleton_returnsValue_orEmpty() {
|
||||
assertEntityNotExist(theEntity);
|
||||
assertThat(transactIfJpaTm(() -> tm().loadSingleton(TestEntity.class))).isEmpty();
|
||||
|
||||
tm().transact(() -> tm().insert(theEntity));
|
||||
assertThat(transactIfJpaTm(() -> tm().loadSingleton(TestEntity.class))).hasValue(theEntity);
|
||||
}
|
||||
|
||||
@TestOfyAndSql
|
||||
void loadSingleton_exceptionOnMultiple() {
|
||||
assertAllEntitiesNotExist(moreEntities);
|
||||
tm().transact(() -> tm().insertAll(moreEntities));
|
||||
assertThat(
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> transactIfJpaTm(() -> tm().loadSingleton(TestEntity.class))))
|
||||
.hasMessageThat()
|
||||
.isEqualTo("Expected at most one entity of type TestEntity, found at least two");
|
||||
}
|
||||
|
||||
private static void assertEntityExists(TestEntity entity) {
|
||||
assertThat(tm().transact(() -> tm().exists(entity))).isTrue();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user