Newer
Older
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
package liber
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
)
func newTestHttpServer(db *Database, updir string) *httptest.Server {
localsrv := &syncServer{db, &FileStorage{Root: updir, Nesting: 2}}
mux := http.NewServeMux()
mux.HandleFunc("/api/sync/upload", localsrv.handleSyncUpload)
mux.HandleFunc("/api/sync/diff", localsrv.handleDiffRequest)
return httptest.NewServer(mux)
}
func TestSync_Sync(t *testing.T) {
// Actually create a file to upload, or the sync will fail.
f, _ := ioutil.TempFile("", "ebook-")
io.WriteString(f, "foo\n")
f.Close()
// Create a temporary directory to store uploads.
updir, _ := ioutil.TempDir("", "ebook-upload-")
defer os.RemoveAll(updir)
td, db := newTestDatabase(t)
defer td.Close()
td2, db2 := newTestDatabase(t)
defer td2.Close()
for i := 0; i < 10; i++ {
db.PutBook(&Book{
Id: NewID(),
Path: f.Name(),
FileType: ".epub",
Metadata: &Metadata{
Title: fmt.Sprintf("Book #%d", i+1),
Creator: []string{"Random Author"},
ISBN: []string{strconv.Itoa(i + 1)},
},
})
}
// Run a sync from db to db2.
srv := newTestHttpServer(db2, updir)
defer srv.Close()
cl := NewRemoteServer(srv.URL)
err := db.Sync(cl)
if err != nil {
t.Fatalf("Sync(): %v", err)
}
// Ensure that books are present in db2.
for i := 0; i < 10; i++ {
metatmpl := &Metadata{
ISBN: []string{strconv.Itoa(i + 1)},
}
if _, err := db2.Find(metatmpl); err != nil {
t.Errorf("Book %d missing from db2: %v", i+1, err)
}
}
}