2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 17:47:13 +00:00
Files
troggle/core/TESTS/test_drawings.py
2025-12-15 16:38:50 +00:00

86 lines
3.0 KiB
Python

import os
import pathlib
import tempfile
from django.test import TestCase
import settings
from troggle.parsers import drawings
from troggle.core.models.survex import DrawingFile
class DrawingsPathlibTests(TestCase):
def test_load_drawings_creates_expected_entries(self):
with tempfile.TemporaryDirectory() as td:
# create a small tree
p = pathlib.Path(td)
(p / 'one.pdf').write_text('pdf')
(p / 'two.txt').write_text('txt')
sub = p / 'dir'
sub.mkdir()
(sub / 'three.png').write_text('png')
sub2 = p / 'dir2'
sub2.mkdir()
(sub2 / 'abc.th2').write_text('th2')
(sub2 / 'abc.th').write_text('th')
# point the module at our tempdir
settings.DRAWINGS_DATA = td
drawings.load_drawings_files()
# all files should be present
self.assertTrue(DrawingFile.objects.filter(dwgpath='one.pdf').exists())
self.assertTrue(DrawingFile.objects.filter(dwgpath='two.txt').exists())
self.assertTrue(DrawingFile.objects.filter(dwgpath='dir/three.png').exists())
self.assertTrue(DrawingFile.objects.filter(dwgpath='dir2/abc.th2').exists())
self.assertTrue(DrawingFile.objects.filter(dwgpath='dir2/abc.th').exists())
def test_hidden_and_backup_skipped(self):
with tempfile.TemporaryDirectory() as td:
p = pathlib.Path(td)
(p / '.hidden').write_text('hid')
(p / 'file~').write_text('bak')
settings.DRAWINGS_DATA = td
drawings.load_drawings_files()
# Should not import hidden or backup files
self.assertFalse(DrawingFile.objects.filter(dwgpath='.hidden').exists())
self.assertFalse(DrawingFile.objects.filter(dwgpath='file~').exists())
def test_no_extension_file(self):
with tempfile.TemporaryDirectory() as td:
p = pathlib.Path(td)
(p / 'noext').write_text('data')
settings.DRAWINGS_DATA = td
drawings.load_drawings_files()
self.assertTrue(DrawingFile.objects.filter(dwgpath='noext').exists())
def test_git_dir_skipped(self):
with tempfile.TemporaryDirectory() as td:
p = pathlib.Path(td)
g = p / '.git'
g.mkdir()
(g / 'secret.txt').write_text('top secret')
settings.DRAWINGS_DATA = td
drawings.load_drawings_files()
self.assertFalse(DrawingFile.objects.filter(dwgpath='.git/secret.txt').exists())
def test_bulk_create_chunks(self):
# Create more than chunk size files to ensure bulk_create is called in multiple chunks
count = 800
with tempfile.TemporaryDirectory() as td:
p = pathlib.Path(td)
for i in range(count):
(p / f'file{i}.txt').write_text('x')
settings.DRAWINGS_DATA = td
drawings.load_drawings_files()
self.assertEqual(DrawingFile.objects.count(), count)