mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-20 05:28:04 +00:00
Merge pull request #120 from zancas/1634-dont-return-none_5
1634 dont return none 5
This commit is contained in:
commit
200492760a
48
src/allmydata/test/test_json_metadata.py
Normal file
48
src/allmydata/test/test_json_metadata.py
Normal file
@ -0,0 +1,48 @@
|
||||
import mock
|
||||
|
||||
from twisted.trial.unittest import TestCase
|
||||
|
||||
from allmydata.web.common import get_filenode_metadata, SDMF_VERSION, MDMF_VERSION
|
||||
from allmydata.immutable.filenode import ImmutableFileNode
|
||||
|
||||
class CommonFixture(object):
|
||||
def setUp(self):
|
||||
self.mockfilenode = mock.Mock()
|
||||
|
||||
def test_size_is_0(self):
|
||||
"""If get_size doesn't return None the returned metadata must contain "size"."""
|
||||
self.mockfilenode.get_size.return_value = 0
|
||||
metadata = get_filenode_metadata(self.mockfilenode)
|
||||
self.failUnlessIn('size', metadata)
|
||||
|
||||
def test_size_is_1000(self):
|
||||
"""1000 is sufficiently large to guarantee the cap is not a literal."""
|
||||
self.mockfilenode.get_size.return_value = 1000
|
||||
metadata = get_filenode_metadata(self.mockfilenode)
|
||||
self.failUnlessIn('size', metadata)
|
||||
|
||||
def test_size_is_None(self):
|
||||
"""If get_size returns None the returned metadata must not contain "size"."""
|
||||
self.mockfilenode.get_size.return_value = None
|
||||
metadata = get_filenode_metadata(self.mockfilenode)
|
||||
self.failIfIn('size', metadata)
|
||||
|
||||
|
||||
class Test_GetFileNodeMetaData_Immutable(CommonFixture, TestCase):
|
||||
def setUp(self):
|
||||
CommonFixture.setUp(self)
|
||||
self.mockfilenode.is_mutable.return_value = False
|
||||
|
||||
|
||||
class Test_GetFileNodeMetaData_SDMF(CommonFixture, TestCase):
|
||||
def setUp(self):
|
||||
CommonFixture.setUp(self)
|
||||
self.mockfilenode.is_mutable.return_value = True
|
||||
self.mockfilenode.get_version.return_value = SDMF_VERSION
|
||||
|
||||
|
||||
class Test_GetFileNodeMetaData_MDMF(CommonFixture, TestCase):
|
||||
def setUp(self):
|
||||
CommonFixture.setUp(self)
|
||||
self.mockfilenode.is_mutable.return_value = True
|
||||
self.mockfilenode.get_version.return_value = MDMF_VERSION
|
@ -18,6 +18,21 @@ from allmydata.util.encodingutil import to_str, quote_output
|
||||
|
||||
TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
|
||||
|
||||
def get_filenode_metadata(filenode):
|
||||
metadata = {'mutable': filenode.is_mutable()}
|
||||
if metadata['mutable']:
|
||||
mutable_type = filenode.get_version()
|
||||
assert mutable_type in (SDMF_VERSION, MDMF_VERSION)
|
||||
if mutable_type == MDMF_VERSION:
|
||||
file_format = "MDMF"
|
||||
else:
|
||||
file_format = "SDMF"
|
||||
else:
|
||||
file_format = "CHK"
|
||||
metadata['format'] = file_format
|
||||
if filenode.get_size() is not None:
|
||||
metadata['size'] = filenode.get_size()
|
||||
return metadata
|
||||
|
||||
class IOpHandleTable(Interface):
|
||||
pass
|
||||
|
@ -26,7 +26,7 @@ from allmydata.web.common import text_plain, WebError, \
|
||||
boolean_of_arg, get_arg, get_root, parse_replace_arg, \
|
||||
should_create_intermediate_directories, \
|
||||
getxmlfile, RenderMixin, humanize_failure, convert_children_json, \
|
||||
get_format, get_mutable_type
|
||||
get_format, get_mutable_type, get_filenode_metadata
|
||||
from allmydata.web.filenode import ReplaceMeMixin, \
|
||||
FileNodeHandler, PlaceHolderNodeHandler
|
||||
from allmydata.web.check_results import CheckResultsRenderer, \
|
||||
@ -869,7 +869,6 @@ class DirectoryAsHTML(rend.Page):
|
||||
req = IRequest(ctx)
|
||||
return get_arg(req, "results", "")
|
||||
|
||||
|
||||
def DirectoryJSONMetadata(ctx, dirnode):
|
||||
d = dirnode.list()
|
||||
def _got(children):
|
||||
@ -879,20 +878,7 @@ def DirectoryJSONMetadata(ctx, dirnode):
|
||||
rw_uri = childnode.get_write_uri()
|
||||
ro_uri = childnode.get_readonly_uri()
|
||||
if IFileNode.providedBy(childnode):
|
||||
kiddata = ("filenode", {'size': childnode.get_size(),
|
||||
'mutable': childnode.is_mutable(),
|
||||
})
|
||||
if childnode.is_mutable():
|
||||
mutable_type = childnode.get_version()
|
||||
assert mutable_type in (SDMF_VERSION, MDMF_VERSION)
|
||||
if mutable_type == MDMF_VERSION:
|
||||
file_format = "MDMF"
|
||||
else:
|
||||
file_format = "SDMF"
|
||||
else:
|
||||
file_format = "CHK"
|
||||
kiddata[1]['format'] = file_format
|
||||
|
||||
kiddata = ("filenode", get_filenode_metadata(childnode))
|
||||
elif IDirectoryNode.providedBy(childnode):
|
||||
kiddata = ("dirnode", {'mutable': childnode.is_mutable()})
|
||||
else:
|
||||
|
@ -6,7 +6,7 @@ from twisted.internet import defer
|
||||
from nevow import url, rend
|
||||
from nevow.inevow import IRequest
|
||||
|
||||
from allmydata.interfaces import ExistingChildError, SDMF_VERSION, MDMF_VERSION
|
||||
from allmydata.interfaces import ExistingChildError
|
||||
from allmydata.monitor import Monitor
|
||||
from allmydata.immutable.upload import FileHandle
|
||||
from allmydata.mutable.publish import MutableFileHandle
|
||||
@ -18,7 +18,7 @@ from allmydata.blacklist import FileProhibited, ProhibitedNode
|
||||
from allmydata.web.common import text_plain, WebError, RenderMixin, \
|
||||
boolean_of_arg, get_arg, should_create_intermediate_directories, \
|
||||
MyExceptionHandler, parse_replace_arg, parse_offset_arg, \
|
||||
get_format, get_mutable_type
|
||||
get_format, get_mutable_type, get_filenode_metadata
|
||||
from allmydata.web.check_results import CheckResultsRenderer, \
|
||||
CheckAndRepairResultsRenderer, LiteralCheckResultsRenderer
|
||||
from allmydata.web.info import MoreInfo
|
||||
@ -498,8 +498,7 @@ class FileDownloader(rend.Page):
|
||||
def FileJSONMetadata(ctx, filenode, edge_metadata):
|
||||
rw_uri = filenode.get_write_uri()
|
||||
ro_uri = filenode.get_readonly_uri()
|
||||
data = ("filenode", {})
|
||||
data[1]['size'] = filenode.get_size()
|
||||
data = ("filenode", get_filenode_metadata(filenode))
|
||||
if ro_uri:
|
||||
data[1]['ro_uri'] = ro_uri
|
||||
if rw_uri:
|
||||
@ -507,21 +506,9 @@ def FileJSONMetadata(ctx, filenode, edge_metadata):
|
||||
verifycap = filenode.get_verify_cap()
|
||||
if verifycap:
|
||||
data[1]['verify_uri'] = verifycap.to_string()
|
||||
data[1]['mutable'] = filenode.is_mutable()
|
||||
if edge_metadata is not None:
|
||||
data[1]['metadata'] = edge_metadata
|
||||
|
||||
if filenode.is_mutable():
|
||||
mutable_type = filenode.get_version()
|
||||
assert mutable_type in (SDMF_VERSION, MDMF_VERSION)
|
||||
if mutable_type == MDMF_VERSION:
|
||||
file_format = "MDMF"
|
||||
else:
|
||||
file_format = "SDMF"
|
||||
else:
|
||||
file_format = "CHK"
|
||||
data[1]['format'] = file_format
|
||||
|
||||
return text_plain(simplejson.dumps(data, indent=1) + "\n", ctx)
|
||||
|
||||
def FileURI(ctx, filenode):
|
||||
|
Loading…
Reference in New Issue
Block a user