update_test.go 2.66 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6
package liber

import (
	"io/ioutil"
	"os"
	"path/filepath"
7
	"strings"
ale's avatar
ale committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
	"testing"

	"git.autistici.org/ale/liber/util"
)

func createTestFs(fs map[string]string) string {
	base, _ := ioutil.TempDir("", "test-fs-")
	for path, contents := range fs {
		path = filepath.Join(base, path)
		os.MkdirAll(filepath.Dir(path), 0700)
		ioutil.WriteFile(path, []byte(contents), 0700)
	}
	return base
}

23 24
func checkDbPathIntegrity(t *testing.T, db *Database) {
	// Files should have relative paths.
25 26
	iter := db.Scan(FileBucket)
	for iter.Next() {
27
		var f File
28
		if err := iter.Value(&f); err != nil {
29 30 31 32 33 34
			t.Fatal(err)
		}
		if strings.HasPrefix(f.Path, "/") {
			t.Errorf("file has absolute path: %v", f.Path)
		}
	}
35 36 37
	if err := iter.Close(); err != nil {
		t.Fatalf("Scan(FileBucket) error: %v", err)
	}
38

39
	// Book cover images should have relative paths.
40 41
	iter = db.Scan(BookBucket)
	for iter.Next() {
42
		var b Book
43
		if err := iter.Value(&b); err != nil {
44 45 46 47 48 49
			t.Fatal(err)
		}
		if b.CoverPath != "" && strings.HasPrefix(b.CoverPath, "/") {
			t.Errorf("file has absolute path: %v", b.CoverPath)
		}
	}
50 51 52
	if err := iter.Close(); err != nil {
		t.Fatalf("Scan(BookBucket) error: %v", err)
	}
53 54
}

ale's avatar
ale committed
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
func TestDatabase_Update(t *testing.T) {
	util.WalkerDefaultMinSize = 0

	td, db := newTestDatabase(t)
	defer td.Close()

	// Make the test book a pdf so we don't attempt to parse it.
	tmpdir := createTestFs(map[string]string{
		"book/Test Ebook.pdf": "foo",
		"book/metadata.opf":   testOpf,
		"book/cover.jpg":      "jpeg",
	})
	defer os.RemoveAll(tmpdir)

	chooserCalled := false
	chooser := func(path string, choices []*Metadata) *Metadata {
		chooserCalled = true
		return nil
	}

	testDb := func(tag string) {
		// The test ebook added in newTestDatabase should not be there
		// any more.
		if _, err := db.GetBook(td.refbookid); err == nil {
			t.Errorf("%s: test book still in database", tag)
		}
81

ale's avatar
ale committed
82 83 84 85 86 87
		// Test OPF ebook should have been found by Update.
		if result, err := db.Search("isbn:9781939293015", 0, 1); err != nil || result.NumResults != 1 {
			t.Errorf("%s: new book not found in database", tag)
		}
	}

88
	// The second update should do nothing.
ale's avatar
ale committed
89 90 91 92
	db.Update(tmpdir, chooser)
	testDb("first update")
	db.Update(tmpdir, chooser)
	testDb("second update")
93

94 95 96 97
	if chooserCalled {
		t.Errorf("chooser function was called")
	}

98 99 100 101 102
	// Check that the test file is there.
	if _, err := db.GetFile("book/Test Ebook.pdf"); err != nil {
		t.Errorf("test file is not in the database")
	}

103 104
	checkDbPathIntegrity(t, db)
}
105

106 107 108 109 110 111 112 113 114 115
func TestDatabase_UpdateEpub(t *testing.T) {
	util.WalkerDefaultMinSize = 0

	td, db := newTestDatabase(t)
	defer td.Close()

	// Read the test epub from testdata/.
	db.Update("testdata", nil)

	checkDbPathIntegrity(t, db)
ale's avatar
ale committed
116
}