|
59 | 59 |
|
60 | 60 | import java.util.ArrayList; |
61 | 61 | import java.util.Arrays; |
| 62 | +import java.util.Collections; |
62 | 63 | import java.util.HashMap; |
63 | 64 | import java.util.Map; |
64 | 65 |
|
@@ -181,6 +182,21 @@ public void validatePredicateGroupForSyncExpressionIsNotWrappedWithAnd() throws |
181 | 182 | true); |
182 | 183 | } |
183 | 184 |
|
| 185 | + /** |
| 186 | + * If a MatchNoneQueryPredicate is provided, it should be wrapped in an AND group. |
| 187 | + * This enables AppSync to optimize by performing an DDB query instead of scan. |
| 188 | + * @throws AmplifyException On failure to parse ModelSchema from model class |
| 189 | + * @throws JSONException from JSONAssert.assertEquals. |
| 190 | + */ |
| 191 | + @Test |
| 192 | + public void validateMatchNonePredicateForSyncExpressionIsWrappedWithAnd() throws AmplifyException, JSONException { |
| 193 | + ModelSchema schema = ModelSchema.fromModelClass(BlogOwner.class); |
| 194 | + final GraphQLRequest<Iterable<Post>> request = |
| 195 | + AppSyncRequestFactory.buildSyncRequest(schema, null, null, QueryPredicates.none()); |
| 196 | + JSONAssert.assertEquals(Resources.readAsString("base-sync-request-with-predicate-match-none.txt"), |
| 197 | + request.getContent(), true); |
| 198 | + } |
| 199 | + |
184 | 200 | /** |
185 | 201 | * Checks that we're getting the expected output for a mutation with predicate. |
186 | 202 | * @throws DataStoreException If the output does not match. |
@@ -279,15 +295,29 @@ public void validateDeleteWithCustomPrimaryKey() throws AmplifyException, JSONEx |
279 | 295 | */ |
280 | 296 | @Test |
281 | 297 | public void validatePredicateGeneration() throws DataStoreException { |
282 | | - Map<String, Object> predicate = |
283 | | - AppSyncRequestFactory.parsePredicate(BlogOwner.NAME.eq("Test Dummy")); |
284 | 298 | assertEquals( |
285 | | - "{name={eq=Test Dummy}}", |
286 | | - predicate.toString() |
| 299 | + Collections.singletonMap("name", Collections.singletonMap("eq", "Test Dummy")), |
| 300 | + AppSyncRequestFactory.parsePredicate(BlogOwner.NAME.eq("Test Dummy")) |
| 301 | + ); |
| 302 | + |
| 303 | + assertEquals( |
| 304 | + Collections.singletonMap("and", Arrays.asList( |
| 305 | + Collections.singletonMap("name", Collections.singletonMap("beginsWith", "A day in the life of a...")), |
| 306 | + Collections.singletonMap("blogOwnerId", Collections.singletonMap("eq", "DUMMY_OWNER_ID")) |
| 307 | + )), |
| 308 | + AppSyncRequestFactory.parsePredicate( |
| 309 | + Blog.NAME.beginsWith("A day in the life of a...").and(Blog.OWNER.eq("DUMMY_OWNER_ID")) |
| 310 | + ) |
287 | 311 | ); |
288 | 312 |
|
289 | | - AppSyncRequestFactory.parsePredicate( |
290 | | - Blog.NAME.beginsWith("A day in the life of a...").and(Blog.OWNER.eq("DUMMY_OWNER_ID")) |
| 313 | + assertEquals( |
| 314 | + Collections.singletonMap("id", Collections.singletonMap("ne", null)), |
| 315 | + AppSyncRequestFactory.parsePredicate(QueryPredicates.all()) |
| 316 | + ); |
| 317 | + |
| 318 | + assertEquals( |
| 319 | + Collections.singletonMap("id", Collections.singletonMap("eq", null)), |
| 320 | + AppSyncRequestFactory.parsePredicate(QueryPredicates.none()) |
291 | 321 | ); |
292 | 322 | } |
293 | 323 |
|
|
0 commit comments