Skip to content

Commit

Permalink
covered all scenario test cases for store
Browse files Browse the repository at this point in the history
  • Loading branch information
gnana997 committed Jan 25, 2024
1 parent 1bf2599 commit 7864945
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
4 changes: 4 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,7 @@ func (s *Store) writeStream(key string, r io.Reader) error {

return nil
}

func (s *Store) Close() error {
return os.RemoveAll(s.Root)
}
76 changes: 47 additions & 29 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"fmt"
"io"
"testing"
)
Expand All @@ -21,44 +22,48 @@ func TestPathTransformFunc(t *testing.T) {
}

func TestStore(t *testing.T) {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
s := NewStore(opts)
key := "TripTomorrow"
data := bytes.NewReader([]byte("dude chill!!!"))
s := newStore()
defer teardown(t, s)

if err := s.writeStream(key, data); err != nil {
t.Error(err)
}
for i := 0; i < 50; i++ {
key := fmt.Sprintf("Trip%d", i)
data := bytes.NewReader([]byte("dude chill!!!"))

if ok := s.Has(key); !ok {
t.Error("expected true, got false")
}
if err := s.writeStream(key, data); err != nil {
t.Error(err)
}

r, err := s.Read(key)
if err != nil {
t.Error(err)
}
if ok := s.Has(key); !ok {
t.Error("expected true, got false")
}

b, err := io.ReadAll(r)
if err != nil {
t.Error(err)
}
if string(b) != "dude chill!!!" {
t.Errorf("expected %s, got %s", "dude chill!!!", string(b))
}
r, err := s.Read(key)
if err != nil {
t.Error(err)
}

if err := s.Delete(key); err != nil {
t.Error(err)
b, err := io.ReadAll(r)
if err != nil {
t.Error(err)
}
if string(b) != "dude chill!!!" {
t.Errorf("expected %s, got %s", "dude chill!!!", string(b))
}

if err := s.Delete(key); err != nil {
t.Error(err)
}

if ok := s.Has(key); ok {
t.Error("expected false, got true")
}
}
}

func TestDeleteKey(t *testing.T) {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
s := NewStore(opts)
s := newStore()
defer teardown(t, s)

key := "TripTomorrow"
data := bytes.NewReader([]byte("dude chill!!!"))

Expand All @@ -70,3 +75,16 @@ func TestDeleteKey(t *testing.T) {
t.Error(err)
}
}

func newStore() *Store {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
return NewStore(opts)
}

func teardown(t *testing.T, s *Store) {
if err := s.Close(); err != nil {
t.Error(err)
}
}

0 comments on commit 7864945

Please sign in to comment.