Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage #4

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (s *Store) Has(key string) bool {
return true
}

func (s *Store) Clear() error {
return os.RemoveAll(s.Root)
}

func (s *Store) Delete(key string) error {
pathKey := s.PathTransformFunc(key)

Expand All @@ -106,6 +110,10 @@ func (s *Store) Delete(key string) error {
return os.RemoveAll(firstPathNameWithRoot)
}

func (s *Store) Write(key string, r io.Reader) error {
return s.writeStream(key, r)
}

func (s *Store) Read(key string) (io.Reader, error) {
f, err := s.readStream(key)
if err != nil {
Expand Down
77 changes: 50 additions & 27 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,63 @@ func TestPathTransformFunc(t *testing.T) {
}

func TestStore(t *testing.T) {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
s := NewStore(opts)
key := "niceFolder"
data := []byte("a lot of data")
s := newStore()

// Write test
if err := s.writeStream(key, bytes.NewReader(data)); err != nil {
t.Error(err)
}
fmt.Println("write test passed ✅")
defer teardown(t, s)

// Has test
if ok := s.Has(key); !ok {
t.Errorf("expected to have key %s", key)
}
fmt.Println("has test passed ✅")
for i := 0; i < 50; i++ {
fmt.Printf("running test %d 🚀\n", i)

// Read test
r, err := s.Read(key)
if err != nil {
t.Error(err)
key := fmt.Sprintf("test_%d", i)
data := []byte("a lot of data")

// Write test
if err := s.Write(key, bytes.NewReader(data)); err != nil {
t.Error(err)
}
fmt.Println("write test passed ✅")

// Has test 1
if ok := s.Has(key); !ok {
t.Errorf("expected to have key %s", key)
}
fmt.Println("has_exists test passed ✅")

// Read test
r, err := s.Read(key)
if err != nil {
t.Error(err)
}
fmt.Println("read test passed ✅")

b, _ := io.ReadAll(r)
if string(b) != string(data) {
t.Errorf("want %s have %s", data, b)
}

// Delete test
if err := s.Delete(key); err != nil {
t.Error(err)
}
fmt.Println("delete test passed ✅")

// Has test 2
if ok := s.Has(key); ok {
t.Errorf("expected to NOT have key %s", key)
}
fmt.Println("has_no_file test passed ✅")
}
fmt.Println("read test passed ✅")
}

b, _ := io.ReadAll(r)
if string(b) != string(data) {
t.Errorf("want %s have %s", data, b)
func newStore() *Store {
opts := StoreOpts{
PathTransformFunc: CASPathTransformFunc,
}
return NewStore(opts)
}

// Delete test
if err := s.Delete(key); err != nil {
func teardown(t *testing.T, s *Store) {
if err := s.Clear(); err != nil {
t.Error(err)
}
fmt.Println("delete test passed ✅")
}