main

mattermost/focalboard

Last updated at: 29/12/2023 09:40

migration40_test.go

TLDR

This file is a test file that tests the functionality of a migration for fixing file info soft deletes.

Methods

Test40FixFileinfoSoftDeletes

This method is the main test function that executes tests for the migration. It sets up a test environment, migrates the database to a specific step, and executes SQL files. It then tests the restoration of file info soft deletes by checking the values of DeleteAt field for different file IDs.

getFileInfo

This helper function retrieves file information from the database. It takes a file ID as input and returns a FileInfo struct with Id and DeleteAt fields.

END

package migrationstests

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func Test40FixFileinfoSoftDeletes(t *testing.T) {
	th, tearDown := SetupPluginTestHelper(t)
	defer tearDown()

	th.f.MigrateToStep(39).
		ExecFile("./fixtures/test40FixFileinfoSoftDeletes.sql").
		MigrateToStep(40)

	type FileInfo struct {
		Id       string
		DeleteAt int
	}

	getFileInfo := func(t *testing.T, id string) FileInfo {
		t.Helper()
		fileInfo := FileInfo{}

		query := "SELECT id, deleteat FROM FileInfo WHERE id = $1"
		if th.IsMySQL() {
			query = "SELECT Id as id, DeleteAt as deleteat FROM FileInfo WHERE Id = ?"
		}

		err := th.f.DB().Get(&fileInfo, query, id)
		require.NoError(t, err)

		return fileInfo
	}

	t.Run("the file infos that don't belong to boards will not be restored", func(t *testing.T) {
		require.Equal(t, 1000, getFileInfo(t, "fileinfo-1").DeleteAt)
		require.Equal(t, 1000, getFileInfo(t, "fileinfo-2").DeleteAt)
		require.Empty(t, getFileInfo(t, "fileinfo-3").DeleteAt)
	})

	t.Run("the file infos that belong to boards should correctly be restored", func(t *testing.T) {
		require.Empty(t, getFileInfo(t, "fileinfo-3").DeleteAt)
		require.Empty(t, getFileInfo(t, "fileinfo-4").DeleteAt)
		require.Empty(t, getFileInfo(t, "fileinfo-5").DeleteAt)
	})
}