def test_can_finalize(blockchain_path, base_filename, optimized_file_path):
    fss = PathOptimizedFileSystemStorage(base_path=blockchain_path)
    fss.save(base_filename, b'\x08Test')
    assert os_stat(optimized_file_path).st_mode & (stat.S_IWGRP | stat.S_IWUSR | stat.S_IWOTH) != 0

    fss.finalize(base_filename)
    assert os_stat(optimized_file_path).st_mode & (stat.S_IWGRP | stat.S_IWUSR | stat.S_IWOTH) == 0
def test_can_load(blockchain_path, base_filename, optimized_file_path):
    binary_data = b'\x08Test'

    fss = PathOptimizedFileSystemStorage(base_path=blockchain_path)
    fss.save(base_filename, binary_data)
    assert os.path.isfile(optimized_file_path)
    with open(optimized_file_path, 'rb') as fo:
        assert fo.read() == binary_data

    assert fss.load(base_filename) == binary_data
def test_can_append(blockchain_path, base_filename, optimized_file_path):
    fss = PathOptimizedFileSystemStorage(base_path=blockchain_path)
    fss.save(base_filename, b'\x08Test')
    assert os.path.isfile(optimized_file_path)
    with open(optimized_file_path, 'rb') as fo:
        assert fo.read() == b'\x08Test'

    fss.append(base_filename, b'\x09\x0aAPPEND')
    with open(optimized_file_path, 'rb') as fo:
        assert fo.read() == b'\x08Test\x09\x0aAPPEND'
def test_compression(blockchain_path, base_filename, optimized_file_path):
    binary_data = b'A' * 10000

    fss = PathOptimizedFileSystemStorage(base_path=blockchain_path, compressors=('gz',))
    fss.save(base_filename, binary_data, is_final=True)
    expected_path = optimized_file_path + '.gz'
    assert os.path.isfile(expected_path)

    with gzip.open(expected_path, 'rb') as fo:
        assert fo.read() == binary_data

    assert fss.load(base_filename) == binary_data
def test_move(blockchain_path):
    source = 'file1.txt'
    destination = 'file2.txt'

    storage = PathOptimizedFileSystemStorage(blockchain_path, max_depth=5)
    storage.save(source, b'AAA')
    assert os.path.isfile(str(blockchain_path / 'f/i/l/e/1/file1.txt'))

    storage.move(source, destination)
    assert os.path.isfile(str(blockchain_path / 'f/i/l/e/2/file2.txt'))
    assert not os.path.isfile(str(blockchain_path / 'f/i/l/e/1/file1.txt'))
    assert storage.load(destination) == b'AAA'
def test_list_directory(blockchain_directory):
    base_directory = os.path.join(blockchain_directory, 'test')

    fss = PathOptimizedFileSystemStorage(base_directory, compressors=('gz',))
    fss.save('1434567890.txt', b'A' * 1000, is_final=True)
    fss.save('1134567890.txt', b'test1')
    fss.save('1234567890.txt', b'test2')
    fss.save('1334567890.txt', b'test3')

    assert {
        '1134567890.txt',
        '1234567890.txt',
        '1334567890.txt',
        '1434567890.txt',
    } == set(fss.list_directory())
def test_can_save_to_optimized_path(blockchain_path):
    storage = PathOptimizedFileSystemStorage(blockchain_path)
    with patch('thenewboston_node.business_logic.storages.file_system.FileSystemStorage.save') as save_mock:
        storage.save('parent/file.txt', b'test data')

    save_mock.assert_called_once_with('parent/f/i/l/e/file.txt', b'test data', is_final=False)