diff options
author | 2024-03-06 11:18:57 +0100 | |
---|---|---|
committer | 2024-03-06 11:18:57 +0100 | |
commit | b22e213e15a7bc64773e626d76305bd860e6301c (patch) | |
tree | bdc0f14481b144f8e4e45a380ea3b7cf78490041 /internal/db/bundb/move_test.go | |
parent | [feature] Filters v1 (#2594) (diff) | |
download | gotosocial-b22e213e15a7bc64773e626d76305bd860e6301c.tar.xz |
[feature/chore] Add Move database functions + cache (#2647)
* [feature/chore] Add Move database functions + cache
* add move mem ratio to envparsing.sh
* update comment
Diffstat (limited to 'internal/db/bundb/move_test.go')
-rw-r--r-- | internal/db/bundb/move_test.go | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/internal/db/bundb/move_test.go b/internal/db/bundb/move_test.go new file mode 100644 index 000000000..1e1a0613f --- /dev/null +++ b/internal/db/bundb/move_test.go @@ -0,0 +1,168 @@ +// GoToSocial +// Copyright (C) GoToSocial Authors admin@gotosocial.org +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +package bundb_test + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +type MoveTestSuite struct { + BunDBStandardTestSuite +} + +func (suite *MoveTestSuite) TestMoveIntegration() { + ctx := context.Background() + firstMove := >smodel.Move{ + ID: "01HPPN38MZYEC6WBTR21J6241N", + OriginURI: "https://example.org/users/my_old_account", + TargetURI: "https://somewhere.else.net/users/my_new_account", + URI: "https://example.org/users/my_old_account/activities/Move/652e8361-0182-407d-8b01-4447e7fd10c0", + } + + // Put the move. + if err := suite.state.DB.PutMove(ctx, firstMove); err != nil { + suite.FailNow(err.Error()) + } + + // Test various ways of retrieving the Move. + if _, err := suite.state.DB.GetMoveByID(ctx, firstMove.ID); err != nil { + suite.FailNow(err.Error()) + } + + if _, err := suite.state.DB.GetMoveByOriginTarget(ctx, firstMove.OriginURI, firstMove.TargetURI); err != nil { + suite.FailNow(err.Error()) + } + + // Keep the last one, and check fields set on it. + dbMove, err := suite.state.DB.GetMoveByURI(ctx, firstMove.URI) + if err != nil { + suite.FailNow(err.Error()) + } + + // Created/Updated should be set when + // it's first inserted into the db. + suite.NotZero(dbMove.CreatedAt) + suite.NotZero(dbMove.UpdatedAt) + + // URIs should be parsed and set + // on the move on population. + suite.NotNil(dbMove.Origin) + suite.NotNil(dbMove.Target) + + // These should not be set as + // they have no default values. + suite.Zero(dbMove.AttemptedAt) + suite.Zero(dbMove.SucceededAt) + + // Update the Move to emulate + // us succeeding in processing it. + dbMove.AttemptedAt = time.Now() + dbMove.SucceededAt = dbMove.AttemptedAt + if err := suite.state.DB.UpdateMove( + ctx, + dbMove, + "attempted_at", + "succeeded_at", + ); err != nil { + suite.FailNow(err.Error()) + } + + // Store dbMove as firstMove var. + firstMove = dbMove + + // Store another Move involving one + // of the original URIs, and mark + // this one as succeeded. Use a time + // a few seconds into the future to + // make sure it's differentiated + // from the first move. + secondMove := >smodel.Move{ + ID: "01HPPPNQWRMQTXRFEPKDV3A4W7", + OriginURI: "https://somewhere.else.net/users/my_new_account", + TargetURI: "http://localhost:8080/users/the_mighty_zork", + URI: "https://somewhere.else.net/activities/01HPPPPPC089VJGV0967P5YQS5", + AttemptedAt: time.Now().Add(5 * time.Second), + SucceededAt: time.Now().Add(5 * time.Second), + } + if err := suite.state.DB.PutMove(ctx, secondMove); err != nil { + suite.FailNow(err.Error()) + } + + // Test getting succeeded using the + // URI shared between the two Moves, + // and some random account. + ts, err := suite.state.DB.GetLatestMoveSuccessInvolvingURIs( + ctx, + secondMove.OriginURI, + "https://a.secret.third.place/users/mystery_meat", + ) + if err != nil { + suite.FailNow(err.Error()) + } + + // Time should be equivalent to secondMove. + suite.EqualValues(secondMove.SucceededAt.UnixMilli(), ts.UnixMilli()) + + // Test getting succeeded using + // both URIs from the first move. + ts, err = suite.state.DB.GetLatestMoveSuccessInvolvingURIs( + ctx, + firstMove.OriginURI, + firstMove.TargetURI, + ) + if err != nil { + suite.FailNow(err.Error()) + } + + // Time should be equivalent to secondMove. + suite.EqualValues(secondMove.SucceededAt.UnixMilli(), ts.UnixMilli()) + + // Test getting succeeded using + // URI from the first Move, and + // some random account. + ts, err = suite.state.DB.GetLatestMoveSuccessInvolvingURIs( + ctx, + firstMove.OriginURI, + "https://a.secret.third.place/users/mystery_meat", + ) + if err != nil { + suite.FailNow(err.Error()) + } + + // Time should be equivalent to firstMove. + suite.EqualValues(firstMove.SucceededAt.UnixMilli(), ts.UnixMilli()) + + // Delete the first Move. + if err := suite.state.DB.DeleteMoveByID(ctx, firstMove.ID); err != nil { + suite.FailNow(err.Error()) + } + + // Ensure first Move deleted. + _, err = suite.state.DB.GetMoveByID(ctx, firstMove.ID) + suite.ErrorIs(err, db.ErrNoEntries) +} + +func TestMoveTestSuite(t *testing.T) { + suite.Run(t, new(MoveTestSuite)) +} |