| 
 | 1 | +// Copyright 2025 The Gitea Authors. All rights reserved.  | 
 | 2 | +// SPDX-License-Identifier: MIT  | 
 | 3 | + | 
 | 4 | +package actions  | 
 | 5 | + | 
 | 6 | +import (  | 
 | 7 | +	"testing"  | 
 | 8 | +	"time"  | 
 | 9 | + | 
 | 10 | +	"code.gitea.io/gitea/modules/webhook"  | 
 | 11 | + | 
 | 12 | +	"github.com/stretchr/testify/assert"  | 
 | 13 | +	"xorm.io/builder"  | 
 | 14 | +)  | 
 | 15 | + | 
 | 16 | +func TestFindRunOptions_ToConds_ExcludePullRequests(t *testing.T) {  | 
 | 17 | +	// Test when ExcludePullRequests is true  | 
 | 18 | +	opts := FindRunOptions{  | 
 | 19 | +		ExcludePullRequests: true,  | 
 | 20 | +	}  | 
 | 21 | +	cond := opts.ToConds()  | 
 | 22 | + | 
 | 23 | +	// Convert the condition to SQL for assertion  | 
 | 24 | +	sql, args, err := builder.ToSQL(cond)  | 
 | 25 | +	assert.NoError(t, err)  | 
 | 26 | +	// The condition should contain the trigger_event not equal to pull_request  | 
 | 27 | +	assert.Contains(t, sql, "`action_run`.trigger_event<>")  | 
 | 28 | +	assert.Contains(t, args, webhook.HookEventPullRequest)  | 
 | 29 | +}  | 
 | 30 | + | 
 | 31 | +func TestFindRunOptions_ToConds_CheckSuiteID(t *testing.T) {  | 
 | 32 | +	// Test when CheckSuiteID is set  | 
 | 33 | +	const testSuiteID int64 = 12345  | 
 | 34 | +	opts := FindRunOptions{  | 
 | 35 | +		CheckSuiteID: testSuiteID,  | 
 | 36 | +	}  | 
 | 37 | +	cond := opts.ToConds()  | 
 | 38 | + | 
 | 39 | +	// Convert the condition to SQL for assertion  | 
 | 40 | +	sql, args, err := builder.ToSQL(cond)  | 
 | 41 | +	assert.NoError(t, err)  | 
 | 42 | +	// The condition should contain the check_suite_id equal to the test value  | 
 | 43 | +	assert.Contains(t, sql, "`action_run`.check_suite_id=")  | 
 | 44 | +	assert.Contains(t, args, testSuiteID)  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +func TestFindRunOptions_ToConds_CreatedDateRange(t *testing.T) {  | 
 | 48 | +	// Test when CreatedAfter and CreatedBefore are set  | 
 | 49 | +	startDate := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)  | 
 | 50 | +	endDate := time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC)  | 
 | 51 | + | 
 | 52 | +	opts := FindRunOptions{  | 
 | 53 | +		CreatedAfter:  startDate,  | 
 | 54 | +		CreatedBefore: endDate,  | 
 | 55 | +	}  | 
 | 56 | +	cond := opts.ToConds()  | 
 | 57 | + | 
 | 58 | +	// Convert the condition to SQL for assertion  | 
 | 59 | +	sql, args, err := builder.ToSQL(cond)  | 
 | 60 | +	assert.NoError(t, err)  | 
 | 61 | +	// The condition should contain created >= startDate and created <= endDate  | 
 | 62 | +	assert.Contains(t, sql, "`action_run`.created>=")  | 
 | 63 | +	assert.Contains(t, sql, "`action_run`.created<=")  | 
 | 64 | +	assert.Contains(t, args, startDate)  | 
 | 65 | +	assert.Contains(t, args, endDate)  | 
 | 66 | +}  | 
 | 67 | + | 
 | 68 | +func TestFindRunOptions_ToConds_CreatedAfterOnly(t *testing.T) {  | 
 | 69 | +	// Test when only CreatedAfter is set  | 
 | 70 | +	startDate := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)  | 
 | 71 | + | 
 | 72 | +	opts := FindRunOptions{  | 
 | 73 | +		CreatedAfter: startDate,  | 
 | 74 | +	}  | 
 | 75 | +	cond := opts.ToConds()  | 
 | 76 | + | 
 | 77 | +	// Convert the condition to SQL for assertion  | 
 | 78 | +	sql, args, err := builder.ToSQL(cond)  | 
 | 79 | +	assert.NoError(t, err)  | 
 | 80 | +	// The condition should contain created >= startDate  | 
 | 81 | +	assert.Contains(t, sql, "`action_run`.created>=")  | 
 | 82 | +	assert.Contains(t, args, startDate)  | 
 | 83 | +	// But should not contain created <= endDate  | 
 | 84 | +	assert.NotContains(t, sql, "`action_run`.created<=")  | 
 | 85 | +}  | 
 | 86 | + | 
 | 87 | +func TestFindRunOptions_ToConds_CreatedBeforeOnly(t *testing.T) {  | 
 | 88 | +	// Test when only CreatedBefore is set  | 
 | 89 | +	endDate := time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC)  | 
 | 90 | + | 
 | 91 | +	opts := FindRunOptions{  | 
 | 92 | +		CreatedBefore: endDate,  | 
 | 93 | +	}  | 
 | 94 | +	cond := opts.ToConds()  | 
 | 95 | + | 
 | 96 | +	// Convert the condition to SQL for assertion  | 
 | 97 | +	sql, args, err := builder.ToSQL(cond)  | 
 | 98 | +	assert.NoError(t, err)  | 
 | 99 | +	// The condition should contain created <= endDate  | 
 | 100 | +	assert.Contains(t, sql, "`action_run`.created<=")  | 
 | 101 | +	assert.Contains(t, args, endDate)  | 
 | 102 | +	// But should not contain created >= startDate  | 
 | 103 | +	assert.NotContains(t, sql, "`action_run`.created>=")  | 
 | 104 | +}  | 
0 commit comments