mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
128 lines
3.9 KiB
Go
128 lines
3.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package db_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
"github.com/hanzoai/git/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{}))
|
|
|
|
err := db.SyncMaxResourceIndex(t.Context(), "test_index", 10, 51)
|
|
assert.NoError(t, err)
|
|
|
|
// sync new max index
|
|
maxIndex, err := getCurrentResourceIndex(t.Context(), "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 51, maxIndex)
|
|
|
|
// smaller index doesn't change
|
|
err = db.SyncMaxResourceIndex(t.Context(), "test_index", 10, 30)
|
|
assert.NoError(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(t.Context(), "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 51, maxIndex)
|
|
|
|
// larger index changes
|
|
err = db.SyncMaxResourceIndex(t.Context(), "test_index", 10, 62)
|
|
assert.NoError(t, err)
|
|
maxIndex, err = getCurrentResourceIndex(t.Context(), "test_index", 10)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 62, 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
|
|
}
|