From 71dcb096be99653734539e4357f4ad3fcdb86e82 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 30 May 2026 15:09:37 +0200 Subject: [PATCH] test(metrics): verify counts are read from the right table --- pkg/models/metrics_count_test.go | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 pkg/models/metrics_count_test.go diff --git a/pkg/models/metrics_count_test.go b/pkg/models/metrics_count_test.go new file mode 100644 index 000000000..c8f64bac6 --- /dev/null +++ b/pkg/models/metrics_count_test.go @@ -0,0 +1,62 @@ +// 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" + "code.vikunja.io/api/pkg/metrics" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestMetricsCountFromDatabase verifies that each metric key counts the right table +// straight from the database. This guards the count key -> table name mapping; the +// caching/expiry/invalidation behaviour itself is covered by the keyvalue RememberFor +// tests. +func TestMetricsCountFromDatabase(t *testing.T) { + cases := map[string]string{ + metrics.UserCountKey: "users", + metrics.ProjectCountKey: "projects", + metrics.TaskCountKey: "tasks", + metrics.TeamCountKey: "teams", + metrics.FilesCountKey: "files", + metrics.AttachmentsCountKey: "task_attachments", + } + + db.LoadAndAssertFixtures(t) + + s := db.NewSession() + defer s.Close() + + for key, table := range cases { + t.Run(table, func(t *testing.T) { + // Drop any value cached by a previous test so we recompute from the DB. + require.NoError(t, metrics.InvalidateCount(key)) + + expected, err := s.Table(table).Count() + require.NoError(t, err) + + count, err := metrics.GetCount(key) + require.NoError(t, err) + assert.Equal(t, expected, count) + assert.Positive(t, count, "fixtures should contain at least one %s", table) + }) + } +}