def test_null_shard():
    with pytest.raises(ValueError):
        sync_kv_insert_object(None, uuid4(), 1000, {'num': 234})
    with pytest.raises(ValueError):
        sync_kv_get_object(None, uuid4())
    with pytest.raises(ValueError):
        sync_kv_get_objects(None, [uuid4()])
def test_objects_of_type_after_first(only_shard):
    shard = only_shard
    id_before, id_one, id_gap, id_two, id_three, id_four = get_sorted_ids(6)

    sync_kv_insert_object(shard, id_one, 1000, {'num': 5})
    sync_kv_insert_object(shard, id_two, 1000, {'num': 6})
    sync_kv_insert_object(shard, id_three, 1000, {'num': 7})
    sync_kv_insert_object(shard, id_four, 1001, {'num': 8})

    after_before_first_one_objs = sync_kv_get_objects_of_type(shard,
                                                              1000,
                                                              after=id_before,
                                                              first=1)
    assert list(after_before_first_one_objs.keys()) == [id_one]

    after_before_first_two_objs = sync_kv_get_objects_of_type(shard,
                                                              1000,
                                                              after=id_before,
                                                              first=2)
    assert list(after_before_first_two_objs.keys()) == [id_one, id_two]

    after_one_first_one_objs = sync_kv_get_objects_of_type(shard,
                                                           1000,
                                                           after=id_one,
                                                           first=1)
    assert list(after_one_first_one_objs.keys()) == [id_two]

    after_gap_first_one_objs = sync_kv_get_objects_of_type(shard,
                                                           1000,
                                                           after=id_gap,
                                                           first=1)
    assert list(after_gap_first_one_objs.keys()) == [id_two]
def test_objects_of_type_after(only_shard):
    shard = only_shard
    id_before, id_one, id_gap, id_two, id_three, id_four, id_off_end = get_sorted_ids(
        7)

    sync_kv_insert_object(shard, id_one, 1000, {'num': 5})
    sync_kv_insert_object(shard, id_two, 1000, {'num': 6})
    sync_kv_insert_object(shard, id_three, 1000, {'num': 7})
    sync_kv_insert_object(shard, id_four, 1001, {'num': 8})

    after_id_before_objs = sync_kv_get_objects_of_type(shard,
                                                       1000,
                                                       after=id_before)
    assert list(after_id_before_objs.keys()) == [id_one, id_two, id_three]

    after_one_objs = sync_kv_get_objects_of_type(shard, 1000, after=id_one)
    assert list(after_one_objs.keys()) == [id_two, id_three]

    after_gap_objs = sync_kv_get_objects_of_type(shard, 1000, after=id_gap)
    assert list(after_gap_objs.keys()) == [id_two, id_three]

    after_two_objs = sync_kv_get_objects_of_type(shard, 1000, after=id_two)
    assert list(after_two_objs.keys()) == [id_three]

    after_three_objs = sync_kv_get_objects_of_type(shard, 1000, after=id_three)
    assert list(after_three_objs.keys()) == []

    after_end_objs = sync_kv_get_objects_of_type(shard, 1000, after=id_off_end)
    assert list(after_end_objs.keys()) == []
def test_objects_of_type_first(only_shard):
    shard = only_shard
    id_one, id_two, id_three, id_four = get_sorted_ids(4)
    sync_kv_insert_object(shard, id_one, 1000, {'num': 5})
    sync_kv_insert_object(shard, id_two, 1000, {'num': 6})
    sync_kv_insert_object(shard, id_three, 1000, {'num': 7})
    sync_kv_insert_object(shard, id_four, 1001, {'num': 8})

    first_one_objs = sync_kv_get_objects_of_type(shard,
                                                 1000,
                                                 after=None,
                                                 first=1)
    assert list(first_one_objs.keys()) == [id_one]

    first_two_objs = sync_kv_get_objects_of_type(shard,
                                                 1000,
                                                 after=None,
                                                 first=2)
    assert list(first_two_objs.keys()) == [id_one, id_two]

    first_three_objs = sync_kv_get_objects_of_type(shard,
                                                   1000,
                                                   after=None,
                                                   first=3)
    assert list(first_three_objs.keys()) == [id_one, id_two, id_three]
