Chore: switch from os.path to pathlib.Path (#9060)

This commit is contained in:
Sebastian Steinbeißer
2025-03-05 22:06:01 +01:00
committed by GitHub
parent aaaa6c1393
commit 76d363f22d
17 changed files with 89 additions and 150 deletions

View File

@@ -1,5 +1,5 @@
import json
import os
from pathlib import Path
from django.contrib.auth.models import User
from rest_framework import status
@@ -136,10 +136,7 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
THEN:
- old app_logo file is deleted
"""
with open(
os.path.join(os.path.dirname(__file__), "samples", "simple.jpg"),
"rb",
) as f:
with (Path(__file__).parent / "samples" / "simple.jpg").open("rb") as f:
self.client.patch(
f"{self.ENDPOINT}1/",
{
@@ -148,15 +145,12 @@ class TestApiAppConfig(DirectoriesMixin, APITestCase):
)
config = ApplicationConfiguration.objects.first()
old_logo = config.app_logo
self.assertTrue(os.path.exists(old_logo.path))
with open(
os.path.join(os.path.dirname(__file__), "samples", "simple.png"),
"rb",
) as f:
self.assertTrue(Path(old_logo.path).exists())
with (Path(__file__).parent / "samples" / "simple.png").open("rb") as f:
self.client.patch(
f"{self.ENDPOINT}1/",
{
"app_logo": f,
},
)
self.assertFalse(os.path.exists(old_logo.path))
self.assertFalse(Path(old_logo.path).exists())

View File

@@ -1,4 +1,3 @@
import os
import re
import shutil
from pathlib import Path
@@ -617,7 +616,7 @@ class TestClassifier(DirectoriesMixin, TestCase):
self.assertListEqual(self.classifier.predict_tags(doc2.content), [])
def test_load_classifier_not_exists(self):
self.assertFalse(os.path.exists(settings.MODEL_FILE))
self.assertFalse(Path(settings.MODEL_FILE).exists())
self.assertIsNone(load_classifier())
@mock.patch("documents.classifier.DocumentClassifier.load")
@@ -632,7 +631,7 @@ class TestClassifier(DirectoriesMixin, TestCase):
},
)
@override_settings(
MODEL_FILE=os.path.join(os.path.dirname(__file__), "data", "model.pickle"),
MODEL_FILE=(Path(__file__).parent / "data" / "model.pickle").as_posix(),
)
@pytest.mark.skip(
reason="Disabled caching due to high memory usage - need to investigate.",
@@ -648,24 +647,24 @@ class TestClassifier(DirectoriesMixin, TestCase):
@mock.patch("documents.classifier.DocumentClassifier.load")
def test_load_classifier_incompatible_version(self, load):
Path(settings.MODEL_FILE).touch()
self.assertTrue(os.path.exists(settings.MODEL_FILE))
self.assertTrue(Path(settings.MODEL_FILE).exists())
load.side_effect = IncompatibleClassifierVersionError("Dummy Error")
self.assertIsNone(load_classifier())
self.assertFalse(os.path.exists(settings.MODEL_FILE))
self.assertFalse(Path(settings.MODEL_FILE).exists())
@mock.patch("documents.classifier.DocumentClassifier.load")
def test_load_classifier_os_error(self, load):
Path(settings.MODEL_FILE).touch()
self.assertTrue(os.path.exists(settings.MODEL_FILE))
self.assertTrue(Path(settings.MODEL_FILE).exists())
load.side_effect = OSError()
self.assertIsNone(load_classifier())
self.assertTrue(os.path.exists(settings.MODEL_FILE))
self.assertTrue(Path(settings.MODEL_FILE).exists())
def test_load_old_classifier_version(self):
shutil.copy(
os.path.join(os.path.dirname(__file__), "data", "v1.17.4.model.pickle"),
Path(__file__).parent / "data" / "v1.17.4.model.pickle",
self.dirs.scratch_dir,
)
with override_settings(

View File

@@ -1,5 +1,5 @@
import os
import shutil
from pathlib import Path
from unittest import mock
from django.core.management import call_command
@@ -22,7 +22,7 @@ class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
filename="test.pdf",
)
shutil.copy(
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
Path(__file__).parent / "samples" / "simple.pdf",
self.d1.source_path,
)
@@ -34,7 +34,7 @@ class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
filename="test2.pdf",
)
shutil.copy(
os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
Path(__file__).parent / "samples" / "simple.pdf",
self.d2.source_path,
)
@@ -46,7 +46,7 @@ class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
filename="test3.pdf",
)
shutil.copy(
os.path.join(os.path.dirname(__file__), "samples", "password-is-test.pdf"),
Path(__file__).parent / "samples" / "password-is-test.pdf",
self.d3.source_path,
)

View File

@@ -1,4 +1,3 @@
import os
import shutil
from datetime import timedelta
from pathlib import Path
@@ -88,18 +87,18 @@ class TestClassifier(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
tasks.train_classifier()
self.assertIsFile(settings.MODEL_FILE)
mtime = os.stat(settings.MODEL_FILE).st_mtime
mtime = Path(settings.MODEL_FILE).stat().st_mtime
tasks.train_classifier()
self.assertIsFile(settings.MODEL_FILE)
mtime2 = os.stat(settings.MODEL_FILE).st_mtime
mtime2 = Path(settings.MODEL_FILE).stat().st_mtime
self.assertEqual(mtime, mtime2)
doc.content = "test2"
doc.save()
tasks.train_classifier()
self.assertIsFile(settings.MODEL_FILE)
mtime3 = os.stat(settings.MODEL_FILE).st_mtime
mtime3 = Path(settings.MODEL_FILE).stat().st_mtime
self.assertNotEqual(mtime2, mtime3)

View File

@@ -1,6 +1,6 @@
import os
import tempfile
from datetime import timedelta
from pathlib import Path
from django.conf import settings
from django.contrib.auth.models import Permission
@@ -107,12 +107,12 @@ class TestViews(DirectoriesMixin, TestCase):
content = b"This is a test"
with open(filename, "wb") as f:
with Path(filename).open("wb") as f:
f.write(content)
doc = Document.objects.create(
title="none",
filename=os.path.basename(filename),
filename=Path(filename).name,
mime_type="application/pdf",
)