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

Commit

Permalink
Split TestCopier and TestMover (#42)
Browse files Browse the repository at this point in the history
* Split TestCopier and TestMover

* Remove direr check

* Modify type assertion

Co-authored-by: Xuanwo <[email protected]>

* Modify type assertion

Co-authored-by: Xuanwo <[email protected]>
  • Loading branch information
JinnyYi and Xuanwo authored Jul 26, 2021
1 parent 19c99c2 commit aaff270
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 67 deletions.
154 changes: 117 additions & 37 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/beyondstorage/go-integration-test/v4
go 1.15

require (
github.com/beyondstorage/go-storage/v4 v4.3.2
github.com/beyondstorage/go-storage/v4 v4.4.0
github.com/google/uuid v1.3.0
github.com/smartystreets/goconvey v1.6.4
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/Xuanwo/templateutils v0.1.0/go.mod h1:OdE0DJ+CJxDBq6psX5DPV+gOZi8bhuHuVUpPCG++Wb8=
github.com/beyondstorage/go-storage/v4 v4.3.2 h1:F3xQVTcBJ+nSzEt/wBZCx4pE7aYhy43+tn1mwMYCacE=
github.com/beyondstorage/go-storage/v4 v4.3.2/go.mod h1:8FHjTUFuwLl/mmIGpOL9g3RTZPyye7vneFN/JkRj5Tg=
github.com/beyondstorage/go-storage/v4 v4.4.0 h1:sWURraKFjNR4qpwthr45cAGOIx6EOLrrJcz6su4Je30=
github.com/beyondstorage/go-storage/v4 v4.4.0/go.mod h1:mc9VzBImjXDg1/1sLfta2MJH79elfM6m47ZZvZ+q/Uw=
github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU=
github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ=
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
Expand All @@ -11,7 +11,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/google/pprof v0.0.0-20181127221834-b4f47329b966/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
Expand Down
133 changes: 107 additions & 26 deletions mover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}
})
})
}

0 comments on commit aaff270

Please sign in to comment.