From d62cb3a3c26dfe1a852cc55e01365c0967dd9810 Mon Sep 17 00:00:00 2001 From: abyss-w Date: Mon, 26 Jul 2021 16:58:18 +0800 Subject: [PATCH] fix:Reslove conflicts --- copier.go | 154 +++++++++++++++++++++++++++++++++++++++++------------- mover.go | 133 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 224 insertions(+), 63 deletions(-) diff --git a/copier.go b/copier.go index 06af0ed..0fd814e 100644 --- a/copier.go +++ b/copier.go @@ -123,43 +123,123 @@ func TestCopier(t *testing.T, store types.Storager) { }) }) }) + }) +} + +func TestCopierWithDir(t *testing.T, store types.Storager) { + Convey("Given a basic Storager", t, func() { + c, ok := store.(types.Copier) + So(ok, ShouldBeTrue) + + d := store.(types.Direr) + + Convey("When Copy to an existing dir", func() { + srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + r := io.LimitReader(randbytes.NewRand(), srcSize) + src := uuid.New().String() + + _, err := store.Write(src, r, srcSize) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(src) + if err != nil { + t.Error(err) + } + }() + + dst := uuid.New().String() + _, err = d.CreateDir(dst) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir)) + if err != nil { + t.Error(err) + } + }() + + err = c.Copy(src, dst) + Convey("The error should be ErrObjectModeInvalid", func() { + So(errors.Is(err, services.ErrObjectModeInvalid), ShouldBeTrue) + }) + }) + }) +} + +func TestCopierWithVirtualDir(t *testing.T, store types.Storager) { + Convey("Given a basic Storager", t, func() { + c, ok := store.(types.Copier) + So(ok, ShouldBeTrue) + + d := store.(types.Direr) + + Convey("When Copy to an existing dir", func() { + srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + r := io.LimitReader(randbytes.NewRand(), srcSize) + src := uuid.New().String() + + _, err := store.Write(src, r, srcSize) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(src) + if err != nil { + t.Error(err) + } + }() + + dst := uuid.New().String() + _, err = d.CreateDir(dst) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir)) + if err != nil { + t.Error(err) + } + }() - if d, ok := store.(types.Direr); ok { - Convey("When Copy to an existing dir", func() { - srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB - r := io.LimitReader(randbytes.NewRand(), srcSize) - src := uuid.New().String() - - _, err := store.Write(src, r, srcSize) - if err != nil { - t.Fatal(err) - } - - defer func() { - err = store.Delete(src) - if err != nil { - t.Error(err) - } - }() - - dst := uuid.New().String() - _, err = d.CreateDir(dst) - if err != nil { - t.Fatal(err) - } - - defer func() { - err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir)) - if err != nil { - t.Error(err) - } - }() - - err = c.Copy(src, dst) - Convey("The error should be ErrObjectModeInvalid", func() { - So(errors.Is(err, services.ErrObjectModeInvalid), ShouldBeTrue) - }) - }) - } + err = c.Copy(src, dst) + + defer func() { + err = store.Delete(dst) + if err != nil { + t.Error(err) + } + }() + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("Stat should get dst object without error", func() { + o, err := store.Stat(dst) + + So(err, ShouldBeNil) + So(o, ShouldNotBeNil) + + Convey("The Object Mode should be read", func() { + So(o.Mode.IsRead(), ShouldBeTrue) + }) + + Convey("The path and size should be match", func() { + So(o, ShouldNotBeNil) + So(o.Path, ShouldEqual, dst) + + osize, ok := o.GetContentLength() + So(ok, ShouldBeTrue) + So(osize, ShouldEqual, srcSize) + }) + }) + }) }) } diff --git a/mover.go b/mover.go index b380e76..10d886a 100644 --- a/mover.go +++ b/mover.go @@ -139,44 +139,125 @@ func TestMover(t *testing.T, store types.Storager) { }) }) }) + }) +} + +func TestMoverWithDir(t *testing.T, store types.Storager) { + Convey("Given a basic Storager", t, func() { + m, ok := store.(types.Mover) + So(ok, ShouldBeTrue) + + d := store.(types.Direr) + + Convey("When Move to an existing dir", func() { + + srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + r := io.LimitReader(randbytes.NewRand(), srcSize) + src := uuid.New().String() + + _, err := store.Write(src, r, srcSize) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(src) + if err != nil { + t.Error(err) + } + }() + + dst := uuid.New().String() + _, err = d.CreateDir(dst) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir)) + if err != nil { + t.Error(err) + } + }() + + err = m.Move(src, dst) + Convey("The error should be ErrObjectModeInvalid", func() { + So(errors.Is(err, services.ErrObjectModeInvalid), ShouldBeTrue) + }) + }) + }) +} + +func TestMoverWithVirtualDir(t *testing.T, store types.Storager) { + Convey("Given a basic Storager", t, func() { + m, ok := store.(types.Mover) + So(ok, ShouldBeTrue) + + d := store.(types.Direr) + + Convey("When Move to an existing dir", func() { + + srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + r := io.LimitReader(randbytes.NewRand(), srcSize) + src := uuid.New().String() + + _, err := store.Write(src, r, srcSize) + if err != nil { + t.Fatal(err) + } - if d, ok := store.(types.Direr); ok { - Convey("When Move to an existing dir", func() { + defer func() { + err = store.Delete(src) + if err != nil { + t.Error(err) + } + }() - srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB - r := io.LimitReader(randbytes.NewRand(), srcSize) - src := uuid.New().String() + dst := uuid.New().String() + _, err = d.CreateDir(dst) + if err != nil { + t.Fatal(err) + } - _, err := store.Write(src, r, srcSize) + defer func() { + err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir)) if err != nil { - t.Fatal(err) + t.Error(err) } + }() - defer func() { - err = store.Delete(src) - if err != nil { - t.Error(err) - } - }() + err = m.Move(src, dst) - dst := uuid.New().String() - _, err = d.CreateDir(dst) + defer func() { + err = store.Delete(dst) if err != nil { - t.Fatal(err) + t.Error(err) } + }() - defer func() { - err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir)) - if err != nil { - t.Error(err) - } - }() + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("Stat should get dst object without error", func() { + o, err := store.Stat(dst) + + So(err, ShouldBeNil) + So(o, ShouldNotBeNil) - err = m.Move(src, dst) - Convey("The error should be ErrObjectModeInvalid", func() { - So(errors.Is(err, services.ErrObjectModeInvalid), ShouldBeTrue) + Convey("The Object Mode should be read", func() { + So(o.Mode.IsRead(), ShouldBeTrue) + }) + + Convey("The path and size should be match", func() { + So(o, ShouldNotBeNil) + So(o.Path, ShouldEqual, dst) + + osize, ok := o.GetContentLength() + So(ok, ShouldBeTrue) + So(osize, ShouldEqual, srcSize) }) }) - } + }) }) }