Task TestLimitToLast()

in firestore/testapp/Assets/Firebase/Sample/Firestore/UIHandlerAutomated.cs [2113:2205]


    Task TestLimitToLast() {
      return Async(() => {
        var c = TestCollection();
        // TODO(b/149105903): Uncomment this line when exception can be raised from SWIG and below.
        // AssertException(typeof(Exception), () => Await(c.LimitToLast(2).GetSnapshotAsync()));

        // Initialize data with a few test documents to query against.
        Await(c.Document("a").SetAsync(new Dictionary<string, object> {
            {"k", "a"},
            {"sort", 0},
        }));
        Await(c.Document("b").SetAsync(new Dictionary<string, object> {
            {"k", "b"},
            {"sort", 1},
        }));
        Await(c.Document("c").SetAsync(new Dictionary<string, object> {
            {"k", "c"},
            {"sort", 1},
        }));
        Await(c.Document("d").SetAsync(new Dictionary<string, object> {
            {"k", "d"},
            {"sort", 2},
        }));

        // Setup `limit` query.
        var limit = c.Limit(2).OrderBy("sort");
        var limitAccumulator = new EventAccumulator<QuerySnapshot>(MainThreadId, FailTest);
        var limitReg = limit.Listen(limitAccumulator.Listener);

        // Setup mirroring `limitToLast` query.
        var limitToLast = c.LimitToLast(2).OrderByDescending("sort");
        var limitToLastAccumulator = new EventAccumulator<QuerySnapshot>(MainThreadId, FailTest);
        var limitToLastReg = limitToLast.Listen(limitToLastAccumulator.Listener);

        // Verify both query get expected result.
        var data = QuerySnapshotToValues(limitAccumulator.Await());
        AssertDeepEq(data, new List<Dictionary<string, object>>
                           { new Dictionary<string, object>{ {"k", "a"}, { "sort", 0L}},
                             new Dictionary<string, object>{ {"k", "b"}, { "sort", 1L}}
                           });
        data = QuerySnapshotToValues(limitToLastAccumulator.Await());
        AssertDeepEq(data, new List<Dictionary<string, object>>
                           { new Dictionary<string, object>{ {"k", "b"}, { "sort", 1L}},
                             new Dictionary<string, object>{ {"k", "a"}, { "sort", 0L}}
                           });

        // Unlisten then re-listen limit query.
        limitReg.Stop();
        limit.Listen(limitAccumulator.Listener);

        // Verify `limit` query still works.
        data = QuerySnapshotToValues(limitAccumulator.Await());
        AssertDeepEq(data, new List<Dictionary<string, object>>
                           { new Dictionary<string, object>{ {"k", "a"}, { "sort", 0L}},
                             new Dictionary<string, object>{ {"k", "b"}, { "sort", 1L}}
                           });

        // Add a document that would change the result set.
        Await(c.Document("d").SetAsync(new Dictionary<string, object> {
            {"k", "e"},
            {"sort", -1},
        }));

        // Verify both query get expected result.
        data = QuerySnapshotToValues(limitAccumulator.Await());
        AssertDeepEq(data, new List<Dictionary<string, object>>
                           { new Dictionary<string, object>{ {"k", "e"}, { "sort", -1L}},
                             new Dictionary<string, object>{ {"k", "a"}, { "sort", 0L}}
                           });
        data = QuerySnapshotToValues(limitToLastAccumulator.Await());
        AssertDeepEq(data, new List<Dictionary<string, object>>
                           { new Dictionary<string, object>{ {"k", "a"}, { "sort", 0L}},
                             new Dictionary<string, object>{ {"k", "e"}, { "sort", -1L}}
                           });

        // Unlisten to limitToLast, update a doc, then relisten to limitToLast
        limitToLastReg.Stop();
        Await(c.Document("a").UpdateAsync("sort", -2));
        limitToLast.Listen(limitToLastAccumulator.Listener);

        // Verify both query get expected result.
        data = QuerySnapshotToValues(limitAccumulator.Await());
        AssertDeepEq(data, new List<Dictionary<string, object>>
                           { new Dictionary<string, object>{ {"k", "a"}, { "sort", -2L}},
                             new Dictionary<string, object>{ {"k", "e"}, { "sort", -1L}}
                           });
        data = QuerySnapshotToValues(limitToLastAccumulator.Await());
        AssertDeepEq(data, new List<Dictionary<string, object>>
                           { new Dictionary<string, object>{ {"k", "e"}, { "sort", -1L}},
                             new Dictionary<string, object>{ {"k", "a"}, { "sort", -2L}}
                           });
      });
    }