// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package db

import (
	"errors"
	"testing"

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

func TestIsErrDeadlock(t *testing.T) {
	tests := []struct {
		name string
		err  error
		want bool
	}{
		{name: "nil", err: nil, want: false},
		{name: "unrelated", err: errors.New("connection refused"), want: false},
		{name: "mysql 1213", err: errors.New("Error 1213 (40001): Deadlock found when trying to get lock; try restarting transaction"), want: true},
		{name: "mysql sqlstate", err: errors.New("SQLSTATE 40001: serialization failure"), want: true},
		{name: "postgres", err: errors.New("pq: deadlock detected"), want: true},
		{name: "sqlite", err: errors.New("database is locked"), want: true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, IsErrDeadlock(tt.err))
		})
	}
}
