Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/test_from_disk.py
# Copyright (C) 2017 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
import pytest | import pytest | ||||
import tarfile | import tarfile | ||||
import tempfile | import tempfile | ||||
import unittest | import unittest | ||||
from collections import defaultdict | |||||
from typing import ClassVar, Optional | from typing import ClassVar, Optional | ||||
from swh.model import from_disk | from swh.model import from_disk | ||||
from swh.model.from_disk import Content, DentryPerms, Directory, DiskBackedContent | from swh.model.from_disk import Content, DentryPerms, Directory, DiskBackedContent | ||||
from swh.model.hashutil import DEFAULT_ALGORITHMS, hash_to_bytes, hash_to_hex | from swh.model.hashutil import DEFAULT_ALGORITHMS, hash_to_bytes, hash_to_hex | ||||
from swh.model import model | from swh.model import model | ||||
TEST_DATA = os.path.join(os.path.dirname(__file__), "data") | TEST_DATA = os.path.join(os.path.dirname(__file__), "data") | ||||
▲ Show 20 Lines • Show All 833 Lines • ▼ Show 20 Lines | def test_contents_match(self): | ||||
if isinstance(obj, Content): | if isinstance(obj, Content): | ||||
self.assertContentEqual(obj, expected) | self.assertContentEqual(obj, expected) | ||||
elif isinstance(obj, Directory): | elif isinstance(obj, Directory): | ||||
self.assertDirectoryEqual(obj, expected) | self.assertDirectoryEqual(obj, expected) | ||||
else: | else: | ||||
raise self.failureException("Unknown type for %s" % obj) | raise self.failureException("Unknown type for %s" % obj) | ||||
class TarballIterDirectory(DataMixin, unittest.TestCase): | |||||
def setUp(self): | |||||
super().setUp() | |||||
self.make_from_tarball(self.tmpdir_name) | |||||
def test_iter_directory(self): | |||||
"""Iter from_disk.directory should yield the full arborescence tree | |||||
""" | |||||
directory = Directory.from_disk( | |||||
path=os.path.join(self.tmpdir_name, b"sample-folder") | |||||
) | |||||
contents, skipped_contents, directories = from_disk.iter_directory(directory) | |||||
expected_nb = defaultdict(int) | |||||
for name in self.tarball_contents.keys(): | |||||
obj = directory[name] | |||||
expected_nb[obj.object_type] += 1 | |||||
assert len(contents) == expected_nb["content"] and len(contents) > 0 | |||||
assert len(skipped_contents) == 0 | |||||
assert len(directories) == expected_nb["directory"] and len(directories) > 0 | |||||
class DirectoryManipulation(DataMixin, unittest.TestCase): | class DirectoryManipulation(DataMixin, unittest.TestCase): | ||||
def test_directory_access_nested(self): | def test_directory_access_nested(self): | ||||
d = Directory() | d = Directory() | ||||
d[b"a"] = Directory() | d[b"a"] = Directory() | ||||
d[b"a/b"] = Directory() | d[b"a/b"] = Directory() | ||||
self.assertEqual(d[b"a/b"].get_data(), self.empty_directory) | self.assertEqual(d[b"a/b"].get_data(), self.empty_directory) | ||||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |