PageRenderTime 27ms CodeModel.GetById 2ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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