Skip to content
Snippets Groups Projects
update_test.go 1.38 KiB
Newer Older
ale's avatar
ale committed
package liber

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"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
}

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)
		}
		// 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)
		}
	}

	db.Update(tmpdir, chooser)
	testDb("first update")
	db.Update(tmpdir, chooser)
	testDb("second update")
}