Exemplo n.º 1
0
def mock_mongo():
    """Inject a MongoDB client connected to a mock server.

    No database in place, guaranteeing that we'll start from a clean slate.
    """
    client = get_client(mock=True)
    client.drop_database(os.getenv("MONGODB_DATABASE"))
    yield get_database(mock=True)
def test_storage_insert_single_doc():
    storage.DATABASE = get_database(True)
    event = EiffelCompositionDefinedEvent()

    assert storage.insert_to_db(event, None)
    collection = storage.DATABASE[event.meta.type]
    result = collection.find_one({"_id": event.meta.event_id})
    del result["_id"]
    assert result == event.json
Exemplo n.º 3
0
def remove(event):
    """Remove an event from the MongoDB database.

    :param event: Event to remove.
    :type event: dict
    """
    database = get_database(mock=True)
    if isinstance(event, list):
        for event_data in event:
            database[event_data["meta"]["type"]].delete_one(event_data)
    else:
        database[event["meta"]["type"]].delete_one(event)
Exemplo n.º 4
0
def insert(event):
    """Insert an event to the MongoDB database.

    :param event: Event to insert.
    :type event: dict
    """
    database = get_database(mock=True)
    if isinstance(event, list):
        for event_data in event:
            database[event_data["meta"]["type"]].insert_one(event_data)
    else:
        database[event["meta"]["type"]].insert_one(event)
Exemplo n.º 5
0
        os.environ["RABBITMQ_PASSWORD"] = password.read()
if os.path.isfile("/etc/rabbitmq/username"):
    with open("/etc/rabbitmq/username", "r") as username:
        os.environ["RABBITMQ_USERNAME"] = username.read()


def insert_to_db(event, context):
    print(event)
    collection = DATABASE[event.meta.type]
    collection.create_index([("meta.id", pymongo.ASCENDING)])
    collection.create_index([("links.target", pymongo.ASCENDING)])
    collection.insert_one(event.json)


if __name__ == "__main__":
    DATABASE = get_database()
    SSL = os.getenv("RABBITMQ_SSL", "true") == "true"
    DATA = {
        "host": os.getenv("RABBITMQ_HOST", "127.0.0.1"),
        "exchange": os.getenv("RABBITMQ_EXCHANGE", "eiffel"),
        "username": os.getenv("RABBITMQ_USERNAME", None),
        "password": os.getenv("RABBITMQ_PASSWORD", None),
        "port": int(os.getenv("RABBITMQ_PORT", "5672")),
        "vhost": os.getenv("RABBITMQ_VHOST", None),
        "ssl": ssl,
        "queue": os.getenv("RABBITMQ_QUEUE", None),
        "routing_key": "#"
    }
    SUBSCRIBER = RabbitMQSubscriber(**data)
    SUBSCRIBER.subscribe("*", insert_to_db)
    SUBSCRIBER.start()
def test_storage_ignores_duplicate_ids():
    storage.DATABASE = get_database(True)
    event = EiffelCompositionDefinedEvent()

    assert storage.insert_to_db(event, None)
    assert not storage.insert_to_db(event, None)