Skip to content

Commit

Permalink
Add encodeID for mongo and add FindUserInfoByID test
Browse files Browse the repository at this point in the history
  • Loading branch information
raararaara committed Jan 10, 2024
1 parent f1cb1d8 commit 6c2c9ee
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
4 changes: 4 additions & 0 deletions server/backend/database/memory/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func TestDB(t *testing.T) {
testcases.RunListUserInfosTest(t, db)
})

t.Run("FindUserInfoByID test", func(t *testing.T) {
testcases.RunFindUserInfoByIDTest(t, db)
})

t.Run("FindProjectInfoBySecretKey test", func(t *testing.T) {
testcases.RunFindProjectInfoBySecretKeyTest(t, db)
})
Expand Down
39 changes: 22 additions & 17 deletions server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,23 +468,6 @@ func (c *Client) FindUserInfo(ctx context.Context, username string) (*database.U
return &userInfo, nil
}

// FindUserInfoByID returns a user by ID.
func (c *Client) FindUserInfoByID(ctx context.Context, id string) (*database.UserInfo, error) {
result := c.collection(colUsers).FindOne(ctx, bson.M{
"_id": id,
})

userInfo := database.UserInfo{}
if err := result.Decode(&userInfo); err != nil {
if err == mongo.ErrNoDocuments {
return nil, fmt.Errorf("%s: %w", id, database.ErrUserNotFound)
}
return nil, fmt.Errorf("decode user info: %w", err)
}

return &userInfo, nil
}

// ListUserInfos returns all users.
func (c *Client) ListUserInfos(
ctx context.Context,
Expand All @@ -502,6 +485,28 @@ func (c *Client) ListUserInfos(
return infos, nil
}

// FindUserInfoByID returns a user by ID.
func (c *Client) FindUserInfoByID(ctx context.Context, clientID types.ID) (*database.UserInfo, error) {
encodedClientID, err := encodeID(clientID)
if err != nil {
return nil, err
}

result := c.collection(colUsers).FindOne(ctx, bson.M{
"_id": encodedClientID,
})

userInfo := database.UserInfo{}
if err := result.Decode(&userInfo); err != nil {
if err == mongo.ErrNoDocuments {
return nil, fmt.Errorf("%s: %w", clientID, database.ErrUserNotFound)
}
return nil, fmt.Errorf("decode user info: %w", err)
}

return &userInfo, nil
}

// ActivateClient activates the client of the given key.
func (c *Client) ActivateClient(ctx context.Context, projectID types.ID, key string) (*database.ClientInfo, error) {
encodedProjectID, err := encodeID(projectID)
Expand Down
4 changes: 4 additions & 0 deletions server/backend/database/mongo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func TestClient(t *testing.T) {
testcases.RunListUserInfosTest(t, cli)
})

t.Run("FindUserInfoByID test", func(t *testing.T) {
testcases.RunFindUserInfoByIDTest(t, cli)
})

t.Run("FindProjectInfoBySecretKey test", func(t *testing.T) {
testcases.RunFindProjectInfoBySecretKeyTest(t, cli)
})
Expand Down
18 changes: 18 additions & 0 deletions server/backend/database/testcases/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@ func RunListUserInfosTest(t *testing.T, db database.Database) {
})
}

// RunFindUserInfoByIDTest runs the FindUserInfoByID test for the given db.
func RunFindUserInfoByIDTest(t *testing.T, db database.Database) {
t.Run("RunFindUserInfoByID test", func(t *testing.T) {
ctx := context.Background()

username := "findUserInfoTestAccount"
password := "temporary-password"

user, _, err := db.EnsureDefaultUserAndProject(ctx, username, password, clientDeactivateThreshold)
assert.NoError(t, err)

info1, err := db.FindUserInfoByID(ctx, user.ID)
assert.NoError(t, err)

assert.Equal(t, user.ID, info1.ID)
})
}

