Exemplo n.º 1
0
def test_find_records_filter():
    filter = {"result": 2403.52}
    collection = flexmock()
    mongo_cursor = flexmock(DummyIterator())
    mongo_client = {"testdb": {"runs": collection}}

    collection.should_receive("find").once().with_args(filter).and_return(
        mongo_cursor)

    generic_dao = GenericDAO(mongo_client, "testdb")
    generic_dao.find_records("runs", query=filter)
Exemplo n.º 2
0
def test_find_record_when_record_does_not_exist():
    test_query = {"_id": "NON_EXISTING_ID"}
    collection = flexmock()
    collection.should_receive("find").once().with_args(test_query).and_return(
        [])
    mongo_client = {"testdb": {"runs": collection}}
    generic_dao = GenericDAO(mongo_client, "testdb")

    r = generic_dao.find_record("runs", test_query)

    assert r is None
Exemplo n.º 3
0
def test_find_records_order_ascending():
    collection = flexmock()
    mongo_cursor = flexmock(DummyIterator())
    mongo_client = {"testdb": {"runs": collection}}

    collection.should_receive("find").once().with_args(
        {}).and_return(mongo_cursor)
    mongo_cursor.should_receive("sort").once().with_args(
        "host.python_version", pymongo.ASCENDING).and_return(mongo_cursor)

    generic_dao = GenericDAO(mongo_client, "testdb")

    generic_dao.find_records("runs", sort_by="host.python_version")
Exemplo n.º 4
0
def test_find_records_limit():
    limit = 42
    mongo_cursor = flexmock(DummyIterator())
    collection = flexmock()
    mongo_client = {"testdb": {"runs": collection}}

    mongo_cursor.should_receive("limit").once().with_args(limit).and_return(
        mongo_cursor)
    collection.should_receive("find").once().with_args(
        {}).and_return(mongo_cursor)

    generic_dao = GenericDAO(mongo_client, "testdb")
    generic_dao.find_records("runs", limit=limit)
Exemplo n.º 5
0
def test_find_records_order(mongo_client):
    generic_dao = GenericDAO(mongo_client, "testdb")
    runs = list(generic_dao.find_records("runs",
                                         sort_by="host.python_version"))
    assert len(runs) == 2
    assert runs[0]["host"]["python_version"] == "3.4.3"
    assert runs[1]["host"]["python_version"] == "3.5.2"

    runs = list(
        generic_dao.find_records("runs",
                                 sort_by="host.python_version",
                                 sort_direction="desc"))
    assert len(runs) == 2
    assert runs[0]["host"]["python_version"] == "3.5.2"
    assert runs[1]["host"]["python_version"] == "3.4.3"
Exemplo n.º 6
0
def test_find_record(mongo_client):
    generic_dao = GenericDAO(mongo_client, "testdb")
    r = generic_dao.find_record("runs", {"_id": "NON_EXISTING_ID"})
    assert r is None

    r = generic_dao.find_record(
        "runs", {"_id": bson.ObjectId("58163443b1758523257c69ca")})
    assert r is not None
    assert r["config"] is not None
    assert r["config"]["seed"] == 185616783

    r = generic_dao.find_record("runs", {"config.learning_rate": 0.0001})
    assert r is not None
    assert r["config"] is not None
    assert r["config"]["seed"] == 144363069
Exemplo n.º 7
0
def test_find_record():
    test_query = {"_id": bson.ObjectId("58163443b1758523257c69ca")}
    collection = flexmock()
    collection.should_receive("find").once().with_args(test_query).and_return(
        [{
            "config": {
                "seed": 185616783
            }
        }])
    mongo_client = {"testdb": {"runs": collection}}
    generic_dao = GenericDAO(mongo_client, "testdb")

    r = generic_dao.find_record("runs", test_query)

    assert r is not None
    assert r["config"] is not None
    assert r["config"]["seed"] == 185616783
Exemplo n.º 8
0
def test_find_records_in_empty_collection():
    mongo_cursor = flexmock(DummyIterator())
    mongo_cursor.should_receive("skip").once().with_args(0).and_return(
        mongo_cursor)
    mongo_cursor.should_receive("count").and_return(0)
    mongo_cursor.should_receive("__next__").and_raise(StopIteration)

    collection = flexmock()
    collection.should_receive("find").once().with_args(
        {}).and_return(mongo_cursor)
    mongo_client = {"testdb": {"EMPTY_COLLECTION": collection}}
    generic_dao = GenericDAO(mongo_client, "testdb")

    r = generic_dao.find_records("EMPTY_COLLECTION")

    assert isinstance(r, Cursor)
    assert r.count() == 0
    assert len(list(r)) == 0
Exemplo n.º 9
0
def test_find_records_in_non_empty_collection():
    mongo_cursor = flexmock(DummyIterator())
    mongo_cursor.should_receive("skip").once().with_args(0).and_return(
        mongo_cursor)
    mongo_cursor.should_receive("count").and_return(0)
    mongo_cursor.should_receive("__next__").and_return({"host": {"hostname": "ntbacer"}})\
        .and_return({"host": {"hostname": "martin-virtual-machine"}})\
        .and_raise(StopIteration)
    mongo_cursor.should_receive("count").and_return(2)

    collection = flexmock()
    collection.should_receive("find").once().with_args(
        {}).and_return(mongo_cursor)
    mongo_client = {"testdb": {"runs": collection}}
    generic_dao = GenericDAO(mongo_client, "testdb")

    runs = list(generic_dao.find_records("runs"))

    assert len(runs) == 2
    assert runs[0]["host"]["hostname"] == "ntbacer"
    assert runs[1]["host"]["hostname"] == "martin-virtual-machine"
Exemplo n.º 10
0
def test_find_records(mongo_client):
    generic_dao = GenericDAO(mongo_client, "testdb")
    r = generic_dao.find_records("EMPTY_COLLECTION")
    assert isinstance(r, Cursor)
    assert r.count() == 0
    assert len(list(r)) == 0

    # Existing collection
    generic_dao = GenericDAO(mongo_client, "testdb")
    runs = list(generic_dao.find_records("runs"))
    assert len(runs) == 2
    assert runs[0]["host"]["hostname"] == "ntbacer"
    assert runs[1]["host"]["hostname"] == "martin-virtual-machine"
Exemplo n.º 11
0
def test_find_records_filter(mongo_client, filter):
    generic_dao = GenericDAO(mongo_client, "testdb")
    runs = list(generic_dao.find_records("runs", query=filter))
    assert len(runs) == 1
    assert runs[0]["host"]["hostname"] == "ntbacer"
Exemplo n.º 12
0
def test_find_records_limit(mongo_client):
    generic_dao = GenericDAO(mongo_client, "testdb")
    runs = list(generic_dao.find_records("runs", limit=1))
    assert len(runs) == 1, ""
    assert runs[0]["host"]["hostname"] == "ntbacer"
Exemplo n.º 13
0
def generic_dao():
    client = mongo_client()
    return GenericDAO(client, "testdb")