package main import ( "fmt" "os" "path/filepath" "reflect" "runtime" "testing" bolt "go.etcd.io/bbolt" ) func TestDbCustomPath(t *testing.T) { cwd, err := os.Getwd() if err != nil { t.Fatalf("An error occurred whilst getting the current directory, %s", err) } tempDataDir := filepath.Join(cwd, "test/temp_data_dir") defer os.RemoveAll(tempDataDir) path := filepath.Join(tempDataDir + "/test.db") got, err := dbPath(path) if err != nil { t.Fatalf("An error occurred whilst executing `dbPath`, %s", err) } if got != path { t.Errorf("Got unexpected database path: want %s, got %s", path, got) } else { t.Logf("Got expected database path: got %s", got) } } // TODO: Finish testing func TestDbDefaultPath(t *testing.T) { var wantPath string cwd, err := os.Getwd() if err != nil { t.Fatalf("An error occurred whilst getting the current directory, %s", err) } testXdgDataHome := fmt.Sprintf("%s/%s/%s/%s", cwd, "test", ".local", "data") defer os.RemoveAll(testXdgDataHome) if err := os.Setenv("XDG_DATA_HOME", testXdgDataHome); err != nil { t.Fatalf("An error occurred whilst setting the XDG_DATA_HOME environment variable, %s", err) } goos := runtime.GOOS switch goos { case "linux": wantPath = filepath.Join(os.Getenv("XDG_DATA_HOME"), "pelican", "pelican.db") default: wantPath = filepath.Join(os.Getenv("HOME"), ".pelican", "pelican.db") } } func TestEnsureBuckets(t *testing.T) { var db *bolt.DB var err error testDB := "test/databases/TestEnsureBuckets.db" os.Remove(testDB) if db, err = openDatabase(testDB); err != nil { t.Fatalf("An error occurred while opening the test database %s, %s.", testDB, err) } defer db.Close() for i := 0; i < 3; i++ { if err := ensureBuckets(db); err != nil { t.Fatalf("An error occurred while executing `ensureBuckets`, %s.", err) } } expectedBuckets := []string{statusBucket, cardBucket} if err = db.View(func(tx *bolt.Tx) error { for _, b := range expectedBuckets { if bucket := tx.Bucket([]byte(b)); bucket == nil { return fmt.Errorf("bucket %s does not exist in the database", b) } } return nil }); err != nil { t.Fatalf("An error occurred whilst checking for the buckets, %s.", err) } } func TestReadAndWriteStatuses(t *testing.T) { var db *bolt.DB var err error testDB := "test/databases/TestWriteAndReadStatuses.db" os.Remove(testDB) if db, err = openDatabase(testDB); err != nil { t.Fatalf("An error occurred whilst opening the test database %s, %s.", testDB, err) } defer db.Close() if err = ensureBuckets(db); err != nil { t.Fatalf("An error occurred whilst executing `ensureBuckets`, %s.", err) } // Begin with an initial list of statuses initialStatusList := []Status{ { Name: "Backlog", CardIds: []int{1, 14, 9, 10}, }, { Name: "Next", CardIds: []int{2, 5, 12}, }, { Name: "In progress", CardIds: []int{3}, }, { Name: "Finished!", CardIds: []int{4, 6, 7, 8, 11, 13}, }, } // save the initial statuses to the board bucket. if err = saveStatuses(db, initialStatusList); err != nil { t.Fatalf("An error occurred whilst writing the initial status list to the database, %s", err) } // load the status list from the database, modify it a little and write it to the database. var modifiedStatusList []Status if modifiedStatusList, err = loadAllStatuses(db); err != nil { t.Fatalf("An error occurred whilst reading the initial status list to the database, %s", err) } modifiedStatusList[2].CardIds = []int{3, 14} archiveStatus := Status{ Name: "Archived", CardIds: []int{34, 51, 894}, } modifiedStatusList = append(modifiedStatusList, archiveStatus) if err := saveStatuses(db, modifiedStatusList); err != nil { t.Fatalf("An error occurred whilst writing the modified status list to the database, %s", err) } // read the final status list from the database and compare to the expected status list. want := []Status{ { Id: 1, Name: "Backlog", CardIds: []int{1, 14, 9, 10}, }, { Id: 2, Name: "Next", CardIds: []int{2, 5, 12}, }, { Id: 3, Name: "In progress", CardIds: []int{3, 14}, }, { Id: 4, Name: "Finished!", CardIds: []int{4, 6, 7, 8, 11, 13}, }, { Id: 5, Name: "Archived", CardIds: []int{34, 51, 894}, }, } var got []Status if got, err = loadAllStatuses(db); err != nil { t.Fatalf("An error occurred whilst reading the modified status list from the database, %s", err) } if !reflect.DeepEqual(got, want) { t.Errorf("Unexpected status list read from the database: got %+v, want %+v", got, want) } else { t.Logf("Expected status list read from the database: got %+v", got) } } func TestReadAndWriteCards(t *testing.T) { var db *bolt.DB var err error testDB := "test/databases/TestReadWriteCards.db" os.Remove(testDB) if db, err = openDatabase(testDB); err != nil { t.Fatalf("An error occurred whilst opening the test database %s, %s.", testDB, err) } if err = ensureBuckets(db); err != nil { t.Fatalf("An error occurred whilst executing `ensureBuckets`, %s.", err) } initialCard := Card{ Title: "A test task.", Content: "This task should be completed.", } // Save the card cardId, err := saveCard(db, initialCard) if err != nil { t.Fatalf("An error occurred whilst saving the initial card to the database, %s", err) } modifiedCard, err := loadCard(db, cardId) if err != nil { t.Fatalf("An error occurred whilst loading the card from the database, %s", err) } modifiedCard.Title = "Task: Foo bar baz." modifiedCardId, err := saveCard(db, modifiedCard) if err != nil { t.Fatalf("An error occurred whilst saving the modified card to the database, %s", err) } want := Card{ Id: 1, Title: "Task: Foo bar baz.", Content: "This task should be completed.", } got, err := loadCard(db, modifiedCardId) if err != nil { t.Fatalf("An error occurred whilst loading the modified from the database, %s", err) } if !reflect.DeepEqual(got, want) { t.Errorf("Unexpected card read from the database: got %+v, want %+v", got, want) } else { t.Logf("Expected card read from the database: got %+v", got) } }