-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_test.go
66 lines (51 loc) · 904 Bytes
/
book_test.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
package notes
import (
"bytes"
"fmt"
"os"
"testing"
)
func Test_Book_Backup_Restore(t *testing.T) {
t.Parallel()
b1 := &Book{}
if b1.Len() != 0 {
t.Fatalf("book should be empty")
}
const N = 5
for i := 0; i < N; i++ {
note := &Note{
Body: "test!",
}
err := b1.Insert(note)
if err != nil {
t.Fatal(err)
}
}
if b1.Len() != N {
t.Fatalf("expected %d, got %d", N, b1.Len())
}
bb := &bytes.Buffer{}
err := b1.Backup(bb)
if err != nil {
t.Fatal(err)
}
act := bb.Bytes()
b2 := &Book{
Logger: os.Stdout,
}
err = b2.Restore(bytes.NewReader(act))
if err != nil {
t.Fatal(err)
}
if b2.Len() != N {
t.Fatalf("expected %d, got %d", N, b2.Len())
}
an := fmt.Sprint(b1.notes)
en := fmt.Sprint(b2.notes)
if an != en {
t.Fatalf("expected %s, got %s", an, en)
}
if b1.curID != b2.curID {
t.Fatalf("expected %d, got %d", b1.curID, b2.curID)
}
}