diff --git a/gpbckpconfig/utils_db_test.go b/gpbckpconfig/utils_db_test.go index f286e63..41cc0e1 100644 --- a/gpbckpconfig/utils_db_test.go +++ b/gpbckpconfig/utils_db_test.go @@ -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) } }) diff --git a/gpbckpconfig/utils_test.go b/gpbckpconfig/utils_test.go index c3faf7b..46cb4c5 100644 --- a/gpbckpconfig/utils_test.go +++ b/gpbckpconfig/utils_test.go @@ -1,6 +1,9 @@ package gpbckpconfig -import "testing" +import ( + "testing" + "time" +) func TestCheckTimestamp(t *testing.T) { tests := []struct { @@ -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") + } +}