Exemple #1
0
class ResourceMapperTestCase(unittest.TestCase):
    """Test the resource mapper."""
    def setUp(self):
        super(ResourceMapperTestCase, self).setUp()
        self.mapper = ResourceMapper()

    def test_mapping(self):
        """Test mapping."""
        self.assertEqual(self.mapper.user(), '')
        self.assertEqual(self.mapper.volume('~/1'), '/volumes/~/1')
        self.assertEqual(self.mapper.node('~/1', '/x'), '/~/1/x')

    def test_mapping_override(self):
        """Test mapping."""
        self.mapper.root = ''
        self.mapper.mapping['NODE_INFO'] = '/n/%(node_path)s'
        self.assertEqual(self.mapper.user(), '')
        self.assertEqual(self.mapper.volume(1), '/volumes/1')
        self.assertEqual(self.mapper.node('a', 'b'), '/n/b')

    def test_user_repr(self):
        """Test Rest conversion of a user."""
        user = MockUser()
        udf = MockVolume()
        info = self.mapper.user_repr(user, [udf])
        self.assertEqual(info['user_id'], user.id)
        self.assertEqual(info['visible_name'], user.visible_name)
        self.assertEqual(info['used_bytes'], user.used_storage_bytes)
        self.assertEqual(info['max_bytes'], user.max_storage_bytes)
        self.assertEqual(info['root_node_path'],
                         self.mapper.node(settings.ROOT_USERVOLUME_PATH))
        self.assertEqual(info['user_node_paths'], [self.mapper.node(udf.path)])

    def test_volume_repr(self):
        """Test Rest conversion of a volume."""
        udf = MockVolume()
        info = self.mapper.volume_repr(udf)
        self.assertEqual(info['resource_path'], '/volumes/~/Documents')
        self.assertEqual(info['type'], 'root' if udf.is_root else 'udf')
        self.assertEqual(info['path'], udf.path)
        self.assertEqual(info['generation'], udf.generation)
        self.assertEqual(info['node_path'], self.mapper.node(udf.path))
        self.assertEqual(info['when_created'],
                         date_formatter(udf.when_created))

    def test_volume_with_delta_repr0(self):
        """Test Rest conversion of a vol with delta information, no nodes."""
        udf = MockVolume()
        nodes = []
        info = self.mapper.volume_repr(volume=udf,
                                       from_generation=0,
                                       nodes=nodes)
        self.assertEqual(info['resource_path'], '/volumes/~/Documents')
        self.assertEqual(info['type'], 'root' if udf.is_root else 'udf')
        self.assertEqual(info['path'], udf.path)
        self.assertEqual(info['generation'], udf.generation)
        self.assertEqual(info['node_path'], self.mapper.node(udf.path))
        self.assertEqual(info['when_created'],
                         date_formatter(udf.when_created))
        self.assertEqual(info['delta']['from_generation'], 0)
        self.assertEqual(info['delta']['nodes'], nodes)

    def test_volume_with_delta_repr1(self):
        """Test Rest conversion of a volume with delta innformation,
        with nodes."""
        udf = MockVolume()
        nodes = [MockNode(), MockNode()]
        info = self.mapper.volume_repr(volume=udf,
                                       from_generation=0,
                                       nodes=nodes)
        self.assertEqual(info['delta']['from_generation'], 0)
        self.assertEqual(info['delta']['nodes'],
                         [self.mapper.node_repr(node) for node in nodes])

    def test_file_node_repr(self):
        """Test Rest conversion of a file node."""
        f1 = MockNode()
        info = self.mapper.node_repr(f1)
        self.assertEqual(info['key'], f1.nodekey)
        self.assertEqual(info['kind'], f1.kind.lower())
        self.assertEqual(info['path'], f1.full_path)
        self.assertEqual(info['hash'], f1.content_hash)
        self.assertEqual(info['when_created'], date_formatter(f1.when_created))
        self.assertEqual(info['when_changed'],
                         date_formatter(f1.when_last_modified))
        self.assertEqual(info['generation'], f1.generation)
        self.assertEqual(info['generation_created'], f1.generation_created)
        self.assertEqual(info['public_url'], f1.public_url)
        self.assertEqual(info['is_public'], f1.is_public)
        self.assertEqual(info['parent_path'],
                         "/%s/a/b/c/d" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(info['volume_path'],
                         "/volumes/%s" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(
            info['content_path'],
            '/content/%s/a/b/c/d/file.txt' % settings.ROOT_USERVOLUME_PATH)
        # make sure file specific rules apply
        self.assertTrue('has_children' not in info)
        self.assertEqual(info['is_live'], True)

    def test_dir_node_repr(self):
        """Utility method to test Rest conversion of a directory node."""
        f1 = MockNode()
        f1.kind = StorageObject.DIRECTORY
        info = self.mapper.node_repr(f1)
        self.assertEqual(info['key'], f1.nodekey)
        self.assertEqual(info['kind'], f1.kind.lower())
        self.assertEqual(info['path'], f1.full_path)
        self.assertEqual(info['when_created'], date_formatter(f1.when_created))
        self.assertEqual(info['when_changed'],
                         date_formatter(f1.when_last_modified))
        self.assertEqual(info['generation'], f1.generation)
        self.assertEqual(info['generation_created'], f1.generation_created)
        self.assertEqual(info['parent_path'],
                         "/%s/a/b/c/d" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(info['volume_path'],
                         "/volumes/%s" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(
            info['content_path'],
            '/content/%s/a/b/c/d/file.txt' % settings.ROOT_USERVOLUME_PATH)
        # make sure directory specific rules apply
        self.assertTrue('hash' not in info)
        self.assertTrue('is_public' not in info)
        self.assertTrue('public_url' not in info)
        self.assertTrue('has_children' in info)
        self.assertEqual(info['is_live'], True)

    def test_root_dir_node_repr(self):
        """Utility method to test Rest conversion of a root directory node."""
        f1 = MockNode()
        f1.kind = StorageObject.DIRECTORY
        f1.name = ""
        f1.path = '/'
        f1.full_path = "/"
        info = self.mapper.node_repr(f1)
        self.assertEqual(info['key'], f1.nodekey)
        self.assertEqual(info['kind'], f1.kind.lower())
        self.assertEqual(info['path'], f1.full_path)
        self.assertEqual(info['when_created'], date_formatter(f1.when_created))
        self.assertEqual(info['when_changed'],
                         date_formatter(f1.when_last_modified))
        self.assertEqual(info['generation'], f1.generation)
        self.assertEqual(info['generation_created'], f1.generation_created)
        self.assertEqual(info['parent_path'], None)
        self.assertEqual(info['volume_path'],
                         "/volumes/%s" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(info['content_path'],
                         '/content/%s' % settings.ROOT_USERVOLUME_PATH)
        # make sure directory specific rules apply
        self.assertTrue('hash' not in info)
        self.assertTrue('is_public' not in info)
        self.assertTrue('public_url' not in info)
        self.assertTrue('has_children' in info)
        self.assertEqual(info['is_live'], True)
class ResourceMapperTestCase(unittest.TestCase):
    """Test the resource mapper."""
    def setUp(self):
        super(ResourceMapperTestCase, self).setUp()
        self.mapper = ResourceMapper()

    def test_mapping(self):
        """Test mapping."""
        self.assertEqual(self.mapper.user(), '')
        self.assertEqual(self.mapper.volume('~/1'), '/volumes/~/1')
        self.assertEqual(self.mapper.node('~/1', '/x'), '/~/1/x')

    def test_mapping_override(self):
        """Test mapping."""
        self.mapper.root = ''
        self.mapper.mapping['NODE_INFO'] = '/n/%(node_path)s'
        self.assertEqual(self.mapper.user(), '')
        self.assertEqual(self.mapper.volume(1), '/volumes/1')
        self.assertEqual(self.mapper.node('a', 'b'), '/n/b')

    def test_user_repr(self):
        """Test Rest conversion of a user."""
        user = MockUser()
        udf = MockVolume()
        info = self.mapper.user_repr(user, [udf])
        self.assertEqual(info['user_id'], user.id)
        self.assertEqual(info['visible_name'], user.visible_name)
        self.assertEqual(info['used_bytes'], user.used_storage_bytes)
        self.assertEqual(info['max_bytes'], user.max_storage_bytes)
        self.assertEqual(
            info['root_node_path'],
            self.mapper.node(settings.ROOT_USERVOLUME_PATH))
        self.assertEqual(
            info['user_node_paths'], [self.mapper.node(udf.path)])

    def test_volume_repr(self):
        """Test Rest conversion of a volume."""
        udf = MockVolume()
        info = self.mapper.volume_repr(udf)
        self.assertEqual(info['resource_path'], '/volumes/~/Documents')
        self.assertEqual(info['type'], 'root' if udf.is_root else 'udf')
        self.assertEqual(info['path'], udf.path)
        self.assertEqual(info['generation'], udf.generation)
        self.assertEqual(info['node_path'], self.mapper.node(udf.path))
        self.assertEqual(
            info['when_created'], date_formatter(udf.when_created))

    def test_volume_with_delta_repr0(self):
        """Test Rest conversion of a vol with delta information, no nodes."""
        udf = MockVolume()
        nodes = []
        info = self.mapper.volume_repr(
            volume=udf, from_generation=0, nodes=nodes)
        self.assertEqual(info['resource_path'], '/volumes/~/Documents')
        self.assertEqual(info['type'], 'root' if udf.is_root else 'udf')
        self.assertEqual(info['path'], udf.path)
        self.assertEqual(info['generation'], udf.generation)
        self.assertEqual(info['node_path'], self.mapper.node(udf.path))
        self.assertEqual(
            info['when_created'], date_formatter(udf.when_created))
        self.assertEqual(info['delta']['from_generation'], 0)
        self.assertEqual(info['delta']['nodes'], nodes)

    def test_volume_with_delta_repr1(self):
        """Test Rest conversion of a volume with delta innformation,
        with nodes."""
        udf = MockVolume()
        nodes = [MockNode(), MockNode()]
        info = self.mapper.volume_repr(
            volume=udf, from_generation=0, nodes=nodes)
        self.assertEqual(info['delta']['from_generation'], 0)
        self.assertEqual(
            info['delta']['nodes'],
            [self.mapper.node_repr(node) for node in nodes])

    def test_file_node_repr(self):
        """Test Rest conversion of a file node."""
        f1 = MockNode()
        info = self.mapper.node_repr(f1)
        self.assertEqual(info['key'], f1.nodekey)
        self.assertEqual(info['kind'], f1.kind.lower())
        self.assertEqual(info['path'], f1.full_path)
        self.assertEqual(info['hash'], f1.content_hash)
        self.assertEqual(
            info['when_created'], date_formatter(f1.when_created))
        self.assertEqual(
            info['when_changed'], date_formatter(f1.when_last_modified))
        self.assertEqual(info['generation'], f1.generation)
        self.assertEqual(info['generation_created'], f1.generation_created)
        self.assertEqual(info['public_url'], f1.public_url)
        self.assertEqual(info['is_public'], f1.is_public)
        self.assertEqual(
            info['parent_path'], "/%s/a/b/c/d" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(
            info['volume_path'], "/volumes/%s" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(
            info['content_path'],
            '/content/%s/a/b/c/d/file.txt' % settings.ROOT_USERVOLUME_PATH)
        # make sure file specific rules apply
        self.assertTrue('has_children' not in info)
        self.assertEqual(info['is_live'], True)

    def test_dir_node_repr(self):
        """Utility method to test Rest conversion of a directory node."""
        f1 = MockNode()
        f1.kind = StorageObject.DIRECTORY
        info = self.mapper.node_repr(f1)
        self.assertEqual(info['key'], f1.nodekey)
        self.assertEqual(info['kind'], f1.kind.lower())
        self.assertEqual(info['path'], f1.full_path)
        self.assertEqual(
            info['when_created'], date_formatter(f1.when_created))
        self.assertEqual(
            info['when_changed'], date_formatter(f1.when_last_modified))
        self.assertEqual(info['generation'], f1.generation)
        self.assertEqual(info['generation_created'], f1.generation_created)
        self.assertEqual(
            info['parent_path'],
            "/%s/a/b/c/d" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(
            info['volume_path'], "/volumes/%s" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(
            info['content_path'],
            '/content/%s/a/b/c/d/file.txt' % settings.ROOT_USERVOLUME_PATH)
        # make sure directory specific rules apply
        self.assertTrue('hash' not in info)
        self.assertTrue('is_public' not in info)
        self.assertTrue('public_url' not in info)
        self.assertTrue('has_children' in info)
        self.assertEqual(info['is_live'], True)

    def test_root_dir_node_repr(self):
        """Utility method to test Rest conversion of a root directory node."""
        f1 = MockNode()
        f1.kind = StorageObject.DIRECTORY
        f1.name = ""
        f1.path = '/'
        f1.full_path = "/"
        info = self.mapper.node_repr(f1)
        self.assertEqual(info['key'], f1.nodekey)
        self.assertEqual(info['kind'], f1.kind.lower())
        self.assertEqual(info['path'], f1.full_path)
        self.assertEqual(info['when_created'], date_formatter(f1.when_created))
        self.assertEqual(
            info['when_changed'], date_formatter(f1.when_last_modified))
        self.assertEqual(info['generation'], f1.generation)
        self.assertEqual(info['generation_created'], f1.generation_created)
        self.assertEqual(info['parent_path'], None)
        self.assertEqual(
            info['volume_path'], "/volumes/%s" % settings.ROOT_USERVOLUME_PATH)
        self.assertEqual(
            info['content_path'],
            '/content/%s' % settings.ROOT_USERVOLUME_PATH)
        # make sure directory specific rules apply
        self.assertTrue('hash' not in info)
        self.assertTrue('is_public' not in info)
        self.assertTrue('public_url' not in info)
        self.assertTrue('has_children' in info)
        self.assertEqual(info['is_live'], True)