|
51 | 51 | @RunWith(RobolectricTestRunner.class)
|
52 | 52 | @Config(manifest = Config.NONE)
|
53 | 53 | public class BundleSerializerTest {
|
54 |
| - // Note: This tests uses single-quoted JSON strings, which are accepted by org.json.JSONObject. |
55 |
| - // While they are invalid JSON, they allow us to no use non-escaped quotes in the test file. |
| 54 | + // Note: This tests uses single-quoted JSON strings, which are accepted by |
| 55 | + // org.json.JSONObject. |
| 56 | + // While they are invalid JSON, they allow us to no use non-escaped quotes in |
| 57 | + // the test file. |
56 | 58 |
|
57 | 59 | private static String TEST_PROJECT = "projects/project/databases/(default)/documents";
|
58 | 60 | private static String TEST_DOCUMENT = TEST_PROJECT + "/coll/doc";
|
@@ -568,9 +570,34 @@ public void testDecodesLimitQuery() throws JSONException {
|
568 | 570 |
|
569 | 571 | @Test
|
570 | 572 | public void testDecodesLimitToLastQuery() throws JSONException {
|
571 |
| - String json = "{ from: [ { collectionId: 'coll' } ], limit: {value: 5 } }"; |
572 |
| - Query query = TestUtil.query("coll").limitToLast(5); |
573 |
| - assertDecodesNamedQuery(json, query); |
| 573 | + String queryJson = |
| 574 | + "{\n" |
| 575 | + + " name: 'query-1',\n" |
| 576 | + + " bundledQuery: {\n" |
| 577 | + + " parent: '" |
| 578 | + + TEST_PROJECT |
| 579 | + + "',\n" |
| 580 | + + " structuredQuery:\n" |
| 581 | + + "{ from: [ { collectionId: 'coll' } ], limit: {value: 5 } }" |
| 582 | + + ",\n" |
| 583 | + + " limitType: 'LAST'\n" |
| 584 | + + " },\n" |
| 585 | + + " readTime: '2020-01-01T00:00:01.000000001Z'\n" |
| 586 | + + "}"; |
| 587 | + NamedQuery actualNamedQuery = serializer.decodeNamedQuery(new JSONObject(queryJson)); |
| 588 | + |
| 589 | + // Note we use limitToFirst instead of limitToLast to avoid order reverse. |
| 590 | + // Because this is what is saved in bundle files. |
| 591 | + Query query = TestUtil.query("coll").limitToFirst(5); |
| 592 | + Target target = query.toTarget(); |
| 593 | + BundledQuery bundledQuery = new BundledQuery(target, Query.LimitType.LIMIT_TO_LAST); |
| 594 | + NamedQuery expectedNamedQuery = |
| 595 | + new NamedQuery( |
| 596 | + "query-1", |
| 597 | + bundledQuery, |
| 598 | + new SnapshotVersion(new com.google.firebase.Timestamp(1577836801, 1))); |
| 599 | + |
| 600 | + assertEquals(expectedNamedQuery, actualNamedQuery); |
574 | 601 | }
|
575 | 602 |
|
576 | 603 | @Test
|
@@ -672,6 +699,33 @@ public void testDecodesBundledDocumentMetadata() throws JSONException {
|
672 | 699 | assertEquals(expectedMetadata, actualMetadata);
|
673 | 700 | }
|
674 | 701 |
|
| 702 | + @Test |
| 703 | + public void testDecodesTargetWithoutImplicitOrderByOnName() throws JSONException { |
| 704 | + String json = |
| 705 | + "{\"name\":\"myNamedQuery\",\"bundledQuery\":{\"parent\":\"projects/project/databases" |
| 706 | + + "/(default)/documents\",\"structuredQuery\":{\"from\":[{\"collectionId\":\"foo\"}]," |
| 707 | + + "\"limit\":{\"value\":10}},\"limitType\":\"FIRST\"}," |
| 708 | + + "\"readTime\":{\"seconds\":\"1679674432\",\"nanos\":579934000}}"; |
| 709 | + NamedQuery query = serializer.decodeNamedQuery(new JSONObject(json)); |
| 710 | + assertEquals( |
| 711 | + TestUtil.query("foo").limitToFirst(10).toTarget(), query.getBundledQuery().getTarget()); |
| 712 | + assertEquals(Query.LimitType.LIMIT_TO_FIRST, query.getBundledQuery().getLimitType()); |
| 713 | + } |
| 714 | + |
| 715 | + @Test |
| 716 | + public void testDecodesLimitToLastTargetWithoutImplicitOrderByOnName() throws JSONException { |
| 717 | + String json = |
| 718 | + "{\"name\":\"myNamedQuery\",\"bundledQuery\":{\"parent\":\"projects/project/databases" |
| 719 | + + "/(default)/documents\",\"structuredQuery\":{\"from\":[{\"collectionId\":\"foo\"}]," |
| 720 | + + "\"limit\":{\"value\":10}},\"limitType\":\"LAST\"}," |
| 721 | + + "\"readTime\":{\"seconds\":\"1679674432\",\"nanos\":579934000}}"; |
| 722 | + NamedQuery query = serializer.decodeNamedQuery(new JSONObject(json)); |
| 723 | + assertEquals( |
| 724 | + // Note that `limitToFirst(10)` is expected. |
| 725 | + TestUtil.query("foo").limitToFirst(10).toTarget(), query.getBundledQuery().getTarget()); |
| 726 | + assertEquals(Query.LimitType.LIMIT_TO_LAST, query.getBundledQuery().getLimitType()); |
| 727 | + } |
| 728 | + |
675 | 729 | private void assertDecodesValue(String json, Value proto) throws JSONException {
|
676 | 730 | String documentJson =
|
677 | 731 | "{\n"
|
@@ -719,15 +773,7 @@ private void assertDecodesNamedQuery(String json, Query query) throws JSONExcept
|
719 | 773 | + "}";
|
720 | 774 | NamedQuery actualNamedQuery = serializer.decodeNamedQuery(new JSONObject(queryJson));
|
721 | 775 |
|
722 |
| - Target target = |
723 |
| - new Target( |
724 |
| - query.getPath(), |
725 |
| - query.getCollectionGroup(), |
726 |
| - query.getFilters(), |
727 |
| - query.getExplicitOrderBy(), |
728 |
| - query.getLimit(), |
729 |
| - query.getStartAt(), |
730 |
| - query.getEndAt()); |
| 776 | + Target target = query.toTarget(); |
731 | 777 | BundledQuery bundledQuery =
|
732 | 778 | new BundledQuery(
|
733 | 779 | target,
|
|
0 commit comments