From 40411e4100fcf307e06f8b5d4294f736f3cbecc8 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 26 Jan 2023 16:06:49 +0100 Subject: [PATCH] chore(task): add test to check if a task's reminders are duplicated --- pkg/db/test.go | 8 ++++++++ pkg/models/tasks_test.go | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pkg/db/test.go b/pkg/db/test.go index 9ebc8c3dc..7a5ff4f40 100644 --- a/pkg/db/test.go +++ b/pkg/db/test.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "testing" + "xorm.io/builder" "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" @@ -102,3 +103,10 @@ func AssertMissing(t *testing.T, table string, values map[string]interface{}) { assert.NoError(t, err, fmt.Sprintf("Failed to assert entries don't exist in db, error was: %s", err)) assert.False(t, exists, fmt.Sprintf("Entries %v exist in table %s", values, table)) } + +// AssertCount checks if a number of entries exists in the database +func AssertCount(t *testing.T, table string, where builder.Cond, count int64) { + dbCount, err := x.Table(table).Where(where).Count() + assert.NoError(t, err, fmt.Sprintf("Failed to assert count in db, error was: %s", err)) + assert.Equal(t, count, dbCount, fmt.Sprintf("Found %d entries instead of expected %d in table %s", dbCount, count, table)) +} diff --git a/pkg/models/tasks_test.go b/pkg/models/tasks_test.go index f2ee58d97..96ac031bf 100644 --- a/pkg/models/tasks_test.go +++ b/pkg/models/tasks_test.go @@ -19,6 +19,7 @@ package models import ( "testing" "time" + "xorm.io/builder" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/events" @@ -367,6 +368,27 @@ func TestTask_Update(t *testing.T) { assert.NoError(t, err) assert.Equal(t, int64(3), task.Index) }) + t.Run("the same date multiple times should be saved once", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + task := &Task{ + ID: 1, + Title: "test", + Reminders: []time.Time{ + time.Unix(1674745156, 0), + time.Unix(1674745156, 223), + }, + ListID: 1, + } + err := task.Update(s, u) + assert.NoError(t, err) + err = s.Commit() + assert.NoError(t, err) + + db.AssertCount(t, "task_reminders", builder.Eq{"task_id": 1}, 1) + }) } func TestTask_Delete(t *testing.T) {