You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.8 KiB

  1. package filer
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. func TestDirectory(t *testing.T) {
  8. dm, _ := NewDirectoryManagerInMap("/tmp/dir.log")
  9. defer func() {
  10. if true {
  11. os.Remove("/tmp/dir.log")
  12. }
  13. }()
  14. dm.MakeDirectory("/a/b/c")
  15. dm.MakeDirectory("/a/b/d")
  16. dm.MakeDirectory("/a/b/e")
  17. dm.MakeDirectory("/a/b/e/f")
  18. dm.MakeDirectory("/a/b/e/f/g")
  19. dm.MoveUnderDirectory("/a/b/e/f/g", "/a/b", "")
  20. if _, err := dm.FindDirectory("/a/b/e/f/g"); err == nil {
  21. t.Fatal("/a/b/e/f/g should not exist any more after moving")
  22. }
  23. if _, err := dm.FindDirectory("/a/b/g"); err != nil {
  24. t.Fatal("/a/b/g should exist after moving")
  25. }
  26. dm.MakeDirectory("/a/b/g/h/i")
  27. dm.DeleteDirectory("/a/b/e/f")
  28. dm.DeleteDirectory("/a/b/e")
  29. dirNames, _ := dm.ListDirectories("/a/b/e")
  30. for _, v := range dirNames {
  31. println("sub1 dir:", v.Name, "id", v.Id)
  32. }
  33. dm.logFile.Close()
  34. var path []string
  35. printTree(dm.Root, path)
  36. dm2, e := NewDirectoryManagerInMap("/tmp/dir.log")
  37. if e != nil {
  38. println("load error", e.Error())
  39. }
  40. if !compare(dm.Root, dm2.Root) {
  41. t.Fatal("restored dir not the same!")
  42. }
  43. printTree(dm2.Root, path)
  44. }
  45. func printTree(node *DirectoryEntryInMap, path []string) {
  46. println(strings.Join(path, "/") + "/" + node.Name)
  47. path = append(path, node.Name)
  48. for _, v := range node.SubDirectories {
  49. printTree(v, path)
  50. }
  51. }
  52. func compare(root1 *DirectoryEntryInMap, root2 *DirectoryEntryInMap) bool {
  53. if len(root1.SubDirectories) != len(root2.SubDirectories) {
  54. return false
  55. }
  56. if root1.Name != root2.Name {
  57. return false
  58. }
  59. if root1.Id != root2.Id {
  60. return false
  61. }
  62. if !(root1.Parent == nil && root2.Parent == nil) {
  63. if root1.Parent.Id != root2.Parent.Id {
  64. return false
  65. }
  66. }
  67. for k, v := range root1.SubDirectories {
  68. if !compare(v, root2.SubDirectories[k]) {
  69. return false
  70. }
  71. }
  72. return true
  73. }