| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2025, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
import io |
| 5 |
import os |
| 6 |
import unittest |
| 7 |
|
| 8 |
from calibre.constants import iswindows |
| 9 |
from calibre.db.tests.base import BaseTest |
| 10 |
|
| 11 |
is_ci = os.environ.get('CI', '').lower() == 'true' |
| 12 |
|
| 13 |
|
| 14 |
@unittest.skipIf(is_ci and iswindows, 'these tests myseteriously fail only in CI + Windows') |
| 15 |
class PageCountTest(BaseTest): |
| 16 |
ae = BaseTest.assertEqual |
| 17 |
|
| 18 |
def test_page_count_in_db(self): |
| 19 |
test_page_count_in_db(self) |
| 20 |
|
| 21 |
def test_page_count_refreshes_file_sizes(self): |
| 22 |
test_page_count_refreshes_file_sizes(self) |
| 23 |
|
| 24 |
def test_page_count(self): |
| 25 |
from calibre.library.page_count import test_page_count |
| 26 |
|
| 27 |
test_page_count(self) |
| 28 |
|
| 29 |
def test_line_counting(self): |
| 30 |
from calibre.library.page_count import test_line_counting |
| 31 |
|
| 32 |
test_line_counting(self) |
| 33 |
|
| 34 |
|
| 35 |
def test_page_count_in_db(self: BaseTest) -> None: |
| 36 |
from calibre.db.constants import Pages |
| 37 |
from calibre.library.page_count import CHARS_PER_PAGE |
| 38 |
from calibre.utils.podofo import sample_pdf_data |
| 39 |
|
| 40 |
txt_data = ('a ' * (2 * CHARS_PER_PAGE + 10)).encode() |
| 41 |
db = self.init_cache() |
| 42 |
|
| 43 |
# test schema upgrade marked all books as needing scan |
| 44 |
def status(): |
| 45 |
return set(db.backend.execute('SELECT pages,needs_scan FROM books_pages_link')) |
| 46 |
|
| 47 |
self.ae(status(), {(0, 1)}) |
| 48 |
self.ae(db.pages_needs_scan((1, 2, 19)), {1, 2}) |
| 49 |
counted = [] |
| 50 |
db.maintain_page_counts.count_callback = counted.append |
| 51 |
db.maintain_page_counts.tick_event.clear() |
| 52 |
db.queue_pages_scan() |
| 53 |
db.maintain_page_counts.tick_event.wait() |
| 54 |
self.assertFalse(counted) |
| 55 |
self.ae(status(), {(-1, 0)}) |
| 56 |
self.ae(db.field_for('pages', 1), -1) |
| 57 |
|
| 58 |
# test that adding a format queues |
| 59 |
def add_format(fmt, data): |
| 60 |
db.maintain_page_counts.tick_event.clear() |
| 61 |
db.add_format(1, fmt, io.BytesIO(data), replace=True) |
| 62 |
db.maintain_page_counts.tick_event.wait() |
| 63 |
|
| 64 |
add_format('txt', txt_data) |
| 65 |
self.ae(status(), {(3, 0), (-1, 0)}) |
| 66 |
self.ae(1, len(counted)) |
| 67 |
p = db.get_pages(1) |
| 68 |
self.ae(p, Pages(3, p.algorithm, 'TXT', len(txt_data), p.timestamp)) |
| 69 |
self.ae(db.field_for('pages', 1), 3) |
| 70 |
self.ae(db.get_metadata(1).pages, 3) |
| 71 |
self.ae(db.get_proxy_metadata(1).pages, 3) |
| 72 |
# test that re-adding the same format does not re-count |
| 73 |
add_format('txt', txt_data) |
| 74 |
self.ae(status(), {(3, 0), (-1, 0)}) |
| 75 |
self.ae(1, len(counted)) |
| 76 |
# test that re-adding a lower priority format does not re-count |
| 77 |
add_format('epub', txt_data) |
| 78 |
self.ae(status(), {(3, 0), (-1, 0)}) |
| 79 |
self.ae(1, len(counted)) |
| 80 |
# test that adding a higher priority format does recount |
| 81 |
add_format('pdf', sample_pdf_data()) |
| 82 |
self.ae(2, len(counted)) |
| 83 |
self.assertTrue(counted[-1].endswith('.pdf')) |
| 84 |
self.ae(status(), {(1, 0), (-1, 0)}) |
| 85 |
p = db.get_pages(1) |
| 86 |
self.ae(p, Pages(1, p.algorithm, 'PDF', len(sample_pdf_data()), p.timestamp)) |
| 87 |
# test forced re-scan |
| 88 |
db.maintain_page_counts.tick_event.clear() |
| 89 |
db.queue_pages_scan(1, force=True) |
| 90 |
db.maintain_page_counts.tick_event.wait() |
| 91 |
self.ae(3, len(counted)) |
| 92 |
# test full force |
| 93 |
db.maintain_page_counts.tick_event.clear() |
| 94 |
db.queue_pages_scan(force=True) |
| 95 |
db.maintain_page_counts.tick_event.wait() |
| 96 |
self.ae(4, len(counted)) |
| 97 |
|
| 98 |
|
| 99 |
def test_page_count_refreshes_file_sizes(self: BaseTest) -> None: |
| 100 |
from calibre.library.page_count import CHARS_PER_PAGE |
| 101 |
|
| 102 |
db = self.init_cache() |
| 103 |
counted = [] |
| 104 |
db.maintain_page_counts.count_callback = counted.append |
| 105 |
|
| 106 |
def run_scan(book_id: int = 0) -> None: |
| 107 |
db.maintain_page_counts.tick_event.clear() |
| 108 |
db.queue_pages_scan(book_id) |
| 109 |
db.maintain_page_counts.tick_event.wait() |
| 110 |
|
| 111 |
txt_data = ('a ' * (2 * CHARS_PER_PAGE + 10)).encode() |
| 112 |
db.maintain_page_counts.tick_event.clear() |
| 113 |
db.add_format(1, 'TXT', io.BytesIO(txt_data), replace=True) |
| 114 |
db.maintain_page_counts.tick_event.wait() |
| 115 |
self.ae(1, len(counted)) |
| 116 |
self.ae(db.format_db_size(1, 'TXT'), len(txt_data)) |
| 117 |
self.ae(db.field_for('pages', 1), 3) |
| 118 |
|
| 119 |
# simulate an edit of the format file outside of calibre |
| 120 |
new_data = ('b ' * (4 * CHARS_PER_PAGE + 10)).encode() |
| 121 |
with open(db.format_abspath(1, 'TXT'), 'wb') as f: |
| 122 |
f.write(new_data) |
| 123 |
self.ae(db.format_db_size(1, 'TXT'), len(txt_data)) # stored size is now stale |
| 124 |
|
| 125 |
# a recount must detect the changed file and update both page count and stored size |
| 126 |
db.mark_for_pages_recount() |
| 127 |
run_scan() |
| 128 |
self.ae(2, len(counted)) |
| 129 |
self.ae(db.field_for('pages', 1), 5) |
| 130 |
self.ae(db.get_pages(1).format_size, len(new_data)) |
| 131 |
self.ae(db.format_db_size(1, 'TXT'), len(new_data)) |
| 132 |
self.ae(db.field_for('size', 1), max(db.format_db_size(1, f) for f in db.formats(1))) |
| 133 |
|
| 134 |
# unchanged books must not be re-counted and sizes must remain correct |
| 135 |
db.mark_for_pages_recount() |
| 136 |
run_scan() |
| 137 |
self.ae(2, len(counted)) |
| 138 |
self.ae(db.pages_needs_scan(), set()) |
| 139 |
self.ae(db.format_db_size(1, 'TXT'), len(new_data)) |