Пример #1
0
def test_cache_where(session):
    browser = Browser(session)
    primers = browser.cached_where({"sample_type_id": 1}, "Sample")

    p = primers[-1]
    p.__dict__["foo"] = "bar"

    cached_primers = browser.cached_where({
        "sample_type_id": 1,
        "id": p.id
    }, "Sample")
    cached_primers2 = browser.cached_where(
        {
            "sample_type_id": [1, 2],
            "id": [p.id]
        }, "Sample")
    empty = browser.cached_where({"sample_type_id": 2, "id": p.id}, "Sample")

    assert empty == [], "should not return any primers since query does not match"
    assert len(cached_primers) == 1, "should return exactly 1 primer"
    assert (cached_primers2 == cached_primers
            ), "should be equivalent as these are equivalent queries"
    assert (
        "foo" in cached_primers[0].__dict__
    ), 'should containing the "foo" attribute that was initially cached'
    assert (cached_primers[0].__dict__["foo"] == "bar"
            ), "should return the very same models that was initially cached"
    assert len(browser.model_cache["Sample"]) > 1
Пример #2
0
def test_cache_where_name(session):
    browser = Browser(session)
    primers = browser.cached_where({"sample_type_id": 1}, "Sample")
    primer1 = primers[0]
    id1 = id(primer1)
    primer2 = browser.find_by_name(primer1.name, "Sample", primary_key="id")
    id2 = id(primer2)
    assert id1 == id2
Пример #3
0
def test_cache_find(session):
    """This test ensures that 'find' preferentially retrieves the models from
    the cache."""
    browser = Browser(session)
    primers = browser.cached_where({"sample_type_id": 1}, "Sample")

    p = primers[-1]
    p.__dict__["foo"] = "bar"

    cached_primer = browser.cached_find("Sample", p.id)
    cached_primers = browser.cached_find("Sample", [p.id])
    empty = browser.cached_find("Sample", 100000)

    assert empty is None, "should not return any primers since query does not match"
    assert cached_primer == p
    assert len(cached_primers) == 1
    assert cached_primer == cached_primers[0]