Merge two PremiumList entities (#690)

This commit is contained in:
Shicong Huang 2020-07-21 18:18:52 -04:00 committed by GitHub
parent d0aa55e976
commit 26e7e72727
14 changed files with 225 additions and 226 deletions

View file

@ -28,12 +28,14 @@ import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key;
import google.registry.model.registry.Registry;
import google.registry.model.registry.label.PremiumList;
import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock;
import java.math.BigDecimal;
import java.util.Optional;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -51,47 +53,63 @@ public class PremiumListDaoTest {
.withClock(fakeClock)
.build();
private static final ImmutableMap<String, BigDecimal> TEST_PRICES =
ImmutableMap.of(
"silver",
BigDecimal.valueOf(10.23),
"gold",
BigDecimal.valueOf(1305.47),
"palladium",
BigDecimal.valueOf(1552.78));
private ImmutableMap<String, BigDecimal> testPrices;
private PremiumList testList;
@BeforeEach
void setUp() {
testPrices =
ImmutableMap.of(
"silver",
BigDecimal.valueOf(10.23),
"gold",
BigDecimal.valueOf(1305.47),
"palladium",
BigDecimal.valueOf(1552.78));
testList =
new PremiumList.Builder()
.setName("testname")
.setCurrency(USD)
.setLabelsToPrices(testPrices)
.setCreationTime(fakeClock.nowUtc())
.build();
}
@Test
public void saveNew_worksSuccessfully() {
PremiumList premiumList = PremiumList.create("testname", USD, TEST_PRICES);
PremiumListDao.saveNew(premiumList);
PremiumListDao.saveNew(testList);
jpaTm()
.transact(
() -> {
Optional<PremiumList> persistedListOpt = PremiumListDao.getLatestRevision("testname");
assertThat(persistedListOpt).isPresent();
PremiumList persistedList = persistedListOpt.get();
assertThat(persistedList.getLabelsToPrices()).containsExactlyEntriesIn(TEST_PRICES);
assertThat(persistedList.getCreationTimestamp()).isEqualTo(fakeClock.nowUtc());
assertThat(persistedList.getLabelsToPrices()).containsExactlyEntriesIn(testPrices);
assertThat(persistedList.getCreationTime()).isEqualTo(fakeClock.nowUtc());
});
}
@Test
public void update_worksSuccessfully() {
PremiumListDao.saveNew(PremiumList.create("testname", CurrencyUnit.USD, TEST_PRICES));
PremiumListDao.saveNew(testList);
Optional<PremiumList> persistedList = PremiumListDao.getLatestRevision("testname");
assertThat(persistedList).isPresent();
long firstRevisionId = persistedList.get().getRevisionId();
PremiumListDao.update(
PremiumList.create(
"testname",
CurrencyUnit.USD,
ImmutableMap.of(
"update",
BigDecimal.valueOf(55343.12),
"new",
BigDecimal.valueOf(0.01),
"silver",
BigDecimal.valueOf(30.03))));
new PremiumList.Builder()
.setName("testname")
.setCurrency(USD)
.setLabelsToPrices(
ImmutableMap.of(
"update",
BigDecimal.valueOf(55343.12),
"new",
BigDecimal.valueOf(0.01),
"silver",
BigDecimal.valueOf(30.03)))
.setCreationTime(fakeClock.nowUtc())
.build());
jpaTm()
.transact(
() -> {
@ -107,20 +125,18 @@ public class PremiumListDaoTest {
BigDecimal.valueOf(0.01),
"silver",
BigDecimal.valueOf(30.03)));
assertThat(updatedList.getCreationTimestamp()).isEqualTo(fakeClock.nowUtc());
assertThat(updatedList.getCreationTime()).isEqualTo(fakeClock.nowUtc());
assertThat(updatedList.getRevisionId()).isGreaterThan(firstRevisionId);
assertThat(updatedList.getCreationTimestamp()).isEqualTo(fakeClock.nowUtc());
assertThat(updatedList.getCreationTime()).isEqualTo(fakeClock.nowUtc());
});
}
@Test
public void saveNew_throwsWhenPremiumListAlreadyExists() {
PremiumListDao.saveNew(PremiumList.create("testlist", USD, TEST_PRICES));
PremiumListDao.saveNew(testList);
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> PremiumListDao.saveNew(PremiumList.create("testlist", USD, TEST_PRICES)));
assertThat(thrown).hasMessageThat().isEqualTo("Premium list 'testlist' already exists");
assertThrows(IllegalArgumentException.class, () -> PremiumListDao.saveNew(testList));
assertThat(thrown).hasMessageThat().isEqualTo("Premium list 'testname' already exists");
}
// TODO(b/147246613): Un-ignore this.
@ -128,19 +144,17 @@ public class PremiumListDaoTest {
@Disabled
public void update_throwsWhenListDoesntExist() {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> PremiumListDao.update(PremiumList.create("testlist", USD, TEST_PRICES)));
assertThrows(IllegalArgumentException.class, () -> PremiumListDao.update(testList));
assertThat(thrown)
.hasMessageThat()
.isEqualTo("Can't update non-existent premium list 'testlist'");
.isEqualTo("Can't update non-existent premium list 'testname'");
}
@Test
public void checkExists_worksSuccessfully() {
assertThat(PremiumListDao.checkExists("testlist")).isFalse();
PremiumListDao.saveNew(PremiumList.create("testlist", USD, TEST_PRICES));
assertThat(PremiumListDao.checkExists("testlist")).isTrue();
assertThat(PremiumListDao.checkExists("testname")).isFalse();
PremiumListDao.saveNew(testList);
assertThat(PremiumListDao.checkExists("testname")).isTrue();
}
@Test
@ -151,8 +165,19 @@ public class PremiumListDaoTest {
@Test
public void getLatestRevision_worksSuccessfully() {
PremiumListDao.saveNew(
PremiumList.create("list1", JPY, ImmutableMap.of("wrong", BigDecimal.valueOf(1000.50))));
PremiumListDao.update(PremiumList.create("list1", JPY, TEST_PRICES));
new PremiumList.Builder()
.setName("list1")
.setCurrency(JPY)
.setLabelsToPrices(ImmutableMap.of("wrong", BigDecimal.valueOf(1000.50)))
.setCreationTime(fakeClock.nowUtc())
.build());
PremiumListDao.update(
new PremiumList.Builder()
.setName("list1")
.setCurrency(JPY)
.setLabelsToPrices(testPrices)
.setCreationTime(fakeClock.nowUtc())
.build());
jpaTm()
.transact(
() -> {
@ -161,7 +186,7 @@ public class PremiumListDaoTest {
assertThat(persistedList.get().getName()).isEqualTo("list1");
assertThat(persistedList.get().getCurrency()).isEqualTo(JPY);
assertThat(persistedList.get().getLabelsToPrices())
.containsExactlyEntriesIn(TEST_PRICES);
.containsExactlyEntriesIn(testPrices);
});
}
@ -182,7 +207,13 @@ public class PremiumListDaoTest {
google.registry.model.registry.label.PremiumList.class,
"premlist"))
.build());
PremiumListDao.saveNew(PremiumList.create("premlist", USD, TEST_PRICES));
PremiumListDao.saveNew(
new PremiumList.Builder()
.setName("premlist")
.setCurrency(USD)
.setLabelsToPrices(testPrices)
.setCreationTime(fakeClock.nowUtc())
.build());
assertThat(PremiumListDao.getPremiumPrice("silver", Registry.get("foobar")))
.hasValue(Money.of(USD, 10.23));
assertThat(PremiumListDao.getPremiumPrice("gold", Registry.get("foobar")))
@ -212,16 +243,19 @@ public class PremiumListDaoTest {
"premlist"))
.build());
PremiumListDao.saveNew(
PremiumList.create(
"premlist",
JPY,
ImmutableMap.of(
"silver",
BigDecimal.valueOf(10.00),
"gold",
BigDecimal.valueOf(1000.0),
"palladium",
BigDecimal.valueOf(15000))));
new PremiumList.Builder()
.setName("premlist")
.setCurrency(JPY)
.setLabelsToPrices(
ImmutableMap.of(
"silver",
BigDecimal.valueOf(10.00),
"gold",
BigDecimal.valueOf(1000.0),
"palladium",
BigDecimal.valueOf(15000)))
.setCreationTime(fakeClock.nowUtc())
.build());
assertThat(PremiumListDao.getPremiumPrice("silver", Registry.get("foobar")))
.hasValue(moneyOf(JPY, 10));
assertThat(PremiumListDao.getPremiumPrice("gold", Registry.get("foobar")))

