testing delete operation of mglib storage

pull/3/head
Eugen Ciur 2020-05-04 16:36:18 +02:00
parent e358e748bd
commit fc0bfb6fc6
4 changed files with 75 additions and 39 deletions

3
mglib/test/__init__.py Normal file
View File

@ -0,0 +1,3 @@
"""
Utilities used for testing
"""

68
mglib/test/utils.py Normal file
View File

@ -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

View File

@ -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()

View File

@ -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