MCPcopy
hub / github.com/pocketbase/pocketbase / TestCreateBackup

Function TestCreateBackup

core/base_backup_test.go:18–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestCreateBackup(t *testing.T) {
19 app, _ := tests.NewTestApp()
20 defer app.Cleanup()
21
22 // set some long app name with spaces and special characters
23 app.Settings().Meta.AppName = "test @! " + strings.Repeat("a", 100)
24
25 expectedAppNamePrefix := "test_" + strings.Repeat("a", 45)
26
27 // test pending error
28 app.Store().Set(core.StoreKeyActiveBackup, "")
29 if err := app.CreateBackup(context.Background(), "test.zip"); err == nil {
30 t.Fatal("Expected pending error, got nil")
31 }
32 app.Store().Remove(core.StoreKeyActiveBackup)
33
34 // create with auto generated name
35 if err := app.CreateBackup(context.Background(), ""); err != nil {
36 t.Fatal("Failed to create a backup with autogenerated name")
37 }
38
39 // create with custom name
40 if err := app.CreateBackup(context.Background(), "custom"); err != nil {
41 t.Fatal("Failed to create a backup with custom name")
42 }
43
44 // create new with the same name (aka. replace)
45 if err := app.CreateBackup(context.Background(), "custom"); err != nil {
46 t.Fatal("Failed to create and replace a backup with the same name")
47 }
48
49 backupsDir := filepath.Join(app.DataDir(), core.LocalBackupsDirName)
50
51 entries, err := os.ReadDir(backupsDir)
52 if err != nil {
53 t.Fatal(err)
54 }
55
56 expectedFiles := []string{
57 `^pb_backup_` + expectedAppNamePrefix + `_\w+\.zip$`,
58 `^pb_backup_` + expectedAppNamePrefix + `_\w+\.zip.attrs$`,
59 "custom",
60 "custom.attrs",
61 }
62
63 if len(entries) != len(expectedFiles) {
64 names := getEntryNames(entries)
65 t.Fatalf("Expected %d backup files, got %d: \n%v", len(expectedFiles), len(entries), names)
66 }
67
68 for i, entry := range entries {
69 if !list.ExistInSliceWithRegex(entry.Name(), expectedFiles) {
70 t.Fatalf("[%d] Missing backup file %q", i, entry.Name())
71 }
72
73 if strings.HasSuffix(entry.Name(), ".attrs") {
74 continue
75 }

Callers

nothing calls this directly

Calls 11

CleanupMethod · 0.95
NewTestAppFunction · 0.92
ExistInSliceWithRegexFunction · 0.92
getEntryNamesFunction · 0.85
verifyBackupContentFunction · 0.85
SettingsMethod · 0.65
SetMethod · 0.65
StoreMethod · 0.65
CreateBackupMethod · 0.65
DataDirMethod · 0.65
RemoveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…