-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock.go
60 lines (48 loc) · 1.27 KB
/
mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cs
import (
"bytes"
"context"
"io"
"github.com/m-mizutani/goerr"
"github.com/m-mizutani/hatchery/pkg/domain/interfaces"
"github.com/m-mizutani/hatchery/pkg/domain/model"
)
type Mock struct {
NewObjectWriterFn func(ctx context.Context, bucket model.CSBucket, object model.CSObjectName) io.WriteCloser
Results []*MockResult
}
var _ interfaces.CloudStorage = &Mock{}
type MockResult struct {
Body Writer
Bucket model.CSBucket
Object model.CSObjectName
}
type Writer struct {
bytes.Buffer
Closed bool
}
func (x *Writer) Close() error {
x.Closed = true
return nil
}
func NewMock() *Mock {
return &Mock{}
}
func (x *Mock) NewObjectWriter(ctx context.Context, bucket model.CSBucket, object model.CSObjectName) io.WriteCloser {
if x.NewObjectWriterFn != nil {
return x.NewObjectWriterFn(ctx, bucket, object)
}
var result MockResult
x.Results = append(x.Results, &result)
result.Bucket = bucket
result.Object = object
return &result.Body
}
func (x *Mock) NewObjectReader(ctx context.Context, bucket model.CSBucket, object model.CSObjectName) (io.ReadCloser, error) {
for _, r := range x.Results {
if r.Bucket == bucket && r.Object == object {
return io.NopCloser(bytes.NewReader(r.Body.Bytes())), nil
}
}
return nil, goerr.New("not found")
}