def insert_properties(properties: Set[str],
                      session: scoped_session) -> Optional[Any]:
    """Insert all the properties as defined in the APIDocumentation into DB."""
    prop_list = [
        BaseProperty(name=prop) for prop in properties
        if not session.query(exists().where(
            BaseProperty.name == prop)).scalar()
    ]
    session.add_all(prop_list)
    session.commit()
    return None


# if __name__ == "__main__":
#     Session = sessionmaker(bind=engine)
#     session = Session()
#
#     doc = doc_gen("test", "test")
#     # Extract all classes with supportedProperty from both
#     classes = get_classes(doc.generate())
#
#     # Extract all properties from both
#     # import pdb; pdb.set_trace()
#     properties = get_all_properties(classes)
#     # Add all the classes
#     insert_classes(classes, session)
#     print("Classes inserted successfully")
#     # Add all the properties
#     insert_properties(properties, session)
#     print("Properties inserted successfully")
Example #2
0
def test_broadcast_move(
        fx_server: WSGIServer,
        fx_session: scoped_session,
        fx_other_server: WSGIServer,
        fx_other_session: Session,
        fx_user: User,
        fx_novice_status: typing.Mapping[str, str],
):
    now = datetime.datetime.utcnow()
    node = Node(url=fx_server.url,
                last_connected_at=now)
    node2 = Node(url=fx_other_server.url,
                 last_connected_at=datetime.datetime.utcnow())
    move = fx_user.create_novice(fx_novice_status)
    fx_session.add_all([node, node2, move])
    fx_session.commit()
    assert not fx_other_session.query(Move).get(move.id)
    serialized = move.serialize(
        use_bencode=False,
        include_signature=True,
        include_id=True,
    )
    multicast(serialized=serialized, broadcast=broadcast_move)
    assert fx_other_session.query(Move).get(move.id)
    assert node.last_connected_at > now
Example #3
0
def test_broadcast_block(
        fx_server: WSGIServer,
        fx_session: scoped_session,
        fx_other_session: Session,
        fx_other_server: WSGIServer,
        fx_user: User
):
    now = datetime.datetime.utcnow()
    node = Node(url=fx_server.url,
                last_connected_at=now)
    node2 = Node(url=fx_other_server.url,
                 last_connected_at=datetime.datetime.utcnow())
    block = Block.create(fx_user, [])
    fx_session.add_all([node, node2, block])
    fx_session.flush()
    assert fx_session.query(Block).get(block.id)
    assert not fx_other_session.query(Block).get(block.id)
    multicast(
        serialized=block.serialize(
            use_bencode=False,
            include_suffix=True,
            include_moves=True,
            include_hash=True
        ),
        broadcast=broadcast_block,
    )
    assert node.last_connected_at > now
    assert fx_session.query(Block).count() == 1
    assert fx_other_session.query(Block).get(block.id)
Example #4
0
def broadcast_move_failed(fx_session: scoped_session, fx_user: User,
                          fx_novice_status: typing.Mapping[str, str], error):
    now = datetime.datetime.utcnow()
    move = fx_user.create_novice(fx_novice_status)
    node = Node(url='http://test.neko', last_connected_at=now)
    fx_session.add_all([node, move])
    fx_session.commit()
    with Mocker() as m:
        serialized = move.serialize(
            use_bencode=False,
            include_signature=True,
            include_id=True,
        )
        m.post('http://test.neko', exc=error)
        broadcast_move(serialized=serialized)
    assert node.last_connected_at == now
Example #5
0
def test_broadcast_move_same_url(fx_session: scoped_session, fx_user: User,
                                 fx_novice_status: typing.Mapping[str, str]):
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    move = fx_user.create_novice(fx_novice_status)
    fx_session.add_all([node, move])
    fx_session.commit()
    with Mocker() as m:
        serialized = move.serialize(
            use_bencode=False,
            include_signature=True,
            include_id=True,
        )
        broadcast_move(serialized=serialized, sent_node=node)
        assert not m.called
    assert node.last_connected_at == now
def insert_classes(classes: List[Dict[str, Any]],
                   session: scoped_session) -> Optional[Any]:
    """Insert all the classes as defined in the APIDocumentation into DB."""
    # print(session.query(exists().where(RDFClass.name == "Datastream")).scalar())
    class_list = [
        RDFClass(name=class_["label"].strip('.')) for class_ in classes
        if "label" in class_ and not session.query(exists().where(
            RDFClass.name == class_["label"].strip('.'))).scalar()
    ]

    class_list = class_list + [
        RDFClass(name=class_["title"].strip('.')) for class_ in classes
        if "title" in class_ and not session.query(exists().where(
            RDFClass.name == class_["title"].strip('.'))).scalar()
    ]
    # print(class_list)
    session.add_all(class_list)
    session.commit()
    return None
