This repository has been archived on 2023-02-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
flamenco-worker/tests/test_commands_create_python_file.py
Sybren A. Stüvel 05408dc8c1 Cleanup: reformat with Black
Reformat the entire project with Black.

No functional changes.
2021-07-09 14:26:13 +02:00

74 lines
2.4 KiB
Python

from pathlib import Path
import os
from tests.test_runner import AbstractCommandTest
class CreatePythonFileTest(AbstractCommandTest):
def setUp(self):
super().setUp()
from flamenco_worker.commands import CreatePythonFile
import tempfile
self.tmpdir = tempfile.TemporaryDirectory()
self.tmppath = Path(self.tmpdir.name)
self.cmd = CreatePythonFile(
worker=self.fworker,
task_id="12345",
command_idx=0,
)
def tearDown(self):
super().tearDown()
self.tmpdir.cleanup()
def test_validate_settings(self):
self.assertIn(
"filepath", self.cmd.validate({"filepath": 12, "contents": "# comment"})
)
self.assertIn(
"filepath", self.cmd.validate({"filepath": "", "contents": "# comment"})
)
self.assertIn(
"filepath", self.cmd.validate({"filepath": "/nonpy/path", "contents": "#"})
)
self.assertIn("filepath", self.cmd.validate({"contents": "#"}))
self.assertIn(
"content", self.cmd.validate({"filepath": "/valid/path.py", "contents": 12})
)
self.assertIn("content", self.cmd.validate({"filepath": "/valid/path.py"}))
self.assertTrue(self.cmd.validate({}))
self.assertFalse(
self.cmd.validate({"filepath": "/valid/path.py", "contents": ""})
)
self.assertFalse(
self.cmd.validate({"filepath": "/valid/path.py", "contents": "#"})
)
self.assertFalse(
self.cmd.validate({"filepath": "/valid/path.py", "contents": "##\na=b\n"})
)
def test_nonexistant_path(self):
filepath = self.tmppath / "nonexisting-dir" / "somefile.py"
task = self.cmd.run({"filepath": str(filepath), "contents": "aapje"})
ok = self.loop.run_until_complete(task)
self.assertTrue(ok)
self.assertTrue(filepath.exists())
self.assertEqual("aapje", filepath.read_text())
def test_existing_path(self):
filepath = self.tmppath / "existing.py"
filepath.write_text("old content")
task = self.cmd.run({"filepath": str(filepath), "contents": "öpje"})
ok = self.loop.run_until_complete(task)
self.assertTrue(ok)
self.assertTrue(filepath.exists())
self.assertEqual("öpje", filepath.read_text(encoding="utf8"))