No effective change - tidy test only, TestStatelessUpdate

This commit is contained in:
rob bygrave committed 2020-12-01 14:29:37 +13:00
1 parent 66454d2e61
commit 2c8cc793ab
1 file changed
+103 -62
@@ -1,9 +1,8 @@
package org.tests.update; package org.tests.update;
import io.ebean.Ebean; import io.ebean.DB;
import io.ebean.EbeanServer;
import io.ebean.TransactionalTestCase; import io.ebean.TransactionalTestCase;
import org.junit.Assert; import io.ebeantest.LoggedSql;
import org.junit.Test; import org.junit.Test;
import org.tests.model.basic.Contact; import org.tests.model.basic.Contact;
import org.tests.model.basic.Customer; import org.tests.model.basic.Customer;
@@ -20,13 +19,12 @@ import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class TestStatelessUpdate extends TransactionalTestCase { public class TestStatelessUpdate extends TransactionalTestCase {
private EbeanServer server = server();
@Test @Test
public void test() { public void test() {
@@ -35,41 +33,50 @@ public class TestStatelessUpdate extends TransactionalTestCase {
e.setStatus(Status.NEW); e.setStatus(Status.NEW);
e.setDescription("wow"); e.setDescription("wow");
server.save(e); DB.save(e);
// confirm saved as expected // confirm saved as expected
EBasic eBasic = server.find(EBasic.class, e.getId()); EBasic original = DB.find(EBasic.class, e.getId());
assertEquals(e.getId(), eBasic.getId()); assertEquals(e.getId(), original.getId());
assertEquals(e.getName(), eBasic.getName()); assertEquals(e.getName(), original.getName());
assertEquals(e.getStatus(), eBasic.getStatus()); assertEquals(e.getStatus(), original.getStatus());
assertEquals(e.getDescription(), eBasic.getDescription()); assertEquals(e.getDescription(), original.getDescription());
// test updating just the name // test updating just the name
EBasic updateAll = new EBasic(); EBasic updateNameOnly = new EBasic();
updateAll.setId(e.getId()); updateNameOnly.setId(e.getId());
updateAll.setName("updAllProps"); updateNameOnly.setName("updateNameOnly");
server.update(updateAll); LoggedSql.start();
DB.update(updateNameOnly);
eBasic = server.find(EBasic.class, e.getId()); List<String> sql = LoggedSql.collect();
assertEquals(e.getStatus(), eBasic.getStatus()); original = DB.find(EBasic.class, e.getId());
assertEquals(e.getDescription(), eBasic.getDescription()); assertEquals(e.getStatus(), original.getStatus());
assertEquals(updateAll.getName(), eBasic.getName()); assertEquals(e.getDescription(), original.getDescription());
assertEquals(updateNameOnly.getName(), original.getName());
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update e_basic set name=? where id=?; -- bind(updateNameOnly");
LoggedSql.collect();
// test setting null // test setting null
EBasic updateDeflt = new EBasic(); EBasic updateWithNull = new EBasic();
updateDeflt.setId(e.getId()); updateWithNull.setId(e.getId());
updateDeflt.setName("updateDeflt"); updateWithNull.setName("updateWithNull");
updateDeflt.setDescription(null); updateWithNull.setDescription(null);
server.update(updateDeflt); DB.update(updateWithNull);
sql = LoggedSql.stop();
// name and description changed (using null) // name and description changed (using null)
eBasic = server.find(EBasic.class, e.getId()); original = DB.find(EBasic.class, e.getId());
assertEquals(e.getStatus(), eBasic.getStatus()); assertEquals(e.getStatus(), original.getStatus());
assertEquals(updateDeflt.getName(), eBasic.getName()); assertEquals(updateWithNull.getName(), original.getName());
assertNull(eBasic.getDescription()); assertNull(original.getDescription());
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update e_basic set name=?, description=? where id=?; -- bind(updateWithNull,null,");
} }
@Test(expected = EntityNotFoundException.class) @Test(expected = EntityNotFoundException.class)
@@ -80,7 +87,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
basic.setName("something"); basic.setName("something");
basic.setStatus(Status.ACTIVE); basic.setStatus(Status.ACTIVE);
Ebean.update(basic); DB.update(basic);
} }
@Test @Test
@@ -91,7 +98,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
basic.setName("something"); basic.setName("something");
basic.setStatus(Status.ACTIVE); basic.setStatus(Status.ACTIVE);
assertThat(Ebean.delete(basic)).isFalse(); assertThat(DB.delete(basic)).isFalse();
} }
/** /**
@@ -106,16 +113,17 @@ public class TestStatelessUpdate extends TransactionalTestCase {
basic.setStatus(Status.NEW); basic.setStatus(Status.NEW);
basic.setDescription("wow"); basic.setDescription("wow");
server.save(basic); DB.save(basic);
LoggedSql.start();
// act // act
EBasic basicWithoutChanges = new EBasic(); EBasic basicWithoutChanges = new EBasic();
basicWithoutChanges.setId(basic.getId()); basicWithoutChanges.setId(basic.getId());
server.update(basicWithoutChanges); DB.update(basicWithoutChanges);
// assert // assert no update executed
// Nothing to check, simply no exception should occur final List<String> sql = LoggedSql.stop();
// maybe ensure that no update has been executed assertThat(sql).isEmpty();
} }
/** /**
@@ -135,17 +143,18 @@ public class TestStatelessUpdate extends TransactionalTestCase {
Customer customer = new Customer(); Customer customer = new Customer();
customer.setName("something"); customer.setName("something");
server.save(customer); DB.save(customer);
LoggedSql.start();
// act // act
Customer customerWithoutChanges = new Customer(); Customer customerWithoutChanges = new Customer();
customerWithoutChanges.setId(customer.getId()); customerWithoutChanges.setId(customer.getId());
server.update(customerWithoutChanges); DB.update(customerWithoutChanges);
final List<String> sql = LoggedSql.stop();
Customer result = Ebean.find(Customer.class, customer.getId()); Customer result = DB.find(Customer.class, customer.getId());
// assert
assertThat(result.getUpdtime()).isEqualToIgnoringMillis(customer.getUpdtime()); assertThat(result.getUpdtime()).isEqualToIgnoringMillis(customer.getUpdtime());
assertThat(sql).isEmpty();
} }
/** /**
@@ -163,7 +172,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
customer.setContacts(new ArrayList<>()); customer.setContacts(new ArrayList<>());
customer.getContacts().add(contact); customer.getContacts().add(contact);
server.save(customer); DB.save(customer);
// act // act
Customer customerWithChange = new Customer(); Customer customerWithChange = new Customer();
@@ -172,13 +181,17 @@ public class TestStatelessUpdate extends TransactionalTestCase {
// contacts is not loaded // contacts is not loaded
assertFalse(containsContacts(customerWithChange)); assertFalse(containsContacts(customerWithChange));
server.update(customerWithChange); LoggedSql.start();
DB.update(customerWithChange);
final List<String> sql = LoggedSql.stop();
Customer result = Ebean.find(Customer.class, customer.getId()); Customer result = DB.find(Customer.class, customer.getId());
// assert null list was ignored (missing children not deleted) // assert null list was ignored (missing children not deleted)
Assert.assertNotNull(result.getContacts()); assertNotNull(result.getContacts());
assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty()); assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty());
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update o_customer set name=?, updtime=? where id=?;");
} }
/** /**
@@ -198,7 +211,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
customer.setContacts(new ArrayList<>()); customer.setContacts(new ArrayList<>());
customer.getContacts().add(contact); customer.getContacts().add(contact);
server.save(customer); DB.save(customer);
// act // act
Customer customerWithChange = new Customer(); Customer customerWithChange = new Customer();
@@ -210,13 +223,17 @@ public class TestStatelessUpdate extends TransactionalTestCase {
// contacts has been initialised to empty BeanList // contacts has been initialised to empty BeanList
assertTrue(containsContacts(customerWithChange)); assertTrue(containsContacts(customerWithChange));
server.update(customerWithChange); LoggedSql.start();
DB.update(customerWithChange);
final List<String> sql = LoggedSql.stop();
Customer result = Ebean.find(Customer.class, customer.getId()); Customer result = DB.find(Customer.class, customer.getId());
// assert empty bean list was ignore (missing children not deleted) // assert empty bean list was ignore (missing children not deleted)
Assert.assertNotNull(result.getContacts()); assertNotNull(result.getContacts());
assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty()); assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty());
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update o_customer set name=?, updtime=? where id=?;");
} }
@Test @Test
@@ -231,7 +248,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
customer.setContacts(new ArrayList<>()); customer.setContacts(new ArrayList<>());
customer.getContacts().add(contact); customer.getContacts().add(contact);
server.save(customer); DB.save(customer);
// act // act
Customer customerWithChange = new Customer(); Customer customerWithChange = new Customer();
@@ -242,16 +259,20 @@ public class TestStatelessUpdate extends TransactionalTestCase {
customerWithChange.setContacts(Collections.<Contact>emptyList()); customerWithChange.setContacts(Collections.<Contact>emptyList());
assertTrue(containsContacts(customerWithChange)); assertTrue(containsContacts(customerWithChange));
server.update(customerWithChange); LoggedSql.start();
DB.update(customerWithChange);
final List<String> sql = LoggedSql.stop();
Customer result = Ebean.find(Customer.class, customer.getId()); Customer result = DB.find(Customer.class, customer.getId());
// assert empty bean list was ignore (missing children not deleted) // assert empty bean list was ignore (missing children not deleted)
assertThat(result.getContacts()).hasSize(1); assertThat(result.getContacts()).hasSize(1);
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("update o_customer set name=?, updtime=? where id=?;");
} }
private boolean containsContacts(Customer cust) { private boolean containsContacts(Customer cust) {
return server.getBeanState(cust).getLoadedProps().contains("contacts"); return DB.getBeanState(cust).getLoadedProps().contains("contacts");
} }
/** /**
@@ -273,7 +294,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
customer.getContacts().add(contact1); customer.getContacts().add(contact1);
customer.getContacts().add(contact2); customer.getContacts().add(contact2);
server.save(customer); DB.save(customer);
// act // act
Contact updateContact1 = new Contact(); Contact updateContact1 = new Contact();
@@ -287,11 +308,13 @@ public class TestStatelessUpdate extends TransactionalTestCase {
updateCustomer.getContacts().add(updateContact1); updateCustomer.getContacts().add(updateContact1);
updateCustomer.getContacts().add(updateContact2); updateCustomer.getContacts().add(updateContact2);
server.update(updateCustomer); LoggedSql.start();
DB.update(updateCustomer);
final List<String> sql = LoggedSql.stop();
// assert // assert
// maybe check if update instead of insert has been executed, assertThat(sql).hasSize(1);
// currently "Unique index or primary key violation" PersistenceException is throwing assertThat(sql.get(0)).contains("update o_customer set updtime=? where id=?;");
} }
@Test @Test
@@ -308,7 +331,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
customer.getContacts().add(contact1); customer.getContacts().add(contact1);
customer.getContacts().add(contact2); customer.getContacts().add(contact2);
server.save(customer); DB.save(customer);
// act // act
@@ -324,10 +347,19 @@ public class TestStatelessUpdate extends TransactionalTestCase {
updateCustomer.getContacts().add(updateContact1); updateCustomer.getContacts().add(updateContact1);
updateCustomer.getContacts().add(updateContact3); updateCustomer.getContacts().add(updateContact3);
server.update(updateCustomer); LoggedSql.start();
DB.update(updateCustomer);
final List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(5);
assertThat(sql.get(0)).contains("update o_customer set updtime=? where id=?");
assertThat(sql.get(1)).contains("insert into contact");
assertThat(sql.get(2)).contains(" -- bind(");
assertThat(sql.get(3)).contains("update contact set last_name=?, customer_id=? where id=?");
assertThat(sql.get(4)).contains(" -- bind(");
// assert // assert
Customer assCustomer = server.find(Customer.class, customer.getId()); Customer assCustomer = DB.find(Customer.class, customer.getId());
List<Contact> assContacts = assCustomer.getContacts(); List<Contact> assContacts = assCustomer.getContacts();
assertThat(assContacts).hasSize(3); assertThat(assContacts).hasSize(3);
Set<Integer> ids = new LinkedHashSet<>(); Set<Integer> ids = new LinkedHashSet<>();
@@ -357,7 +389,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
customer.getContacts().add(contact1); customer.getContacts().add(contact1);
customer.getContacts().add(contact2); customer.getContacts().add(contact2);
server.save(customer); DB.save(customer);
// act // act
@@ -375,10 +407,19 @@ public class TestStatelessUpdate extends TransactionalTestCase {
updateCustomer.getContacts().add(updateContact3); updateCustomer.getContacts().add(updateContact3);
// not adding contact2 but it won't be deleted in this case // not adding contact2 but it won't be deleted in this case
server.update(updateCustomer); LoggedSql.start();
DB.update(updateCustomer);
final List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(5);
assertThat(sql.get(0)).contains("update o_customer set updtime=? where id=?");
assertThat(sql.get(1)).contains("insert into contact");
assertThat(sql.get(2)).contains(" -- bind(");
assertThat(sql.get(3)).contains("update contact set last_name=?, customer_id=? where id=?");
assertThat(sql.get(4)).contains(" -- bind(");
// assert // assert
Customer assCustomer = server.find(Customer.class, customer.getId()); Customer assCustomer = DB.find(Customer.class, customer.getId());
List<Contact> assContacts = assCustomer.getContacts(); List<Contact> assContacts = assCustomer.getContacts();
// contact 2 was not deleted this time // contact 2 was not deleted this time