mirror of https://github.com/papermerge/mglib
testing delete operation of mglib storage
parent
e358e748bd
commit
fc0bfb6fc6
|
@ -0,0 +1,3 @@
|
||||||
|
"""
|
||||||
|
Utilities used for testing
|
||||||
|
"""
|
|
@ -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
|
|
@ -1,6 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
from mglib.test.utils import TemporaryDir
|
from mglib.test.utils import TemporaryNode
|
||||||
from mglib.path import DocumentPath
|
from mglib.path import DocumentPath
|
||||||
from mglib.storage import Storage
|
from mglib.storage import Storage
|
||||||
|
|
||||||
|
@ -18,20 +18,20 @@ class TestStorage(unittest.TestCase):
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
storage = Storage(location=MEDIA_ROOT)
|
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")
|
docs = media_root.add_folder("docs")
|
||||||
res = media_root.add_folder("results")
|
res = media_root.add_folder("results")
|
||||||
f1 = docs.add_folder("user_1/document_2")
|
f1 = docs.add_folder("user_1/document_2")
|
||||||
f1.add_file("doku.pdf")
|
f1.add_file("doku.pdf")
|
||||||
res.add_folder("user_1/document_2/pages")
|
res.add_folder("user_1/document_2/pages")
|
||||||
|
|
||||||
docp = DocumentPath(
|
doc_path = DocumentPath(
|
||||||
user_id=1,
|
user_id=1,
|
||||||
document_id=2,
|
document_id=2,
|
||||||
file_name='doku.pdf'
|
file_name='doku.pdf'
|
||||||
)
|
)
|
||||||
|
|
||||||
storage.delete_document(docp)
|
storage.delete_document(doc_path)
|
||||||
|
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
docs.exists()
|
docs.exists()
|
||||||
|
|
|
@ -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
|
|
Loading…
Reference in New Issue