summaryrefslogtreecommitdiffstats
path: root/models/migrations/v1_22/v290_test.go
blob: ced200f83fadca6438fbc294ea59bbaee7ad308b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_22 //nolint

import (
	"strconv"
	"testing"

	migration_tests "code.gitea.io/gitea/models/migrations/test"
	"code.gitea.io/gitea/modules/timeutil"
	webhook_module "code.gitea.io/gitea/modules/webhook"

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

func Test_AddPayloadVersionToHookTaskTable(t *testing.T) {
	type HookTaskMigrated HookTask

	// HookTask represents a hook task, as of before the migration
	type HookTask struct {
		ID             int64  `xorm:"pk autoincr"`
		HookID         int64  `xorm:"index"`
		UUID           string `xorm:"unique"`
		PayloadContent string `xorm:"LONGTEXT"`
		EventType      webhook_module.HookEventType
		IsDelivered    bool
		Delivered      timeutil.TimeStampNano

		// History info.
		IsSucceed       bool
		RequestContent  string `xorm:"LONGTEXT"`
		ResponseContent string `xorm:"LONGTEXT"`
	}

	// Prepare and load the testing database
	x, deferable := migration_tests.PrepareTestEnv(t, 0, new(HookTask), new(HookTaskMigrated))
	defer deferable()
	if x == nil || t.Failed() {
		return
	}

	require.NoError(t, AddPayloadVersionToHookTaskTable(x))

	expected := []HookTaskMigrated{}
	require.NoError(t, x.Table("hook_task_migrated").Asc("id").Find(&expected))
	assert.Len(t, expected, 2)

	got := []HookTaskMigrated{}
	require.NoError(t, x.Table("hook_task").Asc("id").Find(&got))

	for i, expected := range expected {
		expected, got := expected, got[i]
		t.Run(strconv.FormatInt(expected.ID, 10), func(t *testing.T) {
			assert.Equal(t, expected.PayloadVersion, got.PayloadVersion)
		})
	}
}