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

http://datanucleus-appengine.googlecode.com/ · Java · 257 lines · 210 code · 17 blank · 30 comment · 12 complexity · ca9cc7df292dcfe0bd1f6d67cd4f13b0 MD5 · raw file

  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.datanucleus.DatastoreManager;
  15. import com.google.appengine.datanucleus.jdo.JDOTestCase;
  16. import com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.Course;
  17. import com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.Major;
  18. import com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.Student;
  19. import static com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.newCourse;
  20. import static com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.newMajor;
  21. import static com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.newStudent;
  22. import java.util.Collections;
  23. import javax.jdo.JDOUserException;
  24. import javax.jdo.Query;
  25. /**
  26. * @author Max Ross <maxr@google.com>
  27. */
  28. public class JDOQLQueryUnownedJoinTest extends JDOTestCase {
  29. public void testJoinOnOneToMany_Simple() {
  30. Course course1 = newCourse("Biology");
  31. makePersistentInTxn(course1, TXN_START_END);
  32. Course course2 = newCourse("Not Biology");
  33. makePersistentInTxn(course2, TXN_START_END);
  34. Student student = newStudent(10, course1, course2);
  35. makePersistentInTxn(student, TXN_START_END);
  36. beginTxn();
  37. Query q = pm.newQuery(
  38. "select from " + Student.class.getName() + " where "
  39. + "courses.contains(c) && c.department == 'Biology' && "
  40. + "grade == 10");
  41. q.declareVariables(Course.class.getName() + " c");
  42. assertEquals(Collections.singletonList(student), q.execute());
  43. commitTxn();
  44. }
  45. public void testJoinOnOneToMany_LegalOrderBy() {
  46. Course course1 = newCourse("Biology");
  47. makePersistentInTxn(course1, TXN_START_END);
  48. Course course2 = newCourse("Not Biology");
  49. makePersistentInTxn(course2, TXN_START_END);
  50. Student student = newStudent(10, course1, course2);
  51. makePersistentInTxn(student, TXN_START_END);
  52. beginTxn();
  53. Query q = pm.newQuery(
  54. "select from " + Student.class.getName() + " where "
  55. + "courses.contains(c) && c.department == 'Biology' && "
  56. + "grade == 10 order by courses asc");
  57. q.declareVariables(Course.class.getName() + " c");
  58. assertEquals(Collections.singletonList(student), q.execute());
  59. commitTxn();
  60. }
  61. public void testJoinOnOneToMany_Offset() {
  62. Course course1 = newCourse("Biology");
  63. makePersistentInTxn(course1, TXN_START_END);
  64. Course course2 = newCourse("Not Biology");
  65. makePersistentInTxn(course2, TXN_START_END);
  66. Student student = newStudent(10, course1, course2);
  67. makePersistentInTxn(student, TXN_START_END);
  68. Student student2 = newStudent(11, course1, course2);
  69. makePersistentInTxn(student2, TXN_START_END);
  70. Student student3 = newStudent(10, course1, course2);
  71. makePersistentInTxn(student3, TXN_START_END);
  72. beginTxn();
  73. Query q = pm.newQuery(
  74. "select from " + Student.class.getName() + " where "
  75. + "courses.contains(c) && c.department == 'Biology' && "
  76. + "grade == 10");
  77. q.declareVariables(Course.class.getName() + " c");
  78. q.setRange(1, Long.MAX_VALUE);
  79. assertEquals(Collections.singletonList(student3), q.execute());
  80. q.setRange(2, Long.MAX_VALUE);
  81. assertEquals(Collections.emptyList(), q.execute());
  82. commitTxn();
  83. }
  84. public void testJoinOnOneToMany_Limit() {
  85. Course course1 = newCourse("Biology");
  86. makePersistentInTxn(course1, TXN_START_END);
  87. Course course2 = newCourse("Not Biology");
  88. makePersistentInTxn(course2, TXN_START_END);
  89. Student student = newStudent(10, course1, course2);
  90. makePersistentInTxn(student, TXN_START_END);
  91. Student student2 = newStudent(11, course1, course2);
  92. makePersistentInTxn(student2, TXN_START_END);
  93. Student student3 = newStudent(10, course1, course2);
  94. makePersistentInTxn(student3, TXN_START_END);
  95. beginTxn();
  96. Query q = pm.newQuery(
  97. "select from " + Student.class.getName() + " where "
  98. + "courses.contains(c) && c.department == 'Biology' && "
  99. + "grade == 10");
  100. q.declareVariables(Course.class.getName() + " c");
  101. q.setRange(0, 1);
  102. assertEquals(Collections.singletonList(student), q.execute());
  103. q.setRange(0, 0);
  104. assertEquals(Collections.emptyList(), q.execute());
  105. commitTxn();
  106. }
  107. public void testJoinOnOneToOne_Simple() {
  108. Major major1 = newMajor("Liberal Arts");
  109. makePersistentInTxn(major1, TXN_START_END);
  110. Major major2 = newMajor("Engineering");
  111. makePersistentInTxn(major2, TXN_START_END);
  112. Student student1 = newStudent(10, major1);
  113. Student student2 = newStudent(10, major2);
  114. makePersistentInTxn(student1, TXN_START_END);
  115. makePersistentInTxn(student2, TXN_START_END);
  116. beginTxn();
  117. Query q = pm.newQuery(
  118. "select from " + Student.class.getName() + " where "
  119. + "major == m && m.school == 'Liberal Arts' && "
  120. + "grade == 10");
  121. q.declareVariables(Major.class.getName() + " m");
  122. assertEquals(Collections.singletonList(student1), q.execute());
  123. commitTxn();
  124. }
  125. public void testJoinOnOneToMany_Illegal() {
  126. beginTxn();
  127. // join condition can't be >
  128. Query q = pm.newQuery(
  129. "select from " + Student.class.getName() + " where "
  130. + "courses > c && c.department == 'Biology' && "
  131. + "grade == 10");
  132. q.declareVariables(Course.class.getName() + " c");
  133. q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
  134. try {
  135. q.execute();
  136. fail("expected exception");
  137. } catch (JDOUserException jdoe) {
  138. if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  139. // good
  140. }
  141. else {
  142. throw jdoe;
  143. }
  144. }
  145. // all filters on parent must be equality filters
  146. q = pm.newQuery(
  147. "select from " + Student.class.getName() + " where "
  148. + "courses.contains(c) && c.department == 'Biology' && "
  149. + "grade > 10");
  150. q.declareVariables(Course.class.getName() + " c");
  151. q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
  152. try {
  153. q.execute();
  154. fail("expected exception");
  155. } catch (JDOUserException jdoe) {
  156. if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  157. // good
  158. }
  159. else {
  160. throw jdoe;
  161. }
  162. }
  163. // all filters on child must be equality filters
  164. q = pm.newQuery(
  165. "select from " + Student.class.getName() + " where "
  166. + "courses.contains(c) && c.department > 'Biology' && "
  167. + "grade == 10");
  168. q.declareVariables(Course.class.getName() + " c");
  169. q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
  170. try {
  171. q.execute();
  172. fail("expected exception");
  173. } catch (JDOUserException jdoe) {
  174. if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  175. // good
  176. }
  177. else {
  178. throw jdoe;
  179. }
  180. }
  181. // sort on parent can only be by join column in asc order
  182. q = pm.newQuery(
  183. "select from " + Student.class.getName() + " where "
  184. + "courses.contains(c) && c.department == 'Biology' && "
  185. + "grade == 10 order by grade");
  186. q.declareVariables(Course.class.getName() + " c");
  187. q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
  188. try {
  189. q.execute();
  190. fail("expected exception");
  191. } catch (JDOUserException jdoe) {
  192. if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  193. // good
  194. }
  195. else {
  196. throw jdoe;
  197. }
  198. }
  199. // sort is by the join column but in the wrong order
  200. q = pm.newQuery(
  201. "select from " + Student.class.getName() + " where "
  202. + "courses.contains(c) && c.department == 'Biology' && "
  203. + "grade == 10 order by courses desc");
  204. q.declareVariables(Course.class.getName() + " c");
  205. q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
  206. try {
  207. q.execute();
  208. fail("expected exception");
  209. } catch (JDOUserException jdoe) {
  210. if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  211. // good
  212. }
  213. else {
  214. throw jdoe;
  215. }
  216. }
  217. // can't sort by child property
  218. q = pm.newQuery(
  219. "select from " + Student.class.getName() + " where "
  220. + "courses.contains(c) && c.department == 'Biology' && "
  221. + "grade == 10 order by c.department");
  222. q.declareVariables(Course.class.getName() + " c");
  223. q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
  224. try {
  225. q.execute();
  226. fail("expected exception");
  227. } catch (JDOUserException jdoe) {
  228. if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
  229. // good
  230. }
  231. else {
  232. throw jdoe;
  233. }
  234. }
  235. commitTxn();
  236. }
  237. }