refactor: Remove obsolete test files

- Deleted outdated test files for providers, authentication, password management, configuration, database, email, scheduler, and web handlers.
- Cleaned up the codebase by removing tests that are no longer relevant or have been replaced by new implementations.
- This cleanup helps maintain a more manageable and efficient testing environment.
This commit is contained in:
StarFleetCPTN committed 2025-03-26 16:51:14 -07:00
1 parent 8424e97cf8
commit a7cd021ff2
27 files changed
-11348

No files matched your search

File diff suppressed because it is too large. Load diff
-250
View File
@@ -1,250 +0,0 @@
package db
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestDeleteTransferConfigEdgeCases tests edge cases for the DeleteTransferConfig function
func TestDeleteTransferConfigEdgeCases(t *testing.T) {
db := setupTestDB(t)
// Create a test user
testUser := &User{
Email: "config-edge-test@example.com",
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err := db.CreateUser(testUser)
assert.NoError(t, err)
// Create multiple configs
configs := make([]*TransferConfig, 5)
for i := 0; i < 5; i++ {
config := &TransferConfig{
Name: fmt.Sprintf("Edge Config %d", i),
SourceType: "local",
SourcePath: fmt.Sprintf("/source/path/%d", i),
DestinationType: "local",
DestinationPath: fmt.Sprintf("/destination/path/%d", i),
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(config)
assert.NoError(t, err)
configs[i] = config
}
// Delete them in reverse order
for i := 4; i >= 0; i-- {
err = db.DeleteTransferConfig(configs[i].ID)
assert.NoError(t, err)
// Verify deletion
_, err = db.GetTransferConfig(configs[i].ID)
assert.Error(t, err, "Config should be deleted")
}
// Test deleting a config that has a job associated with it
configWithJob := &TransferConfig{
Name: "Config with Job",
SourceType: "local",
SourcePath: "/source/path/job",
DestinationType: "local",
DestinationPath: "/destination/path/job",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(configWithJob)
assert.NoError(t, err)
// Create a job for this config
job := &Job{
Name: "Job for Config",
ConfigID: configWithJob.ID,
Schedule: "0 * * * *",
Enabled: BoolPtr(true),
CreatedBy: testUser.ID,
}
err = db.CreateJob(job)
assert.NoError(t, err)
// Try to delete the config - this should fail due to foreign key constraint
err = db.DeleteTransferConfig(configWithJob.ID)
assert.Error(t, err, "Should not be able to delete config with associated jobs")
assert.Contains(t, err.Error(), "jobs are using this configuration", "Error should mention jobs")
// Delete the job first
err = db.DeleteJob(job.ID)
assert.NoError(t, err)
// Now delete the config - this should succeed
err = db.DeleteTransferConfig(configWithJob.ID)
assert.NoError(t, err)
// Verify deletion
_, err = db.GetTransferConfig(configWithJob.ID)
assert.Error(t, err, "Config should be deleted")
}
// TestDeleteJobEdgeCases tests edge cases for the DeleteJob function
func TestDeleteJobEdgeCases(t *testing.T) {
db := setupTestDB(t)
// Create a test user
testUser := &User{
Email: "job-edge-test@example.com",
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err := db.CreateUser(testUser)
assert.NoError(t, err)
// Create a test config
config := &TransferConfig{
Name: "Config for Job Edge Cases",
SourceType: "local",
SourcePath: "/source/path",
DestinationType: "local",
DestinationPath: "/destination/path",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(config)
assert.NoError(t, err)
// Create multiple jobs
jobs := make([]*Job, 5)
for i := 0; i < 5; i++ {
job := &Job{
Name: fmt.Sprintf("Edge Job %d", i),
ConfigID: config.ID,
Schedule: "0 * * * *",
Enabled: BoolPtr(true),
CreatedBy: testUser.ID,
}
err = db.CreateJob(job)
assert.NoError(t, err)
jobs[i] = job
}
// Delete them in reverse order
for i := 4; i >= 0; i-- {
err = db.DeleteJob(jobs[i].ID)
assert.NoError(t, err)
// Verify deletion
_, err = db.GetJob(jobs[i].ID)
assert.Error(t, err, "Job should be deleted")
}
// Create a job with history records
jobWithHistory := &Job{
Name: "Job with History",
ConfigID: config.ID,
Schedule: "0 * * * *",
Enabled: BoolPtr(true),
CreatedBy: testUser.ID,
}
err = db.CreateJob(jobWithHistory)
assert.NoError(t, err)
// Create history records
for i := 0; i < 3; i++ {
startTime := time.Now().Add(time.Duration(-i) * time.Hour)
endTime := startTime.Add(30 * time.Minute)
history := &JobHistory{
JobID: jobWithHistory.ID,
StartTime: startTime,
EndTime: &endTime,
Status: "completed",
BytesTransferred: int64(1024 * (i + 1)),
FilesTransferred: i + 1,
}
err = db.CreateJobHistory(history)
assert.NoError(t, err)
}
// Now delete the job - this should succeed even with history records
// (due to foreign key constraints in the database)
err = db.DeleteJob(jobWithHistory.ID)
assert.NoError(t, err)
// Verify deletion
_, err = db.GetJob(jobWithHistory.ID)
assert.Error(t, err, "Job should be deleted")
}
// TestInitializeEdgeCases tests edge cases for the Initialize function
func TestInitializeEdgeCases(t *testing.T) {
// Test with a read-only directory (if possible)
tempDir, err := os.MkdirTemp("", "gomft_test_readonly")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)
// Try to make the directory read-only
// Note: This may not work on all systems due to permissions
origPerms, err := os.Stat(tempDir)
if err != nil {
t.Fatalf("Failed to stat directory: %v", err)
}
// Try to make it read-only
err = os.Chmod(tempDir, 0400) // read-only
if err != nil {
t.Logf("Warning: Could not set directory to read-only: %v", err)
t.Skip("Could not set directory to read-only, skipping test")
}
defer os.Chmod(tempDir, origPerms.Mode()) // restore original permissions
dbPath := filepath.Join(tempDir, "readonly.db")
// This might fail because the directory is read-only
db, err := Initialize(dbPath)
if err != nil {
// Expected error due to read-only directory
t.Logf("Got expected error for read-only directory: %v", err)
} else {
// If it succeeded, clean up
t.Logf("Warning: DB initialization succeeded even with read-only directory!")
err = db.Close()
assert.NoError(t, err)
}
}
// TestCloseEdgeCases tests edge cases for the Close function
func TestCloseEdgeCases(t *testing.T) {
// Create a temporary database
tempDir, err := os.MkdirTemp("", "gomft_test_close_edge")
assert.NoError(t, err)
defer os.RemoveAll(tempDir)
dbPath := filepath.Join(tempDir, "close_edge.db")
db, err := Initialize(dbPath)
assert.NoError(t, err)
// Test calling methods after close
sqlDB, err := db.DB.DB()
assert.NoError(t, err)
// Get initial stats
stats := sqlDB.Stats()
t.Logf("Initial stats: MaxOpenConnections=%d, OpenConnections=%d, InUse=%d",
stats.MaxOpenConnections, stats.OpenConnections, stats.InUse)
// Close the DB
err = db.Close()
assert.NoError(t, err)
// Try to get stats again - this might fail
stats = sqlDB.Stats()
t.Logf("After close stats: MaxOpenConnections=%d, OpenConnections=%d, InUse=%d",
stats.MaxOpenConnections, stats.OpenConnections, stats.InUse)
// Verify that DB operations fail after close
_, err = db.GetUserByEmail("test@example.com")
assert.Error(t, err, "DB operations should fail after close")
}
-163
View File
@@ -1,163 +0,0 @@
package db
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Tests for error handling in GetUserByEmail
func TestGetUserByEmailError(t *testing.T) {
db := setupTestDB(t)
// Test the error case with a non-existent email
user, err := db.GetUserByEmail("nonexistent@example.com")
// Verify expectations
assert.Error(t, err, "Should return an error when user is not found")
assert.Nil(t, user, "User should be nil when an error occurs")
}
// Tests for error handling in GetUserByID
func TestGetUserByIDError(t *testing.T) {
db := setupTestDB(t)
// Test the error case with a non-existent ID
user, err := db.GetUserByID(9999)
// Verify expectations
assert.Error(t, err, "Should return an error when user is not found")
assert.Nil(t, user, "User should be nil when an error occurs")
}
// Tests for error handling in GetPasswordResetToken
func TestGetPasswordResetTokenError(t *testing.T) {
db := setupTestDB(t)
// Test the error case with an invalid token
token, err := db.GetPasswordResetToken("invalid-token")
// Verify expectations
assert.Error(t, err, "Should return an error when token is not found")
assert.Nil(t, token, "Token should be nil when an error occurs")
// Test with an expired token
testUser := &User{
Email: "expired-token@example.com",
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err = db.CreateUser(testUser)
assert.NoError(t, err)
// Create an expired token (expired 1 hour ago)
expiredToken := &PasswordResetToken{
UserID: testUser.ID,
Token: "expired-token",
ExpiresAt: time.Now().Add(-1 * time.Hour),
}
err = db.CreatePasswordResetToken(expiredToken)
assert.NoError(t, err)
// Try to get the expired token
retrievedToken, err := db.GetPasswordResetToken("expired-token")
assert.Error(t, err, "Should return an error for expired token")
assert.Nil(t, retrievedToken, "Token should be nil for expired token")
// Create a used token
usedToken := &PasswordResetToken{
UserID: testUser.ID,
Token: "used-token",
ExpiresAt: time.Now().Add(1 * time.Hour),
Used: BoolPtr(true),
}
err = db.CreatePasswordResetToken(usedToken)
assert.NoError(t, err)
// Try to get the used token
retrievedToken, err = db.GetPasswordResetToken("used-token")
assert.Error(t, err, "Should return an error for used token")
assert.Nil(t, retrievedToken, "Token should be nil for used token")
}
// Tests for error handling in DeleteTransferConfig
func TestDeleteTransferConfigError(t *testing.T) {
db := setupTestDB(t)
// Test deleting a non-existent config
err := db.DeleteTransferConfig(9999)
// Verify expectations - should not return an error even if the record doesn't exist
assert.NoError(t, err, "Should not return an error when deleting non-existent config")
}
// Tests for error handling in DeleteJob
func TestDeleteJobError(t *testing.T) {
db := setupTestDB(t)
// Test deleting a non-existent job
err := db.DeleteJob(9999)
// Verify expectations - should not return an error even if the record doesn't exist
assert.NoError(t, err, "Should not return an error when deleting non-existent job")
}
// Tests for error handling in GetFileMetadataByHash
func TestGetFileMetadataByHashError(t *testing.T) {
db := setupTestDB(t)
// Test the error case with an invalid hash
metadata, err := db.GetFileMetadataByHash("invalid-hash")
// Verify expectations
assert.Error(t, err, "Should return an error when metadata is not found")
assert.Nil(t, metadata, "Metadata should be nil when an error occurs")
}
// Tests for error handling in Initialize
func TestInitializeErrors(t *testing.T) {
// Test with a path that is a directory, not a file
// This should cause an error when trying to open a SQLite database
_, err := Initialize("/dev/null/cannot_be_a_db")
assert.Error(t, err, "Should return an error with invalid path")
}
// Tests for error handling in GenerateRcloneConfig
func TestGenerateRcloneConfigErrors(t *testing.T) {
db := setupTestDB(t)
// Create a test user
testUser := &User{
Email: "config-error-test@example.com",
PasswordHash: "hashed_password",
IsAdmin: BoolPtr(false),
LastPasswordChange: time.Now(),
}
err := db.CreateUser(testUser)
assert.NoError(t, err)
// Create a config with invalid credentials for an SFTP connection
invalidConfig := &TransferConfig{
Name: "Invalid Config",
SourceType: "sftp", // Using SFTP with invalid host to force error
SourceHost: "nonexistent.host",
SourcePort: 22,
SourceUser: "invaliduser",
SourcePath: "/source/path",
DestinationType: "local",
DestinationPath: "/destination/path",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(invalidConfig)
assert.NoError(t, err)
// Set a non-existent RCLONE_PATH to force error
t.Setenv("RCLONE_PATH", "/nonexistent/rclone")
// This should return an error because the rclone command doesn't exist
err = db.GenerateRcloneConfig(invalidConfig)
assert.Error(t, err, "Should return an error when rclone command fails")
}
-134
View File
@@ -1,134 +0,0 @@
package db
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
// TestInitializeWithNonExistentDirectory tests initialization with a directory that doesn't exist
func TestInitializeWithNonExistentDirectory(t *testing.T) {
// Create a temporary directory path
tempDir := filepath.Join(os.TempDir(), "gomft_test_nonexistent")
// Make sure the directory doesn't exist
_ = os.RemoveAll(tempDir)
// Create a path inside the non-existent directory
dbPath := filepath.Join(tempDir, "test.db")
// Initialize the database - this should create the directory
db, err := Initialize(dbPath)
assert.NoError(t, err)
assert.NotNil(t, db)
// Verify the directory was created
_, err = os.Stat(tempDir)
assert.NoError(t, err, "Directory should be created")
// Close and clean up
err = db.Close()
assert.NoError(t, err)
// Clean up
_ = os.RemoveAll(tempDir)
}
// TestInitializeWithInvalidDBPath tests initialization with an invalid DB path
func TestInitializeWithInvalidDBPath(t *testing.T) {
// Create a file path that can't be a SQLite database
invalidPath := "/dev/null/invalid.db"
// Attempt to initialize with an invalid path
db, err := Initialize(invalidPath)
assert.Error(t, err)
assert.Nil(t, db)
}
// TestInitializeWithExistingDB tests initialization with an existing database
func TestInitializeWithExistingDB(t *testing.T) {
// Create a temporary directory
tempDir, err := os.MkdirTemp("", "gomft_test_existing")
assert.NoError(t, err)
defer os.RemoveAll(tempDir)
// Create a database path
dbPath := filepath.Join(tempDir, "existing.db")
// Initialize the database for the first time
db1, err := Initialize(dbPath)
assert.NoError(t, err)
assert.NotNil(t, db1)
// Create a test user to verify the database works
user := &User{
Email: "test@example.com",
PasswordHash: "hash",
IsAdmin: BoolPtr(true),
}
err = db1.CreateUser(user)
assert.NoError(t, err)
assert.NotZero(t, user.ID)
// Close the first database connection
err = db1.Close()
assert.NoError(t, err)
// Initialize the database again with the same path
db2, err := Initialize(dbPath)
assert.NoError(t, err)
assert.NotNil(t, db2)
// Verify we can read the user that was created earlier
retrievedUser, err := db2.GetUserByEmail("test@example.com")
assert.NoError(t, err)
assert.Equal(t, user.ID, retrievedUser.ID)
// Close the second database connection
err = db2.Close()
assert.NoError(t, err)
}
// TestCloseMultipleTimes tests closing the database multiple times
func TestCloseMultipleTimes(t *testing.T) {
// Create a temporary directory
tempDir, err := os.MkdirTemp("", "gomft_test_close")
assert.NoError(t, err)
defer os.RemoveAll(tempDir)
// Create a database path
dbPath := filepath.Join(tempDir, "close.db")
// Initialize the database
db, err := Initialize(dbPath)
assert.NoError(t, err)
assert.NotNil(t, db)
// Close the database
err = db.Close()
assert.NoError(t, err)
// Trying to close it again - for some DB drivers this might cause an error
// but SQLite in-memory seems to handle this gracefully
err = db.Close()
// We won't assert error here since it depends on the driver
t.Logf("Second close resulted in: %v", err)
// Instead, let's test that DB operations fail after close
_, err = db.GetUserByEmail("test@example.com")
assert.Error(t, err, "DB operations should fail after close")
}
// TestInitializeWithMigrationFailure tests when AutoMigrate fails
func TestInitializeWithMigrationFailure(t *testing.T) {
// We can't easily cause a migration failure with SQLite
// but we can skip this test and document that it's hard to test
t.Skip("Testing migration failure is difficult with SQLite")
// In a real-world scenario, this might happen if:
// 1. The schema changed significantly between versions
// 2. The database is corrupted
// 3. There are permission issues
}
-251
View File
@@ -1,251 +0,0 @@
package db
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestGetConfigRclonePathWithEnv tests the GetConfigRclonePath function with different environment variables
func TestGetConfigRclonePathWithEnv(t *testing.T) {
// Save original environment variable
originalDataDir := os.Getenv("DATA_DIR")
defer os.Setenv("DATA_DIR", originalDataDir)
// Set a custom data directory
customDir := "/tmp/custom_data_dir"
os.Setenv("DATA_DIR", customDir)
db := setupTestDB(t)
// Create a test config
testUser := &User{
Email: "rclone-env-test@example.com",
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err := db.CreateUser(testUser)
assert.NoError(t, err)
testConfig := &TransferConfig{
Name: "Test Config",
SourceType: "local",
SourcePath: "/source/path",
DestinationType: "local",
DestinationPath: "/dest/path",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(testConfig)
assert.NoError(t, err)
// Test GetConfigRclonePath with custom DATA_DIR
configPath := db.GetConfigRclonePath(testConfig)
assert.Equal(t,
filepath.Join(customDir, "configs", fmt.Sprintf("config_%d.conf", testConfig.ID)),
configPath,
"Should use DATA_DIR environment variable")
}
// TestGenerateRcloneConfigWithoutRclone tests error handling when rclone executable is not available
func TestGenerateRcloneConfigWithoutRclone(t *testing.T) {
// Save original environment variable
originalRclonePath := os.Getenv("RCLONE_PATH")
defer os.Setenv("RCLONE_PATH", originalRclonePath)
// Set a nonexistent rclone path
os.Setenv("RCLONE_PATH", "/nonexistent/rclone")
db := setupTestDB(t)
// Create a test user
testUser := &User{
Email: "rclone-missing-test@example.com",
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err := db.CreateUser(testUser)
assert.NoError(t, err)
// Test configs for different source types
sourceTypes := []string{"sftp", "s3", "minio", "b2", "smb", "ftp", "webdav", "nextcloud", "onedrive", "gdrive"}
for _, sourceType := range sourceTypes {
testConfig := &TransferConfig{
Name: fmt.Sprintf("Test %s Config", sourceType),
SourceType: sourceType,
SourceHost: "example.com",
SourcePort: 22,
SourceUser: "testuser",
SourcePath: "/source/path",
SourceAccessKey: "access_key",
SourceSecretKey: "secret_key",
SourceRegion: "us-east-1",
SourceEndpoint: "endpoint.example.com",
SourceClientID: "client_id",
SourceClientSecret: "client_secret",
DestinationType: "local",
DestinationPath: "/dest/path",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(testConfig)
assert.NoError(t, err)
// This should return an error because rclone is not available
err = db.GenerateRcloneConfig(testConfig)
assert.Error(t, err, "Should return an error when rclone executable is not found for source type: %s", sourceType)
}
// Test configs for different destination types
destTypes := []string{"sftp", "s3", "minio", "b2", "smb", "ftp", "webdav", "nextcloud", "onedrive", "gdrive"}
for _, destType := range destTypes {
testConfig := &TransferConfig{
Name: fmt.Sprintf("Test Dest %s Config", destType),
SourceType: "local",
SourcePath: "/source/path",
DestinationType: destType,
DestHost: "example.com",
DestPort: 22,
DestUser: "testuser",
DestinationPath: "/dest/path",
DestAccessKey: "access_key",
DestSecretKey: "secret_key",
DestRegion: "us-east-1",
DestEndpoint: "endpoint.example.com",
DestClientID: "client_id",
DestClientSecret: "client_secret",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(testConfig)
assert.NoError(t, err)
// This should return an error because rclone is not available
err = db.GenerateRcloneConfig(testConfig)
if destType != "local" {
assert.Error(t, err, "Should return an error when rclone executable is not found for dest type: %s", destType)
} else {
// Local destination type might not error since it doesn't need to call rclone
t.Logf("Local destination type might not error")
}
}
}
func TestGoogleDriveRcloneConfig(t *testing.T) {
// Skip if rclone not available
rclonePath := os.Getenv("RCLONE_PATH")
if rclonePath == "" {
rclonePath = "rclone" // default to PATH lookup
}
_, err := exec.Command(rclonePath, "--version").CombinedOutput()
if err != nil {
t.Skip("Skipping test as rclone is not available")
}
db := setupTestDB(t)
// Create a test user
testUser := &User{
Email: fmt.Sprintf("google-rclone-test-%d@example.com", time.Now().UnixNano()),
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err = db.CreateUser(testUser)
if err != nil {
t.Fatalf("Failed to create user: %v", err)
}
// Create Google Drive source config
googleSourceConfig := &TransferConfig{
Name: "Google Drive Source Rclone Test",
SourceType: "gdrive",
SourcePath: "/path/in/google/drive",
SourceClientID: "source_google_client_id",
SourceClientSecret: "source_google_client_secret",
SourceTeamDrive: "source_team_drive_id",
DestinationType: "local",
DestinationPath: "/local/destination/path",
FilePattern: "*.pdf",
CreatedBy: testUser.ID,
}
// Set authenticated status
authenticated := true
googleSourceConfig.GoogleDriveAuthenticated = &authenticated
// Create the config
err = db.CreateTransferConfig(googleSourceConfig)
assert.NoError(t, err)
err = db.GenerateRcloneConfigWithToken(googleSourceConfig, "test_token")
assert.NoError(t, err)
// Generate rclone config for source
configPath := db.GetConfigRclonePath(googleSourceConfig)
// Check that the file exists
_, err = os.Stat(configPath)
assert.NoError(t, err, "Rclone config file should exist")
// Read the config file
configContent, err := os.ReadFile(configPath)
assert.NoError(t, err)
content := string(configContent)
// Verify it contains Google Drive specific content
assert.Contains(t, content, "type = drive")
assert.Contains(t, content, fmt.Sprintf("client_id = %s", googleSourceConfig.SourceClientID))
assert.Contains(t, content, "source")
assert.Contains(t, content, fmt.Sprintf("team_drive = %s", googleSourceConfig.SourceTeamDrive))
// Create Google Drive destination config
googleDestConfig := &TransferConfig{
Name: "Google Drive Dest Rclone Test",
SourceType: "local",
SourcePath: "/local/source/path",
DestinationType: "gdrive",
DestinationPath: "/dest/path/in/google/drive",
DestClientID: "dest_google_client_id",
DestClientSecret: "dest_google_client_secret",
DestTeamDrive: "dest_team_drive_id",
FilePattern: "*.pdf",
CreatedBy: testUser.ID,
}
// Set authenticated status
googleDestConfig.GoogleDriveAuthenticated = &authenticated
// Create the config
err = db.CreateTransferConfig(googleDestConfig)
assert.NoError(t, err)
// Generate rclone config for destination
configPath = db.GetConfigRclonePath(googleDestConfig)
// Check that the file exists
_, err = os.Stat(configPath)
assert.NoError(t, err, "Rclone config file should exist")
// Read the config file
configContent, err = os.ReadFile(configPath)
assert.NoError(t, err)
content = string(configContent)
// Verify it contains Google Drive specific content
assert.Contains(t, content, "type = drive")
assert.Contains(t, content, fmt.Sprintf("client_id = %s", googleDestConfig.DestClientID))
assert.Contains(t, content, "dest")
assert.Contains(t, content, fmt.Sprintf("team_drive = %s", googleDestConfig.DestTeamDrive))
// Clean up
err = db.Delete(&googleSourceConfig).Error
assert.NoError(t, err)
err = db.Delete(&googleDestConfig).Error
assert.NoError(t, err)
}
-199
View File
@@ -1,199 +0,0 @@
package db
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
// TestDeleteTransferConfigWithTransaction tests the DeleteTransferConfig function with transaction scenarios
func TestDeleteTransferConfigWithTransaction(t *testing.T) {
db := setupTestDB(t)
// Create a test user
testUser := &User{
Email: "delete-config-test@example.com",
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err := db.CreateUser(testUser)
assert.NoError(t, err)
// Create a test config
testConfig := &TransferConfig{
Name: "Test Delete Config",
SourceType: "local",
SourcePath: "/source/path",
DestinationType: "local",
DestinationPath: "/destination/path",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(testConfig)
assert.NoError(t, err)
// Test successful deletion
err = db.DeleteTransferConfig(testConfig.ID)
assert.NoError(t, err)
// Verify deletion
_, err = db.GetTransferConfig(testConfig.ID)
assert.Error(t, err, "Config should be deleted")
// Test deletion with transaction that's rolled back
// Create another config
testConfig2 := &TransferConfig{
Name: "Test Delete Config 2",
SourceType: "local",
SourcePath: "/source/path2",
DestinationType: "local",
DestinationPath: "/destination/path2",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(testConfig2)
assert.NoError(t, err)
// Start a transaction
tx := db.Begin()
assert.NotNil(t, tx)
// Delete the config within the transaction
err = tx.Delete(&TransferConfig{}, testConfig2.ID).Error
assert.NoError(t, err)
// Rollback the transaction
tx.Rollback()
// Verify the config still exists
config, err := db.GetTransferConfig(testConfig2.ID)
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, testConfig2.ID, config.ID)
// Test deletion with a committed transaction
tx = db.Begin()
assert.NotNil(t, tx)
// Delete the config within the transaction
err = tx.Delete(&TransferConfig{}, testConfig2.ID).Error
assert.NoError(t, err)
// Commit the transaction
tx.Commit()
// Verify the config is deleted
_, err = db.GetTransferConfig(testConfig2.ID)
assert.Error(t, err, "Config should be deleted after commit")
}
// TestDeleteJobWithTransaction tests the DeleteJob function with transaction scenarios
func TestDeleteJobWithTransaction(t *testing.T) {
db := setupTestDB(t)
// Create a test user
testUser := &User{
Email: "delete-job-test@example.com",
PasswordHash: "hashed_password",
LastPasswordChange: time.Now(),
}
err := db.CreateUser(testUser)
assert.NoError(t, err)
// Create a test transfer config
testConfig := &TransferConfig{
Name: "Test Delete Job Config",
SourceType: "local",
SourcePath: "/source/path",
DestinationType: "local",
DestinationPath: "/destination/path",
CreatedBy: testUser.ID,
}
err = db.CreateTransferConfig(testConfig)
assert.NoError(t, err)
// Create a test job
testJob := &Job{
Name: "Test Delete Job",
ConfigID: testConfig.ID,
Schedule: "0 * * * *", // Run hourly
Enabled: BoolPtr(true),
CreatedBy: testUser.ID,
}
err = db.CreateJob(testJob)
assert.NoError(t, err)
// Test successful deletion
err = db.DeleteJob(testJob.ID)
assert.NoError(t, err)
// Verify deletion
_, err = db.GetJob(testJob.ID)
assert.Error(t, err, "Job should be deleted")
// Test deletion with transaction that's rolled back
// Create another job
testJob2 := &Job{
Name: "Test Delete Job 2",
ConfigID: testConfig.ID,
Schedule: "0 * * * *", // Run hourly
Enabled: BoolPtr(true),
CreatedBy: testUser.ID,
}
err = db.CreateJob(testJob2)
assert.NoError(t, err)
// Start a transaction
tx := db.Begin()
assert.NotNil(t, tx)
// Delete the job within the transaction
err = tx.Delete(&Job{}, testJob2.ID).Error
assert.NoError(t, err)
// Rollback the transaction
tx.Rollback()
// Verify the job still exists
job, err := db.GetJob(testJob2.ID)
assert.NoError(t, err)
assert.NotNil(t, job)
assert.Equal(t, testJob2.ID, job.ID)
// Test deletion with a committed transaction
tx = db.Begin()
assert.NotNil(t, tx)
// Delete the job within the transaction
err = tx.Delete(&Job{}, testJob2.ID).Error
assert.NoError(t, err)
// Commit the transaction
tx.Commit()
// Verify the job is deleted
_, err = db.GetJob(testJob2.ID)
assert.Error(t, err, "Job should be deleted after commit")
}
// TestTransactionHelpers tests transaction helper methods
func TestTransactionHelpers(t *testing.T) {
db := setupTestDB(t)
// Test Begin and Rollback
tx := db.Begin()
assert.NotNil(t, tx)
assert.IsType(t, &gorm.DB{}, tx)
// Rollback should succeed
err := tx.Rollback().Error
assert.NoError(t, err)
// Test Begin and Commit
tx = db.Begin()
assert.NotNil(t, tx)
// Commit should succeed
err = tx.Commit().Error
assert.NoError(t, err)
}