def test_json(self): p1 = Page(bytes("abc", "utf-8"), "jpg") pj = p1.to_json() p2 = Page.from_json(pj) self.assertEqual(p2.to_json(), pj) self.assertIsInstance(p2.raw_data, bytes) self.assertEqual(p1, p2)
def test_json(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") c1 = Chapter("CH1", [p1, p2]) cj = c1.to_json() c2 = Chapter.from_json(cj) self.assertEqual(c2.to_json(), cj) self.assertEqual(c1, c2)
def test_chapter_add_no_work(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") p3 = Page(bytes("zyx", "utf-8"), "png") p4 = Page(bytes("xyz", "utf-8"), "jpg") p5 = Page(bytes("zzz", "utf-8"), "jpg") chapter_title = "Only Chapter" c1 = Chapter(chapter_title, [p1, p2, p3, p4, p5]) title = "Title" author = "Author" with self.assertRaises(StorageError): self.storage.add_chapters(title, author, [c1])
def test_json(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") p3 = Page(bytes("xyz", "utf-8"), "png") p4 = Page(bytes("zyx", "utf-8"), "jpg") c1 = Chapter("CH1", [p1, p2]) c2 = Chapter("CH2", [p3, p4]) w1 = Work("Title", "Author", [c1, c2]) wj = w1.to_json() w2 = Work.from_json(wj) self.assertEqual(w2.to_json(), wj) self.assertEqual(w1, w2)
def test_add_duplicate_work(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") p3 = Page(bytes("zyx", "utf-8"), "png") p4 = Page(bytes("xyz", "utf-8"), "jpg") p5 = Page(bytes("zzz", "utf-8"), "jpg") c1 = Chapter("CH1", [p1, p2]) c2 = Chapter("CH2", [p3, p4, p5]) title = "Title" author = "Author" w1 = Work(title, author, [c1, c2]) self.storage.add_works([w1]) with self.assertRaises(StorageError): self.storage.add_works([w1])
def test_work_add_get(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") p3 = Page(bytes("zyx", "utf-8"), "png") p4 = Page(bytes("xyz", "utf-8"), "jpg") p5 = Page(bytes("zzz", "utf-8"), "jpg") c1 = Chapter("CH1", [p1, p2]) c2 = Chapter("CH2", [p3, p4, p5]) title = "Title" author = "Author" w1 = Work(title, author, [c1, c2]) self.storage.add_works([w1]) w2 = self.storage.get_work(title, author) self.assertEqual(w1, w2)
def test_chapter_add_get(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") p3 = Page(bytes("zyx", "utf-8"), "png") p4 = Page(bytes("xyz", "utf-8"), "jpg") p5 = Page(bytes("zzz", "utf-8"), "jpg") chapter_title = "Only Chapter" c1 = Chapter(chapter_title, [p1, p2, p3, p4, p5]) title = "Title" author = "Author" self.storage.new_work(title, author) self.storage.add_chapters(title, author, [c1]) c2 = self.storage.get_chapter(title, author, chapter_title) self.assertEqual(c1, c2)
def test_add_chapter_to_work_twice(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") p3 = Page(bytes("zyx", "utf-8"), "png") p4 = Page(bytes("xyz", "utf-8"), "jpg") p5 = Page(bytes("zzz", "utf-8"), "jpg") c1 = Chapter("ZZZ", [p1, p2]) c2 = Chapter("AAA", [p3, p4, p5]) title = "Title" author = "Author" w1 = Work(title, author, [c1, c2]) self.storage.new_work(title, author) self.storage.add_chapters(title, author, [c1]) self.storage.add_chapters(title, author, [c2]) w2 = self.storage.get_work(title, author) self.assertEqual(w1, w2)
def test_populate_get_catalog(self): p1 = Page(bytes("abc", "utf-8"), "jpg") p2 = Page(bytes("cba", "utf-8"), "png") p3 = Page(bytes("zyx", "utf-8"), "png") p4 = Page(bytes("xyz", "utf-8"), "jpg") p5 = Page(bytes("zzz", "utf-8"), "jpg") c1 = Chapter("CH1", [p1, p2]) c2 = Chapter("CH2", [p3, p4, p5]) title = "Title" author = "Author" w1 = Work(title, author, [c1, c2]) self.storage.add_works([w1]) cc1 = CChapter("CH1", 2) cc2 = CChapter("CH2", 3) cw1 = CWork(title, author, [cc1, cc2]) catalog = Catalog([cw1]) self.assertEqual(catalog, self.storage.get_catalog())
def get_chapter(self, title: str, author: str, chapter_title: str) -> Optional[Chapter]: if not self.contains_chapter(title, author, chapter_title): return None chapter_path = self._root_path.joinpath(author, title, chapter_title) pages = [] entries = list(self._scandir(chapter_path)) entries.sort(key=lambda x: x.name) for entry in entries: with open(entry, "rb") as f: raw_data = f.read() extension = Path(entry.path).suffix[1:] pages.append(Page(raw_data, extension)) return Chapter(chapter_title, pages)
def get_chapter(self, title: str, author: str, chapter_title: str) -> Optional[Chapter]: if not self.contains_chapter(title, author, chapter_title): return None conn = sqlite3.connect(self._connection_string) conn.row_factory = sqlite3.Row pages = [] query = ("SELECT *" " FROM WorksView" " WHERE title=? AND author=? AND chaptertitle=?" " ORDER BY pageindex ASC") for row in conn.execute(query, (title, author, chapter_title)): pages.append(Page(row["rawdata"], row["extension"])) conn.close() return Chapter(chapter_title, pages)
def get_work(self, title: str, author: str) -> Optional[Work]: if not self.contains_work(title, author): return None conn = sqlite3.connect(self._connection_string) conn.row_factory = sqlite3.Row current_chaptertitle = None chapters = [] query = ("SELECT *" " FROM WorksView" " WHERE title=? AND author=?" " ORDER BY chapterindex ASC, pageindex ASC") for row in conn.execute(query, (title, author)): if row["chaptertitle"] != current_chaptertitle: if current_chaptertitle is not None: chapters.append(Chapter(current_chaptertitle, pages)) current_chaptertitle = row["chaptertitle"] pages = [] pages.append(Page(row["rawdata"], row["extension"])) chapters.append(Chapter(current_chaptertitle, pages)) conn.close() return Work(title, author, chapters)