Example #1
0
def test_nested_cache(session):
    """Tests whether the sample pulled."""
    browser = Browser(session)
    browser.use_cache = True

    st_from_session = session.SampleType.find(1)
    sample_from_session = st_from_session.samples[0]

    sample = browser.find(sample_from_session.id, "Sample")
    st = browser.find(st_from_session.id, "SampleType")

    sample_from_cache = browser.find(sample_from_session.id, "Sample")
    st_from_cache = browser.find(st_from_session.id, "SampleType")

    # check if sample type pulle from cache is always the same
    assert st_from_session is not st
    assert st_from_cache is st

    # check if sample pulled from cache is the same
    assert sample_from_session is not sample
    assert sample_from_cache is sample

    assert (
        st_from_cache.samples[0].id == sample.id
    ), "The first indexed sample should be the same sample found by the browser"
Example #2
0
def test_recursive_cache(session):

    browser = Browser(session)
    samples = browser.interface("Sample").where({"sample_type_id": 1})

    # should preload SampleType into cache
    st = browser.find(1, "SampleType")
    assert browser.model_cache["SampleType"][st.id] == st

    found_st = browser.find(1, "SampleType")
    assert found_st is st

    st_from_where = browser.where({"id": samples[0].sample_type_id},
                                  "SampleType")
    assert (
        st_from_where[0] is st
    ), "SampleType retrieved by where should find the SampleType in the cache"

    # should retrieve the exact model that was preloaded
    print(browser.model_cache)
    session.set_verbose(True)
    browser.session.set_verbose(True)
    browser.retrieve(samples, "sample_type")
    print(st)
    print(samples[0]._get_data()["sample_type"])
    print(samples[0].sample_type)
    assert samples[0].sample_type is st

    # affecting sample_types from these models should refer to the same sample type
    assert samples[0].sample_type is samples[1].sample_type