Files
momentry_core/scripts/test_probe_file.py

93 lines
2.8 KiB
Python

#!/opt/homebrew/bin/python3.11
"""Unit tests for probe_file.py"""
import sys
import json
import os
import unittest
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import probe_file # noqa: E402
class TestProbePDF(unittest.TestCase):
def setUp(self):
self.path = "/tmp/_test_probe.pdf"
if not os.path.exists(self.path):
from PyPDF2 import PdfWriter
w = PdfWriter()
w.add_blank_page(210, 297)
with open(self.path, 'wb') as f:
w.write(f)
def test_pdf_has_pages(self):
result = json.loads(probe_file.probe(self.path))
self.assertIn("document", result)
self.assertGreater(result["document"]["pages"], 0)
def test_pdf_has_streams(self):
result = json.loads(probe_file.probe(self.path))
self.assertEqual(result["streams"], [])
class TestProbeDOCX(unittest.TestCase):
def setUp(self):
self.path = "/tmp/_test_probe.docx"
if not os.path.exists(self.path):
from docx import Document
d = Document()
d.add_paragraph("Test paragraph.")
d.save(self.path)
def test_docx_has_paragraphs(self):
result = json.loads(probe_file.probe(self.path))
self.assertIn("document", result)
self.assertGreater(result["document"]["paragraphs"], 0)
class TestProbeXLSX(unittest.TestCase):
def setUp(self):
self.path = "/tmp/_test_probe.xlsx"
if not os.path.exists(self.path):
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Sheet1"
ws.append(["A", 1])
wb.save(self.path)
def test_xlsx_has_sheets(self):
result = json.loads(probe_file.probe(self.path))
self.assertIn("spreadsheet", result)
self.assertGreater(result["spreadsheet"]["sheet_count"], 0)
class TestProbePPTX(unittest.TestCase):
def setUp(self):
self.path = "/tmp/_test_probe.pptx"
if not os.path.exists(self.path):
from pptx import Presentation
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[0])
slide.shapes.title.text = "Test"
prs.save(self.path)
def test_pptx_has_slides(self):
result = json.loads(probe_file.probe(self.path))
self.assertIn("presentation", result)
self.assertGreater(result["presentation"]["slide_count"], 0)
class TestProbeUnknown(unittest.TestCase):
def test_unknown_extension(self):
path = "/tmp/_test_probe.xyz"
with open(path, 'w') as f:
f.write("test")
result = json.loads(probe_file.probe(path))
os.remove(path)
self.assertEqual(result["streams"], [])
if __name__ == "__main__":
unittest.main()