Exemplo n.º 1
0
    def test_get_file_map_memoization(self):
        node = factories.NodeFactory()
        comp1 = factories.NodeFactory(parent=node)
        comp1a = factories.NodeFactory(parent=comp1)
        comp2 = factories.NodeFactory(parent=node)

        file_trees = {
            n._id: file_tree_factory(3, 3, 3)
            for n in [node, comp1, comp1a, comp2]
        }
        patches = {}
        mocks = {}
        for n in [node, comp1, comp1a, comp2]:
            file_tree = file_trees[n._id]
            osfstorage = n.get_addon('osfstorage')
            mocked = mock.Mock(return_value=file_tree)
            patch = mock.patch.object(osfstorage, '_get_file_tree', mocked)
            patch.start()
            patches[n._id] = patch
            mocks[n._id] = mocked
        # first call
        file_map = archiver_utils.get_file_map(node)
        file_map = {sha256: value for sha256, value, _ in file_map}
        for mocked in mocks.values():
            mocked.assert_called_once()
        # second call
        file_map = archiver_utils.get_file_map(node)
        file_map = {sha256: value for sha256, value, _ in file_map}
        for mocked in mocks.values():
            mocked.assert_called_once()
        for patch in patches.values():
            patch.stop()
Exemplo n.º 2
0
    def test_get_file_map_memoization(self):
        node = factories.NodeFactory()
        comp1 = factories.NodeFactory(parent=node)
        factories.NodeFactory(parent=comp1)
        factories.NodeFactory(parent=node)

        with mock.patch.object(BaseStorageAddon, '_get_file_tree') as mock_get_file_tree:
            mock_get_file_tree.return_value = file_tree_factory(3, 3, 3)

            # first call
            archiver_utils.get_file_map(node)
            call_count = mock_get_file_tree.call_count
            # second call
            archiver_utils.get_file_map(node)
            assert_equal(mock_get_file_tree.call_count, call_count)
Exemplo n.º 3
0
    def test_get_file_map_memoization(self):
        node = factories.NodeFactory()
        comp1 = factories.NodeFactory(parent=node)
        factories.NodeFactory(parent=comp1)
        factories.NodeFactory(parent=node)

        with mock.patch.object(BaseStorageAddon, '_get_file_tree') as mock_get_file_tree:
            mock_get_file_tree.return_value = file_tree_factory(3, 3, 3)

            # first call
            archiver_utils.get_file_map(node)
            call_count = mock_get_file_tree.call_count
            # second call
            archiver_utils.get_file_map(node)
            assert_equal(mock_get_file_tree.call_count, call_count)
Exemplo n.º 4
0
    def test_get_file_map_with_components(self):
        node = factories.NodeFactory()
        comp1 = factories.NodeFactory(parent=node)
        comp1a = factories.NodeFactory(parent=comp1)
        comp2 = factories.NodeFactory(parent=node)

        file_trees = {
            n._id: file_tree_factory(3, 3, 3)
            for n in [node, comp1, comp1a, comp2]
        }

        patches = []
        for n in [node, comp1, comp1a, comp2]:
            file_tree = file_trees[n._id]
            osfstorage = n.get_addon('osfstorage')
            patch = mock.patch.object(osfstorage, '_get_file_tree',
                                      mock.Mock(return_value=file_tree))
            patch.start()
            patches.append(patch)

        file_map = archiver_utils.get_file_map(node)
        stack = file_trees.values()
        file_map = {sha256: value for sha256, value, _ in file_map}
        while len(stack):
            item = stack.pop(0)
            if item['kind'] == 'file':
                sha256 = item['extra']['hashes']['sha256']
                assert_in(sha256, file_map)
                map_file = file_map[sha256]
                assert_equal(item, map_file)
            else:
                stack = stack + item['children']

        for patch in patches:
            patch.stop()
Exemplo n.º 5
0
    def test_get_file_map_memoization(self):
        node = factories.NodeFactory()
        comp1 = factories.NodeFactory(parent=node)
        comp1a = factories.NodeFactory(parent=comp1)
        comp2 = factories.NodeFactory(parent=node)

        file_trees = {
            n._id: file_tree_factory(3, 3, 3)
            for n in [node, comp1, comp1a, comp2]
        }
        patches = {}
        mocks = {}
        for n in [node, comp1, comp1a, comp2]:
            file_tree = file_trees[n._id]
            osfstorage = n.get_addon('osfstorage')
            mocked = mock.Mock(return_value=file_tree)
            patch = mock.patch.object(osfstorage, '_get_file_tree', mocked)
            patch.start()
            patches[n._id] = patch
            mocks[n._id] = mocked
            n_patch = mock.patch.object(
                n,
                'get_addon',
                mock.Mock(return_value=osfstorage)
            )
            n_patch.start()
            patches[osfstorage._id] = n_patch

        # first call
        file_map = archiver_utils.get_file_map(node)
        file_map = {
            sha256: value
            for sha256, value, _ in file_map
        }
        for mocked in mocks.values():
            assert_equal(mocked.call_count, 1)
        # second call
        file_map = archiver_utils.get_file_map(node)
        file_map = {
            sha256: value
            for sha256, value, _ in file_map
        }
        for mocked in mocks.values():
            assert_equal(mocked.call_count, 1)
        for patch in patches.values():
            patch.stop()
