aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm.test/.settings/org.eclipse.core.runtime.prefs
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2025-08-06 14:13:56 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2025-08-06 14:13:56 +0200
commit2a635383d0c0f63c79bd7df7af0c8ac4fd7c4a2b (patch)
treed2bbcd1ab5d1a5c406ebadd51559fe1deb31a3f4 /org.eclipse.jgit.pgm.test/.settings/org.eclipse.core.runtime.prefs
parent0a32120fac17af19af2dc6e76226952a7eef1a9b (diff)
parent97c257a57e59dc4247490cc328757416aa5aa84d (diff)
downloadjgit-master.tar.gz
jgit-master.zip
Merge branch 'stable-7.3'HEADmaster
* stable-7.3: Shortcut PackWriter reuse selection when possible Don't use Yoda style conditions to improve readability Change-Id: Ic8ca92cfc294ca01540218151bafca889256847b
Diffstat (limited to 'org.eclipse.jgit.pgm.test/.settings/org.eclipse.core.runtime.prefs')
0 files changed, 0 insertions, 0 deletions
{ color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package db_test

import (
	"context"
	"errors"
	"fmt"
	"testing"

	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/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(db.DefaultContext, "test_index", 10, 51)
	assert.NoError(t, err)

	// sync new max index
	maxIndex, err := getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
	assert.NoError(t, err)
	assert.EqualValues(t, 51, maxIndex)

	// smaller index doesn't change
	err = db.SyncMaxResourceIndex(db.DefaultContext, "test_index", 10, 30)
	assert.NoError(t, err)
	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
	assert.NoError(t, err)
	assert.EqualValues(t, 51, maxIndex)

	// larger index changes
	err = db.SyncMaxResourceIndex(db.DefaultContext, "test_index", 10, 62)
	assert.NoError(t, err)
	maxIndex, err = getCurrentResourceIndex(db.DefaultContext, "test_index", 10)
	assert.NoError(t, err)
	assert.EqualValues(t, 62, maxIndex)

	// commit transaction
	err = db.WithTx(db.DefaultContext, 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(db.DefaultContext, "test_index", 10)
	assert.NoError(t, err)
	assert.EqualValues(t, 73, maxIndex)

	// rollback transaction
	err = db.WithTx(db.DefaultContext, 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(db.DefaultContext, "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(db.DefaultContext, "test_index", 20)
	assert.NoError(t, err)
	assert.EqualValues(t, 1, maxIndex)

	// increase the existing record
	maxIndex, err = db.GetNextResourceIndex(db.DefaultContext, "test_index", 20)
	assert.NoError(t, err)
	assert.EqualValues(t, 2, maxIndex)

	// commit transaction
	err = db.WithTx(db.DefaultContext, 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(db.DefaultContext, "test_index", 20)
	assert.NoError(t, err)
	assert.EqualValues(t, 3, maxIndex)

	// rollback transaction
	err = db.WithTx(db.DefaultContext, 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(db.DefaultContext, "test_index", 20)
	assert.NoError(t, err)
	assert.EqualValues(t, 3, maxIndex) // the max index doesn't change because the transaction was rolled back
}