Skip to content

Commit

Permalink
Fix fields parameter not working properly in listdir() (issue #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivknv committed Sep 17, 2018
1 parent 4c76d4c commit e966b9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ def test_listdir(self):

self.assertEqual(result, names)

def test_listdir_fields(self):
names = ["dir1", "dir2", "dir3"]

for name in names:
path = posixpath.join(self.path, name)

self.yadisk.mkdir(path)

result = [(i.name, i.type, i.file) for i in self.yadisk.listdir(self.path, fields=["name", "type"])]

for name in names:
path = posixpath.join(self.path, name)

self.yadisk.remove(path, permanently=True)

self.assertEqual(result, [(name, "dir", None) for name in names])

def test_listdir_on_file(self):
buf = BytesIO()
buf.write(b"0" * 1000)
Expand Down
15 changes: 15 additions & 0 deletions yadisk/functions/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,21 @@ def _listdir(get_meta_function, session, path, **kwargs):
kwargs = dict(kwargs)
kwargs.setdefault("limit", 10000)

if kwargs.get("fields") is None:
kwargs["fields"] = []

kwargs["fields"] = ["embedded.items.%s" % (k,) for k in kwargs["fields"]]

# Fields that are absolutely necessary
NECESSARY_FIELDS = ["type",
"embedded",
"embedded.offset",
"embedded.limit",
"embedded.total",
"embedded.items"]

kwargs["fields"].extend(NECESSARY_FIELDS)

result = get_meta_function(session, path, **kwargs)

if result.type == "file":
Expand Down

0 comments on commit e966b9f

Please sign in to comment.