Skip to content

Commit

Permalink
Added tests for kepub conversion and packing
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Jul 28, 2017
1 parent 77ddd91 commit a7b28c3
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 0 deletions.
21 changes: 21 additions & 0 deletions kepub/epub_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package kepub

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,3 +19,20 @@ func TestIsDir(t *testing.T) {
assert.True(t, isDir("."), ". should be a dir")
assert.False(t, isDir("./epub_test.go"), "./epub_test.go should not be a dir")
}

func TestPackUnpack(t *testing.T) {
td, err := ioutil.TempDir("", "kepubify")
if err != nil {
assert.FailNow(t, fmt.Sprintf("%v", err), "Could not create temp dir")
}
defer os.RemoveAll(td)

assert.Nil(t, PackEPUB(filepath.Join("testdata", "books", "test1"), filepath.Join(td, "test1.epub"), true), "packepub should not return an error")
assert.True(t, exists(filepath.Join(td, "test1.epub")), "output epub should exist")

assert.Nil(t, UnpackEPUB(filepath.Join(td, "test1.epub"), filepath.Join(td, "test1"), true), "unpackepub should not return an error")
assert.True(t, exists(filepath.Join(td, "test1")), "output dir should exist")
assert.True(t, exists(filepath.Join(td, "test1", "META-INF", "container.xml")), "META-INF/container.xml should exist in output dir")

assert.NotNil(t, PackEPUB(filepath.Join("testdata", "books", "invalid"), filepath.Join(td, "invalid.epub"), true), "packepub should return an error for an epub withot a container.xml")
}
35 changes: 35 additions & 0 deletions kepub/kepub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kepub

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestKepubify(t *testing.T) {
td, err := ioutil.TempDir("", "kepubify")
if err != nil {
assert.FailNow(t, fmt.Sprintf("%v", err), "Could not create temp dir")
}
defer os.RemoveAll(td)

wd, err := os.Getwd()
if err != nil {
assert.FailNow(t, fmt.Sprintf("%v", err), "Could not get current dir")
}

kepub := filepath.Join(td, "test1.kepub.epub")
kepubunp := filepath.Join(td, "test1.kepub.epub_unpacked")

err = Kepubify(filepath.Join(wd, "testdata", "books", "test1.epub"), kepub, false)
assert.Nil(t, err, "should not error when converting book")
assert.True(t, exists(kepub), "converted kepub should exist")

assert.Nil(t, UnpackEPUB(kepub, kepubunp, true), "should not error when unpacking converted kepub")
assert.True(t, exists(kepubunp), "unpacked kepub dir should exist")
assert.True(t, exists(filepath.Join(kepubunp, "META-INF", "container.xml")), "kepub should have a container.xml")
}
1 change: 1 addition & 0 deletions kepub/testdata/books/invalid/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gitkeep
Binary file added kepub/testdata/books/test1.epub
Binary file not shown.
7 changes: 7 additions & 0 deletions kepub/testdata/books/test1/META-INF/container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>

25 changes: 25 additions & 0 deletions kepub/testdata/books/test1/content.opf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="uuid_id">
<metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:publisher>Patrick G</dc:publisher>
<dc:description>&lt;p&gt;This is a test book for &lt;i&gt;kepubify&lt;/i&gt;.&lt;/p&gt;</dc:description>
<meta name="calibre:series" content="Test Series"/>
<meta name="calibre:series_index" content="1"/>
<dc:language>en</dc:language>
<dc:creator opf:role="aut">Patrick G</dc:creator>
<dc:title>epubtool Test Book 1</dc:title>
<meta name="cover" content="cover"/>
<dc:date>2017-07-26T14:00:00+00:00</dc:date>
<dc:identifier id="uuid_id" opf:scheme="uuid">cf8fd6fa-3998-4e25-bfc0-8e9b529f8556</dc:identifier>
</metadata>
<manifest>
<item href="cover.jpeg" id="cover" media-type="image/jpeg"/>
<item href="title.html" id="p0" media-type="application/xhtml+xml"/>
<item href="text01.html" id="p1" media-type="application/xhtml+xml"/>
<item href="toc.ncx" media-type="application/x-dtbncx+xml" id="ncx"/>
</manifest>
<spine toc="ncx">
<itemref idref="p0"/>
<itemref idref="p1"/>
</spine>
</package>
Binary file added kepub/testdata/books/test1/cover.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions kepub/testdata/books/test1/mimetype
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip
20 changes: 20 additions & 0 deletions kepub/testdata/books/test1/text01.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>kepubify Test Book 1</title>
<meta content="http://www.w3.org/1999/xhtml; charset=utf-8" http-equiv="Content-Type"/>
<style type="text/css">
@page { margin-bottom: 5.000000pt; margin-top: 5.000000pt; }
</style>
</head>
<body id="p1">
<div>
<p>
This is the text of a kepubify test book. A symbol: &gt;
</p>
<p>
Lorem ipsum dolor <i>sit</i> amet, consectetur <b><i>adipisicing </i>elit</b>. Molestias reprehenderit sit quaerat dicta doloremque velit impedit, quod minus aliquam fugit quia, pariatur repellat explicabo itaque <b>molestiae. Fugiat </b>consectetur, <a href="title.html">asperiores</a> debitis!
</p>
</div>
</body>
</html>
19 changes: 19 additions & 0 deletions kepub/testdata/books/test1/title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="calibre:cover" content="true"/>
<title>Cover</title>
<style type="text/css" title="override_css">
@page {padding: 0pt; margin:0pt}
body { text-align: center; padding:0pt; margin: 0pt; }
</style>
</head>
<body id="p0">
<div>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 322 500" preserveAspectRatio="none">
<image width="322" height="500" xlink:href="cover.jpeg"/>
</svg>
</div>
</body>
</html>
26 changes: 26 additions & 0 deletions kepub/testdata/books/test1/toc.ncx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version='1.0' encoding='utf-8'?>
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="en">
<head>
<meta content="a22ae125-8044-432d-8b09-98192d0e357c" name="dtb:uid"/>
<meta content="2" name="dtb:depth"/>
<meta content="0" name="dtb:totalPageCount"/>
<meta content="0" name="dtb:maxPageNumber"/>
</head>
<docTitle>
<text>kepubify Test Book 1</text>
</docTitle>
<navMap>
<navPoint class="chapter" id="p0" playOrder="1">
<navLabel>
<text>Cover</text>
</navLabel>
<content src="title.html"/>
</navPoint>
<navPoint class="chapter" id="p1" playOrder="2">
<navLabel>
<text>Chapter 1</text>
</navLabel>
<content src="text01.html"/>
</navPoint>
</navMap>
</ncx>

0 comments on commit a7b28c3

Please sign in to comment.