From 963235c0ce3744c737344faa4a5597e63d85c03c Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 25 Feb 2026 11:27:34 +0100 Subject: [PATCH] test: add failing tests for RepairOrphanedProjects --- pkg/models/project_repair_test.go | 83 +++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 pkg/models/project_repair_test.go diff --git a/pkg/models/project_repair_test.go b/pkg/models/project_repair_test.go new file mode 100644 index 000000000..5ba911833 --- /dev/null +++ b/pkg/models/project_repair_test.go @@ -0,0 +1,83 @@ +// 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 . + +package models + +import ( + "testing" + + "code.vikunja.io/api/pkg/db" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRepairOrphanedProjects(t *testing.T) { + t.Run("finds and repairs orphaned projects", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + result, err := RepairOrphanedProjects(s, false) + require.NoError(t, err) + + assert.Equal(t, 1, result.Repaired) + + // Verify the project was re-parented to top level + project := &Project{ID: 39} + has, err := s.Get(project) + require.NoError(t, err) + assert.True(t, has) + assert.Equal(t, int64(0), project.ParentProjectID) + }) + + t.Run("dry run does not modify anything", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + result, err := RepairOrphanedProjects(s, true) + require.NoError(t, err) + + assert.Equal(t, 1, result.Found) + assert.Equal(t, 0, result.Repaired) + + // Verify the project was NOT changed + project := &Project{ID: 39} + has, err := s.Get(project) + require.NoError(t, err) + assert.True(t, has) + assert.Equal(t, int64(999999), project.ParentProjectID) + }) + + t.Run("no orphans returns zero counts", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + // First, repair all orphans + _, err := RepairOrphanedProjects(s, false) + require.NoError(t, err) + + // Run again - should find nothing + result, err := RepairOrphanedProjects(s, false) + require.NoError(t, err) + + assert.Equal(t, 0, result.Found) + assert.Equal(t, 0, result.Repaired) + }) +} +