From fc0bfb6fc6288c2c2959aa2af2107aaf023a7472 Mon Sep 17 00:00:00 2001 From: Eugen Ciur Date: Mon, 4 May 2020 16:36:18 +0200 Subject: [PATCH] testing delete operation of mglib storage --- mglib/test/__init__.py | 3 ++ mglib/test/utils.py | 68 ++++++++++++++++++++++++++++++++++++++++++ test/test_storage.py | 8 ++--- test/utils.py | 35 ---------------------- 4 files changed, 75 insertions(+), 39 deletions(-) create mode 100644 mglib/test/__init__.py create mode 100644 mglib/test/utils.py delete mode 100644 test/utils.py diff --git a/mglib/test/__init__.py b/mglib/test/__init__.py new file mode 100644 index 0000000..c191676 --- /dev/null +++ b/mglib/test/__init__.py @@ -0,0 +1,3 @@ +""" +Utilities used for testing +""" diff --git a/mglib/test/utils.py b/mglib/test/utils.py new file mode 100644 index 0000000..3f919ff --- /dev/null +++ b/mglib/test/utils.py @@ -0,0 +1,68 @@ +import os + + +class TemporaryNode: + """ + Handy class when it comes to testing files/directories + structures. + + Example of usage: + + with TemporaryNode(MEDIA_ROOT) as media_root: + docs = media_root.add_folder("docs") + res = media_root.add_folder("results") + f1 = docs.add_folder("user_1/document_2") + f1.add_file("doku.pdf") + res.add_folder("user_1/document_2/pages") + + docp = DocumentPath( + user_id=1, + document_id=2, + file_name='doku.pdf' + ) + + storage.delete_document(docp) + + self.assertFalse( + docs.exists() + ) + + self.assertFalse( + res.exists() + ) + """ + + def __init__(self, location): + self._location = location + + @property + def location(self): + return self._location + + def __enter__(self): + if not os.path.exists(self.location): + os.makedirs(self.location) + + return self + + def __exit__(self): + if os.path.exists(self.location): + if os.path.is_dir(self.location): + os.rmdir(self.location) + else: + os.remove(self.location) + + def add_folder(self, folder): + new_location = os.path.join( + self.location, + folder + ) + os.makedirs(new_location) + return TemporaryNode(new_location) + + def exists(self): + return os.path.exists(self.location) + + def add_file(self, file): + pass + return self diff --git a/test/test_storage.py b/test/test_storage.py index eabd15e..05b76b2 100644 --- a/test/test_storage.py +++ b/test/test_storage.py @@ -1,6 +1,6 @@ import os import unittest -from mglib.test.utils import TemporaryDir +from mglib.test.utils import TemporaryNode from mglib.path import DocumentPath from mglib.storage import Storage @@ -18,20 +18,20 @@ class TestStorage(unittest.TestCase): def test_delete(self): storage = Storage(location=MEDIA_ROOT) - with TemporaryDir(MEDIA_ROOT) as media_root: + with TemporaryNode(MEDIA_ROOT) as media_root: docs = media_root.add_folder("docs") res = media_root.add_folder("results") f1 = docs.add_folder("user_1/document_2") f1.add_file("doku.pdf") res.add_folder("user_1/document_2/pages") - docp = DocumentPath( + doc_path = DocumentPath( user_id=1, document_id=2, file_name='doku.pdf' ) - storage.delete_document(docp) + storage.delete_document(doc_path) self.assertFalse( docs.exists() diff --git a/test/utils.py b/test/utils.py deleted file mode 100644 index 874f670..0000000 --- a/test/utils.py +++ /dev/null @@ -1,35 +0,0 @@ - - -class TemporaryDir: - """ - Handy class when it comes to testing files/directories - structures. - - Example of usage: - - with TemporaryDir(MEDIA_ROOT) as media_root: - docs = media_root.add_folder("docs") - res = media_root.add_folder("results") - f1 = docs.add_folder("user_1/document_2") - f1.add_file("doku.pdf") - res.add_folder("user_1/document_2/pages") - - docp = DocumentPath( - user_id=1, - document_id=2, - file_name='doku.pdf' - ) - - storage.delete_document(docp) - - self.assertFalse( - docs.exists() - ) - - self.assertFalse( - res.exists() - ) - """ - - def __init__(self, location): - pass