Created
June 27, 2012 09:03
-
-
Save mgenov/3002628 to your computer and use it in GitHub Desktop.
QueriesLearningTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class CalendarUtil { | |
@SuppressWarnings("unused") | |
CalendarUtil() { | |
} | |
public static Date january(int year, int day) { | |
return newDate(year, 1, day); | |
} | |
public static Date february(int year, int day) { | |
return newDate(year, 2, day); | |
} | |
public static Date newDate(int year, int month, int day) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.set(Calendar.YEAR, year); | |
calendar.set(Calendar.MONTH, month - 1); | |
calendar.set(Calendar.DAY_OF_MONTH, day); | |
calendar.set(Calendar.HOUR, 1); | |
calendar.set(Calendar.MINUTE, 1); | |
calendar.set(Calendar.SECOND, 1); | |
clearNonDateFields(calendar); | |
return calendar.getTime(); | |
} | |
private static Calendar clearNonDateFields(Calendar c) { | |
c.clear(Calendar.HOUR); | |
c.clear(Calendar.MINUTE); | |
c.clear(Calendar.SECOND); | |
c.clear(Calendar.MILLISECOND); | |
return c; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QueriesLearningTest { | |
@Rule | |
public DatastoreRule rule = new DatastoreRule(); | |
@Test | |
public void learnToUseConditionalQueries() { | |
DatastoreService dss = DatastoreServiceFactory.getDatastoreService(); | |
insert(january(2012, 10)); | |
insert(january(2012, 11)); | |
insert(january(2012, 12)); | |
insert(january(2012, 20)); | |
Query query = new Query("Test"); | |
query.addFilter("date", FilterOperator.GREATER_THAN_OR_EQUAL, january(2012,10)); | |
query.addFilter("date", FilterOperator.LESS_THAN_OR_EQUAL, january(2012, 15)); | |
List<Entity> entities = Lists.newArrayList(dss.prepare(query).asIterable()); | |
assertThat(entities.size(),is(equalTo(3))); | |
} | |
private void insert(Date date) { | |
Entity entity = new Entity("Test"); | |
entity.setProperty("date", date); | |
DatastoreService dss = DatastoreServiceFactory.getDatastoreService(); | |
dss.put(entity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment