def test_put_item(tmp_dir_fixture): # NOQA from dtoolcore.storagebroker import DiskStorageBroker destination_path = os.path.join(tmp_dir_fixture, 'my_proto_dataset') storagebroker = DiskStorageBroker(destination_path) storagebroker.create_structure() input_file_path = os.path.join(TEST_SAMPLE_DATA, 'tiny.png') storagebroker.put_item(fpath=input_file_path, relpath='tiny.png') handles = list(storagebroker.iter_item_handles()) assert 'tiny.png' in handles
def test_unix_relpaths_from_iter_item_handles(tmp_dir_fixture): # NOQA from dtoolcore.storagebroker import DiskStorageBroker destination_path = os.path.join(tmp_dir_fixture, 'my_proto_dataset') storagebroker = DiskStorageBroker(destination_path) storagebroker.create_structure() # Add a data file. data_subdir = os.path.join(storagebroker._data_abspath, "level") os.mkdir(data_subdir) data_fpath = os.path.join(data_subdir, "sample.txt") with open(data_fpath, "w") as fh: fh.write("some sample data") handles = [h for h in storagebroker.iter_item_handles()] assert len(handles) == 1 assert handles[0] == "level/sample.txt"
def test_item_properties(tmp_dir_fixture): # NOQA from dtoolcore.storagebroker import DiskStorageBroker destination_path = os.path.join(tmp_dir_fixture, 'my_proto_dataset') storagebroker = DiskStorageBroker(destination_path) storagebroker.create_structure() input_file_path = os.path.join(TEST_SAMPLE_DATA, 'tiny.png') storagebroker.put_item(fpath=input_file_path, relpath='tiny.png') handles = list(storagebroker.iter_item_handles()) handle = handles[0] item_properties = storagebroker.item_properties(handle) # Check size_in_bytes property assert item_properties['size_in_bytes'] == 276 # Check timestamp property assert 'utc_timestamp' in item_properties time_from_item = datetime.datetime.fromtimestamp(float( item_properties['utc_timestamp']), tz=pytz.UTC) time.sleep(0.1) # Make tests more robust on Windows. time_delta = datetime.datetime.now(tz=pytz.UTC) - time_from_item assert time_delta.days == 0 assert time_delta.seconds < 20 # Check hash property from dtoolcore.filehasher import md5sum_hexdigest expected_hash = md5sum_hexdigest(input_file_path) assert item_properties['hash'] == expected_hash # Check relpath property assert item_properties['relpath'] == 'tiny.png'