This commit introduces test cases for the API, archive, store, and filesystem functionalities, as well as a functional test for a full workflow. It ensures robust testing for snapshot operations, archiving, and blob management, significantly improving reliability.
241 lines
6.7 KiB
Go
241 lines
6.7 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.unprism.ru/KRBL/Agate/store"
|
|
)
|
|
|
|
func TestNewSQLiteStore(t *testing.T) {
|
|
// Create a temporary directory for tests
|
|
tempDir, err := os.MkdirTemp("", "agate-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir) // Clean up after test
|
|
|
|
// Create a new store
|
|
dbPath := filepath.Join(tempDir, "test.db")
|
|
s, err := NewSQLiteStore(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create SQLite store: %v", err)
|
|
}
|
|
defer s.Close()
|
|
|
|
// Check that the database file was created
|
|
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
|
t.Fatalf("Database file was not created")
|
|
}
|
|
}
|
|
|
|
func TestSaveAndGetSnapshotMetadata(t *testing.T) {
|
|
// Create a temporary directory for tests
|
|
tempDir, err := os.MkdirTemp("", "agate-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir) // Clean up after test
|
|
|
|
// Create a new store
|
|
dbPath := filepath.Join(tempDir, "test.db")
|
|
s, err := NewSQLiteStore(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create SQLite store: %v", err)
|
|
}
|
|
defer s.Close()
|
|
|
|
// Create a test snapshot
|
|
now := time.Now().UTC().Truncate(time.Second) // SQLite doesn't store nanoseconds
|
|
testSnapshot := store.Snapshot{
|
|
ID: "test-snapshot-id",
|
|
Name: "Test Snapshot",
|
|
ParentID: "parent-snapshot-id",
|
|
CreationTime: now,
|
|
Files: []store.FileInfo{
|
|
{
|
|
Path: "/test/file1.txt",
|
|
Size: 100,
|
|
IsDir: false,
|
|
SHA256: "hash1",
|
|
},
|
|
{
|
|
Path: "/test/dir1",
|
|
Size: 0,
|
|
IsDir: true,
|
|
SHA256: "",
|
|
},
|
|
},
|
|
}
|
|
|
|
// Save the snapshot
|
|
ctx := context.Background()
|
|
err = s.SaveSnapshotMetadata(ctx, testSnapshot)
|
|
if err != nil {
|
|
t.Fatalf("Failed to save snapshot metadata: %v", err)
|
|
}
|
|
|
|
// Retrieve the snapshot
|
|
retrievedSnapshot, err := s.GetSnapshotMetadata(ctx, testSnapshot.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to retrieve snapshot metadata: %v", err)
|
|
}
|
|
|
|
// Check that the retrieved snapshot matches the original
|
|
if retrievedSnapshot.ID != testSnapshot.ID {
|
|
t.Errorf("Retrieved snapshot ID does not match: got %s, want %s", retrievedSnapshot.ID, testSnapshot.ID)
|
|
}
|
|
if retrievedSnapshot.Name != testSnapshot.Name {
|
|
t.Errorf("Retrieved snapshot name does not match: got %s, want %s", retrievedSnapshot.Name, testSnapshot.Name)
|
|
}
|
|
if retrievedSnapshot.ParentID != testSnapshot.ParentID {
|
|
t.Errorf("Retrieved snapshot parent ID does not match: got %s, want %s", retrievedSnapshot.ParentID, testSnapshot.ParentID)
|
|
}
|
|
if !retrievedSnapshot.CreationTime.Equal(testSnapshot.CreationTime) {
|
|
t.Errorf("Retrieved snapshot creation time does not match: got %v, want %v", retrievedSnapshot.CreationTime, testSnapshot.CreationTime)
|
|
}
|
|
if len(retrievedSnapshot.Files) != len(testSnapshot.Files) {
|
|
t.Errorf("Retrieved snapshot has wrong number of files: got %d, want %d", len(retrievedSnapshot.Files), len(testSnapshot.Files))
|
|
}
|
|
}
|
|
|
|
func TestListSnapshotsMetadata(t *testing.T) {
|
|
// Create a temporary directory for tests
|
|
tempDir, err := os.MkdirTemp("", "agate-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir) // Clean up after test
|
|
|
|
// Create a new store
|
|
dbPath := filepath.Join(tempDir, "test.db")
|
|
s, err := NewSQLiteStore(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create SQLite store: %v", err)
|
|
}
|
|
defer s.Close()
|
|
|
|
// Create test snapshots
|
|
ctx := context.Background()
|
|
now := time.Now().UTC().Truncate(time.Second)
|
|
|
|
testSnapshots := []store.Snapshot{
|
|
{
|
|
ID: "snapshot-1",
|
|
Name: "Snapshot 1",
|
|
ParentID: "",
|
|
CreationTime: now.Add(-2 * time.Hour),
|
|
Files: []store.FileInfo{},
|
|
},
|
|
{
|
|
ID: "snapshot-2",
|
|
Name: "Snapshot 2",
|
|
ParentID: "snapshot-1",
|
|
CreationTime: now.Add(-1 * time.Hour),
|
|
Files: []store.FileInfo{},
|
|
},
|
|
{
|
|
ID: "snapshot-3",
|
|
Name: "Snapshot 3",
|
|
ParentID: "snapshot-2",
|
|
CreationTime: now,
|
|
Files: []store.FileInfo{},
|
|
},
|
|
}
|
|
|
|
// Save the snapshots
|
|
for _, snap := range testSnapshots {
|
|
err = s.SaveSnapshotMetadata(ctx, snap)
|
|
if err != nil {
|
|
t.Fatalf("Failed to save snapshot metadata: %v", err)
|
|
}
|
|
}
|
|
|
|
// List the snapshots
|
|
snapshots, err := s.ListSnapshotsMetadata(ctx)
|
|
if err != nil {
|
|
t.Fatalf("Failed to list snapshots: %v", err)
|
|
}
|
|
|
|
// Check that all snapshots are listed
|
|
if len(snapshots) != len(testSnapshots) {
|
|
t.Errorf("Wrong number of snapshots listed: got %d, want %d", len(snapshots), len(testSnapshots))
|
|
}
|
|
|
|
// Check that the snapshots have the correct information
|
|
for i, snap := range testSnapshots {
|
|
found := false
|
|
for _, listedSnap := range snapshots {
|
|
if listedSnap.ID == snap.ID {
|
|
found = true
|
|
if listedSnap.Name != snap.Name {
|
|
t.Errorf("Snapshot %d has wrong name: got %s, want %s", i, listedSnap.Name, snap.Name)
|
|
}
|
|
if listedSnap.ParentID != snap.ParentID {
|
|
t.Errorf("Snapshot %d has wrong parent ID: got %s, want %s", i, listedSnap.ParentID, snap.ParentID)
|
|
}
|
|
if !listedSnap.CreationTime.Equal(snap.CreationTime) {
|
|
t.Errorf("Snapshot %d has wrong creation time: got %v, want %v", i, listedSnap.CreationTime, snap.CreationTime)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Snapshot %d (%s) not found in listed snapshots", i, snap.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeleteSnapshotMetadata(t *testing.T) {
|
|
// Create a temporary directory for tests
|
|
tempDir, err := os.MkdirTemp("", "agate-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir) // Clean up after test
|
|
|
|
// Create a new store
|
|
dbPath := filepath.Join(tempDir, "test.db")
|
|
s, err := NewSQLiteStore(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create SQLite store: %v", err)
|
|
}
|
|
defer s.Close()
|
|
|
|
// Create a test snapshot
|
|
ctx := context.Background()
|
|
testSnapshot := store.Snapshot{
|
|
ID: "test-snapshot-id",
|
|
Name: "Test Snapshot",
|
|
ParentID: "",
|
|
CreationTime: time.Now().UTC().Truncate(time.Second),
|
|
Files: []store.FileInfo{},
|
|
}
|
|
|
|
// Save the snapshot
|
|
err = s.SaveSnapshotMetadata(ctx, testSnapshot)
|
|
if err != nil {
|
|
t.Fatalf("Failed to save snapshot metadata: %v", err)
|
|
}
|
|
|
|
// Delete the snapshot
|
|
err = s.DeleteSnapshotMetadata(ctx, testSnapshot.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to delete snapshot metadata: %v", err)
|
|
}
|
|
|
|
// Try to retrieve the deleted snapshot
|
|
_, err = s.GetSnapshotMetadata(ctx, testSnapshot.ID)
|
|
if err == nil {
|
|
t.Fatalf("Expected error when retrieving deleted snapshot, got nil")
|
|
}
|
|
|
|
// Deleting a non-existent snapshot should not return an error
|
|
err = s.DeleteSnapshotMetadata(ctx, "non-existent-id")
|
|
if err != nil {
|
|
t.Fatalf("DeleteSnapshotMetadata returned an error for non-existent snapshot: %v", err)
|
|
}
|
|
} |