Example #7
0
def test_broadcast_move_my_node(fx_session: scoped_session, fx_user: User,
                                fx_novice_status: typing.Mapping[str, str]):
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    move = fx_user.create_novice(fx_novice_status)
    fx_session.add_all([node, move])
    fx_session.commit()
    with Mocker() as m:
        m.post('http://test.neko/moves', json={'result': 'success'})
        expected = serialized = move.serialize(
            use_bencode=False,
            include_signature=True,
            include_id=True,
        )
        broadcast_move(serialized=serialized, my_node=node)
        expected['sent_node'] = 'http://test.neko'
        assert node.last_connected_at > now
        # check request.json value
        assert m.request_history[0].json() == expected
Example #8
0
def insert_multiple(objects_: List[Dict[str,
                                        Any]],
                    session: scoped_session,
                    id_: Optional[str] = "") -> List[int]:
    """
    Adds a list of object with given ids to the database
    :param objects_: List of dict's to be added to the database
    :param session: scoped session from getSession in utils
    :param id_: optional parameter containing the ids of objects that have to be inserted
    :return: Ids that have been inserted
    """
    # instance list to store instances
    instance_list = list()
    triples_list = list()
    properties_list = list()
    instances = list()
    id_list = id_.split(',')
    instance_id_list = list()

    # the number of objects would be the same as number of instances
    for index in range(len(objects_)):
        try:
            rdf_class = session.query(RDFClass).filter(
                RDFClass.name == objects_[index]["@type"]).one()
        except NoResultFound:
            raise ClassNotFound(type_=objects_[index]["@type"])
        if index in range(len(id_list)) and id_list[index] != "":
            if session.query(
                    exists().where(
                        Instance.id == id_list[index])).scalar():
                print(session.query(
                    exists().where(
                        Instance.id == id_list[index])))
                # TODO handle where intance already exists , if instance is
                # fetched later anyways remove this
                raise InstanceExists(type_=rdf_class.name, id_=id_list[index])
            else:
                instance = Instance(id=id_list[index], type_=rdf_class.id)
                instances.append(instance)
        else:
            instance = Instance(type_=rdf_class.id)
            instances.append(instance)

    session.add_all(instances)
    session.flush()
    for i in range(len(instances)):
        instance_id_list.append(instances[i].id)

    for index in range(len(objects_)):
        for prop_name in objects_[index]:
            if prop_name not in ["@type", "@context"]:
                try:
                    property_ = session.query(properties).filter(
                        properties.name == prop_name).one()
                except NoResultFound:
                    # Adds new Property
                    session.close()
                    raise PropertyNotFound(type_=prop_name)

                # For insertion in III
                if isinstance(objects_[index][prop_name], dict):
                    instance_id = insert(
                        objects_[index][prop_name], session=session)
                    instance_object = session.query(Instance).filter(
                        Instance.id == instance_id).one()

                    if property_.type_ == "PROPERTY" or property_.type_ == "INSTANCE":
                        property_.type_ = "INSTANCE"
                        properties_list.append(property_)
                        triple = GraphIII(
                            subject=instances[index].id,
                            predicate=property_.id,
                            object_=instance_object.id)
                        triples_list.append(triple)
                    else:
                        session.close()
                        raise NotInstanceProperty(type_=prop_name)

                # For insertion in IAC
                elif session.query(
                        exists().where(RDFClass.name == str(objects_[index][prop_name]))).scalar():
                    if property_.type_ == "PROPERTY" or property_.type_ == "ABSTRACT":
                        property_.type_ = "ABSTRACT"
                        properties_list.append(property_)
                        class_ = session.query(RDFClass).filter(
                            RDFClass.name == objects_[index][prop_name]).one()
                        triple = GraphIAC(
                            subject=instances[index].id,
                            predicate=property_.id,
                            object_=class_.id)
                        triples_list.append(triple)

                    else:
                        session.close()
                        raise NotAbstractProperty(type_=prop_name)

                # For insertion in IIT
                else:
                    terminal = Terminal(value=objects_[index][prop_name])
                    session.add(terminal)
                    session.flush()  # Assigns ID without committing

                    if property_.type_ == "PROPERTY" or property_.type_ == "INSTANCE":
                        property_.type_ = "INSTANCE"
                        properties_list.append(property_)
                        triple = GraphIIT(
                            subject=instances[index].id,
                            predicate=property_.id,
                            object_=terminal.id)
                        # Add things directly to session, if anything fails
                        # whole transaction is aborted
                        triples_list.append(triple)
                    else:
                        session.close()
                        raise NotInstanceProperty(type_=prop_name)
    session.bulk_save_objects(properties_list)
    session.bulk_save_objects(triples_list)
    session.commit()
    return instance_id_list