Skip to content

Commit

Permalink
#114 Made path a sub-module
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb committed Apr 23, 2024
1 parent b6b4fc8 commit 1f2d399
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
10 changes: 2 additions & 8 deletions exasol/bucketfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@
as_hash,
as_string,
)
from exasol.bucketfs._path import (
PathLike,
StorageBackend,
build_path
)
from exasol.bucketfs import _path as path
from exasol.bucketfs._error import BucketFsError
from exasol.bucketfs._service import Service

Expand All @@ -70,9 +66,7 @@
"Bucket",
"MappedBucket",
"BucketFsError",
"PathLike",
"StorageBackend",
"build_path",
"path",
"as_bytes",
"as_string",
"as_file",
Expand Down
22 changes: 14 additions & 8 deletions test/integration/test_bucket_path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from typing import ByteString
import pytest
from exasol.bucketfs._path import PathLike, build_path, StorageBackend
import exasol.bucketfs as bfs
from integration.conftest import delete_file


Expand All @@ -25,7 +25,7 @@ def classic_poem() -> ByteString:
return poem_text


def _collect_all_names(path: PathLike) -> set[str]:
def _collect_all_names(path: bfs.path.PathLike) -> set[str]:
all_names = []
for _, dirs, files in path.walk():
all_names.extend(dirs)
Expand All @@ -35,8 +35,10 @@ def _collect_all_names(path: PathLike) -> set[str]:

def test_write_read_back_onprem(test_config, children_poem):

base_path = build_path(backend=StorageBackend.onprem, url=test_config.url, verify=False,
username=test_config.username, password=test_config.password)
base_path = bfs.path.build_path(backend=bfs.path.StorageBackend.onprem,
url=test_config.url, verify=False,
username=test_config.username,
password=test_config.password)
file_name = 'my_poems/little_star.txt'
poem_path = base_path / file_name

Expand All @@ -57,8 +59,10 @@ def test_write_read_back_onprem(test_config, children_poem):

def test_write_list_files_onprem(test_config, children_poem, classic_poem):

base_path = build_path(backend=StorageBackend.onprem, url=test_config.url, path='my_poems',
verify=False, username=test_config.username, password=test_config.password)
base_path = bfs.path.build_path(backend=bfs.path.StorageBackend.onprem,
url=test_config.url, path='my_poems', verify=False,
username=test_config.username,
password=test_config.password)
poem_path1 = base_path / 'children/little_star.txt'
poem_path2 = base_path / 'classic/highlands.txt'

Expand All @@ -81,8 +85,10 @@ def test_write_list_files_onprem(test_config, children_poem, classic_poem):

def test_write_delete_onprem(test_config, children_poem, classic_poem):

base_path = build_path(backend=StorageBackend.onprem, url=test_config.url, verify=False,
username=test_config.username, password=test_config.password)
base_path = bfs.path.build_path(backend=bfs.path.StorageBackend.onprem,
url=test_config.url, verify=False,
username=test_config.username,
password=test_config.password)
poems_root = base_path / 'my_other_poems'
poem_path1 = poems_root / 'children/little_star.txt'
poem_path2 = poems_root / 'classic/highlands.txt'
Expand Down
56 changes: 28 additions & 28 deletions test/unit/test_bucket_path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from itertools import chain
import pytest
from exasol.bucketfs._path import BucketPath
import exasol.bucketfs as bfs


@pytest.mark.parametrize("test_path, should_exist", [
Expand All @@ -11,7 +11,7 @@
('dir1/dir3', False)
])
def test_file_exists(bucket_fake, test_path, should_exist):
path = BucketPath(test_path, bucket_api=bucket_fake)
path = bfs.path.BucketPath(test_path, bucket_api=bucket_fake)
assert path.exists() == should_exist


Expand All @@ -22,7 +22,7 @@ def test_file_exists(bucket_fake, test_path, should_exist):
('dir1/dir3', False)
])
def test_is_dir(bucket_fake, test_path, is_dir):
path = BucketPath(test_path, bucket_api=bucket_fake)
path = bfs.path.BucketPath(test_path, bucket_api=bucket_fake)
assert path.is_dir() == is_dir


Expand All @@ -33,81 +33,81 @@ def test_is_dir(bucket_fake, test_path, is_dir):
('dir1/dir3', False)
])
def test_is_file(bucket_fake, test_path, is_file):
path = BucketPath(test_path, bucket_api=bucket_fake)
path = bfs.path.BucketPath(test_path, bucket_api=bucket_fake)
assert path.is_file() == is_file


def test_rm(bucket_fake):
path = BucketPath('dir1/dir12/file120.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1/dir12/file120.dat', bucket_api=bucket_fake)
path.rm()
assert not path.exists()


def test_rm_not_exist(bucket_fake):
path = BucketPath('dir1/dir12/file125.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1/dir12/file125.dat', bucket_api=bucket_fake)
with pytest.raises(FileNotFoundError):
path.rm()


