Skip to content

Commit

Permalink
Add update operators data document fields order test (#1238)
Browse files Browse the repository at this point in the history
This PR closes #1138.
  • Loading branch information
Dmitry committed Oct 11, 2022
1 parent 470940b commit 8ab516d
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions integration/update_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,3 +1406,112 @@ func TestUpdateFieldPopArrayOperator(t *testing.T) {
}
})
}

// This test is to ensure that the order of fields in the document is preserved.
func TestUpdateDocumentFieldsOrder(t *testing.T) {
setup.SkipForTigrisWithReason(t, "Tigris schema would fail this test")

ctx, collection := setup.Setup(t, shareddata.Composites)

_, err := collection.UpdateOne(
ctx,
bson.D{{"_id", "document"}},
bson.D{{"$set", bson.D{{"foo", int32(42)}, {"bar", "baz"}}}},
)
require.NoError(t, err)

var updated bson.D

err = collection.FindOne(ctx, bson.D{{"_id", "document"}}).Decode(&updated)
require.NoError(t, err)

expected := bson.D{
{"_id", "document"},
{"v", bson.D{{"foo", int32(42)}}},
{"bar", "baz"},
{"foo", int32(42)},
}

AssertEqualDocuments(t, expected, updated)

_, err = collection.UpdateOne(
ctx,
bson.D{{"_id", "document"}},
bson.D{{"$unset", bson.D{{"foo", ""}}}},
)
require.NoError(t, err)

err = collection.FindOne(ctx, bson.D{{"_id", "document"}}).Decode(&updated)
require.NoError(t, err)

expected = bson.D{
{"_id", "document"},
{"v", bson.D{{"foo", int32(42)}}},
{"bar", "baz"},
}

AssertEqualDocuments(t, expected, updated)

_, err = collection.UpdateOne(
ctx,
bson.D{{"_id", "document"}},
bson.D{{"$set", bson.D{{"abc", int32(42)}}}},
)
require.NoError(t, err)

err = collection.FindOne(ctx, bson.D{{"_id", "document"}}).Decode(&updated)
require.NoError(t, err)

expected = bson.D{
{"_id", "document"},
{"v", bson.D{{"foo", int32(42)}}},
{"bar", "baz"},
{"abc", int32(42)},
}

AssertEqualDocuments(t, expected, updated)
}

// This test is to ensure that the order of fields in the document is preserved.
func TestUpdateDocumentFieldsOrderSimplified(t *testing.T) {
ctx, collection := setup.Setup(t)

_, err := collection.InsertOne(ctx, bson.D{{"_id", "document"}, {"foo", int32(42)}, {"bar", "baz"}})
require.NoError(t, err)

_, err = collection.UpdateOne(
ctx,
bson.D{{"_id", "document"}},
bson.D{{"$unset", bson.D{{"foo", ""}, {"bar", ""}}}},
)
require.NoError(t, err)

var updated bson.D

err = collection.FindOne(ctx, bson.D{{"_id", "document"}}).Decode(&updated)
require.NoError(t, err)

expected := bson.D{
{"_id", "document"},
}

_, err = collection.UpdateOne(
ctx,
bson.D{{"_id", "document"}},
bson.D{{"$set", bson.D{{"foo", int32(42)}, {"bar", "baz"}}}},
)
require.NoError(t, err)

AssertEqualDocuments(t, expected, updated)

err = collection.FindOne(ctx, bson.D{{"_id", "document"}}).Decode(&updated)
require.NoError(t, err)

expected = bson.D{
{"_id", "document"},
{"bar", "baz"},
{"foo", int32(42)},
}

AssertEqualDocuments(t, expected, updated)
}

1 comment on commit 8ab516d

@vercel
Copy link

@vercel vercel bot commented on 8ab516d Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ferret-db – ./

ferret-db-git-main-ferretdb.vercel.app
ferret-db-ferretdb.vercel.app
ferret-db.vercel.app

Please sign in to comment.