View file

@ -19,16 +19,19 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.hash.BloomFilter;
import google.registry.model.registry.label.PremiumList;
import google.registry.testing.DatastoreEntityExtension;
import java.math.BigDecimal;
import org.joda.money.CurrencyUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/** Unit tests for {@link PremiumList}. */
@RunWith(JUnit4.class)
public class PremiumListTest {
@RegisterExtension
public DatastoreEntityExtension datastoreEntityExtension = new DatastoreEntityExtension();
private static final ImmutableMap<String, BigDecimal> TEST_PRICES =
ImmutableMap.of(
"silver",
@ -41,7 +44,12 @@ public class PremiumListTest {
@Test
public void bloomFilter_worksCorrectly() {
BloomFilter<String> bloomFilter =
PremiumList.create("testname", CurrencyUnit.USD, TEST_PRICES).getBloomFilter();
new PremiumList.Builder()
.setName("testname")
.setCurrency(CurrencyUnit.USD)
.setLabelsToPrices(TEST_PRICES)
.build()
.getBloomFilter();
ImmutableSet.of("silver", "gold", "palladium")
.forEach(l -> assertThat(bloomFilter.mightContain(l)).isTrue());
ImmutableSet.of("dirt", "pyrite", "zirconia")

View file

@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.schema.tld.PremiumListUtils.parseToPremiumList;
import static org.junit.Assert.assertThrows;
import google.registry.model.registry.label.PremiumList;
import google.registry.testing.AppEngineRule;
import java.math.BigDecimal;
import org.junit.Rule;

View file

@ -94,8 +94,7 @@ public class UpdatePremiumListActionTest {
jpaTm()
.transact(
() -> {
google.registry.schema.tld.PremiumList persistedList =
PremiumListDao.getLatestRevision("foo").get();
PremiumList persistedList = PremiumListDao.getLatestRevision("foo").get();
assertThat(persistedList.getLabelsToPrices())
.containsEntry("rich", new BigDecimal("75.00"));
assertThat(persistedList.getLabelsToPrices())