def test_initial_values(): b = BPlusTree(filename, page_size=512, value_size=128) assert b._tree_conf.page_size == 512 assert b._tree_conf.order == 100 assert b._tree_conf.key_size == 8 assert b._tree_conf.value_size == 128 b.close()
def test_iter_slice(b): with pytest.raises(ValueError): next(b._iter_slice(slice(None, None, -1))) with pytest.raises(ValueError): next(b._iter_slice(slice(10, 0, None))) # Contains from 0 to 9 included for i in range(10): b.insert(i, str(i).encode()) iter = b._iter_slice(slice(None, 2)) assert next(iter).key == 0 assert next(iter).key == 1 with pytest.raises(StopIteration): next(iter) iter = b._iter_slice(slice(5, 7)) assert next(iter).key == 5 assert next(iter).key == 6 with pytest.raises(StopIteration): next(iter) iter = b._iter_slice(slice(8, 9)) assert next(iter).key == 8 with pytest.raises(StopIteration): next(iter) iter = b._iter_slice(slice(9, 12)) assert next(iter).key == 9 with pytest.raises(StopIteration): next(iter) iter = b._iter_slice(slice(15, 17)) with pytest.raises(StopIteration): next(iter) iter = b._iter_slice(slice(-2, 17)) assert next(iter).key == 0 b.close() # Contains from 10, 20, 30 .. 200 b = BPlusTree(filename, order=5) for i in range(10, 201, 10): b.insert(i, str(i).encode()) iter = b._iter_slice(slice(65, 85)) assert next(iter).key == 70 assert next(iter).key == 80 with pytest.raises(StopIteration): next(iter)
def test_left_record_node_in_tree(): b = BPlusTree(filename, order=3) assert b._left_record_node == b._root_node assert isinstance(b._left_record_node, LonelyRootNode) b.insert(1, b'1') b.insert(2, b'2') b.insert(3, b'3') assert isinstance(b._left_record_node, LeafNode) b.close()
def test_closing_context_manager(mock_close): with BPlusTree(filename, page_size=512, value_size=128) as b: pass mock_close.assert_called_once_with()
def test_insert_split_in_tree(iterator, order, page_size, k_size, v_size, serialize_class, cache_size): inserted = list() for i in iterator: v = str(i).encode() k = i if serialize_class is StrSerializer: k = str(i) inserted.append((k, v)) b = BPlusTree(filename, order=order, page_size=page_size, key_size=k_size, value_size=v_size, cache_size=cache_size, serializer=serialize_class()) if sorted(inserted) == inserted: b.batch_insert(inserted) else: for k, v in inserted: b.insert(k, v) # Reload tree from file before checking values b.close() b = BPlusTree(filename, order=order, page_size=page_size, key_size=k_size, value_size=v_size, cache_size=cache_size, serializer=serialize_class()) for k, v in inserted: assert b.get(k) == v b.close()
def test_create_and_load_file(): b = BPlusTree(filename) assert isinstance(b._mem, FileMemory) b.insert(5, b'foo') b.close() b = BPlusTree(filename) assert isinstance(b._mem, FileMemory) assert b.get(5) == b'foo' b.close()
def b(): b = BPlusTree(filename, key_size=16, value_size=16, order=4) yield b b.close()
def test_length_hint_tree(): b = BPlusTree(filename, key_size=16, value_size=16, order=100) assert b.__length_hint__() == 49 b.insert(1, b'foo') assert b.__length_hint__() == 49 for i in range(2, 10001): b.insert(i, str(i).encode()) assert b.__length_hint__() == 7242 b.close()