|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { CompositeIndexTestHelper } from '../util/composite_index_test_helper'; |
| 19 | +import { |
| 20 | + where, |
| 21 | + orderBy, |
| 22 | + limit, |
| 23 | + limitToLast, |
| 24 | + or |
| 25 | +} from '../util/firebase_export'; |
| 26 | +import { apiDescribe } from '../util/helpers'; |
| 27 | + |
| 28 | +/* |
| 29 | + * Guidance for Creating Tests: |
| 30 | + * ---------------------------- |
| 31 | + * When creating tests that require composite indexes, it is recommended to utilize the |
| 32 | + * "CompositeIndexTestHelper" class. This utility class provides methods for creating |
| 33 | + * and setting test documents and running queries with ease, ensuring proper data |
| 34 | + * isolation and query construction. |
| 35 | + * |
| 36 | + * Please remember to update the main index configuration file (firestore_index_config.tf) |
| 37 | + * with any new composite indexes needed for the tests. This ensures synchronization with |
| 38 | + * other testing environments, including CI. You can generate the required index link by |
| 39 | + * clicking on the Firebase console link in the error message while running tests locally. |
| 40 | + */ |
| 41 | + |
| 42 | +apiDescribe('Composite Index Queries', persistence => { |
| 43 | + // OR Query tests only run when the SDK's local cache is configured to use |
| 44 | + // LRU garbage collection (rather than eager garbage collection) because |
| 45 | + // they validate that the result from server and cache match. |
| 46 | + // eslint-disable-next-line no-restricted-properties |
| 47 | + (persistence.gc === 'lru' ? describe : describe.skip)('OR Queries', () => { |
| 48 | + it('can use query overloads', () => { |
| 49 | + const testDocs = { |
| 50 | + doc1: { a: 1, b: 0 }, |
| 51 | + doc2: { a: 2, b: 1 }, |
| 52 | + doc3: { a: 3, b: 2 }, |
| 53 | + doc4: { a: 1, b: 3 }, |
| 54 | + doc5: { a: 1, b: 1 } |
| 55 | + }; |
| 56 | + const testHelper = new CompositeIndexTestHelper(); |
| 57 | + return testHelper.withTestDocs(persistence, testDocs, async coll => { |
| 58 | + // a == 1, limit 2, b - desc |
| 59 | + await testHelper.assertOnlineAndOfflineResultsMatch( |
| 60 | + testHelper.query( |
| 61 | + coll, |
| 62 | + where('a', '==', 1), |
| 63 | + limit(2), |
| 64 | + orderBy('b', 'desc') |
| 65 | + ), |
| 66 | + 'doc4', |
| 67 | + 'doc5' |
| 68 | + ); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('can use or queries', () => { |
| 73 | + const testDocs = { |
| 74 | + doc1: { a: 1, b: 0 }, |
| 75 | + doc2: { a: 2, b: 1 }, |
| 76 | + doc3: { a: 3, b: 2 }, |
| 77 | + doc4: { a: 1, b: 3 }, |
| 78 | + doc5: { a: 1, b: 1 } |
| 79 | + }; |
| 80 | + const testHelper = new CompositeIndexTestHelper(); |
| 81 | + return testHelper.withTestDocs(persistence, testDocs, async coll => { |
| 82 | + // with one inequality: a>2 || b==1. |
| 83 | + await testHelper.assertOnlineAndOfflineResultsMatch( |
| 84 | + testHelper.compositeQuery( |
| 85 | + coll, |
| 86 | + or(where('a', '>', 2), where('b', '==', 1)) |
| 87 | + ), |
| 88 | + 'doc5', |
| 89 | + 'doc2', |
| 90 | + 'doc3' |
| 91 | + ); |
| 92 | + |
| 93 | + // Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2 |
| 94 | + await testHelper.assertOnlineAndOfflineResultsMatch( |
| 95 | + testHelper.compositeQuery( |
| 96 | + coll, |
| 97 | + or(where('a', '==', 1), where('b', '>', 0)), |
| 98 | + limit(2) |
| 99 | + ), |
| 100 | + 'doc1', |
| 101 | + 'doc2' |
| 102 | + ); |
| 103 | + |
| 104 | + // Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2 |
| 105 | + // Note: The public query API does not allow implicit ordering when limitToLast is used. |
| 106 | + await testHelper.assertOnlineAndOfflineResultsMatch( |
| 107 | + testHelper.compositeQuery( |
| 108 | + coll, |
| 109 | + or(where('a', '==', 1), where('b', '>', 0)), |
| 110 | + limitToLast(2), |
| 111 | + orderBy('b') |
| 112 | + ), |
| 113 | + 'doc3', |
| 114 | + 'doc4' |
| 115 | + ); |
| 116 | + |
| 117 | + // Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1 |
| 118 | + await testHelper.assertOnlineAndOfflineResultsMatch( |
| 119 | + testHelper.compositeQuery( |
| 120 | + coll, |
| 121 | + or(where('a', '==', 2), where('b', '==', 1)), |
| 122 | + limit(1), |
| 123 | + orderBy('a') |
| 124 | + ), |
| 125 | + 'doc5' |
| 126 | + ); |
| 127 | + |
| 128 | + // Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1 |
| 129 | + await testHelper.assertOnlineAndOfflineResultsMatch( |
| 130 | + testHelper.compositeQuery( |
| 131 | + coll, |
| 132 | + or(where('a', '==', 2), where('b', '==', 1)), |
| 133 | + limitToLast(1), |
| 134 | + orderBy('a') |
| 135 | + ), |
| 136 | + 'doc2' |
| 137 | + ); |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments