fix(import): create related tasks without an id

This commit is contained in:
kolaente 2023-09-07 11:16:04 +02:00
parent 68d4dcd7e6
commit c6bdb5752a
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 81 additions and 81 deletions

View File

@ -223,7 +223,7 @@ func createProjectWithEverything(s *xorm.Session, project *models.ProjectWithTas
for _, rt := range tasks { for _, rt := range tasks {
// First create the related tasks if they do not exist // First create the related tasks if they do not exist
if _, exists := tasksByOldID[rt.ID]; !exists { if _, exists := tasksByOldID[rt.ID]; !exists || rt.ID == 0 {
oldid := rt.ID oldid := rt.ID
setBucketOrDefault(rt) setBucketOrDefault(rt)
rt.ProjectID = t.ProjectID rt.ProjectID = t.ProjectID

View File

@ -35,6 +35,7 @@ func TestInsertFromStructure(t *testing.T) {
testStructure := []*models.ProjectWithTasksAndBuckets{ testStructure := []*models.ProjectWithTasksAndBuckets{
{ {
Project: models.Project{ Project: models.Project{
ID: 1,
Title: "Test1", Title: "Test1",
Description: "Lorem Ipsum", Description: "Lorem Ipsum",
}, },
@ -45,113 +46,112 @@ func TestInsertFromStructure(t *testing.T) {
}, },
}, },
}, },
ChildProjects: []*models.ProjectWithTasksAndBuckets{ },
{
Project: models.Project{
Title: "Testproject1",
Description: "Something",
ParentProjectID: 1,
},
Buckets: []*models.Bucket{
{ {
Project: models.Project{ ID: 1234,
Title: "Testproject1", Title: "Test Bucket",
Description: "Something", },
},
Tasks: []*models.TaskWithComments{
{
Task: models.Task{
Title: "Task1",
Description: "Lorem",
}, },
Buckets: []*models.Bucket{ },
{ {
ID: 1234, Task: models.Task{
Title: "Test Bucket", Title: "Task with related tasks",
RelatedTasks: map[models.RelationKind][]*models.Task{
models.RelationKindSubtask: {
{
Title: "Related to task with related task",
Description: "As subtask",
},
},
}, },
}, },
Tasks: []*models.TaskWithComments{ },
{ {
Task: models.Task{ Task: models.Task{
Title: "Task1", Title: "Task with attachments",
Description: "Lorem", Attachments: []*models.TaskAttachment{
}, {
}, File: &files.File{
{ Name: "testfile",
Task: models.Task{ Size: 4,
Title: "Task with related tasks", FileContent: []byte{1, 2, 3, 4},
RelatedTasks: map[models.RelationKind][]*models.Task{
models.RelationKindSubtask: {
{
Title: "Related to task with related task",
Description: "As subtask",
},
},
}, },
}, },
}, },
{ },
Task: models.Task{ },
Title: "Task with attachments", {
Attachments: []*models.TaskAttachment{ Task: models.Task{
{ Title: "Task with labels",
File: &files.File{ Labels: []*models.Label{
Name: "testfile", {
Size: 4, Title: "Label1",
FileContent: []byte{1, 2, 3, 4}, HexColor: "ff00ff",
}, },
}, {
}, Title: "Label2",
HexColor: "ff00ff",
}, },
}, },
{ },
Task: models.Task{ },
Title: "Task with labels", {
Labels: []*models.Label{ Task: models.Task{
{ Title: "Task with same label",
Title: "Label1", Labels: []*models.Label{
HexColor: "ff00ff", {
}, Title: "Label1",
{ HexColor: "ff00ff",
Title: "Label2",
HexColor: "ff00ff",
},
},
},
},
{
Task: models.Task{
Title: "Task with same label",
Labels: []*models.Label{
{
Title: "Label1",
HexColor: "ff00ff",
},
},
},
},
{
Task: models.Task{
Title: "Task in a bucket",
BucketID: 1234,
},
},
{
Task: models.Task{
Title: "Task in a nonexisting bucket",
BucketID: 1111,
}, },
}, },
}, },
}, },
{
Task: models.Task{
Title: "Task in a bucket",
BucketID: 1234,
},
},
{
Task: models.Task{
Title: "Task in a nonexisting bucket",
BucketID: 1111,
},
},
}, },
}, },
} }
err := InsertFromStructure(testStructure, u) err := InsertFromStructure(testStructure, u)
assert.NoError(t, err) assert.NoError(t, err)
db.AssertExists(t, "projects", map[string]interface{}{ db.AssertExists(t, "projects", map[string]interface{}{
"title": testStructure[0].ChildProjects[0].Title, "title": testStructure[1].Title,
"description": testStructure[0].ChildProjects[0].Description, "description": testStructure[1].Description,
}, false) }, false)
db.AssertExists(t, "tasks", map[string]interface{}{ db.AssertExists(t, "tasks", map[string]interface{}{
"title": testStructure[0].ChildProjects[0].Tasks[5].Title, "title": testStructure[1].Tasks[5].Title,
"bucket_id": testStructure[0].ChildProjects[0].Buckets[0].ID, "bucket_id": testStructure[1].Buckets[0].ID,
}, false) }, false)
db.AssertMissing(t, "tasks", map[string]interface{}{ db.AssertMissing(t, "tasks", map[string]interface{}{
"title": testStructure[0].ChildProjects[0].Tasks[6].Title, "title": testStructure[1].Tasks[6].Title,
"bucket_id": 1111, // No task with that bucket should exist "bucket_id": 1111, // No task with that bucket should exist
}) })
db.AssertExists(t, "tasks", map[string]interface{}{ db.AssertExists(t, "tasks", map[string]interface{}{
"title": testStructure[0].Tasks[0].Title, "title": testStructure[0].Tasks[0].Title,
}, false) }, false)
assert.NotEqual(t, 0, testStructure[0].ChildProjects[0].Tasks[0].BucketID) // Should get the default bucket assert.NotEqual(t, 0, testStructure[1].Tasks[0].BucketID) // Should get the default bucket
assert.NotEqual(t, 0, testStructure[0].ChildProjects[0].Tasks[6].BucketID) // Should get the default bucket assert.NotEqual(t, 0, testStructure[1].Tasks[6].BucketID) // Should get the default bucket
}) })
} }