Exemplo n.º 6
0
def find_registration_file(value, node):
    orig_sha256 = value['extra']['sha256']
    orig_name = value['extra']['selectedFileName']
    orig_node = value['extra']['nodeId']
    file_map = utils.get_file_map(node)
    for sha256, value, node_id in file_map:
        registered_from_id = Node.load(node_id).registered_from._id
        if sha256 == orig_sha256 and registered_from_id == orig_node and orig_name == value['name']:
            return value, node_id
    return None, None
Exemplo n.º 7
0
def find_registration_file(value, node):
    orig_sha256 = value['extra']['sha256']
    orig_name = value['extra']['selectedFileName']
    orig_node = value['extra']['nodeId']
    file_map = utils.get_file_map(node)
    for sha256, value, node_id in file_map:
        registered_from_id = Node.load(node_id).registered_from._id
        if sha256 == orig_sha256 and registered_from_id == orig_node and orig_name == value['name']:
            return value, node_id
    return None, None
Exemplo n.º 8
0
 def test_get_file_map(self):
     node = factories.NodeFactory(creator=self.user)
     file_tree = file_tree_factory(3, 3, 3)
     with mock.patch.object(StorageAddonBase, '_get_file_tree',
                            mock.Mock(return_value=file_tree)):
         file_map = archiver_utils.get_file_map(node)
     stack = [file_tree]
     file_map = {sha256: value for sha256, value, _ in file_map}
     while len(stack):
         item = stack.pop(0)
         if item['kind'] == 'file':
             sha256 = item['extra']['hashes']['sha256']
             assert_in(sha256, file_map)
             map_file = file_map[sha256]
             assert_equal(item, map_file)
         else:
             stack = stack + item['children']
Exemplo n.º 9
0
    def test_get_file_map_with_components(self):
        node = factories.NodeFactory()
        comp1 = factories.NodeFactory(parent=node)
        comp1a = factories.NodeFactory(parent=comp1)
        comp2 = factories.NodeFactory(parent=node)

        file_trees = {
            n._id: file_tree_factory(3, 3, 3)
            for n in [node, comp1, comp1a, comp2]
        }

        patches = []
        for n in [node, comp1, comp1a, comp2]:
            file_tree = file_trees[n._id]
            osfstorage = n.get_addon('osfstorage')
            patch = mock.patch.object(osfstorage, '_get_file_tree', mock.Mock(return_value=file_tree))
            patch.start()
            patches.append(patch)
            n_patch = mock.patch.object(
                n,
                'get_addon',
                mock.Mock(return_value=osfstorage)
            )
            n_patch.start()
            patches.append(n_patch)

        file_map = archiver_utils.get_file_map(node)
        stack = file_trees.values()
        file_map = {
            sha256: value
            for sha256, value, _ in file_map
        }
        while len(stack):
            item = stack.pop(0)
            if item['kind'] == 'file':
                sha256 = item['extra']['hashes']['sha256']
                assert_in(sha256, file_map)
                map_file = file_map[sha256]
                assert_equal(item, map_file)
            else:
                stack = stack + item['children']

        for patch in patches:
            patch.stop()
Exemplo n.º 10
0
 def test_get_file_map(self):
     node = factories.NodeFactory(creator=self.user)
     file_tree = file_tree_factory(3, 3, 3)
     with mock.patch.object(StorageAddonBase, '_get_file_tree', mock.Mock(return_value=file_tree)):
         file_map = archiver_utils.get_file_map(node)
     stack = [file_tree]
     file_map = {
         sha256: value
         for sha256, value, _ in file_map
     }
     while len(stack):
         item = stack.pop(0)
         if item['kind'] == 'file':
             sha256 = item['extra']['hashes']['sha256']
             assert_in(sha256, file_map)
             map_file = file_map[sha256]
             assert_equal(item, map_file)
         else:
             stack = stack + item['children']