def test_rm_directory(bucket_fake):
path = BucketPath('dir1/dir12', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1/dir12', bucket_api=bucket_fake)
with pytest.raises(IsADirectoryError):
path.rm()


def test_rmdir(bucket_fake):
for i in range(2):
BucketPath(f'dir1/dir12/file12{i}.dat', bucket_api=bucket_fake).rm()
path = BucketPath('dir1/dir12', bucket_api=bucket_fake)
bfs.path.BucketPath(f'dir1/dir12/file12{i}.dat', bucket_api=bucket_fake).rm()
path = bfs.path.BucketPath('dir1/dir12', bucket_api=bucket_fake)
path.rmdir(recursive=False)
assert not path.exists()


def test_rmdir_recursive(bucket_fake):
path = BucketPath('dir1', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1', bucket_api=bucket_fake)
path.rmdir(recursive=True)
assert not path.exists()


def test_rmdir_not_empty(bucket_fake):
path = BucketPath('dir1', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1', bucket_api=bucket_fake)
with pytest.raises(OSError):
path.rmdir(recursive=False)


def test_rmdir_not_exist(bucket_fake):
path = BucketPath('dir1/dir5', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1/dir5', bucket_api=bucket_fake)
path.rmdir()


def test_rmdir_file(bucket_fake):
path = BucketPath('dir1/dir12/file120.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1/dir12/file120.dat', bucket_api=bucket_fake)
with pytest.raises(NotADirectoryError):
path.rmdir()


def test_joinpath(bucket_fake):
path1 = BucketPath('dir1', bucket_api=bucket_fake)
path1 = bfs.path.BucketPath('dir1', bucket_api=bucket_fake)
path2 = 'dir11'
path3 = BucketPath('dir111/dir1111', bucket_api=bucket_fake)
path3 = bfs.path.BucketPath('dir111/dir1111', bucket_api=bucket_fake)
path4 = Path('dir11111/file111110.dat')
path = path1.joinpath(path2, path3, path4)
assert isinstance(path, BucketPath)
assert isinstance(path, bfs.path.BucketPath)
assert str(path) == 'dir1/dir11/dir111/dir1111/dir11111/file111110.dat'


def test_truediv(bucket_fake):
path1 = BucketPath('dir1', bucket_api=bucket_fake)
path1 = bfs.path.BucketPath('dir1', bucket_api=bucket_fake)
path2 = 'dir11'
path3 = BucketPath('dir111/dir1111', bucket_api=bucket_fake)
path3 = bfs.path.BucketPath('dir111/dir1111', bucket_api=bucket_fake)
path4 = Path('dir11111/file111110.dat')
path = path1 / path2 / path3 / path4
assert isinstance(path, BucketPath)
assert isinstance(path, bfs.path.BucketPath)
assert str(path) == 'dir1/dir11/dir111/dir1111/dir11111/file111110.dat'


def test_walk_top_down(bucket_fake):
path = BucketPath('', bucket_api=bucket_fake)
path = bfs.path.BucketPath('', bucket_api=bucket_fake)
content = [','.join(chain([pth.name, '/'], sorted(dirs), sorted(files)))
for pth, dirs, files in path.walk(top_down=True)]
expected_content = [
Expand All @@ -123,7 +123,7 @@ def test_walk_top_down(bucket_fake):


def test_walk_bottom_up(bucket_fake):
path = BucketPath('', bucket_api=bucket_fake)
path = bfs.path.BucketPath('', bucket_api=bucket_fake)
content = [','.join(chain([pth.name, '/'], sorted(dirs), sorted(files)))
for pth, dirs, files in path.walk(top_down=False)]
expected_content = [
Expand All @@ -139,7 +139,7 @@ def test_walk_bottom_up(bucket_fake):


def test_iterdir(bucket_fake):
path = BucketPath('dir1', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1', bucket_api=bucket_fake)
content = set(str(node) for node in path.iterdir())
expected_content = {
'dir1/dir11',
Expand All @@ -151,37 +151,37 @@ def test_iterdir(bucket_fake):


def test_read(bucket_fake):
path = BucketPath('dir1/dir12/file121.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1/dir12/file121.dat', bucket_api=bucket_fake)
expected_chunk = bytes([12] * 8)
for chunk in path.read(chunk_size=8):
assert chunk == expected_chunk


def test_read_not_found(bucket_fake):
path = BucketPath('dir1/file12.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir1/file12.dat', bucket_api=bucket_fake)
with pytest.raises(FileNotFoundError):
list(path.read())


@pytest.mark.parametrize("file_name", ['file23.dat', 'file20.dat'])
def test_write_bytes(bucket_fake, file_name):
data = b'abcd'
path = BucketPath(f'dir2/{file_name}', bucket_api=bucket_fake)
path = bfs.path.BucketPath(f'dir2/{file_name}', bucket_api=bucket_fake)
path.write(data)
data_back = next(iter(path.read(100)))
assert data_back == data


def test_write_chunks(bucket_fake):
data_chunks = [b'abc', b'def', b'gh']
path = BucketPath('dir2/file23.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir2/file23.dat', bucket_api=bucket_fake)
path.write(data_chunks)
data_back = next(iter(path.read(100)))
assert data_back == b'abcdefgh'


def test_write_file(bucket_fake):
path = BucketPath('dir2/file_copy.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir2/file_copy.dat', bucket_api=bucket_fake)
source_file = bucket_fake.root / 'dir2/file21.dat'
with open(source_file, 'rb') as f:
path.write(f)
Expand All @@ -190,7 +190,7 @@ def test_write_file(bucket_fake):


def test_write_and_create_parent(bucket_fake):
path = BucketPath('dir2/dir21/file_copy.dat', bucket_api=bucket_fake)
path = bfs.path.BucketPath('dir2/dir21/file_copy.dat', bucket_api=bucket_fake)
assert not path.exists()
source_file = bucket_fake.root / 'dir2/file21.dat'
with open(source_file, 'rb') as f:
Expand Down

0 comments on commit 1f2d399

Please sign in to comment.