memory_store_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package container
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestNewMemoryStore(t *testing.T) {
  7. s := NewMemoryStore()
  8. m, ok := s.(*memoryStore)
  9. if !ok {
  10. t.Fatalf("store is not a memory store %v", s)
  11. }
  12. if m.s == nil {
  13. t.Fatal("expected store map to not be nil")
  14. }
  15. }
  16. func TestAddContainers(t *testing.T) {
  17. s := NewMemoryStore()
  18. s.Add("id", NewBaseContainer("id", "root"))
  19. if s.Size() != 1 {
  20. t.Fatalf("expected store size 1, got %v", s.Size())
  21. }
  22. }
  23. func TestGetContainer(t *testing.T) {
  24. s := NewMemoryStore()
  25. s.Add("id", NewBaseContainer("id", "root"))
  26. c := s.Get("id")
  27. if c == nil {
  28. t.Fatal("expected container to not be nil")
  29. }
  30. }
  31. func TestDeleteContainer(t *testing.T) {
  32. s := NewMemoryStore()
  33. s.Add("id", NewBaseContainer("id", "root"))
  34. s.Delete("id")
  35. if c := s.Get("id"); c != nil {
  36. t.Fatalf("expected container to be nil after removal, got %v", c)
  37. }
  38. if s.Size() != 0 {
  39. t.Fatalf("expected store size to be 0, got %v", s.Size())
  40. }
  41. }
  42. func TestListContainers(t *testing.T) {
  43. s := NewMemoryStore()
  44. cont := NewBaseContainer("id", "root")
  45. cont.Created = time.Now()
  46. cont2 := NewBaseContainer("id2", "root")
  47. cont2.Created = time.Now().Add(24 * time.Hour)
  48. s.Add("id", cont)
  49. s.Add("id2", cont2)
  50. list := s.List()
  51. if len(list) != 2 {
  52. t.Fatalf("expected list size 2, got %v", len(list))
  53. }
  54. if list[0].ID != "id2" {
  55. t.Fatalf("expected older container to be first, got %v", list[0].ID)
  56. }
  57. }
  58. func TestFirstContainer(t *testing.T) {
  59. s := NewMemoryStore()
  60. s.Add("id", NewBaseContainer("id", "root"))
  61. s.Add("id2", NewBaseContainer("id2", "root"))
  62. first := s.First(func(cont *Container) bool {
  63. return cont.ID == "id2"
  64. })
  65. if first == nil {
  66. t.Fatal("expected container to not be nil")
  67. }
  68. if first.ID != "id2" {
  69. t.Fatalf("expected id2, got %v", first)
  70. }
  71. }
  72. func TestApplyAllContainer(t *testing.T) {
  73. s := NewMemoryStore()
  74. s.Add("id", NewBaseContainer("id", "root"))
  75. s.Add("id2", NewBaseContainer("id2", "root"))
  76. s.ApplyAll(func(cont *Container) {
  77. if cont.ID == "id2" {
  78. cont.ID = "newID"
  79. }
  80. })
  81. cont := s.Get("id2")
  82. if cont == nil {
  83. t.Fatal("expected container to not be nil")
  84. }
  85. if cont.ID != "newID" {
  86. t.Fatalf("expected newID, got %v", cont)
  87. }
  88. }