mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
Consolidate all Go tests. Coverage is identical to before. Go tests are around 5% faster while being 7% smaller. No non-test code is touched.
122 lines
3.6 KiB
Go
122 lines
3.6 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package db_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gitea.dev/models/db"
|
|
"gitea.dev/models/unittest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type TestIndex db.ResourceIndex
|
|
|
|
func getCurrentResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
|
|
e := db.GetEngine(ctx)
|
|
var idx int64
|
|
has, err := e.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id=?", tableName), groupID).Get(&idx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if !has {
|
|
return 0, errors.New("no record")
|
|
}
|
|
return idx, nil
|
|
}
|
|
|
|
func TestSyncMaxResourceIndex(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
xe := unittest.GetXORMEngine()
|
|
assert.NoError(t, xe.Sync(&TestIndex{}))
|
|
|
|
var maxIndex int64
|
|
var err error
|
|
for _, test := range []struct {
|
|
index int64
|
|
expected int
|
|
}{
|
|
{51, 51}, // sync new max index
|
|
{30, 51}, // smaller index doesn't change
|
|
{62, 62}, // larger index changes
|
|
} {
|
|
err = db.SyncMaxResourceIndex(t.Context(), "test_index", 10, test.index)
|
|
assert.NoError(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(t.Context(), "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, test.expected, maxIndex)
|
|
}
|
|
|
|
// commit transaction
|
|
err = db.WithTx(t.Context(), func(ctx context.Context) error {
|
|
err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 73)
|
|
assert.NoError(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 73, maxIndex)
|
|
return nil
|
|
})
|
|
assert.NoError(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(t.Context(), "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 73, maxIndex)
|
|
|
|
// rollback transaction
|
|
err = db.WithTx(t.Context(), func(ctx context.Context) error {
|
|
err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 84)
|
|
maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 84, maxIndex)
|
|
return errors.New("test rollback")
|
|
})
|
|
assert.Error(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(t.Context(), "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 73, maxIndex) // the max index doesn't change because the transaction was rolled back
|
|
}
|
|
|
|
func TestGetNextResourceIndex(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
xe := unittest.GetXORMEngine()
|
|
assert.NoError(t, xe.Sync(&TestIndex{}))
|
|
|
|
// create a new record
|
|
maxIndex, err := db.GetNextResourceIndex(t.Context(), "test_index", 20)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 1, maxIndex)
|
|
|
|
// increase the existing record
|
|
maxIndex, err = db.GetNextResourceIndex(t.Context(), "test_index", 20)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 2, maxIndex)
|
|
|
|
// commit transaction
|
|
err = db.WithTx(t.Context(), func(ctx context.Context) error {
|
|
maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 3, maxIndex)
|
|
return nil
|
|
})
|
|
assert.NoError(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(t.Context(), "test_index", 20)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 3, maxIndex)
|
|
|
|
// rollback transaction
|
|
err = db.WithTx(t.Context(), func(ctx context.Context) error {
|
|
maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 4, maxIndex)
|
|
return errors.New("test rollback")
|
|
})
|
|
assert.Error(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(t.Context(), "test_index", 20)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 3, maxIndex) // the max index doesn't change because the transaction was rolled back
|
|
}
|