main

mattermost/focalboard

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

schema_table_migration_test.go

TLDR

This file contains tests for the SQL store package in the Demo Projects project. It includes tests for the getEmbeddedMigrations and filterMigrations methods.

Methods

TestGetEmbeddedMigrations

This method tests the functionality of the getEmbeddedMigrations function. It verifies that the function is able to find migrations from the embedded assets and that it returns a non-empty list of migrations.

TestFilterMigrations

This method tests the functionality of the filterMigrations function. It includes several sub-tests that check different aspects of the filtering logic. The tests verify that only up migrations are included, only migrations below or equal to the legacy schema version are included, migrations are included even if they're not sorted, and that the filtered migrations match the expected versions.

package sqlstore

import (
	"testing"

	"github.com/mattermost/morph/models"
	"github.com/stretchr/testify/require"
)

func TestGetEmbeddedMigrations(t *testing.T) {
	t.Run("should find migrations on the embedded assets", func(t *testing.T) {
		migrations, err := getEmbeddedMigrations()
		require.NoError(t, err)
		require.NotEmpty(t, migrations)
	})
}

func TestFilterMigrations(t *testing.T) {
	migrations := []*models.Migration{
		{Direction: models.Up, Version: 1},
		{Direction: models.Down, Version: 1},
		{Direction: models.Up, Version: 2},
		{Direction: models.Down, Version: 2},
		{Direction: models.Up, Version: 3},
		{Direction: models.Down, Version: 3},
		{Direction: models.Up, Version: 4},
		{Direction: models.Down, Version: 4},
	}

	t.Run("only up migrations should be included", func(t *testing.T) {
		filteredMigrations := filterMigrations(migrations, 4)
		require.Len(t, filteredMigrations, 4)
		for _, migration := range filteredMigrations {
			require.Equal(t, models.Up, migration.Direction)
		}
	})

	t.Run("only migrations below or equal to the legacy schema version should be included", func(t *testing.T) {
		testCases := []struct {
			Name             string
			LegacyVersion    uint32
			ExpectedVersions []uint32
		}{
			{"All should be included", 4, []uint32{1, 2, 3, 4}},
			{"Only half should be included", 2, []uint32{1, 2}},
			{"Three including the third should be included", 3, []uint32{1, 2, 3}},
		}

		for _, tc := range testCases {
			t.Run(tc.Name, func(t *testing.T) {
				filteredMigrations := filterMigrations(migrations, tc.LegacyVersion)
				require.Len(t, filteredMigrations, int(tc.LegacyVersion))

				versions := make([]uint32, len(filteredMigrations))
				for i, migration := range filteredMigrations {
					versions[i] = migration.Version
				}

				require.ElementsMatch(t, versions, tc.ExpectedVersions)
			})
		}
	})

	t.Run("migrations should be included even if they're not sorted", func(t *testing.T) {
		unsortedMigrations := []*models.Migration{
			{Direction: models.Up, Version: 4},
			{Direction: models.Down, Version: 4},
			{Direction: models.Up, Version: 1},
			{Direction: models.Down, Version: 2},
			{Direction: models.Down, Version: 1},
			{Direction: models.Up, Version: 3},
			{Direction: models.Down, Version: 3},
			{Direction: models.Up, Version: 2},
		}

		testCases := []struct {
			Name             string
			LegacyVersion    uint32
			ExpectedVersions []uint32
		}{
			{"All should be included", 4, []uint32{1, 2, 3, 4}},
			{"Only half should be included", 2, []uint32{1, 2}},
			{"Three including the third should be included", 3, []uint32{1, 2, 3}},
		}

		for _, tc := range testCases {
			t.Run(tc.Name, func(t *testing.T) {
				filteredMigrations := filterMigrations(unsortedMigrations, tc.LegacyVersion)
				require.Len(t, filteredMigrations, int(tc.LegacyVersion))

				versions := make([]uint32, len(filteredMigrations))
				for i, migration := range filteredMigrations {
					versions[i] = migration.Version
				}

				require.ElementsMatch(t, versions, tc.ExpectedVersions)
			})
		}
	})
}