From a4f959aa96621186dddfedc4e079971a27030ed2 Mon Sep 17 00:00:00 2001 From: christopher-besch Date: Thu, 10 Apr 2025 17:01:21 +0200 Subject: [PATCH] test: TestGetRunBefore --- models/actions/main_test.go | 1 + models/actions/run_test.go | 96 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 models/actions/run_test.go diff --git a/models/actions/main_test.go b/models/actions/main_test.go index 27916f29ac..2eb923d9d0 100644 --- a/models/actions/main_test.go +++ b/models/actions/main_test.go @@ -13,6 +13,7 @@ func TestMain(m *testing.M) { unittest.MainTest(m, &unittest.TestOptions{ FixtureFiles: []string{ "action_runner.yml", + "repository.yml", "action_runner_token.yml", }, }) diff --git a/models/actions/run_test.go b/models/actions/run_test.go new file mode 100644 index 0000000000..051764788d --- /dev/null +++ b/models/actions/run_test.go @@ -0,0 +1,96 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package actions + +import ( + "testing" + "time" + + "forgejo.org/models/db" + "forgejo.org/models/unittest" + + "forgejo.org/modules/timeutil" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetRunBefore(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + + // this repo is part of the test database requiring loading "repository.yml" in main_test.go + var repoID int64 = 1 + + workflowID := "test_workflow" + + // third completed run + time1, err := time.Parse(time.RFC3339, "2024-07-31T15:47:55+08:00") + require.NoError(t, err) + timeutil.MockSet(time1) + run1 := ActionRun{ + ID: 1, + Index: 1, + RepoID: repoID, + Stopped: timeutil.TimeStampNow(), + WorkflowID: workflowID, + } + + // fourth completed run + time2, err := time.Parse(time.RFC3339, "2024-08-31T15:47:55+08:00") + require.NoError(t, err) + timeutil.MockSet(time2) + run2 := ActionRun{ + ID: 2, + Index: 2, + RepoID: repoID, + Stopped: timeutil.TimeStampNow(), + WorkflowID: workflowID, + } + + // second completed run + time3, err := time.Parse(time.RFC3339, "2024-07-31T15:47:54+08:00") + require.NoError(t, err) + timeutil.MockSet(time3) + run3 := ActionRun{ + ID: 3, + Index: 3, + RepoID: repoID, + Stopped: timeutil.TimeStampNow(), + WorkflowID: workflowID, + } + + // first completed run + time4, err := time.Parse(time.RFC3339, "2024-06-30T15:47:54+08:00") + require.NoError(t, err) + timeutil.MockSet(time4) + run4 := ActionRun{ + ID: 4, + Index: 4, + RepoID: repoID, + Stopped: timeutil.TimeStampNow(), + WorkflowID: workflowID, + } + require.NoError(t, db.Insert(db.DefaultContext, &run1)) + runBefore, err := GetRunBefore(db.DefaultContext, repoID, run1.Stopped) + // there is no run before run1 + require.Error(t, err) + require.Nil(t, runBefore) + + // now there is only run3 before run1 + require.NoError(t, db.Insert(db.DefaultContext, &run3)) + runBefore, err = GetRunBefore(db.DefaultContext, repoID, run1.Stopped) + require.NoError(t, err) + assert.Equal(t, run3.ID, runBefore.ID) + + // there still is only run3 before run1 + require.NoError(t, db.Insert(db.DefaultContext, &run2)) + runBefore, err = GetRunBefore(db.DefaultContext, repoID, run1.Stopped) + require.NoError(t, err) + assert.Equal(t, run3.ID, runBefore.ID) + + // run4 is further away from run1 + require.NoError(t, db.Insert(db.DefaultContext, &run4)) + runBefore, err = GetRunBefore(db.DefaultContext, repoID, run1.Stopped) + require.NoError(t, err) + assert.Equal(t, run3.ID, runBefore.ID) +}