|
| 1 | +// Vikunja is a to-do list application to facilitate your life. |
| 2 | +// Copyright 2018-present Vikunja and contributors. All rights reserved. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package models |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "code.vikunja.io/api/pkg/db" |
| 23 | + "code.vikunja.io/api/pkg/metrics" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +// TestMetricsCountFromDatabase verifies that each metric key counts the right table |
| 30 | +// straight from the database. This guards the count key -> table name mapping; the |
| 31 | +// caching/expiry/invalidation behaviour itself is covered by the keyvalue RememberFor |
| 32 | +// tests. |
| 33 | +func TestMetricsCountFromDatabase(t *testing.T) { |
| 34 | + cases := map[string]string{ |
| 35 | + metrics.UserCountKey: "users", |
| 36 | + metrics.ProjectCountKey: "projects", |
| 37 | + metrics.TaskCountKey: "tasks", |
| 38 | + metrics.TeamCountKey: "teams", |
| 39 | + metrics.FilesCountKey: "files", |
| 40 | + metrics.AttachmentsCountKey: "task_attachments", |
| 41 | + } |
| 42 | + |
| 43 | + db.LoadAndAssertFixtures(t) |
| 44 | + |
| 45 | + s := db.NewSession() |
| 46 | + defer s.Close() |
| 47 | + |
| 48 | + for key, table := range cases { |
| 49 | + t.Run(table, func(t *testing.T) { |
| 50 | + // Drop any value cached by a previous test so we recompute from the DB. |
| 51 | + require.NoError(t, metrics.InvalidateCount(key)) |
| 52 | + |
| 53 | + expected, err := s.Table(table).Count() |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + count, err := metrics.GetCount(key) |
| 57 | + require.NoError(t, err) |
| 58 | + assert.Equal(t, expected, count) |
| 59 | + assert.Positive(t, count, "fixtures should contain at least one %s", table) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
0 commit comments