def test_cache_miss_value_error_compute_time(): """ ValueError should be raised if we try to get the compute time of something not in cache. """ connection_mock = Mock() connection_mock.fetch.return_value = [] with pytest.raises(ValueError): get_compute_time(connection_mock, "DUMMY_ID")
def test_get_compute_time(): """ Compute time should take value returned in ms and turn it into seconds. """ connection_mock = Mock() connection_mock.fetch.return_value = [[10]] assert 10 / 1000 == get_compute_time(connection_mock, "DUMMY_ID")
def test_scoring(flowmachine_connect): """ Test that score updating algorithm is correct by comparing to cachey as reference implementation """ dl = daily_location("2016-01-01").store().result() dl_time = get_compute_time(get_db(), dl.query_id) dl_size = get_size_of_table(get_db(), dl.table_name, "cache") initial_score = get_score(get_db(), dl.query_id) cachey_scorer = Scorer(halflife=1000.0) cache_score = cachey_scorer.touch("dl", dl_time / dl_size) assert cache_score == pytest.approx(initial_score) # Touch again new_score = touch_cache(get_db(), dl.query_id) updated_cache_score = cachey_scorer.touch("dl") assert updated_cache_score == pytest.approx(new_score) # Add another unrelated cache record, which should have a higher initial score dl_2 = daily_location("2016-01-02").store().result() dl_time = get_compute_time(get_db(), dl_2.query_id) dl_size = get_size_of_table(get_db(), dl_2.table_name, "cache") cache_score = cachey_scorer.touch("dl_2", dl_time / dl_size) assert cache_score == pytest.approx(get_score(get_db(), dl_2.query_id))