|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore.pipeline |
| 16 | + |
| 17 | +import com.google.common.truth.Truth.assertThat |
| 18 | +import com.google.firebase.firestore.RealtimePipelineSource |
| 19 | +import com.google.firebase.firestore.TestUtil |
| 20 | +import com.google.firebase.firestore.model.MutableDocument |
| 21 | +import com.google.firebase.firestore.runPipeline |
| 22 | +import com.google.firebase.firestore.testutil.TestUtilKtx.doc |
| 23 | +import kotlinx.coroutines.flow.flowOf |
| 24 | +import kotlinx.coroutines.flow.toList |
| 25 | +import kotlinx.coroutines.runBlocking |
| 26 | +import org.junit.Test |
| 27 | +import org.junit.runner.RunWith |
| 28 | +import org.robolectric.RobolectricTestRunner |
| 29 | + |
| 30 | +@RunWith(RobolectricTestRunner::class) |
| 31 | +internal class LimitTests { |
| 32 | + |
| 33 | + private val db = TestUtil.firestore() |
| 34 | + |
| 35 | + private fun createDocs(): List<MutableDocument> { |
| 36 | + val doc1 = doc("k/a", 1000, mapOf("a" to 1L, "b" to 2L)) |
| 37 | + val doc2 = doc("k/b", 1000, mapOf("a" to 3L, "b" to 4L)) |
| 38 | + val doc3 = doc("k/c", 1000, mapOf("a" to 5L, "b" to 6L)) |
| 39 | + val doc4 = doc("k/d", 1000, mapOf("a" to 7L, "b" to 8L)) |
| 40 | + return listOf(doc1, doc2, doc3, doc4) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `limit zero`(): Unit = runBlocking { |
| 45 | + val documents = createDocs() |
| 46 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(0) |
| 47 | + |
| 48 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 49 | + assertThat(result).isEmpty() |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `limit zero duplicated`(): Unit = runBlocking { |
| 54 | + val documents = createDocs() |
| 55 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(0).limit(0).limit(0) |
| 56 | + |
| 57 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 58 | + assertThat(result).isEmpty() |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `limit one`(): Unit = runBlocking { |
| 63 | + val documents = createDocs() |
| 64 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(1) |
| 65 | + |
| 66 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 67 | + assertThat(result).hasSize(1) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `limit one duplicated`(): Unit = runBlocking { |
| 72 | + val documents = createDocs() |
| 73 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(1).limit(1).limit(1) |
| 74 | + |
| 75 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 76 | + assertThat(result).hasSize(1) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `limit two`(): Unit = runBlocking { |
| 81 | + val documents = createDocs() |
| 82 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(2) |
| 83 | + |
| 84 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 85 | + assertThat(result).hasSize(2) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `limit two duplicated`(): Unit = runBlocking { |
| 90 | + val documents = createDocs() |
| 91 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(2).limit(2).limit(2) |
| 92 | + |
| 93 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 94 | + assertThat(result).hasSize(2) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `limit three`(): Unit = runBlocking { |
| 99 | + val documents = createDocs() |
| 100 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(3) |
| 101 | + |
| 102 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 103 | + assertThat(result).hasSize(3) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun `limit three duplicated`(): Unit = runBlocking { |
| 108 | + val documents = createDocs() |
| 109 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(3).limit(3).limit(3) |
| 110 | + |
| 111 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 112 | + assertThat(result).hasSize(3) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `limit four`(): Unit = runBlocking { |
| 117 | + val documents = createDocs() |
| 118 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(4) |
| 119 | + |
| 120 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 121 | + assertThat(result).hasSize(4) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `limit four duplicated`(): Unit = runBlocking { |
| 126 | + val documents = createDocs() |
| 127 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(4).limit(4).limit(4) |
| 128 | + |
| 129 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 130 | + assertThat(result).hasSize(4) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun `limit five`(): Unit = runBlocking { |
| 135 | + val documents = createDocs() // Only 4 docs created |
| 136 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(5) |
| 137 | + |
| 138 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 139 | + assertThat(result).hasSize(4) // Limited by actual doc count |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun `limit five duplicated`(): Unit = runBlocking { |
| 144 | + val documents = createDocs() // Only 4 docs created |
| 145 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(5).limit(5).limit(5) |
| 146 | + |
| 147 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 148 | + assertThat(result).hasSize(4) // Limited by actual doc count |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `limit max`(): Unit = runBlocking { |
| 153 | + val documents = createDocs() |
| 154 | + val pipeline = RealtimePipelineSource(db).collection("k").limit(Int.MAX_VALUE) |
| 155 | + |
| 156 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 157 | + assertThat(result).hasSize(4) |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + fun `limit max duplicated`(): Unit = runBlocking { |
| 162 | + val documents = createDocs() |
| 163 | + val pipeline = |
| 164 | + RealtimePipelineSource(db).collection("k").limit(Int.MAX_VALUE).limit(Int.MAX_VALUE).limit(Int.MAX_VALUE) |
| 165 | + |
| 166 | + val result = runPipeline(db, pipeline, flowOf(*documents.toTypedArray())).toList() |
| 167 | + assertThat(result).hasSize(4) |
| 168 | + } |
| 169 | +} |
0 commit comments