Skip to content

Commit

Permalink
Return values from DatastoreAccess#update() methods may not reflect the
Browse files Browse the repository at this point in the history
changes made by custom mappers #76
  • Loading branch information
sai-pullabhotla committed Nov 3, 2016
1 parent 759c0ae commit baa9267
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/jmethods/catatumbo/impl/DatastoreUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.jmethods.catatumbo.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.cloud.datastore.Datastore;
Expand Down Expand Up @@ -66,6 +67,23 @@ static <E> List<E> toEntities(Class<E> entityClass, List<Entity> nativeEntities)
return entities;
}

/**
* Converts the given array of native entities to a list of model objects of
* given type, <code>entityClass</code>.
*
* @param entityClass
* the entity class
* @param nativeEntities
* native entities to convert
* @return the list of model objects
*/
static <E> List<E> toEntities(Class<E> entityClass, Entity[] nativeEntities) {
if (nativeEntities == null || nativeEntities.length == 0) {
return new ArrayList<>();
}
return toEntities(entityClass, Arrays.asList(nativeEntities));
}

/**
* Converts the given list of model objects to an array of FullEntity
* objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ public <E> List<E> insert(List<E> entities) {
* @throws EntityManagerException
* if any error occurs while updating.
*/
@SuppressWarnings("unchecked")
public <E> E update(E entity) {
try {
entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entity);
Entity nativeEntity = (Entity) Marshaller.marshal(datastore, entity, true);
nativeWriter.update(nativeEntity);
entityManager.executeEntityListeners(CallbackType.POST_UPDATE, entity);
return entity;
E updatedEntity = (E) Unmarshaller.unmarshal(nativeEntity, entity.getClass());
entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntity);
return updatedEntity;
} catch (DatastoreException exp) {
throw new EntityManagerException(exp);
}
Expand Down Expand Up @@ -241,16 +243,19 @@ private <E> E updateWithOptimisticLockingInternal(E entity, PropertyMetadata ver
* @throws EntityManagerException
* if any error occurs while inserting.
*/
@SuppressWarnings("unchecked")
public <E> List<E> update(List<E> entities) {
if (entities == null || entities.isEmpty()) {
return new ArrayList<>();
}
try {
Class<E> entityClass = (Class<E>) entities.get(0).getClass();
entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entities);
Entity[] nativeEntities = toNativeEntities(entities, datastore);
nativeWriter.update(nativeEntities);
entityManager.executeEntityListeners(CallbackType.POST_UPDATE, entities);
return entities;
List<E> updatedEntities = toEntities(entityClass, nativeEntities);
entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntities);
return updatedEntities;
} catch (DatastoreException exp) {
throw new EntityManagerException(exp);
}
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/jmethods/catatumbo/DecimalFieldsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.junit.BeforeClass;
Expand Down Expand Up @@ -334,4 +335,51 @@ public void testQuery_1() {

}

@Test
public void testUpdate_n10_1() {
DecimalFields entity = new DecimalFields();
BigDecimal n = new BigDecimal("-5.0000");
entity.setN10(n);
entity = em.insert(entity);
entity = em.load(DecimalFields.class, entity.getId());
entity.setN10(new BigDecimal("9.000"));
entity = em.update(entity);
assertEquals(new BigDecimal("9"), entity.getN10());
}

@Test
public void testUpdate_n73_1() {
DecimalFields entity = new DecimalFields();
BigDecimal n = new BigDecimal("9.99");
entity.setN73(n);
entity = em.insert(entity);
entity = em.load(DecimalFields.class, entity.getId());
entity.setN73(new BigDecimal("99.99"));
entity = em.update(entity);
assertEquals(new BigDecimal("99.990"), entity.getN73());
}

@Test
public void testUpdate_n73_2() {
BigDecimal[] inputs = { new BigDecimal("0"), new BigDecimal("-1"), new BigDecimal("5"), new BigDecimal("6.1"),
new BigDecimal("7.55"), new BigDecimal("9.328") };
List<DecimalFields> entities = new ArrayList<>(inputs.length);
for (BigDecimal input : inputs) {
entities.add(new DecimalFields());
}
entities = em.insert(entities);
int i = 0;
for (DecimalFields entity : entities) {
entity.setN73(inputs[i++]);
}
List<DecimalFields> updatedEntities = em.update(entities);
assertFalse(entities.equals(updatedEntities));
assertEquals(new BigDecimal("0.000"), updatedEntities.get(0).getN73());
assertEquals(new BigDecimal("-1.000"), updatedEntities.get(1).getN73());
assertEquals(new BigDecimal("5.000"), updatedEntities.get(2).getN73());
assertEquals(new BigDecimal("6.100"), updatedEntities.get(3).getN73());
assertEquals(new BigDecimal("7.550"), updatedEntities.get(4).getN73());
assertEquals(new BigDecimal("9.328"), updatedEntities.get(5).getN73());
}

}

0 comments on commit baa9267

Please sign in to comment.