Skip to content

Commit ef7cbda

Browse files
committed
Add tests
1 parent 5abf13f commit ef7cbda

File tree

4 files changed

+83
-22
lines changed

4 files changed

+83
-22
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/PipelineTest.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.google.firebase.firestore.pipeline.Function.logicalMin;
2929
import static com.google.firebase.firestore.pipeline.Function.lt;
3030
import static com.google.firebase.firestore.pipeline.Function.lte;
31+
import static com.google.firebase.firestore.pipeline.Function.mapGet;
3132
import static com.google.firebase.firestore.pipeline.Function.neq;
3233
import static com.google.firebase.firestore.pipeline.Function.not;
3334
import static com.google.firebase.firestore.pipeline.Function.or;
@@ -105,7 +106,10 @@ public void tearDown() {
105106
entry("published", 1979),
106107
entry("rating", 4.2),
107108
entry("tags", ImmutableList.of("comedy", "space", "adventure")),
108-
entry("awards", ImmutableMap.of("hugo", true, "nebula", false)))),
109+
entry("awards", ImmutableMap.of("hugo", true, "nebula", false)),
110+
entry(
111+
"nestedField",
112+
ImmutableMap.of("level.1", ImmutableMap.of("level.2", true))))),
109113
entry(
110114
"book2",
111115
mapOfEntries(
@@ -714,12 +718,6 @@ public void testMapGet() {
714718
ImmutableMap.of("hugoAward", true, "title", "Dune"));
715719
}
716720

717-
@Test
718-
public void testParent() {}
719-
720-
@Test
721-
public void testCollectionId() {}
722-
723721
@Test
724722
public void testDistanceFunctions() {
725723
double[] sourceVector = {0.1, 0.1};
@@ -745,10 +743,43 @@ public void testDistanceFunctions() {
745743
}
746744

747745
@Test
748-
public void testNestedFields() {}
746+
public void testNestedFields() {
747+
Task<PipelineSnapshot> execute =
748+
randomCol
749+
.pipeline()
750+
.where(eq("awards.hugo", true))
751+
.select("title", "awards.hugo")
752+
.execute();
753+
assertThat(waitFor(execute).getResults())
754+
.comparingElementsUsing(DATA_CORRESPONDENCE)
755+
.containsExactly(
756+
ImmutableMap.of("title", "The Hitchhiker's Guide to the Galaxy", "awards.hugo", true),
757+
ImmutableMap.of("title", "Dune", "awards.hugo", true));
758+
}
749759

750760
@Test
751-
public void testMapGetWithFieldNameIncludingNotation() {}
761+
public void testMapGetWithFieldNameIncludingNotation() {
762+
Task<PipelineSnapshot> execute =
763+
randomCol
764+
.pipeline()
765+
.where(eq("awards.hugo", true))
766+
.select(
767+
"title",
768+
Field.of("nestedField.level.1"),
769+
mapGet("nestedField", "level.1").mapGet("level.2").as("nested"))
770+
.execute();
771+
assertThat(waitFor(execute).getResults())
772+
.comparingElementsUsing(DATA_CORRESPONDENCE)
773+
.containsExactly(
774+
mapOfEntries(
775+
entry("title", "The Hitchhiker's Guide to the Galaxy"),
776+
entry("nestedField.level.`1`", null),
777+
entry("nested", true)),
778+
mapOfEntries(
779+
entry("title", "Dune"),
780+
entry("nestedField.level.`1`", null),
781+
entry("nested", null)));
782+
}
752783

753784
static <T> Map.Entry<String, T> entry(String key, T value) {
754785
return new Map.Entry<String, T>() {

firebase-firestore/src/main/java/com/google/firebase/firestore/Pipeline.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ internal constructor(
107107
return append(SelectStage(fields.map(Field::of).toTypedArray()))
108108
}
109109

110+
fun select(vararg fields: Any): Pipeline {
111+
return append(SelectStage(fields.map(Selectable::toSelectable).toTypedArray()))
112+
}
113+
110114
fun sort(vararg orders: Ordering): Pipeline {
111115
return append(SortStage(orders))
112116
}
@@ -131,6 +135,10 @@ internal constructor(
131135
return append(DistinctStage(groups.map(Field::of).toTypedArray()))
132136
}
133137

138+
fun distinct(vararg groups: Any): Pipeline {
139+
return append(DistinctStage(groups.map(Selectable::toSelectable).toTypedArray()))
140+
}
141+
134142
fun aggregate(vararg accumulators: AccumulatorWithAlias): Pipeline {
135143
return append(AggregateStage.withAccumulators(*accumulators))
136144
}

firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline/expression.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,29 @@ abstract class Expr protected constructor() {
266266
internal abstract fun toProto(): Value
267267
}
268268

269-
abstract class Selectable(internal val alias: String) : Expr()
269+
abstract class Selectable() : Expr() {
270+
internal abstract fun getAlias(): String
270271

271-
open class ExprWithAlias internal constructor(alias: String, private val expr: Expr) :
272-
Selectable(alias) {
272+
internal companion object {
273+
fun toSelectable(o: Any): Selectable {
274+
return when (o) {
275+
is Selectable -> o
276+
is String -> Field.of(o)
277+
is FieldPath -> Field.of(o)
278+
else -> throw IllegalArgumentException("Unknown Selectable type: $o")
279+
}
280+
}
281+
}
282+
}
283+
284+
open class ExprWithAlias internal constructor(private val alias: String, private val expr: Expr) :
285+
Selectable() {
286+
override fun getAlias() = alias
273287
override fun toProto(): Value = expr.toProto()
274288
}
275289

276290
class Field private constructor(private val fieldPath: ModelFieldPath) :
277-
Selectable(fieldPath.canonicalString()) {
291+
Selectable() {
278292
companion object {
279293

280294
@JvmStatic
@@ -293,11 +307,14 @@ class Field private constructor(private val fieldPath: ModelFieldPath) :
293307
return Field(fieldPath.internalPath)
294308
}
295309
}
310+
311+
override fun getAlias(): String = fieldPath.canonicalString()
312+
296313
override fun toProto() =
297314
Value.newBuilder().setFieldReferenceValue(fieldPath.canonicalString()).build()
298315
}
299316

300-
class ListOfExprs(val expressions: Array<out Expr>) : Expr() {
317+
class ListOfExprs(private val expressions: Array<out Expr>) : Expr() {
301318
override fun toProto(): Value {
302319
val builder = ArrayValue.newBuilder()
303320
for (expr in expressions) {
@@ -740,7 +757,6 @@ class BooleanExpr internal constructor(name: String, params: Array<out Expr>) :
740757

741758
fun countIf(): Accumulator = Accumulator.countIf(this)
742759

743-
744760
fun ifThen(then: Expr) = Function.ifThen(this, then)
745761

746762
fun ifThen(then: Any) = Function.ifThen(this, then)

firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline/stage.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ class DocumentsSource internal constructor(private val documents: Array<out Stri
5757
class AddFieldsStage internal constructor(private val fields: Array<out Selectable>) :
5858
Stage("add_fields") {
5959
override fun args(): Sequence<Value> =
60-
sequenceOf(encodeValue(fields.associate { it.alias to it.toProto() }))
60+
sequenceOf(encodeValue(fields.associate { it.getAlias() to it.toProto() }))
6161
}
6262

6363
class AggregateStage
6464
internal constructor(
6565
private val accumulators: Map<String, Accumulator>,
6666
private val groups: Map<String, Expr>
6767
) : Stage("aggregate") {
68-
internal constructor(accumulators: Map<String, Accumulator>) : this(accumulators, emptyMap())
68+
private constructor(accumulators: Map<String, Accumulator>) : this(accumulators, emptyMap())
6969
companion object {
7070
@JvmStatic
7171
fun withAccumulators(vararg accumulators: AccumulatorWithAlias): AggregateStage {
@@ -78,12 +78,18 @@ internal constructor(
7878
}
7979
}
8080

81-
fun withGroups(vararg selectable: Selectable) =
82-
AggregateStage(accumulators, selectable.associateBy(Selectable::alias))
81+
fun withGroups(vararg groups: Selectable) =
82+
AggregateStage(accumulators, groups.associateBy(Selectable::getAlias))
8383

8484
fun withGroups(vararg fields: String) =
8585
AggregateStage(accumulators, fields.associateWith(Field::of))
8686

87+
fun withGroups(vararg selectable: Any) =
88+
AggregateStage(
89+
accumulators,
90+
selectable.map(Selectable::toSelectable).associateBy(Selectable::getAlias)
91+
)
92+
8793
override fun args(): Sequence<Value> =
8894
sequenceOf(
8995
encodeValue(accumulators.mapValues { entry -> entry.value.toProto() }),
@@ -141,7 +147,7 @@ class OffsetStage internal constructor(private val offset: Long) : Stage("offset
141147
class SelectStage internal constructor(private val fields: Array<out Selectable>) :
142148
Stage("select") {
143149
override fun args(): Sequence<Value> =
144-
sequenceOf(encodeValue(fields.associate { it.alias to it.toProto() }))
150+
sequenceOf(encodeValue(fields.associate { it.getAlias() to it.toProto() }))
145151
}
146152

147153
class SortStage internal constructor(private val orders: Array<out Ordering>) : Stage("sort") {
@@ -151,7 +157,7 @@ class SortStage internal constructor(private val orders: Array<out Ordering>) :
151157
class DistinctStage internal constructor(private val groups: Array<out Selectable>) :
152158
Stage("distinct") {
153159
override fun args(): Sequence<Value> =
154-
sequenceOf(encodeValue(groups.associate { it.alias to it.toProto() }))
160+
sequenceOf(encodeValue(groups.associate { it.getAlias() to it.toProto() }))
155161
}
156162

157163
class RemoveFieldsStage internal constructor(private val fields: Array<out Field>) :
@@ -192,5 +198,5 @@ class UnionStage internal constructor(private val other: com.google.firebase.fir
192198

193199
class UnnestStage internal constructor(private val selectable: Selectable) : Stage("unnest") {
194200
override fun args(): Sequence<Value> =
195-
sequenceOf(encodeValue(selectable.alias), selectable.toProto())
201+
sequenceOf(encodeValue(selectable.getAlias()), selectable.toProto())
196202
}

0 commit comments

Comments
 (0)