emmental/tests/test_tmpdir.py

34 lines
1.1 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Test our temporary directory handling code."""
import tempfile
import pathlib
import unittest
import emmental.tmpdir
TMP_DIR = pathlib.Path(tempfile.gettempdir())
class TestTmpDir(unittest.TestCase):
"""Our teporary directory test case."""
def setUp(self):
"""Prepare for testing."""
emmental.tmpdir.cover_jpg().unlink(missing_ok=True)
def test_tmpdir(self):
"""Test TMP_DIR location and existence."""
self.assertEqual(emmental.tmpdir.TMP_DIR,
TMP_DIR / emmental.gsetup.APPLICATION_ID)
self.assertTrue(emmental.tmpdir.TMP_DIR.is_dir())
def test_cover_jpg(self):
"""Test temporary cover.jpg handling."""
cover = emmental.tmpdir.cover_jpg()
self.assertEqual(cover, emmental.tmpdir.TMP_DIR / "cover.jpg")
self.assertFalse(cover.is_file())
cover = emmental.tmpdir.cover_jpg("Test Text".encode("utf-8"))
self.assertTrue(cover.is_file())
with cover.open("rb") as f:
self.assertEqual(f.read(), "Test Text".encode("utf-8"))