Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
woblerr committed Jan 7, 2024
1 parent 5339a5c commit be87668
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
22 changes: 15 additions & 7 deletions gpbckpconfig/utils_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,27 @@ func TestGetBackupNameQuery(t *testing.T) {

func TestGetBackupDependenciesQuery(t *testing.T) {
tests := []struct {
name string
value string
want string
name string
value string
function func(string) string
want string
}{
{
name: "Test valid result",
value: "TestBackup",
want: `SELECT timestamp FROM restore_plans WHERE timestamp != 'TestBackup' AND restore_plan_timestamp = 'TestBackup' ORDER BY timestamp DESC;`,
name: "Test getBackupDependenciesQuery",
value: "TestBackup",
function: getBackupDependenciesQuery,
want: `SELECT timestamp FROM restore_plans WHERE timestamp != 'TestBackup' AND restore_plan_timestamp = 'TestBackup' ORDER BY timestamp DESC;`,
},
{
name: "Test getBackupNameBeforeTimestampQuery",
value: "20240101120000",
function: getBackupNameBeforeTimestampQuery,
want: `SELECT timestamp FROM backups WHERE timestamp < '20240101120000' AND status != 'Failure' AND date_deleted IN ('', 'Plugin Backup Delete Failed', 'Local Delete Failed') ORDER BY timestamp DESC;`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getBackupDependenciesQuery(tt.value); got != tt.want {
if got := tt.function(tt.value); got != tt.want {
t.Errorf("getBackupDependenciesQuery(%v):\n%v\nwant:\n%v", tt.value, got, tt.want)
}
})
Expand Down
23 changes: 22 additions & 1 deletion gpbckpconfig/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gpbckpconfig

import "testing"
import (
"testing"
"time"
)

func TestCheckTimestamp(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -208,3 +211,21 @@ func TestBackupPluginCustomReportPath(t *testing.T) {
})
}
}

func TestGetTimestampOlderThen(t *testing.T) {
// Call the function with a known input
input := uint(1)
got := GetTimestampOlderThen(input)

// Parse the returned timestamp
parsedTime, err := time.Parse(Layout, got)
if err != nil {
t.Errorf("Failed to parse timestamp: %v", err)
}

// Check if the returned timestamp is within the expected range
now := time.Now()
if !parsedTime.Before(now.Add(-time.Duration(input)*24*time.Hour)) || parsedTime.After(now) {
t.Errorf("Returned timestamp is not within the expected range")
}
}

0 comments on commit be87668

Please sign in to comment.