|
27 | 27 | import com.google.cloud.datastore.Cursor; |
28 | 28 | import com.google.cloud.datastore.Datastore; |
29 | 29 | import com.google.cloud.datastore.DatastoreException; |
| 30 | +import com.google.cloud.datastore.DatastoreOptions; |
30 | 31 | import com.google.cloud.datastore.Entity; |
31 | 32 | import com.google.cloud.datastore.EntityQuery; |
32 | 33 | import com.google.cloud.datastore.FullEntity; |
@@ -83,6 +84,7 @@ public class ConceptsTest { |
83 | 84 | private static final FullEntity<IncompleteKey> TEST_FULL_ENTITY = FullEntity.newBuilder().build(); |
84 | 85 |
|
85 | 86 | private Datastore datastore; |
| 87 | + private Datastore datastoreRealBackend; |
86 | 88 | private KeyFactory keyFactory; |
87 | 89 | private Key taskKey; |
88 | 90 | private Entity testEntity; |
@@ -123,6 +125,8 @@ public void setUp() { |
123 | 125 | endDate = Timestamp.of(calendar.getTime()); |
124 | 126 | calendar.set(1999, DECEMBER, 31); |
125 | 127 | includedDate = Timestamp.of(calendar.getTime()); |
| 128 | + // Create a client for tests that require a real backend |
| 129 | + datastoreRealBackend = DatastoreOptions.getDefaultInstance().getService(); |
126 | 130 | } |
127 | 131 |
|
128 | 132 | /** |
@@ -384,7 +388,7 @@ private void setUpQueryTests() { |
384 | 388 | .set( |
385 | 389 | "description", |
386 | 390 | StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build()) |
387 | | - .set("tag", "fun", "l", "programming") |
| 391 | + .set("tag", "fun", "l", "programming", "learn") |
388 | 392 | .build()); |
389 | 393 | } |
390 | 394 |
|
@@ -1044,4 +1048,101 @@ public void testPropertyFilteringRunQuery() { |
1044 | 1048 | ImmutableMap.of("Task", ImmutableSet.of("priority", "tag")); |
1045 | 1049 | assertEquals(expected, propertiesByKind); |
1046 | 1050 | } |
| 1051 | + |
| 1052 | + @Test |
| 1053 | + public void testEqQuerySorted() { |
| 1054 | + setUpQueryTests(); |
| 1055 | + // [START datastore_eq_query_sorted] |
| 1056 | + Query<Entity> query = |
| 1057 | + Query.newEntityQueryBuilder() |
| 1058 | + .setKind("Task") |
| 1059 | + .setFilter(PropertyFilter.eq("tag", "learn")) |
| 1060 | + .setOrderBy(OrderBy.asc("tag")) |
| 1061 | + .build(); |
| 1062 | + // [END datastore_eq_query_sorted] |
| 1063 | + assertValidQuery(query); |
| 1064 | + } |
| 1065 | + |
| 1066 | + /** Start tests using a real backend. */ |
| 1067 | + private <V> V assertValidQueryRealBackend(Query<V> query) { |
| 1068 | + QueryResults<V> results = datastoreRealBackend.run(query); |
| 1069 | + V result = results.next(); |
| 1070 | + // assertFalse(results.hasNext()); |
| 1071 | + return result; |
| 1072 | + } |
| 1073 | + |
| 1074 | + private void setUpQueryTestsRealBackend() { |
| 1075 | + Key taskKey = |
| 1076 | + datastoreRealBackend |
| 1077 | + .newKeyFactory() |
| 1078 | + .setKind("Task") |
| 1079 | + .addAncestors(PathElement.of("TaskList", "default")) |
| 1080 | + .newKey("someTask"); |
| 1081 | + datastoreRealBackend.put( |
| 1082 | + Entity.newBuilder(taskKey) |
| 1083 | + .set("category", "Personal") |
| 1084 | + .set("done", false) |
| 1085 | + .set("completed", false) |
| 1086 | + .set("priority", 4) |
| 1087 | + .set("created", includedDate) |
| 1088 | + .set("percent_complete", 10.0) |
| 1089 | + .set( |
| 1090 | + "description", |
| 1091 | + StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build()) |
| 1092 | + .set("tag", "fun", "l", "programming", "learn") |
| 1093 | + .build()); |
| 1094 | + } |
| 1095 | + |
| 1096 | + @Test |
| 1097 | + public void testInQuery() { |
| 1098 | + setUpQueryTestsRealBackend(); |
| 1099 | + // [START datastore_in_query] |
| 1100 | + Query<Entity> query = |
| 1101 | + Query.newEntityQueryBuilder() |
| 1102 | + .setKind("Task") |
| 1103 | + .setFilter(PropertyFilter.in("tag", ListValue.of("learn", "study"))) |
| 1104 | + .build(); |
| 1105 | + // [END datastore_in_query] |
| 1106 | + assertValidQueryRealBackend(query); |
| 1107 | + } |
| 1108 | + |
| 1109 | + @Test |
| 1110 | + public void testNotEqualsQuery() { |
| 1111 | + setUpQueryTestsRealBackend(); |
| 1112 | + // [START datastore_not_equals_query] |
| 1113 | + Query<Entity> query = |
| 1114 | + Query.newEntityQueryBuilder() |
| 1115 | + .setKind("Task") |
| 1116 | + .setFilter(PropertyFilter.neq("category", "Work")) |
| 1117 | + .build(); |
| 1118 | + // [END datastore_not_equals_query] |
| 1119 | + assertValidQueryRealBackend(query); |
| 1120 | + } |
| 1121 | + |
| 1122 | + @Test |
| 1123 | + public void testNotInQuery() { |
| 1124 | + setUpQueryTestsRealBackend(); |
| 1125 | + // [START datastore_not_in_query] |
| 1126 | + Query<Entity> query = |
| 1127 | + Query.newEntityQueryBuilder() |
| 1128 | + .setKind("Task") |
| 1129 | + .setFilter(PropertyFilter.not_in("category", ListValue.of("Work", "Chores", "School"))) |
| 1130 | + .build(); |
| 1131 | + // [END datastore_not_in_query] |
| 1132 | + assertValidQueryRealBackend(query); |
| 1133 | + } |
| 1134 | + |
| 1135 | + @Test |
| 1136 | + public void testInQuerySorted() { |
| 1137 | + setUpQueryTestsRealBackend(); |
| 1138 | + // [START datastore_in_query_sorted] |
| 1139 | + Query<Entity> query = |
| 1140 | + Query.newEntityQueryBuilder() |
| 1141 | + .setKind("Task") |
| 1142 | + .setFilter(PropertyFilter.in("tag", ListValue.of("learn", "study"))) |
| 1143 | + .setOrderBy(OrderBy.asc("tag")) |
| 1144 | + .build(); |
| 1145 | + // [END datastore_in_query_sorted] |
| 1146 | + assertValidQueryRealBackend(query); |
| 1147 | + } |
1047 | 1148 | } |
0 commit comments