Files
vikunja/pkg/db/helpers_test.go

264 lines
9.1 KiB
Go

// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package db
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"xorm.io/builder"
)
func TestMultiFieldSearchParadeDB(t *testing.T) {
// Save and restore global state
originalParadeDB := paradedbInstalled
defer func() { paradedbInstalled = originalParadeDB }()
// These tests verify the SQL generation for the ParadeDB path.
// Since Type() requires an initialized engine, and the test engine is SQLite,
// we cannot call MultiFieldSearchWithTableAlias directly for the ParadeDB path.
// Instead, we verify the builder.Expr conditions that would be generated.
t.Run("single field generates fuzzy disjunction", func(t *testing.T) {
// Simulate what MultiFieldSearchWithTableAlias generates for ParadeDB single field
cond := builder.Expr("title ||| ?::pdb.fuzzy(1, t)", "landing")
w := builder.NewWriter()
err := cond.WriteTo(w)
require.NoError(t, err)
assert.Equal(t, "title ||| ?::pdb.fuzzy(1, t)", w.String())
assert.Equal(t, []interface{}{"landing"}, w.Args())
})
t.Run("multi field generates OR of fuzzy disjunctions", func(t *testing.T) {
// Simulate what MultiFieldSearchWithTableAlias generates for ParadeDB multi field
cond := builder.Or(
builder.Expr("title ||| ?::pdb.fuzzy(1, t)", "landing"),
builder.Expr("description ||| ?::pdb.fuzzy(1, t)", "landing"),
)
w := builder.NewWriter()
err := cond.WriteTo(w)
require.NoError(t, err)
assert.Equal(t, "(title ||| ?::pdb.fuzzy(1, t)) OR (description ||| ?::pdb.fuzzy(1, t))", w.String())
assert.Equal(t, []interface{}{"landing", "landing"}, w.Args())
})
t.Run("table alias prefixes field name", func(t *testing.T) {
// Simulate what MultiFieldSearchWithTableAlias generates with table alias
cond := builder.Expr("tasks.title ||| ?::pdb.fuzzy(1, t)", "test")
w := builder.NewWriter()
err := cond.WriteTo(w)
require.NoError(t, err)
assert.Equal(t, "tasks.title ||| ?::pdb.fuzzy(1, t)", w.String())
assert.Equal(t, []interface{}{"test"}, w.Args())
})
t.Run("multi field with table alias", func(t *testing.T) {
cond := builder.Or(
builder.Expr("tasks.title ||| ?::pdb.fuzzy(1, t)", "test"),
builder.Expr("tasks.description ||| ?::pdb.fuzzy(1, t)", "test"),
)
w := builder.NewWriter()
err := cond.WriteTo(w)
require.NoError(t, err)
assert.Equal(t, "(tasks.title ||| ?::pdb.fuzzy(1, t)) OR (tasks.description ||| ?::pdb.fuzzy(1, t))", w.String())
assert.Equal(t, []interface{}{"test", "test"}, w.Args())
})
}
func TestMultiFieldSearchILIKEFallback(t *testing.T) {
// These tests verify the ILIKE fallback path (non-ParadeDB).
// Since Type() requires an initialized engine, we test the builder.Like
// conditions directly — this is what ILIKE returns for non-Postgres databases.
t.Run("single field generates LIKE condition", func(t *testing.T) {
// This is what ILIKE("title", "test") returns for SQLite/MySQL
cond := &builder.Like{"title", "%test%"}
w := builder.NewWriter()
err := cond.WriteTo(w)
require.NoError(t, err)
assert.Equal(t, "title LIKE ?", w.String())
assert.Equal(t, []interface{}{"%test%"}, w.Args())
})
t.Run("ILIKE wraps search with wildcards", func(t *testing.T) {
// This is what ILIKE("description", "landing") returns for SQLite/MySQL
cond := &builder.Like{"description", "%landing%"}
w := builder.NewWriter()
err := cond.WriteTo(w)
require.NoError(t, err)
assert.Equal(t, "description LIKE ?", w.String())
assert.Equal(t, []interface{}{"%landing%"}, w.Args())
})
t.Run("multi field ILIKE generates OR of LIKE conditions", func(t *testing.T) {
// This is what MultiFieldSearch generates for non-ParadeDB databases
cond := builder.Or(
&builder.Like{"title", "%landing%"},
&builder.Like{"description", "%landing%"},
)
w := builder.NewWriter()
err := cond.WriteTo(w)
require.NoError(t, err)
assert.Equal(t, "title LIKE ? OR description LIKE ?", w.String())
assert.Equal(t, []interface{}{"%landing%", "%landing%"}, w.Args())
})
}
func TestIsMySQLDuplicateEntryError(t *testing.T) {
tests := []struct {
name string
err error
constraintName string
expected bool
}{
{
name: "nil error",
err: nil,
constraintName: "UQE_task_buckets_task_project_view",
expected: false,
},
{
name: "matching MySQL duplicate entry error",
err: errors.New("Error 1062 (23000): Duplicate entry '424-557' for key 'UQE_task_buckets_task_project_view'"),
constraintName: "UQE_task_buckets_task_project_view",
expected: true,
},
{
name: "MySQL duplicate entry error with different constraint",
err: errors.New("Error 1062 (23000): Duplicate entry '424-557' for key 'some_other_constraint'"),
constraintName: "UQE_task_buckets_task_project_view",
expected: false,
},
{
name: "non-MySQL error",
err: errors.New("some other database error"),
constraintName: "UQE_task_buckets_task_project_view",
expected: false,
},
{
name: "case insensitive matching",
err: errors.New("ERROR 1062 (23000): DUPLICATE ENTRY '424-557' FOR KEY 'UQE_TASK_BUCKETS_TASK_PROJECT_VIEW'"),
constraintName: "uqe_task_buckets_task_project_view",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsMySQLDuplicateEntryError(tt.err, tt.constraintName)
assert.Equal(t, tt.expected, result)
})
}
}
func TestIsUniqueConstraintError(t *testing.T) {
tests := []struct {
name string
err error
constraintName string
expected bool
}{
{
name: "nil error",
err: nil,
constraintName: "UQE_task_buckets_task_project_view",
expected: false,
},
// MySQL tests
{
name: "MySQL duplicate entry error",
err: errors.New("Error 1062 (23000): Duplicate entry '424-557' for key 'UQE_task_buckets_task_project_view'"),
constraintName: "UQE_task_buckets_task_project_view",
expected: true,
},
{
name: "MySQL duplicate entry error with different constraint",
err: errors.New("Error 1062 (23000): Duplicate entry '424-557' for key 'some_other_constraint'"),
constraintName: "UQE_task_buckets_task_project_view",
expected: false,
},
// PostgreSQL tests
{
name: "PostgreSQL duplicate key error",
err: errors.New(`duplicate key value violates unique constraint "UQE_task_buckets_task_project_view"`),
constraintName: "UQE_task_buckets_task_project_view",
expected: true,
},
{
name: "PostgreSQL duplicate key error with different constraint",
err: errors.New(`duplicate key value violates unique constraint "some_other_constraint"`),
constraintName: "UQE_task_buckets_task_project_view",
expected: false,
},
// SQLite tests
{
name: "SQLite unique constraint failed",
err: errors.New("UNIQUE constraint failed: task_buckets.task_id, task_buckets.project_view_id"),
constraintName: "UQE_task_buckets_task_project_view",
expected: true,
},
{
name: "SQLite constraint failed with unique",
err: errors.New("constraint failed: UNIQUE constraint failed: task_buckets.task_id"),
constraintName: "UQE_task_buckets_task_project_view",
expected: true, // Should match on task_buckets table pattern
},
// General tests
{
name: "non-constraint error",
err: errors.New("some other database error"),
constraintName: "UQE_task_buckets_task_project_view",
expected: false,
},
{
name: "case insensitive matching - MySQL",
err: errors.New("ERROR 1062 (23000): DUPLICATE ENTRY '424-557' FOR KEY 'UQE_TASK_BUCKETS_TASK_PROJECT_VIEW'"),
constraintName: "uqe_task_buckets_task_project_view",
expected: true,
},
{
name: "case insensitive matching - PostgreSQL",
err: errors.New(`DUPLICATE KEY VALUE VIOLATES UNIQUE CONSTRAINT "UQE_TASK_BUCKETS_TASK_PROJECT_VIEW"`),
constraintName: "uqe_task_buckets_task_project_view",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsUniqueConstraintError(tt.err, tt.constraintName)
assert.Equal(t, tt.expected, result)
})
}
}