From b16bcc4a44fda27a7b90e74c1cf7bfacd3fc716c Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Wed, 29 Sep 2021 10:19:46 +0800 Subject: [PATCH 1/2] test: Add read with offset and size test case --- storager.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/storager.go b/storager.go index bffe108..163a799 100644 --- a/storager.go +++ b/storager.go @@ -77,6 +77,43 @@ func TestStorager(t *testing.T, store types.Storager) { }) }) + Convey("When Read a file with offset and 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) + } + }() + + 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) From 4832711ce6441a96fd992502a0b334cdd4c70f37 Mon Sep 17 00:00:00 2001 From: JinnyYi Date: Wed, 29 Sep 2021 11:02:59 +0800 Subject: [PATCH 2/2] Add test cases for offset only and size only --- storager.go | 60 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/storager.go b/storager.go index 163a799..f651382 100644 --- a/storager.go +++ b/storager.go @@ -77,7 +77,7 @@ func TestStorager(t *testing.T, store types.Storager) { }) }) - Convey("When Read a file with offset and size", func() { + 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 { @@ -96,21 +96,59 @@ func TestStorager(t *testing.T, store types.Storager) { } }() - offset := rand.Int63n(size) - len := rand.Int63n(size - offset) + Convey("When Read with offset", func() { + offset := rand.Int63n(size) - var buf bytes.Buffer - n, err := store.Read(path, &buf, ps.WithOffset(offset), ps.WithSize(len)) + 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 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("The content should be match", func() { - So(buf, ShouldNotBeNil) + Convey("When Read with size", func() { + len := rand.Int63n(size) - So(n, ShouldEqual, len) - So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content[offset:offset+len])) + 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])) + }) }) })