PageRenderTime 44ms CodeModel.GetById 21ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/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/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.query;
 17
 18
 19import com.google.appengine.datanucleus.DatastoreManager;
 20import com.google.appengine.datanucleus.jdo.JDOTestCase;
 21import com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.Course;
 22import com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.Major;
 23import com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.Student;
 24
 25import static com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.newCourse;
 26import static com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.newMajor;
 27import static com.google.appengine.datanucleus.test.jdo.UnownedJoinsJDO.newStudent;
 28
 29import java.util.Collections;
 30
 31import javax.jdo.JDOUserException;
 32import javax.jdo.Query;
 33
 34/**
 35 * @author Max Ross <maxr@google.com>
 36 */
 37public class JDOQLQueryUnownedJoinTest extends JDOTestCase {
 38
 39  public void testJoinOnOneToMany_Simple() {
 40    Course course1 = newCourse("Biology");
 41    makePersistentInTxn(course1, TXN_START_END);
 42    Course course2 = newCourse("Not Biology");
 43    makePersistentInTxn(course2, TXN_START_END);
 44    Student student = newStudent(10, course1, course2);
 45    makePersistentInTxn(student, TXN_START_END);
 46    beginTxn();
 47    Query q = pm.newQuery(
 48        "select from " + Student.class.getName() + " where "
 49        + "courses.contains(c) && c.department == 'Biology' && "
 50        + "grade == 10");
 51    q.declareVariables(Course.class.getName() + " c");
 52    assertEquals(Collections.singletonList(student), q.execute());
 53    commitTxn();
 54  }
 55
 56  public void testJoinOnOneToMany_LegalOrderBy() {
 57    Course course1 = newCourse("Biology");
 58    makePersistentInTxn(course1, TXN_START_END);
 59    Course course2 = newCourse("Not Biology");
 60    makePersistentInTxn(course2, TXN_START_END);
 61    Student student = newStudent(10, course1, course2);
 62    makePersistentInTxn(student, TXN_START_END);
 63    beginTxn();
 64    Query q = pm.newQuery(
 65        "select from " + Student.class.getName() + " where "
 66        + "courses.contains(c) && c.department == 'Biology' && "
 67        + "grade == 10 order by courses asc");
 68    q.declareVariables(Course.class.getName() + " c");
 69    assertEquals(Collections.singletonList(student), q.execute());
 70    commitTxn();
 71  }
 72
 73  public void testJoinOnOneToMany_Offset() {
 74    Course course1 = newCourse("Biology");
 75    makePersistentInTxn(course1, TXN_START_END);
 76    Course course2 = newCourse("Not Biology");
 77    makePersistentInTxn(course2, TXN_START_END);
 78    Student student = newStudent(10, course1, course2);
 79    makePersistentInTxn(student, TXN_START_END);
 80    Student student2 = newStudent(11, course1, course2);
 81    makePersistentInTxn(student2, TXN_START_END);
 82    Student student3 = newStudent(10, course1, course2);
 83    makePersistentInTxn(student3, TXN_START_END);
 84    beginTxn();
 85    Query q = pm.newQuery(
 86        "select from " + Student.class.getName() + " where "
 87        + "courses.contains(c) && c.department == 'Biology' && "
 88        + "grade == 10");
 89    q.declareVariables(Course.class.getName() + " c");
 90    q.setRange(1, Long.MAX_VALUE);
 91    assertEquals(Collections.singletonList(student3), q.execute());
 92    q.setRange(2, Long.MAX_VALUE);
 93    assertEquals(Collections.emptyList(), q.execute());
 94    commitTxn();
 95  }
 96
 97  public void testJoinOnOneToMany_Limit() {
 98    Course course1 = newCourse("Biology");
 99    makePersistentInTxn(course1, TXN_START_END);
100    Course course2 = newCourse("Not Biology");
101    makePersistentInTxn(course2, TXN_START_END);
102    Student student = newStudent(10, course1, course2);
103    makePersistentInTxn(student, TXN_START_END);
104    Student student2 = newStudent(11, course1, course2);
105    makePersistentInTxn(student2, TXN_START_END);
106    Student student3 = newStudent(10, course1, course2);
107    makePersistentInTxn(student3, TXN_START_END);
108    beginTxn();
109    Query q = pm.newQuery(
110        "select from " + Student.class.getName() + " where "
111        + "courses.contains(c) && c.department == 'Biology' && "
112        + "grade == 10");
113    q.declareVariables(Course.class.getName() + " c");
114    q.setRange(0, 1);
115    assertEquals(Collections.singletonList(student), q.execute());
116    q.setRange(0, 0);
117    assertEquals(Collections.emptyList(), q.execute());
118    commitTxn();
119  }
120
121  public void testJoinOnOneToOne_Simple() {
122    Major major1 = newMajor("Liberal Arts");
123    makePersistentInTxn(major1, TXN_START_END);
124    Major major2 = newMajor("Engineering");
125    makePersistentInTxn(major2, TXN_START_END);
126    Student student1 = newStudent(10, major1);
127    Student student2 = newStudent(10, major2);
128    makePersistentInTxn(student1, TXN_START_END);
129    makePersistentInTxn(student2, TXN_START_END);
130    beginTxn();
131    Query q = pm.newQuery(
132        "select from " + Student.class.getName() + " where "
133        + "major == m && m.school == 'Liberal Arts' && "
134        + "grade == 10");
135    q.declareVariables(Major.class.getName() + " m");
136    assertEquals(Collections.singletonList(student1), q.execute());
137    commitTxn();
138  }
139
140  public void testJoinOnOneToMany_Illegal() {
141    beginTxn();
142    // join condition can't be >
143    Query q = pm.newQuery(
144        "select from " + Student.class.getName() + " where "
145        + "courses > c && c.department == 'Biology' && "
146        + "grade == 10");
147    q.declareVariables(Course.class.getName() + " c");
148    q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
149    try {
150      q.execute();
151      fail("expected exception");
152    } catch (JDOUserException jdoe) {
153        if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
154          // good
155        }
156        else {
157          throw jdoe;
158        }
159    }
160
161    // all filters on parent must be equality filters
162    q = pm.newQuery(
163        "select from " + Student.class.getName() + " where "
164        + "courses.contains(c) && c.department == 'Biology' && "
165        + "grade > 10");
166    q.declareVariables(Course.class.getName() + " c");
167    q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
168    try {
169      q.execute();
170      fail("expected exception");
171    } catch (JDOUserException jdoe) {
172        if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
173          // good
174        }
175        else {
176          throw jdoe;
177        }
178    }
179
180    // all filters on child must be equality filters
181    q = pm.newQuery(
182        "select from " + Student.class.getName() + " where "
183        + "courses.contains(c) && c.department > 'Biology' && "
184        + "grade == 10");
185    q.declareVariables(Course.class.getName() + " c");
186    q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
187    try {
188      q.execute();
189      fail("expected exception");
190    } catch (JDOUserException jdoe) {
191        if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
192          // good
193        }
194        else {
195          throw jdoe;
196        }
197    }
198
199    // sort on parent can only be by join column in asc order
200    q = pm.newQuery(
201        "select from " + Student.class.getName() + " where "
202        + "courses.contains(c) && c.department == 'Biology' && "
203        + "grade == 10 order by grade");
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
218    // sort is by the join column but in the wrong order
219    q = pm.newQuery(
220        "select from " + Student.class.getName() + " where "
221        + "courses.contains(c) && c.department == 'Biology' && "
222        + "grade == 10 order by courses desc");
223    q.declareVariables(Course.class.getName() + " c");
224    q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
225    try {
226      q.execute();
227      fail("expected exception");
228    } catch (JDOUserException jdoe) {
229        if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
230          // good
231        }
232        else {
233          throw jdoe;
234        }
235    }
236
237    // can't sort by child property
238    q = pm.newQuery(
239        "select from " + Student.class.getName() + " where "
240        + "courses.contains(c) && c.department == 'Biology' && "
241        + "grade == 10 order by c.department");
242    q.declareVariables(Course.class.getName() + " c");
243    q.addExtension(DatastoreManager.QUERYEXT_INMEMORY_WHEN_UNSUPPORTED, "false");
244    try {
245      q.execute();
246      fail("expected exception");
247    } catch (JDOUserException jdoe) {
248        if (jdoe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
249          // good
250        }
251        else {
252          throw jdoe;
253        }
254    }
255    commitTxn();
256  }
257}