PageRenderTime 22ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/query/JPQLDeleteTest.java

http://datanucleus-appengine.googlecode.com/
Java | 197 lines | 151 code | 26 blank | 20 comment | 0 complexity | 6bad587d03f22c303f15f6b33179f45e MD5 | raw file
Possible License(s): Apache-2.0
  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus.query;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.api.datastore.EntityNotFoundException;
  16. import com.google.appengine.api.datastore.Key;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.appengine.datanucleus.DatastoreManager;
  19. import com.google.appengine.datanucleus.Utils;
  20. import com.google.appengine.datanucleus.jpa.JPATestCase;
  21. import com.google.appengine.datanucleus.test.jpa.Book;
  22. import com.google.appengine.datanucleus.test.jpa.HasKeyAncestorKeyPkJPA;
  23. import com.google.appengine.datanucleus.test.jpa.HasOneToManyListJPA;
  24. import javax.persistence.PersistenceException;
  25. import javax.persistence.Query;
  26. /**
  27. * @author Max Ross <maxr@google.com>
  28. */
  29. public class JPQLDeleteTest extends JPATestCase {
  30. public void testDelete_Txn_MultipleEntityGroups() {
  31. ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67890"));
  32. ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67891"));
  33. Query q = em.createQuery("DELETE FROM " + Book.class.getName() + " b");
  34. beginTxn();
  35. try {
  36. q.executeUpdate();
  37. fail("expected exception");
  38. } catch (PersistenceException e) {
  39. // good - can't delete books from multiple entity groups in a txn
  40. }
  41. rollbackTxn();
  42. assertEquals(2, countForClass(Book.class));
  43. }
  44. public void testDelete_Txn_OneEntityGroup() {
  45. ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67890"));
  46. ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67891"));
  47. Query q = em.createQuery("DELETE FROM " + Book.class.getName() + " b");
  48. beginTxn();
  49. try {
  50. q.executeUpdate();
  51. fail("expected exception");
  52. } catch (PersistenceException e) {
  53. // good - can't delete books from multiple entity groups in a txn
  54. }
  55. rollbackTxn();
  56. assertEquals(2, countForClass(Book.class));
  57. }
  58. public void testDelete_NoTxn() {
  59. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  60. ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67890"));
  61. ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67891"));
  62. Query q = em.createQuery("DELETE FROM " + Book.class.getName() + " b");
  63. assertEquals(2, q.executeUpdate());
  64. assertEquals(0, countForClass(Book.class));
  65. }
  66. public void testDeleteAncestorQuery_Txn() {
  67. Key parentKey = KeyFactory.createKey("yar", 23);
  68. Entity pojo1 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey);
  69. Entity pojo2 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey);
  70. ds.put(pojo1);
  71. ds.put(pojo2);
  72. Query q = em.createQuery("DELETE FROM " + HasKeyAncestorKeyPkJPA.class.getName() + " b WHERE ancestorKey = :p1");
  73. q.setParameter("p1", parentKey);
  74. beginTxn();
  75. assertEquals(2, q.executeUpdate());
  76. commitTxn();
  77. assertEquals(0, countForClass(HasKeyAncestorKeyPkJPA.class));
  78. }
  79. public void testDeleteAncestorQuery_TxnRollback() throws EntityNotFoundException {
  80. Key parentKey = KeyFactory.createKey("yar", 23);
  81. Entity pojo1 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey);
  82. Entity pojo2 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey);
  83. ds.put(pojo1);
  84. ds.put(pojo2);
  85. Query q = em.createQuery("DELETE FROM " + HasKeyAncestorKeyPkJPA.class.getName() + " b WHERE ancestorKey = :p1");
  86. q.setParameter("p1", parentKey);
  87. beginTxn();
  88. assertEquals(2, q.executeUpdate());
  89. rollbackTxn();
  90. assertEquals(2, countForClass(HasKeyAncestorKeyPkJPA.class));
  91. }
  92. public void testDeleteAncestorQuery_NoTxn() {
  93. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  94. Key parentKey = KeyFactory.createKey("yar", 23);
  95. Entity pojo1 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey);
  96. Entity pojo2 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey);
  97. ds.put(pojo1);
  98. ds.put(pojo2);
  99. Query q = em.createQuery("DELETE FROM " + HasKeyAncestorKeyPkJPA.class.getName() + " b WHERE ancestorKey = :p1");
  100. q.setParameter("p1", parentKey);
  101. assertEquals(2, q.executeUpdate());
  102. assertEquals(0, countForClass(HasKeyAncestorKeyPkJPA.class));
  103. }
  104. public void testBatchDelete_NoTxn_FastButInaccurate() {
  105. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  106. Entity e1 = Book.newBookEntity("title", "auth", "123432", -40);
  107. ds.put(e1);
  108. Entity e2 = Book.newBookEntity("title", "auth", "123432", -40);
  109. ds.put(e2);
  110. Entity e3 = Book.newBookEntity("title", "auth", "123432", -40);
  111. ds.put(e3);
  112. Key key = KeyFactory.createKey("yar", "does not exist");
  113. Query q = em.createQuery("delete from " + Book.class.getName() + " b where id = :ids");
  114. q.setParameter("ids", Utils.newArrayList(key, e1.getKey(), e2.getKey()));
  115. assertEquals(3, q.executeUpdate());
  116. assertEquals(1, countForClass(Book.class));
  117. }
  118. public void testBatchDelete_NoTxn_SlowButAccurate() {
  119. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  120. Entity e1 = Book.newBookEntity("title", "auth", "123432", -40);
  121. ds.put(e1);
  122. Entity e2 = Book.newBookEntity("title", "auth", "123432", -40);
  123. ds.put(e2);
  124. Entity e3 = Book.newBookEntity("title", "auth", "123432", -40);
  125. ds.put(e3);
  126. Key key = KeyFactory.createKey("yar", "does not exist");
  127. Query q = em.createQuery("delete from " + Book.class.getName() + " b where id = :ids");
  128. q.setHint(DatastoreManager.QUERYEXT_SLOW_BUT_MORE_ACCURATE_JPQL_DELETE, true);
  129. q.setParameter("ids", Utils.newArrayList(key, e1.getKey(), e2.getKey()));
  130. assertEquals(2, q.executeUpdate());
  131. assertEquals(1, countForClass(Book.class));
  132. }
  133. public void testBatchDelete_Txn() {
  134. Key parent = KeyFactory.createKey("yar", 23);
  135. Entity e1 = Book.newBookEntity(parent, "title", "auth", "123432", -40);
  136. ds.put(e1);
  137. Entity e2 = Book.newBookEntity(parent, "title", "auth", "123432", -40);
  138. ds.put(e2);
  139. Entity e3 = Book.newBookEntity(parent, "title", "auth", "123432", -40);
  140. ds.put(e3);
  141. beginTxn();
  142. Query q = em.createQuery("delete from " + Book.class.getName() + " b where id = :ids");
  143. q.setParameter("ids", Utils.newArrayList(parent, e1.getKey(), e2.getKey()));
  144. assertEquals(3, q.executeUpdate());
  145. assertEquals(3, countForClass(Book.class));
  146. commitTxn();
  147. assertEquals(1, countForClass(Book.class));
  148. }
  149. public void testDeleteDoesNotCascade() {
  150. HasOneToManyListJPA parent = new HasOneToManyListJPA();
  151. Book b = new Book();
  152. b.setAuthor("author");
  153. parent.getBooks().add(b);
  154. beginTxn();
  155. em.persist(parent);
  156. commitTxn();
  157. assertEquals(1, countForClass(Book.class));
  158. assertEquals(1, countForClass(HasOneToManyListJPA.class));
  159. beginTxn();
  160. Query q = em.createQuery("delete from " + HasOneToManyListJPA.class.getName() + " b");
  161. assertEquals(1, q.executeUpdate());
  162. assertEquals(1, countForClass(Book.class));
  163. assertEquals(1, countForClass(HasOneToManyListJPA.class));
  164. commitTxn();
  165. assertEquals(1, countForClass(Book.class));
  166. assertEquals(0, countForClass(HasOneToManyListJPA.class));
  167. }
  168. }