From: Brian Warner Date: Mon, 11 Feb 2008 21:53:28 +0000 (-0700) Subject: dirnode.py: add metadata= to add_file(), add tests X-Git-Tag: allmydata-tahoe-0.8.0~101 X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/flags/rgr-080307.php?a=commitdiff_plain;h=e6ddd9f3da8bcec7c3bea16346ea535bc0c49a66;p=tahoe-lafs%2Ftahoe-lafs.git dirnode.py: add metadata= to add_file(), add tests --- diff --git a/src/allmydata/dirnode.py b/src/allmydata/dirnode.py index 17070777..72d44ca9 100644 --- a/src/allmydata/dirnode.py +++ b/src/allmydata/dirnode.py @@ -298,7 +298,7 @@ class NewDirectoryNode: return d - def add_file(self, name, uploadable): + def add_file(self, name, uploadable, metadata=None): """I upload a file (using the given IUploadable), then attach the resulting FileNode to the directory at the given name. I return a Deferred that fires (with the IFileNode of the uploaded file) when @@ -308,7 +308,7 @@ class NewDirectoryNode: d = self._client.upload(uploadable) d.addCallback(lambda results: results.uri) d.addCallback(self._client.create_node_from_uri) - d.addCallback(lambda node: self.set_node(name, node)) + d.addCallback(lambda node: self.set_node(name, node, metadata)) return d def delete(self, name): diff --git a/src/allmydata/test/test_dirnode.py b/src/allmydata/test/test_dirnode.py index 5e27e4a5..1de81a4b 100644 --- a/src/allmydata/test/test_dirnode.py +++ b/src/allmydata/test/test_dirnode.py @@ -184,7 +184,7 @@ class Dirnode(unittest.TestCase, testutil.ShouldFailMixin): ffu_v = m.get_verifier() assert isinstance(ffu_v, str) self.expected_manifest.append(ffu_v) - d.addCallback(lambda res: n.set_uri("child", fake_file_uri, {})) + d.addCallback(lambda res: n.set_uri("child", fake_file_uri)) d.addCallback(lambda res: n.create_empty_directory("subdir")) def _created(subdir): @@ -218,15 +218,108 @@ class Dirnode(unittest.TestCase, testutil.ShouldFailMixin): n.get_uri())) d.addCallback(lambda res: n.get_metadata_for("child")) + d.addCallback(lambda metadata: + self.failUnlessEqual(sorted(metadata.keys()), + ["ctime", "mtime"])) + + # set_uri + metadata + # it should be possible to add a child without any metadata + d.addCallback(lambda res: n.set_uri("c2", fake_file_uri, {})) + d.addCallback(lambda res: n.get_metadata_for("c2")) d.addCallback(lambda metadata: self.failUnlessEqual(metadata, {})) + # if we don't set any defaults, the child should get timestamps + d.addCallback(lambda res: n.set_uri("c3", fake_file_uri)) + d.addCallback(lambda res: n.get_metadata_for("c3")) + d.addCallback(lambda metadata: + self.failUnlessEqual(sorted(metadata.keys()), + ["ctime", "mtime"])) + + # or we can add specific metadata at set_uri() time, which + # overrides the timestamps + d.addCallback(lambda res: n.set_uri("c4", fake_file_uri, + {"key": "value"})) + d.addCallback(lambda res: n.get_metadata_for("c4")) + d.addCallback(lambda metadata: + self.failUnlessEqual(metadata, {"key": "value"})) + + d.addCallback(lambda res: n.delete("c2")) + d.addCallback(lambda res: n.delete("c3")) + d.addCallback(lambda res: n.delete("c4")) + + # set_node + metadata + # it should be possible to add a child without any metadata + d.addCallback(lambda res: n.set_node("d2", n, {})) + d.addCallback(lambda res: n.get_metadata_for("d2")) + d.addCallback(lambda metadata: self.failUnlessEqual(metadata, {})) + + # if we don't set any defaults, the child should get timestamps + d.addCallback(lambda res: n.set_node("d3", n)) + d.addCallback(lambda res: n.get_metadata_for("d3")) + d.addCallback(lambda metadata: + self.failUnlessEqual(sorted(metadata.keys()), + ["ctime", "mtime"])) + + # or we can add specific metadata at set_node() time, which + # overrides the timestamps + d.addCallback(lambda res: n.set_node("d4", n, + {"key": "value"})) + d.addCallback(lambda res: n.get_metadata_for("d4")) + d.addCallback(lambda metadata: + self.failUnlessEqual(metadata, {"key": "value"})) + + d.addCallback(lambda res: n.delete("d2")) + d.addCallback(lambda res: n.delete("d3")) + d.addCallback(lambda res: n.delete("d4")) + + # metadata through set_uris() + d.addCallback(lambda res: n.set_uris([ ("e1", fake_file_uri), + ("e2", fake_file_uri, {}), + ("e3", fake_file_uri, + {"key": "value"}), + ])) + d.addCallback(lambda res: n.get_metadata_for("e1")) + d.addCallback(lambda metadata: + self.failUnlessEqual(sorted(metadata.keys()), + ["ctime", "mtime"])) + d.addCallback(lambda res: n.get_metadata_for("e2")) + d.addCallback(lambda metadata: self.failUnlessEqual(metadata, {})) + d.addCallback(lambda res: n.get_metadata_for("e3")) + d.addCallback(lambda metadata: + self.failUnlessEqual(metadata, {"key": "value"})) + + d.addCallback(lambda res: n.delete("e1")) + d.addCallback(lambda res: n.delete("e2")) + d.addCallback(lambda res: n.delete("e3")) + + # metadata through set_nodes() + d.addCallback(lambda res: n.set_nodes([ ("f1", n), + ("f2", n, {}), + ("f3", n, + {"key": "value"}), + ])) + d.addCallback(lambda res: n.get_metadata_for("f1")) + d.addCallback(lambda metadata: + self.failUnlessEqual(sorted(metadata.keys()), + ["ctime", "mtime"])) + d.addCallback(lambda res: n.get_metadata_for("f2")) + d.addCallback(lambda metadata: self.failUnlessEqual(metadata, {})) + d.addCallback(lambda res: n.get_metadata_for("f3")) + d.addCallback(lambda metadata: + self.failUnlessEqual(metadata, {"key": "value"})) + + d.addCallback(lambda res: n.delete("f1")) + d.addCallback(lambda res: n.delete("f2")) + d.addCallback(lambda res: n.delete("f3")) + + d.addCallback(lambda res: n.set_metadata_for("child", - {"tags": "web2.0-compatible"})) + {"tags": ["web2.0-compatible"]})) d.addCallback(lambda n1: n1.get_metadata_for("child")) d.addCallback(lambda metadata: self.failUnlessEqual(metadata, - {"tags": "web2.0-compatible"})) + {"tags": ["web2.0-compatible"]})) def _start(res): self._start_timestamp = time.time() @@ -270,6 +363,21 @@ class Dirnode(unittest.TestCase, testutil.ShouldFailMixin): d.addCallback(lambda children: self.failUnlessEqual(sorted(children.keys()), sorted(["child", "newfile"]))) + d.addCallback(lambda res: n.get_metadata_for("newfile")) + d.addCallback(lambda metadata: + self.failUnlessEqual(sorted(metadata.keys()), + ["ctime", "mtime"])) + + uploadable = upload.Data("some data") + d.addCallback(lambda res: n.add_file("newfile-metadata", + uploadable, + {"key": "value"})) + d.addCallback(lambda newnode: + self.failUnless(IFileNode.providedBy(newnode))) + d.addCallback(lambda res: n.get_metadata_for("newfile-metadata")) + d.addCallback(lambda metadata: + self.failUnlessEqual(metadata, {"key": "value"})) + d.addCallback(lambda res: n.delete("newfile-metadata")) d.addCallback(lambda res: n.create_empty_directory("subdir2")) def _created2(subdir2):