-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.go
118 lines (92 loc) · 2.42 KB
/
tests.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package tests
import (
"bufio"
"bytes"
"errors"
"io/ioutil"
"os"
"github.com/hyperboloide/pipe"
"github.com/hyperboloide/pipe/encoders"
"github.com/hyperboloide/pipe/rw"
)
func TestReadWriteDeleter(rwd rw.ReadWriteDeleter, id, file string) error {
if err := rwd.Start(); err != nil {
return err
}
w, err := rwd.NewWriter(id)
if err != nil {
return err
}
reader, err := os.Open(file)
if err != nil {
return err
}
defer reader.Close()
if err := pipe.New(reader).ToCloser(w).Exec(); err != nil {
return err
}
r, err := rwd.NewReader(id)
if err != nil {
return err
}
var result bytes.Buffer
writer := bufio.NewWriter(&result)
if err := pipe.New(r).To(writer).Exec(); err != nil {
return err
}
bin, err := ioutil.ReadFile(file)
if err != nil {
return err
}
if bytes.Equal(result.Bytes(), bin) == false {
return errors.New("file content do not match original.")
}
if err := rwd.Delete(id); err != nil {
return err
}
if _, err := rwd.NewReader(id); err == nil {
return errors.New("object not deleted.")
}
return nil
}
func TestEncoderDecoder(ed encoders.EncoderDecoder, file string) error {
var err error
var encoded bytes.Buffer
encodedWriter := bufio.NewWriter(&encoded)
var decoded bytes.Buffer
decodedWriter := bufio.NewWriter(&decoded)
var originalReader *os.File
var originalByte []byte
// Should start without errors
if err := ed.Start(); err != nil {
return err
}
// should open the test file and return a reader
if originalReader, err = os.Open(file); err != nil {
return err
} else {
defer originalReader.Close()
}
// should open the test file and return the bytes
if originalByte, err = ioutil.ReadFile("../../tests/test.jpg"); err != nil {
return err
}
// should encode test file reader
if err := pipe.New(originalReader).Push(ed.Encode).To(encodedWriter).Exec(); err != nil {
return err
}
// the encoded bytes should not match the original
if bytes.Equal(encoded.Bytes(), originalByte) == true {
return errors.New("the encoded bytes should not match the original")
}
encodedReader := bytes.NewReader(encoded.Bytes())
// should decoded the encoded writer
if err := pipe.New(encodedReader).Push(ed.Decode).To(decodedWriter).Exec(); err != nil {
return err
}
// the decoded bytes should match the original
if bytes.Equal(decoded.Bytes(), originalByte) == false {
return errors.New("the decoded bytes should match the original")
}
return nil
}