Esempio n. 1
0
def test_copy_all():
    item = FileItem(
        filename='foo.txt',
        path=('folder', ),
        data=BytesIO(b'contents'),
        media_type='stuff',
    )

    new_item = item.copy()

    # Identical tuples are identical
    assert new_item == item
    assert new_item.data is item.data
Esempio n. 2
0
def test_copy_new_data():
    item = FileItem(
        filename='foo.txt',
        path=('folder', ),
        data=BytesIO(b'contents'),
        media_type='stuff',
    )

    new_item = item.copy(data=BytesIO(b'other'))

    # Tuple is no longer identical as the data is different
    assert new_item != item
    assert new_item.filename == item.filename
    assert new_item.path == item.path
    assert new_item.media_type == item.media_type
Esempio n. 3
0
def test_fs_path():
    item = FileItem(filename='foo.txt', path=('folder', 'subfolder'))

    if os.name == 'nt':
        assert item.fs_path == 'folder\\subfolder\\foo.txt'
    else:
        assert item.fs_path == 'folder/subfolder/foo.txt'
Esempio n. 4
0
async def test_async_fileitem_reader():
    item = FileItem(filename='foo.txt', data=BytesIO(b'contents'))
    item.data.seek(3)

    async with item as f:
        assert await f.read() == b'contents'
        await f.seek(3)
        assert await f.read() == b'tents'
Esempio n. 5
0
 def assert_get_modified_time(
     self, filename: str, path: Tuple[str, ...], date: datetime
 ) -> None:
     """Assert that given file modification time is equal to the anticipated time."""
     assert (
         self._get_modified_time(FileItem(filename=filename, path=path))
         == date
     )
Esempio n. 6
0
    async def async_resolve_filename(self, item: FileItem) -> FileItem:
        """Ensures a unique name for this file in the folder"""
        if not await self._async_exists(item):
            return item

        basename, ext = os.path.splitext(item.filename)
        for counter in range(1, 1000000):
            filename = f'{basename}-{counter}{ext}'
            item = item.copy(filename=filename)
            if not await self._async_exists(item):
                return item
        else:
            raise RuntimeError(
                f'Cannot get unique name for file {basename}{ext}')
Esempio n. 7
0
def test_has_data():
    item1 = FileItem(filename='foo.txt')
    item2 = FileItem(filename='foo.txt', data=BytesIO(b''))

    assert not item1.has_data
    assert item2.has_data
Esempio n. 8
0
def test_url_path():
    item = FileItem(filename='foo.txt', path=('folder', 'subfolder'))

    assert item.url_path == 'folder/subfolder/foo.txt'
Esempio n. 9
0
 def assert_file_contains(
     self, filename: str, path: Tuple[str, ...], data: bytes
 ) -> None:
     """Assert that the given file contains the given data."""
     item = FileItem(filename=filename, path=path)
     assert self.files[item.url_path] == data
Esempio n. 10
0
 def _apply(self, item: FileItem) -> FileItem:
     return item.copy(filename='filtered_name.txt')
def test_get_item():
    handler = DummyHandler(path=['foo'])
    item = handler.get_item('file.txt')

    assert isinstance(item, FileItem)
    assert item == FileItem(filename='file.txt', path=('foo', ))
 def _apply(self, item: FileItem) -> FileItem:
     self.mock._apply(item)
     # append the id_ to the filename
     return item.copy(filename=item.filename + self.id_)
Esempio n. 13
0
 def assert_get_size(
     self, filename: str, path: Tuple[str, ...], size: int
 ) -> None:
     """Assert that given file size is equal to the anticipated size."""
     assert self._get_size(FileItem(filename=filename, path=path)) == size
Esempio n. 14
0
 def assert_exists(self, filename: str, path: Tuple[str, ...]) -> None:
     """Assert that the given file exists in the dummy file system."""
     assert self._exists(FileItem(filename=filename, path=path))
Esempio n. 15
0
 def get_file_key(self, item: FileItem) -> FileItem:
     return item.copy(data=None)
Esempio n. 16
0
def test_content_type_guess(filename, mediatype):
    item = FileItem(filename=filename)

    assert item.content_type == mediatype
Esempio n. 17
0
def text_content_type_fixed():
    item = FileItem(filename='foo.txt', media_type='wacky')

    assert item.content_type == 'wacky'
def item():
    return FileItem(
        filename='file.txt', path=('folder',), data=BytesIO(b'content')
    )
    def _apply(self, item: FileItem) -> FileItem:

        name, ext = os.path.splitext(item.filename)
        randomized_name = self.name_generator(name) + ext.lower()

        return item.copy(filename=randomized_name)