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

Commit

Permalink
test: Add read with offset and size test case (#63)
Browse files Browse the repository at this point in the history
* test: Add read with offset and size test case

* Add test cases for offset only and size only
  • Loading branch information
JinnyYi authored Sep 29, 2021
1 parent b6fc390 commit 9456c45
Showing 1 changed file with 75 additions and 0 deletions.
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

0 comments on commit 9456c45

Please sign in to comment.