Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

test: Add read with offset and size test case #63

Merged
merged 2 commits into from
Sep 29, 2021
Merged
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
75 changes: 75 additions & 0 deletions storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,81 @@ func TestStorager(t *testing.T, store types.Storager) {
})
})

Convey("When Read a file with offset or size", func() {
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}

path := uuid.New().String()
_, err = store.Write(path, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

Convey("When Read with offset", func() {
offset := rand.Int63n(size)

var buf bytes.Buffer
n, err := store.Read(path, &buf, ps.WithOffset(offset))

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})

Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)

So(n, ShouldEqual, size-offset)
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content[offset:]))
})
})

Convey("When Read with size", func() {
len := rand.Int63n(size)

var buf bytes.Buffer
n, err := store.Read(path, &buf, ps.WithSize(len))

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})

Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)

So(n, ShouldEqual, len)
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content[:len]))
})
})

Convey("When Read with offset and size", func() {
offset := rand.Int63n(size)
len := rand.Int63n(size - offset)

var buf bytes.Buffer
n, err := store.Read(path, &buf, ps.WithOffset(offset), ps.WithSize(len))

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})

Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)

So(n, ShouldEqual, len)
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content[offset:offset+len]))
})
})
})

Convey("When Write a file", func() {
firstSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), firstSize)
Expand Down