New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: support array of references for IN queries #211
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
import com.google.api.gax.rpc.ServerStreamingCallable; | ||
import com.google.cloud.Timestamp; | ||
import com.google.cloud.firestore.spi.v1.FirestoreRpc; | ||
import com.google.firestore.v1.ArrayValue; | ||
import com.google.firestore.v1.RunQueryRequest; | ||
import com.google.firestore.v1.StructuredQuery; | ||
import com.google.firestore.v1.StructuredQuery.Direction; | ||
|
@@ -273,6 +274,87 @@ public void withFieldPathFilter() throws Exception { | |
} | ||
} | ||
|
||
@Test | ||
public void inQueriesWithReferenceArray() throws Exception { | ||
doAnswer(queryResponse()) | ||
.when(firestoreMock) | ||
.streamRequest( | ||
runQuery.capture(), | ||
streamObserverCapture.capture(), | ||
Matchers.<ServerStreamingCallable>any()); | ||
|
||
query | ||
.whereIn( | ||
FieldPath.documentId(), | ||
Arrays.<Object>asList("doc", firestoreMock.document("coll/doc"))) | ||
.get() | ||
.get(); | ||
|
||
Value value = | ||
Value.newBuilder() | ||
.setArrayValue( | ||
ArrayValue.newBuilder() | ||
.addValues(reference(DOCUMENT_NAME)) | ||
.addValues(reference(DOCUMENT_NAME)) | ||
.build()) | ||
.build(); | ||
RunQueryRequest expectedRequest = query(filter(Operator.IN, "__name__", value)); | ||
|
||
assertEquals(expectedRequest, runQuery.getValue()); | ||
} | ||
|
||
@Test | ||
public void validatesInQueries() throws Exception { | ||
try { | ||
query.whereIn(FieldPath.documentId(), Arrays.<Object>asList("foo", 42)).get(); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals( | ||
"The corresponding value for FieldPath.documentId() must be a String or a " | ||
+ "DocumentReference, but was: 42.", | ||
e.getMessage()); | ||
} | ||
|
||
try { | ||
query.whereIn(FieldPath.documentId(), Arrays.<Object>asList(42)).get(); | ||
fail(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Node tests uses "42", not "Arrays.asList(42)", which tests a different case. I think you are re-testing the case from right above here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed test, since passing in "42" causes compiler error. |
||
} catch (IllegalArgumentException e) { | ||
assertEquals( | ||
"The corresponding value for FieldPath.documentId() must be a String or a " | ||
+ "DocumentReference, but was: 42.", | ||
e.getMessage()); | ||
} | ||
|
||
try { | ||
query.whereIn(FieldPath.documentId(), Arrays.<Object>asList()).get(); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals( | ||
"Invalid Query. A non-empty array is required for 'IN' filters.", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void validatesQueryOperatorForFieldPathDocumentId() throws Exception { | ||
try { | ||
query.whereArrayContains(FieldPath.documentId(), "bar"); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals( | ||
"Invalid query. You cannot perform 'ARRAY_CONTAINS' queries on FieldPath.documentId().", | ||
e.getMessage()); | ||
} | ||
|
||
try { | ||
query.whereArrayContainsAny(FieldPath.documentId(), Collections.<Object>singletonList("bar")); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals( | ||
"Invalid query. You cannot perform 'ARRAY_CONTAINS_ANY' queries on FieldPath.documentId().", | ||
e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void withDocumentIdFilter() throws Exception { | ||
doAnswer(queryResponse()) | ||
|
@@ -523,7 +605,7 @@ public void withInvalidStartAt() throws Exception { | |
} catch (IllegalArgumentException e) { | ||
assertEquals( | ||
"The corresponding value for FieldPath.documentId() must be a String or a " | ||
+ "DocumentReference.", | ||
+ "DocumentReference, but was: 42.", | ||
e.getMessage()); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Prefer
isEmpty()
as it is guaranteed to run inO(1)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.