def test_first_edge(test_shard_single_edge):
    shard, edges, _ = test_shard_single_edge
    id_one = UUID('f2d41433-a996-40c9-ba3b-6047b2bd27f7')
    id_two = UUID('54c4c971-e7a3-42ff-858a-64a73cca3286')
    id_three = UUID('0ddf55de-0f37-4152-8f41-7e64c7359a9e')
    id_four = UUID('e5a34f23-c14f-49cc-a9ad-f3a8165e141c')

    sync_kv_insert_object(shard, id_one, 1000, {})
    sync_kv_insert_object(shard, id_two, 1000, {})
    sync_kv_insert_object(shard, id_three, 1000, {})
    sync_kv_insert_object(shard, id_four, 1000, {})

    related_edge = edges['related_edge']

    sync_kv_insert_edge(shard, related_edge, id_one, id_two)
    sync_kv_insert_edge(shard, related_edge, id_one, id_three)
    sync_kv_insert_edge(shard, related_edge, id_one, id_four)

    assert len(sync_kv_get_edge_ids(shard, related_edge, id_one)) == 3

    def get_after(a, f=None):
        return sync_kv_get_edge_ids(shard,
                                    related_edge,
                                    id_one,
                                    after=a,
                                    first=f)

    assert len(get_after(id_two)) == 2
    assert not id_two in get_after(id_two)
    assert id_three in get_after(id_two)
    assert id_four in get_after(id_two)

    assert len(get_after(id_three)) == 1
    assert not id_two in get_after(id_three)
    assert not id_three in get_after(id_three)
    assert id_four in get_after(id_three)

    after_four_len = len(get_after(id_four))
    assert after_four_len == 0
def test_get_objects_of_type(only_shard):
    shard = only_shard
    id_one, id_two, id_three, id_four = get_sorted_ids(4)
    sync_kv_insert_object(shard, id_one, 1000, {'num': 5})
    sync_kv_insert_object(shard, id_two, 1000, {'num': 6})
    sync_kv_insert_object(shard, id_three, 1000, {'num': 7})
    sync_kv_insert_object(shard, id_four, 1001, {'num': 8})
    ids_out_1000 = [
        obj_id for obj_id in sync_kv_get_objects_of_type(shard, 1000).keys()
    ]
    assert ids_out_1000 == [id_one, id_two, id_three]
    datas_out_1000 = [
        obj for obj in sync_kv_get_objects_of_type(shard, 1000).values()
    ]
    assert [data['num'] for data in datas_out_1000] == [5, 6, 7]
    ids_out_1001 = [
        obj_id for obj_id in sync_kv_get_objects_of_type(shard, 1001).keys()
    ]
    assert ids_out_1001 == [id_four]
    datas_out_1001 = [
        obj for obj in sync_kv_get_objects_of_type(shard, 1001).values()
    ]
    assert [data['num'] for data in datas_out_1001] == [8]
def test_after_edge(test_shard_single_edge):
    shard, edges, _ = test_shard_single_edge
    id_one = UUID('50ce64f1-3e76-462c-8a03-91475503e496')
    id_two = UUID('a5c99e91-8084-4405-8378-db0af38d8b61')
    id_three = UUID('5301d18f-c3bd-42c4-bcfb-50924b3aca39')
    data_one = {'num': 4}
    data_two = {'num': 5, 'related_id': id_one}
    data_three = {'num': 6, 'related_id': id_one}
    sync_kv_insert_object(shard, id_one, 1000, data_one)
    sync_kv_insert_object(shard, id_two, 1000, data_two)
    sync_kv_insert_object(shard, id_three, 1000, data_three)

    related_edge = edges['related_edge']
    assert related_edge is not None

    sync_kv_insert_edge(shard, related_edge, id_one, id_two)
    sync_kv_insert_edge(shard, related_edge, id_one, id_three)

    all_edge_ids = sync_kv_get_edge_ids(shard, related_edge, id_one)
    assert all_edge_ids == [id_two, id_three]
    edge_ids = sync_kv_get_edge_ids(shard, related_edge, id_one, after=id_two)
    assert len(edge_ids) == 1
    assert not id_two in edge_ids
    assert id_three in edge_ids
def test_object_insert_id(only_shard):
    new_id = uuid4()
    with pytest.raises(ValueError):
        sync_kv_insert_object(only_shard, new_id, 1000, None)
    with pytest.raises(ValueError):
        sync_kv_insert_object(only_shard, new_id, 1000, [])
    with pytest.raises(ValueError):
        sync_kv_insert_object(only_shard, new_id, 1000, {'obj_id': 234})
    with pytest.raises(ValueError):
        sync_kv_insert_object(only_shard, new_id, 1000, {'type_id': 234})
    with pytest.raises(ValueError):
        sync_kv_insert_object(only_shard, new_id, None, {'name': 'Joe'})
    with pytest.raises(ValueError):
        sync_kv_insert_object(only_shard, None, 1000, {})
    with pytest.raises(ValueError):
        sync_kv_insert_object(only_shard, 101, 1000, {})
def insert_test_obj(shard, data):
    new_id = uuid4()
    sync_kv_insert_object(shard, new_id, 1000, data)
    return new_id