Skip to content

Commit

Permalink
correct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samwaseda committed Aug 16, 2024
1 parent 29e25f7 commit 83c5f4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
7 changes: 5 additions & 2 deletions pyiron_snippets/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def __new__(cls, file_name: str, directory: DirectoryObject = None):
if directory is None:
full_path = Path(file_name)
else:
if isinstance(directory, str):
directory = DirectoryObject(directory)
full_path = directory.joinpath(file_name)
instance = super().__new__(cls, full_path)
return instance
Expand All @@ -111,7 +113,7 @@ def read(self, mode="r"):
return f.read()

def is_file(self):
return self.exists() and self.is_file()
return self.exists() and super().is_file()

def delete(self):
self.unlink()
Expand All @@ -131,8 +133,9 @@ def copy(

if directory is None:
directory = self.parent
elif isinstance(directory, str):
directory = DirectoryObject(directory)

new_file = directory.joinpath(new_file_name)
shutil.copy(str(self), str(new_file))
return FileObject(new_file_name, DirectoryObject(directory))

29 changes: 22 additions & 7 deletions tests/unit/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from pyiron_snippets.files import DirectoryObject, FileObject


class TestFiles(unittest.TestCase):
def setUp(self):
self.directory = DirectoryObject("test")
Expand All @@ -31,11 +30,6 @@ def test_file_instantiation(self):
msg="File path not the same as directory path"
)

if system() == "Windows":
self.assertRaises(ValueError, FileObject, "C:\\test.txt", "test")
else:
self.assertRaises(ValueError, FileObject, "/test.txt", "test")

def test_directory_exists(self):
self.assertTrue(self.directory.exists() and self.directory.is_dir())

Expand Down Expand Up @@ -125,7 +119,28 @@ def test_remove(self):
def test_copy(self):
f = FileObject("test_copy.txt", self.directory)
f.write("sam wrote this wonderful thing")
new_file_1
new_file_1 = f.copy("another_test")
self.assertEqual(new_file_1.read(), "sam wrote this wonderful thing")
new_file_2 = f.copy("another_test", ".")
with open("another_test", "r") as file:
txt = file.read()
self.assertEqual(txt, "sam wrote this wonderful thing")
new_file_2.delete() # needed because current directory
new_file_3 = f.copy(str(f.parent / "another_test"), ".")
self.assertEqual(new_file_1, new_file_3)
new_file_4 = f.copy(directory=".")
with open("test_copy.txt", "r") as file:
txt = file.read()
self.assertEqual(txt, "sam wrote this wonderful thing")
new_file_4.delete() # needed because current directory
with self.assertRaises(ValueError):
f.copy()

def test_str(self):
f = FileObject("test_copy.txt", self.directory)
expected_path = str(self.directory / "test_copy.txt")
self.assertEqual(str(f), expected_path)


if __name__ == '__main__':
unittest.main()

0 comments on commit 83c5f4f

Please sign in to comment.