Skip to content

Commit 5ef93de

Browse files
authored
fix(firestore): Compare full resource path when docs ordered by __name__ (#8409)
* fix(firestore): Compare full resource path when docs ordered by __name__
1 parent 9cfcaf6 commit 5ef93de

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

firestore/integration_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,55 @@ func TestIntegration_FieldTransforms_Set(t *testing.T) {
17431743

17441744
type imap map[string]interface{}
17451745

1746+
func TestIntegration_Serialize_Deserialize_WatchQuery(t *testing.T) {
1747+
h := testHelper{t}
1748+
collID := collectionIDs.New()
1749+
ctx := context.Background()
1750+
client := integrationClient(t)
1751+
1752+
partitionedQueries, err := client.CollectionGroup(collID).GetPartitionedQueries(ctx, 10)
1753+
h.failIfNotNil(err)
1754+
1755+
qProtoBytes, err := partitionedQueries[0].Serialize()
1756+
h.failIfNotNil(err)
1757+
1758+
q, err := client.CollectionGroup(collID).Deserialize(qProtoBytes)
1759+
h.failIfNotNil(err)
1760+
1761+
qSnapIt := q.Snapshots(ctx)
1762+
defer qSnapIt.Stop()
1763+
1764+
// Check if at least one snapshot exists
1765+
_, err = qSnapIt.Next()
1766+
if err == iterator.Done {
1767+
t.Fatalf("Expected snapshot, found none")
1768+
}
1769+
1770+
// Add new document to query results
1771+
createdDocRefs := h.mustCreateMulti(collID, []testDocument{
1772+
{data: map[string]interface{}{"some-key": "should-be-found"}},
1773+
})
1774+
wds := h.mustGet(createdDocRefs[0])
1775+
1776+
// Check if new snapshot is available
1777+
qSnap, err := qSnapIt.Next()
1778+
if err == iterator.Done {
1779+
t.Fatalf("Expected snapshot, found none")
1780+
}
1781+
1782+
// Check the changes in snapshot
1783+
if len(qSnap.Changes) != 1 {
1784+
t.Fatalf("Expected one change, found none")
1785+
}
1786+
1787+
wantChange := DocumentChange{Kind: DocumentAdded, Doc: wds, OldIndex: -1, NewIndex: 0}
1788+
gotChange := qSnap.Changes[0]
1789+
copts := append([]cmp.Option{cmpopts.IgnoreFields(DocumentSnapshot{}, "ReadTime")}, cmpOpts...)
1790+
if diff := testutil.Diff(gotChange, wantChange, copts...); diff != "" {
1791+
t.Errorf("got: %v, want: %v, diff: %v", gotChange, wantChange, diff)
1792+
}
1793+
}
1794+
17461795
func TestIntegration_WatchQuery(t *testing.T) {
17471796
ctx := context.Background()
17481797
coll := integrationColl(t)
@@ -1957,6 +2006,39 @@ type testHelper struct {
19572006
t *testing.T
19582007
}
19592008

2009+
func (h testHelper) failIfNotNil(err error) {
2010+
if err != nil {
2011+
h.t.Fatal(err)
2012+
}
2013+
}
2014+
2015+
type testDocument struct {
2016+
id string
2017+
data map[string]interface{}
2018+
}
2019+
2020+
func (h testHelper) mustCreateMulti(collectionPath string, docsData []testDocument) []*DocumentRef {
2021+
client := integrationClient(h.t)
2022+
collRef := client.Collection(collectionPath)
2023+
docsCreated := []*DocumentRef{}
2024+
for _, data := range docsData {
2025+
var docRef *DocumentRef
2026+
if len(data.id) == 0 {
2027+
docRef = collRef.NewDoc()
2028+
} else {
2029+
docRef = collRef.Doc(data.id)
2030+
}
2031+
h.mustCreate(docRef, data.data)
2032+
docsCreated = append(docsCreated, docRef)
2033+
}
2034+
2035+
h.t.Cleanup(func() {
2036+
deleteDocuments(docsCreated)
2037+
})
2038+
2039+
return docsCreated
2040+
}
2041+
19602042
func (h testHelper) mustCreate(doc *DocumentRef, data interface{}) *WriteResult {
19612043
wr, err := doc.Create(context.Background(), data)
19622044
if err != nil {

firestore/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ func (q Query) compareFunc() func(d1, d2 *DocumentSnapshot) (int, error) {
637637
return func(d1, d2 *DocumentSnapshot) (int, error) {
638638
for _, ord := range orders {
639639
var cmp int
640-
if len(ord.fieldPath) == 1 && ord.fieldPath[0] == DocumentID {
640+
if ord.isDocumentID() {
641641
cmp = compareReferences(d1.Ref.Path, d2.Ref.Path)
642642
} else {
643643
v1, err := valueAtPath(ord.fieldPath, d1.proto.Fields)

0 commit comments

Comments
 (0)