Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/adetunjii/FerretDB into fix…
Browse files Browse the repository at this point in the history
…/group-stage-consumevalues
  • Loading branch information
adetunjii committed Sep 12, 2023
2 parents 17e1855 + ee5304e commit 3c786a7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ tasks:
deps:
- test-integration-pg
- test-integration-mongodb
# no test-integration-sqlite yet
- test-integration-sqlite
# no test-integration-hana

test-integration-pg:
Expand Down
17 changes: 14 additions & 3 deletions integration/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,22 +401,33 @@ func CollectKeys(t testtb.TB, doc bson.D) []string {

// FetchAll fetches all documents from the cursor, closing it.
func FetchAll(t testtb.TB, ctx context.Context, cursor *mongo.Cursor) []bson.D {
t.Helper()

var res []bson.D
err := cursor.All(ctx, &res)
require.NoError(t, cursor.Close(ctx))
require.NoError(t, err)
return res
}

// FindAll returns all documents from the given collection sorted by _id.
func FindAll(t testtb.TB, ctx context.Context, collection *mongo.Collection) []bson.D {
// FilterAll returns filtered documented from the given collection sorted by _id.
func FilterAll(t testtb.TB, ctx context.Context, collection *mongo.Collection, filter bson.D) []bson.D {
t.Helper()

opts := options.Find().SetSort(bson.D{{"_id", 1}})
cursor, err := collection.Find(ctx, bson.D{}, opts)
cursor, err := collection.Find(ctx, filter, opts)
require.NoError(t, err)

return FetchAll(t, ctx, cursor)
}

// FindAll returns all documents from the given collection sorted by _id.
func FindAll(t testtb.TB, ctx context.Context, collection *mongo.Collection) []bson.D {
t.Helper()

return FilterAll(t, ctx, collection, bson.D{})
}

// generateDocuments generates documents with _id ranging from startID to endID.
// It returns bson.A and []bson.D both containing same bson.D documents.
func generateDocuments(startID, endID int32) (bson.A, []bson.D) {
Expand Down
29 changes: 29 additions & 0 deletions integration/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,32 @@ func TestQueryCommandLimitPushDown(t *testing.T) {
})
}
}

// TestQueryIDDoc checks that the order of fields in the _id document matters.
func TestQueryIDDoc(t *testing.T) {
t.Parallel()

ctx, collection := setup.Setup(t)

_, err := collection.InsertOne(ctx, bson.D{
{"_id", bson.D{{"a", int32(1)}, {"z", int32(2)}}},
{"v", int32(1)},
})
require.NoError(t, err)
_, err = collection.InsertOne(ctx, bson.D{
{"_id", bson.D{{"a", int32(3)}, {"z", int32(4)}}},
{"v", int32(2)},
})
require.NoError(t, err)

expected := []bson.D{{
{"_id", bson.D{{"a", int32(3)}, {"z", int32(4)}}},
{"v", int32(2)},
}}
actual := FilterAll(t, ctx, collection, bson.D{{"_id", bson.D{{"a", int32(3)}, {"z", int32(4)}}}})
AssertEqualDocumentsSlice(t, expected, actual)

expected = []bson.D{}
actual = FilterAll(t, ctx, collection, bson.D{{"_id", bson.D{{"z", int32(4)}, {"a", int32(3)}}}})
AssertEqualDocumentsSlice(t, expected, actual)
}
45 changes: 45 additions & 0 deletions integration/update_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,51 @@ func TestUpdateFieldSet(t *testing.T) {
}
}

// TestUpdateFieldSetIDDoc checks that the order of fields in the _id document matters.
func TestUpdateFieldSetIDDoc(t *testing.T) {
t.Parallel()

ctx, collection := setup.Setup(t)

_, err := collection.InsertOne(ctx, bson.D{
{"_id", bson.D{{"a", int32(1)}, {"z", int32(2)}}},
{"v", int32(1)},
})
require.NoError(t, err)
_, err = collection.InsertOne(ctx, bson.D{
{"_id", bson.D{{"a", int32(3)}, {"z", int32(4)}}},
{"v", int32(2)},
})
require.NoError(t, err)

res, err := collection.UpdateByID(
ctx,
bson.D{{"a", int32(3)}, {"z", int32(4)}},
bson.D{{"$set", bson.D{{"v", int32(3)}}}},
)
require.NoError(t, err)
assert.Equal(t, &mongo.UpdateResult{MatchedCount: 1, ModifiedCount: 1}, res)

expected := []bson.D{{
{"_id", bson.D{{"a", int32(1)}, {"z", int32(2)}}},
{"v", int32(1)},
}, {
{"_id", bson.D{{"a", int32(3)}, {"z", int32(4)}}},
{"v", int32(3)},
}}
AssertEqualDocumentsSlice(t, expected, FindAll(t, ctx, collection))

res, err = collection.UpdateByID(
ctx,
bson.D{{"z", int32(4)}, {"a", int32(3)}},
bson.D{{"$set", bson.D{{"v", int32(4)}}}},
)
require.NoError(t, err)
assert.Equal(t, new(mongo.UpdateResult), res)

AssertEqualDocumentsSlice(t, expected, FindAll(t, ctx, collection))
}

func TestUpdateFieldSetUpdateManyUpsert(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 4 additions & 4 deletions internal/backends/sqlite/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
// TODO https://github.com/FerretDB/FerretDB/issues/226

const (
// IDColumn is a SQLite path expression for _id field.
IDColumn = "_ferretdb_sjson->'$._id'"

// DefaultColumn is a column name for all fields expect _id.
// DefaultColumn is a column name for all fields.
DefaultColumn = "_ferretdb_sjson"

// IDColumn is a SQLite path expression for _id field.
IDColumn = DefaultColumn + "->'$._id'"
)

// Collection represents collection metadata.
Expand Down

0 comments on commit 3c786a7

Please sign in to comment.