// RunActivateClientDeactivateClientTest runs the ActivateClient and DeactivateClient tests for the given db.
func RunActivateClientDeactivateClientTest(t *testing.T, db database.Database, projectID types.ID) {
t.Run("activate and find client test", func(t *testing.T) {
Expand Down

1 comment on commit 6c2c9ee

@github-actions
Copy link

Choose a reason for hiding this comment

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

Go Benchmark

Benchmark suite Current: 6c2c9ee Previous: d391669 Ratio
BenchmarkDocument/constructor_test - ns/op 1407 ns/op 1348 ns/op 1.04
BenchmarkDocument/constructor_test - B/op 1208 B/op 1208 B/op 1
BenchmarkDocument/constructor_test - allocs/op 20 allocs/op 20 allocs/op 1
BenchmarkDocument/status_test - ns/op 791.2 ns/op 774.9 ns/op 1.02
BenchmarkDocument/status_test - B/op 1176 B/op 1176 B/op 1
BenchmarkDocument/status_test - allocs/op 18 allocs/op 18 allocs/op 1
BenchmarkDocument/equals_test - ns/op 7162 ns/op 7095 ns/op 1.01
BenchmarkDocument/equals_test - B/op 6913 B/op 6913 B/op 1
BenchmarkDocument/equals_test - allocs/op 120 allocs/op 120 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 16351 ns/op 18023 ns/op 0.91
BenchmarkDocument/nested_update_test - B/op 11963 B/op 11963 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 254 allocs/op 254 allocs/op 1
BenchmarkDocument/delete_test - ns/op 22149 ns/op 21860 ns/op 1.01
BenchmarkDocument/delete_test - B/op 15188 B/op 15188 B/op 1
BenchmarkDocument/delete_test - allocs/op 333 allocs/op 333 allocs/op 1
BenchmarkDocument/object_test - ns/op 9686 ns/op 8299 ns/op 1.17
BenchmarkDocument/object_test - B/op 6721 B/op 6721 B/op 1
BenchmarkDocument/object_test - allocs/op 116 allocs/op 116 allocs/op 1
BenchmarkDocument/array_test - ns/op 29123 ns/op 28416 ns/op 1.02
BenchmarkDocument/array_test - B/op 11819 B/op 11819 B/op 1
BenchmarkDocument/array_test - allocs/op 270 allocs/op 270 allocs/op 1
BenchmarkDocument/text_test - ns/op 30926 ns/op 30734 ns/op 1.01
BenchmarkDocument/text_test - B/op 14886 B/op 14795 B/op 1.01
BenchmarkDocument/text_test - allocs/op 468 allocs/op 468 allocs/op 1
BenchmarkDocument/text_composition_test - ns/op 28966 ns/op 28436 ns/op 1.02
BenchmarkDocument/text_composition_test - B/op 18396 B/op 18278 B/op 1.01
BenchmarkDocument/text_composition_test - allocs/op 477 allocs/op 477 allocs/op 1
BenchmarkDocument/rich_text_test - ns/op 81617 ns/op 79377 ns/op 1.03
BenchmarkDocument/rich_text_test - B/op 38645 B/op 38540 B/op 1.00
BenchmarkDocument/rich_text_test - allocs/op 1147 allocs/op 1147 allocs/op 1
BenchmarkDocument/counter_test - ns/op 16890 ns/op 16429 ns/op 1.03
BenchmarkDocument/counter_test - B/op 10210 B/op 10210 B/op 1
BenchmarkDocument/counter_test - allocs/op 236 allocs/op 236 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 2940029 ns/op 2870698 ns/op 1.02
BenchmarkDocument/text_edit_gc_100 - B/op 1658702 B/op 1655216 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17092 allocs/op 17091 allocs/op 1.00
BenchmarkDocument/text_edit_gc_1000 - ns/op 238194127 ns/op 227911236 ns/op 1.05
BenchmarkDocument/text_edit_gc_1000 - B/op 144363644 B/op 144348859 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 200855 allocs/op 200920 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 3424261 ns/op 3341420 ns/op 1.02
BenchmarkDocument/text_split_gc_100 - B/op 2317140 B/op 2313404 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16197 allocs/op 16193 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 295314834 ns/op 284979132 ns/op 1.04
BenchmarkDocument/text_split_gc_1000 - B/op 228925684 B/op 228892912 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 203974 allocs/op 203950 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 11746112 ns/op 10649517 ns/op 1.10
BenchmarkDocument/text_delete_all_10000 - B/op 5811201 B/op 5809883 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 40678 allocs/op 40671 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 201098387 ns/op 186393795 ns/op 1.08
BenchmarkDocument/text_delete_all_100000 - B/op 81906354 B/op 81904469 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 411647 allocs/op 411644 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 223504 ns/op 216510 ns/op 1.03
BenchmarkDocument/text_100 - B/op 120107 B/op 118483 B/op 1.01
BenchmarkDocument/text_100 - allocs/op 5080 allocs/op 5080 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 2428463 ns/op 2354446 ns/op 1.03
BenchmarkDocument/text_1000 - B/op 1169093 B/op 1153053 B/op 1.01
BenchmarkDocument/text_1000 - allocs/op 50084 allocs/op 50084 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 1244081 ns/op 1167715 ns/op 1.07
BenchmarkDocument/array_1000 - B/op 1091224 B/op 1091275 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11825 allocs/op 11825 allocs/op 1
BenchmarkDocument/array_10000 - ns/op 13400414 ns/op 12925120 ns/op 1.04
BenchmarkDocument/array_10000 - B/op 9797943 B/op 9799997 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120283 allocs/op 120290 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 144182 ns/op 143218 ns/op 1.01
BenchmarkDocument/array_gc_100 - B/op 132484 B/op 132500 B/op 1.00
BenchmarkDocument/array_gc_100 - allocs/op 1248 allocs/op 1249 allocs/op 1.00
BenchmarkDocument/array_gc_1000 - ns/op 1382594 ns/op 1362603 ns/op 1.01
BenchmarkDocument/array_gc_1000 - B/op 1158871 B/op 1158952 B/op 1.00
BenchmarkDocument/array_gc_1000 - allocs/op 12864 allocs/op 12865 allocs/op 1.00
BenchmarkDocument/counter_1000 - ns/op 198272 ns/op 192998 ns/op 1.03
BenchmarkDocument/counter_1000 - B/op 192852 B/op 192852 B/op 1
BenchmarkDocument/counter_1000 - allocs/op 5765 allocs/op 5765 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 2163265 ns/op 2121356 ns/op 1.02
BenchmarkDocument/counter_10000 - B/op 2087768 B/op 2087765 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 59772 allocs/op 59772 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 1341051 ns/op 1323428 ns/op 1.01
BenchmarkDocument/object_1000 - B/op 1428038 B/op 1427932 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9845 allocs/op 9844 allocs/op 1.00
BenchmarkDocument/object_10000 - ns/op 15200029 ns/op 14952592 ns/op 1.02
BenchmarkDocument/object_10000 - B/op 12169336 B/op 12166471 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 100569 allocs/op 100558 allocs/op 1.00
BenchmarkDocument/tree_100 - ns/op 1082516 ns/op 998783 ns/op 1.08
BenchmarkDocument/tree_100 - B/op 943680 B/op 943673 B/op 1.00
BenchmarkDocument/tree_100 - allocs/op 6099 allocs/op 6099 allocs/op 1
BenchmarkDocument/tree_1000 - ns/op 79887795 ns/op 74036459 ns/op 1.08
BenchmarkDocument/tree_1000 - B/op 86460520 B/op 86460238 B/op 1.00
BenchmarkDocument/tree_1000 - allocs/op 60113 allocs/op 60112 allocs/op 1.00
BenchmarkDocument/tree_10000 - ns/op 9870024879 ns/op 9238843028 ns/op 1.07
BenchmarkDocument/tree_10000 - B/op 8580987672 B/op 8580993112 B/op 1.00
BenchmarkDocument/tree_10000 - allocs/op 600214 allocs/op 600251 allocs/op 1.00
BenchmarkDocument/tree_delete_all_1000 - ns/op 74887710 ns/op 71588951 ns/op 1.05
BenchmarkDocument/tree_delete_all_1000 - B/op 86990832 B/op 86990417 B/op 1.00
BenchmarkDocument/tree_delete_all_1000 - allocs/op 67752 allocs/op 67750 allocs/op 1.00
BenchmarkDocument/tree_edit_gc_100 - ns/op 3711059 ns/op 3588143 ns/op 1.03
BenchmarkDocument/tree_edit_gc_100 - B/op 4121018 B/op 4120927 B/op 1.00
BenchmarkDocument/tree_edit_gc_100 - allocs/op 14356 allocs/op 14355 allocs/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - ns/op 308047220 ns/op 292095970 ns/op 1.05
BenchmarkDocument/tree_edit_gc_1000 - B/op 383464122 B/op 383466214 B/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - allocs/op 145404 allocs/op 145408 allocs/op 1.00
BenchmarkDocument/tree_split_gc_100 - ns/op 2523895 ns/op 2419488 ns/op 1.04
BenchmarkDocument/tree_split_gc_100 - B/op 2386835 B/op 2386838 B/op 1.00
BenchmarkDocument/tree_split_gc_100 - allocs/op 10341 allocs/op 10341 allocs/op 1
BenchmarkDocument/tree_split_gc_1000 - ns/op 189014382 ns/op 175905530 ns/op 1.07
BenchmarkDocument/tree_split_gc_1000 - B/op 221990293 B/op 221990052 B/op 1.00
BenchmarkDocument/tree_split_gc_1000 - allocs/op 112247 allocs/op 112245 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 364948976 ns/op 345650310 ns/op 1.06
BenchmarkRPC/client_to_server - B/op 17529781 B/op 16395133 B/op 1.07
BenchmarkRPC/client_to_server - allocs/op 166877 allocs/op 166812 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 622619564 ns/op 599230474 ns/op 1.04
BenchmarkRPC/client_to_client_via_server - B/op 31239008 B/op 31200524 B/op 1.00
BenchmarkRPC/client_to_client_via_server - allocs/op 312432 allocs/op 312356 allocs/op 1.00
BenchmarkRPC/attach_large_document - ns/op 1338352398 ns/op 1320790007 ns/op 1.01
BenchmarkRPC/attach_large_document - B/op 1890023872 B/op 1889214992 B/op 1.00
BenchmarkRPC/attach_large_document - allocs/op 7557 allocs/op 7549 allocs/op 1.00
BenchmarkRPC/adminCli_to_server - ns/op 549444394 ns/op 524792658 ns/op 1.05
BenchmarkRPC/adminCli_to_server - B/op 38011964 B/op 35979672 B/op 1.06
BenchmarkRPC/adminCli_to_server - allocs/op 289694 allocs/op 289662 allocs/op 1.00
BenchmarkLocker - ns/op 65.49 ns/op 64.08 ns/op 1.02
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 46.05 ns/op 37.88 ns/op 1.22
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 144.5 ns/op 138.8 ns/op 1.04
BenchmarkLockerMoreKeys - B/op 15 B/op 15 B/op 1
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkChange/Push_10_Changes - ns/op 3854274 ns/op 3738509 ns/op 1.03
BenchmarkChange/Push_10_Changes - B/op 125880 B/op 127083 B/op 0.99
BenchmarkChange/Push_10_Changes - allocs/op 1254 allocs/op 1254 allocs/op 1
BenchmarkChange/Push_100_Changes - ns/op 14300967 ns/op 13951923 ns/op 1.03
BenchmarkChange/Push_100_Changes - B/op 642036 B/op 646730 B/op 0.99
BenchmarkChange/Push_100_Changes - allocs/op 6539 allocs/op 6540 allocs/op 1.00
BenchmarkChange/Push_1000_Changes - ns/op 114198557 ns/op 111633212 ns/op 1.02
BenchmarkChange/Push_1000_Changes - B/op 6133474 B/op 6052291 B/op 1.01
BenchmarkChange/Push_1000_Changes - allocs/op 62158 allocs/op 62156 allocs/op 1.00
BenchmarkChange/Pull_10_Changes - ns/op 2880964 ns/op 2807725 ns/op 1.03
BenchmarkChange/Pull_10_Changes - B/op 100033 B/op 101299 B/op 0.99
BenchmarkChange/Pull_10_Changes - allocs/op 952 allocs/op 951 allocs/op 1.00
BenchmarkChange/Pull_100_Changes - ns/op 4355535 ns/op 4259998 ns/op 1.02
BenchmarkChange/Pull_100_Changes - B/op 256474 B/op 259044 B/op 0.99
BenchmarkChange/Pull_100_Changes - allocs/op 3155 allocs/op 3154 allocs/op 1.00
BenchmarkChange/Pull_1000_Changes - ns/op 8534947 ns/op 8186496 ns/op 1.04
BenchmarkChange/Pull_1000_Changes - B/op 1396773 B/op 1399630 B/op 1.00
BenchmarkChange/Pull_1000_Changes - allocs/op 26865 allocs/op 26877 allocs/op 1.00
BenchmarkSnapshot/Push_3KB_snapshot - ns/op 16944966 ns/op 16788712 ns/op 1.01
BenchmarkSnapshot/Push_3KB_snapshot - B/op 804264 B/op 814611 B/op 0.99
BenchmarkSnapshot/Push_3KB_snapshot - allocs/op 6542 allocs/op 6542 allocs/op 1
BenchmarkSnapshot/Push_30KB_snapshot - ns/op 117362978 ns/op 116153491 ns/op 1.01
BenchmarkSnapshot/Push_30KB_snapshot - B/op 6149621 B/op 6295727 B/op 0.98
BenchmarkSnapshot/Push_30KB_snapshot - allocs/op 62161 allocs/op 62346 allocs/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - ns/op 6620068 ns/op 6588486 ns/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - B/op 903200 B/op 908101 B/op 0.99
BenchmarkSnapshot/Pull_3KB_snapshot - allocs/op 14879 allocs/op 14882 allocs/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - ns/op 14598541 ns/op 14361476 ns/op 1.02
BenchmarkSnapshot/Pull_30KB_snapshot - B/op 6984420 B/op 6995643 B/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - allocs/op 144140 allocs/op 144141 allocs/op 1.00
BenchmarkSync/memory_sync_10_test - ns/op 6737 ns/op 6687 ns/op 1.01
BenchmarkSync/memory_sync_10_test - B/op 1286 B/op 1286 B/op 1
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 51185 ns/op 52081 ns/op 0.98
BenchmarkSync/memory_sync_100_test - B/op 8658 B/op 8642 B/op 1.00
BenchmarkSync/memory_sync_100_test - allocs/op 274 allocs/op 273 allocs/op 1.00
BenchmarkSync/memory_sync_1000_test - ns/op 595156 ns/op 587862 ns/op 1.01
BenchmarkSync/memory_sync_1000_test - B/op 74201 B/op 74357 B/op 1.00
BenchmarkSync/memory_sync_1000_test - allocs/op 2102 allocs/op 2110 allocs/op 1.00
BenchmarkSync/memory_sync_10000_test - ns/op 7133085 ns/op 6961544 ns/op 1.02
BenchmarkSync/memory_sync_10000_test - B/op 760708 B/op 764082 B/op 1.00
BenchmarkSync/memory_sync_10000_test - allocs/op 20497 allocs/op 20603 allocs/op 0.99
BenchmarkTextEditing - ns/op 18715952566 ns/op 18032931695 ns/op 1.04
BenchmarkTextEditing - B/op 9042117776 B/op 9038055344 B/op 1.00
BenchmarkTextEditing - allocs/op 19923244 allocs/op 19923783 allocs/op 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.