def test_hdf5_array_repository_remove_records(array_repository_w_saved_data):
    _, repo, _, ids = array_repository_w_saved_data

    prev_indices = {}
    for index in repo.indices:
        prev_indices[index] = repo.get_index(index)

    removed_ids = ranges.Key([ids[0]])
    repo.remove_records(removed_ids)

    removed_locations = repo.find(removed_ids)
    locations = repo.find(ids)

    found_ids = []
    for path, indices in locations.items():
        with hdf5.HDF5File() as file:
            dataset = file[path]
            for _id, idx in dataset.attrs.items():
                if idx in indices:
                    found_ids.append(_id)

    assert repo.get_num_records() == len(ids) - 1
    assert len(removed_locations) == 0
    assert len(found_ids) == len(ids) - 1
    assert sorted(found_ids) != ids

    for index in repo.indices:
        assert prev_indices[index] != repo.get_index(index)
def test_hdf5_file_context_manager():
    with hdf5.HDF5File() as file:
        assert os.path.exists(config.HDF5_FILEPATH)
        assert file
        assert len(list(file.keys())) == 0

    assert not file
def test_hdf5_array_repository_get_datasets(array_repository_w_saved_data):
    _, repo, datasets, ids = array_repository_w_saved_data

    with hdf5.HDF5File() as file:
        found_datasets = recursive_search(file[repo.GROUP], repo.GROUP)
        remove_meta_data_datasets(repo, found_datasets)

    assert repo.get_datasets() == found_datasets
def test_hdf5_file_create_group():
    group_name = 'test'
    with hdf5.HDF5File() as file:
        group1 = file.create_group(group_name)
        group2 = file.create_group(group_name)

        assert isinstance(group1, h5py.Group)
        assert isinstance(group2, h5py.Group)
        assert group1.name == group2.name
def test_hdf5_file_regular_open():
    file = hdf5.HDF5File()

    assert os.path.exists(config.HDF5_FILEPATH)
    assert file
    assert len(list(file.keys())) == 0

    file.close()

    assert not file
def test_hd5f_array_repository_save(array_repository_w_saved_data):
    arrays, repo, datasets, _ = array_repository_w_saved_data

    num_records = len(arrays.reg_combos) + len(arrays.cap_combos)

    # verify datasets have been made properly
    with hdf5.HDF5File() as file:
        found_datasets = recursive_search(file[repo.GROUP], repo.GROUP)
        assert found_datasets == datasets
        for dataset in datasets:
            if repo.INDEX_DATASET not in dataset:
                num_records -= len(file[repo.GROUP][dataset])
        assert num_records == 0
def test_hdf5_array_repository_find(array_repository_w_saved_data):
    _, repo, _, ids = array_repository_w_saved_data

    locations = repo.find(ids)

    found_ids = []
    for path, indices in locations.items():
        with hdf5.HDF5File() as file:
            dataset = file[path]
            for _id, idx in dataset.attrs.items():
                if idx in indices:
                    found_ids.append(_id)

    assert sorted(found_ids) == ids
def test_hdf5_array_repository_remove_dataset(array_repository_w_saved_data):
    _, repo, datasets, ids = array_repository_w_saved_data

    prev_indices = {}
    for index in repo.indices:
        prev_indices[index] = repo.get_index(index)

    repo.remove_dataset(datasets[0])

    with hdf5.HDF5File() as file:
        group = file[repo.GROUP]
        found_datasets = sorted(list(group.keys()))

        assert datasets != found_datasets
        for index in repo.indices:
            assert prev_indices[index] != repo.get_index(index)
def test_hdf5_obj_repository_save(obj_repository_w_saved_data):
    dicts, repo, datasets, _ = obj_repository_w_saved_data

    # verify datasets have been made properly
    with hdf5.HDF5File() as file:
        found_datasets = sorted(recursive_search(file[repo.GROUP], repo.GROUP))
        assert found_datasets == datasets
        assert len(file[repo.GROUP][datasets[0]]) == len(dicts)

    # verify indices have been made properly
    for index in repo.indices:
        stored_index = repo.get_index(index)
        for doc in dicts:
            attr_val = doc[index]
            assert attr_val in stored_index
            stored_index.pop(attr_val)