test: clean up channel store data after TestChannelStore (#36066)

TestChannelStore sub-tests create channels, members, and team members
using fake TeamIds and UserIds (model.NewId() for non-existent rows).
These records are left in the database and cause integrity tests
(TestCheck*) running in the same binary to fail their full-table scans.

Register a t.Cleanup on TestChannelStore that purges the affected
tables entirely. A blanket purge is safe: the schema enforces no FK
constraints, and every test suite creates its own data independently.
This commit is contained in:
Jesse Hallam
2026-04-14 16:22:44 +02:00
committed by GitHub
parent 5af77b7e08
commit 2b2ae4778f
@@ -43,6 +43,20 @@ type SqlXExecutor interface {
Select(dest any, query string, args ...any) error
}
// cleanupChannelStoreData purges all channel-related data written by TestChannelStore
// sub-tests. The integrity tests (TestCheck*) do full-table scans and fail if any
// orphaned rows remain. A blanket purge is safe: no FK constraints are enforced in the
// schema, and every test suite creates its own data independently.
func cleanupChannelStoreData(t *testing.T, s SqlStore) {
t.Helper()
db := s.GetMaster()
db.Exec(`DELETE FROM Threads`)
db.Exec(`DELETE FROM ChannelMemberHistory`)
db.Exec(`DELETE FROM ChannelMembers`)
db.Exec(`DELETE FROM Channels`)
db.Exec(`DELETE FROM TeamMembers`)
}
func cleanupChannels(t *testing.T, rctx request.CTX, ss store.Store) {
list, err := ss.Channel().GetAllChannels(0, 100000, store.ChannelSearchOpts{IncludeDeleted: true})
require.NoError(t, err, "error cleaning all channels", err)
@@ -68,6 +82,7 @@ func channelMemberToJSON(t *testing.T, cm *model.ChannelMember) string {
func TestChannelStore(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore) {
createDefaultRoles(ss)
t.Cleanup(func() { cleanupChannelStoreData(t, s) })
t.Run("Save", func(t *testing.T) { testChannelStoreSave(t, rctx, ss) })
t.Run("SaveDirectChannel", func(t *testing.T) { testChannelStoreSaveDirectChannel(t, rctx, ss, s) })