code.gitea.io/gitea@v1.21.7/models/db/index_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package db_test
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/models/db"
    13  	"code.gitea.io/gitea/models/unittest"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  type TestIndex db.ResourceIndex
    19  
    20  func getCurrentResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
    21  	e := db.GetEngine(ctx)
    22  	var idx int64
    23  	has, err := e.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id=?", tableName), groupID).Get(&idx)
    24  	if err != nil {
    25  		return 0, err
    26  	}
    27  	if !has {
    28  		return 0, errors.New("no record")
    29  	}
    30  	return idx, nil
    31  }
    32  
    33  func TestSyncMaxResourceIndex(t *testing.T) {
    34  	assert.NoError(t, unittest.PrepareTestDatabase())
    35  	xe := unittest.GetXORMEngine()
    36  	assert.NoError(t, xe.Sync(&TestIndex{}))
    37  
    38  	err := db.SyncMaxResourceIndex(db.DefaultContext, "test_index", 10, 51)
    39  	assert.NoError(t, err)
    40  
    41  	// sync new max index
    42  	maxIndex, err := getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
    43  	assert.NoError(t, err)
    44  	assert.EqualValues(t, 51, maxIndex)
    45  
    46  	// smaller index doesn't change
    47  	err = db.SyncMaxResourceIndex(db.DefaultContext, "test_index", 10, 30)
    48  	assert.NoError(t, err)
    49  	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
    50  	assert.NoError(t, err)
    51  	assert.EqualValues(t, 51, maxIndex)
    52  
    53  	// larger index changes
    54  	err = db.SyncMaxResourceIndex(db.DefaultContext, "test_index", 10, 62)
    55  	assert.NoError(t, err)
    56  	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
    57  	assert.NoError(t, err)
    58  	assert.EqualValues(t, 62, maxIndex)
    59  
    60  	// commit transaction
    61  	err = db.WithTx(db.DefaultContext, func(ctx context.Context) error {
    62  		err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 73)
    63  		assert.NoError(t, err)
    64  		maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10)
    65  		assert.NoError(t, err)
    66  		assert.EqualValues(t, 73, maxIndex)
    67  		return nil
    68  	})
    69  	assert.NoError(t, err)
    70  	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
    71  	assert.NoError(t, err)
    72  	assert.EqualValues(t, 73, maxIndex)
    73  
    74  	// rollback transaction
    75  	err = db.WithTx(db.DefaultContext, func(ctx context.Context) error {
    76  		err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 84)
    77  		maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10)
    78  		assert.NoError(t, err)
    79  		assert.EqualValues(t, 84, maxIndex)
    80  		return errors.New("test rollback")
    81  	})
    82  	assert.Error(t, err)
    83  	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
    84  	assert.NoError(t, err)
    85  	assert.EqualValues(t, 73, maxIndex) // the max index doesn't change because the transaction was rolled back
    86  }
    87  
    88  func TestGetNextResourceIndex(t *testing.T) {
    89  	assert.NoError(t, unittest.PrepareTestDatabase())
    90  	xe := unittest.GetXORMEngine()
    91  	assert.NoError(t, xe.Sync(&TestIndex{}))
    92  
    93  	// create a new record
    94  	maxIndex, err := db.GetNextResourceIndex(db.DefaultContext, "test_index", 20)
    95  	assert.NoError(t, err)
    96  	assert.EqualValues(t, 1, maxIndex)
    97  
    98  	// increase the existing record
    99  	maxIndex, err = db.GetNextResourceIndex(db.DefaultContext, "test_index", 20)
   100  	assert.NoError(t, err)
   101  	assert.EqualValues(t, 2, maxIndex)
   102  
   103  	// commit transaction
   104  	err = db.WithTx(db.DefaultContext, func(ctx context.Context) error {
   105  		maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20)
   106  		assert.NoError(t, err)
   107  		assert.EqualValues(t, 3, maxIndex)
   108  		return nil
   109  	})
   110  	assert.NoError(t, err)
   111  	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 20)
   112  	assert.NoError(t, err)
   113  	assert.EqualValues(t, 3, maxIndex)
   114  
   115  	// rollback transaction
   116  	err = db.WithTx(db.DefaultContext, func(ctx context.Context) error {
   117  		maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20)
   118  		assert.NoError(t, err)
   119  		assert.EqualValues(t, 4, maxIndex)
   120  		return errors.New("test rollback")
   121  	})
   122  	assert.Error(t, err)
   123  	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 20)
   124  	assert.NoError(t, err)
   125  	assert.EqualValues(t, 3, maxIndex) // the max index doesn't change because the transaction was rolled back
   126  }