|
1 | | -import json |
| 1 | +import unittest |
2 | 2 |
|
3 | 3 | import fitz |
4 | | -import os |
5 | | - |
6 | | -import unittest |
7 | | -from os import listdir |
8 | | -from os.path import isfile, join |
9 | 4 |
|
10 | | -from title import generate_title |
| 5 | +import title |
11 | 6 |
|
12 | 7 | class TestGenerateTitle(unittest.TestCase): |
13 | | - # TODO: Good use for parameterized tests. |
14 | 8 | def test_prefers_metadata_title(self): |
15 | 9 | with fitz.open("./testdata/lithium-longterm.pdf") as doc: |
16 | | - self.assertEqual("Long-Term Lithium Therapy: Side Effects and Interactions", generate_title(doc)) |
| 10 | + self.assertEqual("Long-Term Lithium Therapy: Side Effects and Interactions", title.generate_title(doc)) |
17 | 11 | pass |
18 | 12 |
|
19 | 13 | def test_falls_back_to_first_sentence(self): |
20 | 14 | with fitz.open("./testdata/advancespharmaco.pdf") as doc: |
21 | 15 | expected_title = "Advances in Mood Disorder Pharmacotherapy: Evaluating New Antipsychotics and Mood Stabilizers for Bipolar Disorder and Schizophrenia" |
22 | | - self.assertEqual(expected_title, generate_title(doc)) |
| 16 | + self.assertEqual(expected_title, title.generate_title(doc)) |
23 | 17 |
|
24 | 18 | def test_australasian_psychiatry(self): |
25 | 19 | with fitz.open("./testdata/creativitystabilizer.pdf") as doc: |
26 | 20 | expected_title = "Impact of mood stabilizers on creativity" |
27 | | - self.assertEqual(expected_title, generate_title(doc)) |
28 | | - |
29 | | - def test_largest_text(self): |
30 | | - first_page_json = None |
31 | | - with open("/home/ricanontherun/Documents/balancer/uploads/output.json", "r") as f: |
32 | | - content = "" |
33 | | - for line in f: |
34 | | - content += line |
35 | | - first_page_json = json.loads(content) |
36 | | - |
37 | | - blocks = first_page_json["blocks"] |
38 | | - text_blocks = [block for block in blocks if block["type"] == 0] |
39 | | - |
40 | | - print(json.dumps(text_blocks[0], indent=4)) |
41 | | - |
42 | | - def test_remaining(self): |
43 | | - uploads_dir = "~/Documents/balancer/uploads" |
44 | | - |
45 | | - # iterate over the files in uploads_dir |
46 | | - # Expand the ~ to the actual home directory path |
47 | | - expanded_path = os.path.expanduser(uploads_dir) |
48 | | - |
49 | | - # Get all files in the directory |
50 | | - onlyfiles = [f for f in listdir(expanded_path) if isfile(join(expanded_path, f))] |
51 | | - |
52 | | - # Filter for PDF files |
53 | | - pdf_files = [f for f in onlyfiles if f.lower().endswith('.pdf')] |
54 | | - |
55 | | - for pdf_file in pdf_files: |
56 | | - file_path = join(expanded_path, pdf_file) |
57 | | - with fitz.open(file_path) as doc: |
58 | | - title = generate_title(doc) |
59 | | - # Print the filename and its generated title for debugging |
60 | | - print(f"File: {pdf_file}, Title: {title}") |
61 | | - # Assert that a title is generated (not empty) |
62 | | - self.assertIsNotNone(title) |
63 | | - self.assertNotEqual("", title) |
64 | | - |
| 21 | + self.assertEqual(expected_title, title.generate_title(doc)) |
0 commit comments