persistentmap_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package datautil
  10. import (
  11. "flag"
  12. "fmt"
  13. "os"
  14. "testing"
  15. "devt.de/krotik/common/fileutil"
  16. )
  17. const testdbdir = "test"
  18. const invalidFileName = "**" + string(0x0)
  19. func TestMain(m *testing.M) {
  20. flag.Parse()
  21. // Setup
  22. if res, _ := fileutil.PathExists(testdbdir); res {
  23. os.RemoveAll(testdbdir)
  24. }
  25. err := os.Mkdir(testdbdir, 0770)
  26. if err != nil {
  27. fmt.Print("Could not create test directory:", err.Error())
  28. os.Exit(1)
  29. }
  30. // Run the tests
  31. res := m.Run()
  32. // Teardown
  33. err = os.RemoveAll(testdbdir)
  34. if err != nil {
  35. fmt.Print("Could not remove test directory:", err.Error())
  36. }
  37. os.Exit(res)
  38. }
  39. func TestPersistentMap(t *testing.T) {
  40. // Test main scenario
  41. pm, err := NewPersistentMap(testdbdir + "/testmap.map")
  42. if err != nil {
  43. t.Error(nil)
  44. return
  45. }
  46. pm.Data["test1"] = "test1data"
  47. pm.Data["test2"] = "test2data"
  48. pm.Flush()
  49. pm2, _ := LoadPersistentMap(testdbdir + "/testmap.map")
  50. if len(pm2.Data) != 2 {
  51. t.Error("Unexpected size of map")
  52. return
  53. }
  54. if pm.Data["test1"] != "test1data" || pm.Data["test2"] != "test2data" {
  55. t.Error("Unexpected data in map:", pm.Data)
  56. return
  57. }
  58. // Test error cases
  59. pm, err = NewPersistentMap(invalidFileName)
  60. if err == nil {
  61. t.Error("Unexpected result of new map")
  62. return
  63. }
  64. pm, err = LoadPersistentMap(invalidFileName)
  65. if err == nil {
  66. t.Error("Unexpected result of new map")
  67. return
  68. }
  69. pm = &PersistentMap{invalidFileName, make(map[string]interface{})}
  70. if err := pm.Flush(); err == nil {
  71. t.Error("Unexpected result of new map")
  72. return
  73. }
  74. }
  75. func TestPersistentStringMap(t *testing.T) {
  76. // Test main scenario
  77. pm, err := NewPersistentStringMap(testdbdir + "/teststringmap.map")
  78. if err != nil {
  79. t.Error(nil)
  80. return
  81. }
  82. pm.Data["test1"] = "test1data"
  83. pm.Data["test2"] = "test2data"
  84. pm.Flush()
  85. pm2, _ := LoadPersistentStringMap(testdbdir + "/teststringmap.map")
  86. if len(pm2.Data) != 2 {
  87. t.Error("Unexpected size of map")
  88. return
  89. }
  90. if pm.Data["test1"] != "test1data" || pm.Data["test2"] != "test2data" {
  91. t.Error("Unexpected data in map:", pm.Data)
  92. return
  93. }
  94. // Test error cases
  95. pm, err = NewPersistentStringMap(invalidFileName)
  96. if err == nil {
  97. t.Error("Unexpected result of new map")
  98. return
  99. }
  100. pm, err = LoadPersistentStringMap(invalidFileName)
  101. if err == nil {
  102. t.Error("Unexpected result of new map")
  103. return
  104. }
  105. pm = &PersistentStringMap{invalidFileName, make(map[string]string)}
  106. if err := pm.Flush(); err == nil {
  107. t.Error("Unexpected result of new map")
  108. return
  109. }
  110. }