<commit_before>import mock
import os
import dbm
from datetime import datetime

samples = {
    u'elapsed': 1.2000219821929932,
    u'granularity': 0.001,
    u'stacks': [
        {u'count': 128, u'frame': u'test_sampler(test_sampler);testing_function2(test_sampler)'},
        {u'count': 126, u'frame': u'test_sampler(test_sampler);testing_function3(test_sampler)'},
        {u'count': 50, u'frame': u'test_sampler(test_sampler);testing_function(test_sampler)'},
        {u'count': 100, u'frame': u'test_sampler(test_sampler);testing_function(test_sampler)'}
    ]
}

def test_db_ctx_manager(db):
    with db.getdb() as d:
        d['foo'] = 'bar'

    with db.getdb() as d:
        assert d['foo'] == 'bar'


def test_db_path(db):
    now = datetime.now()
    expected_db_name = "{}_{}_{}".format(now.year, now.month, now.day)
    with db.getdb() as d:
        d['key'] = 'value'
    assert dbm.open(os.path.join(db.dbdir, expected_db_name))['key'] == 'value'


def test_save(db):
    with mock.patch('time.time', return_value=1):
        db.save('localhost', samples)

    with db.getdb() as d:
        assert dict([(k, d[k]) for k in d.keys()]) == {
            'test_sampler(test_sampler);testing_function(test_sampler)': '[{"count": 50, "host": "localhost", "time": 1}, {"count": 100, "host": "localhost", "time": 1}]',
            'test_sampler(test_sampler);testing_function2(test_sampler)': '[{"count": 128, "host": "localhost", "time": 1}]',
            'test_sampler(test_sampler);testing_function3(test_sampler)': '[{"count": 126, "host": "localhost", "time": 1}]'
        }

def test_load(db):
    with mock.patch('time.time', return_value=1):
        db.save('localhost', samples)

    assert (['test_sampler(test_sampler)', 'testing_function3(test_sampler)'], 126) in db.load()
    assert (['test_sampler(test_sampler)', 'testing_function2(test_sampler)'], 128) in db.load()
    assert (['test_sampler(test_sampler)', 'testing_function(test_sampler)'], 150) in db.load()<commit_msg>model test refactoring<commit_after>import mock
import os
import dbm
from datetime import datetime

samples = {
    u'elapsed': 1.2000219821929932,
    u'granularity': 0.001,
    u'stacks': [
        {u'count': 128, u'frame': u'test_sampler(test_sampler);testing_function2(test_sampler)'},
        {u'count': 126, u'frame': u'test_sampler(test_sampler);testing_function3(test_sampler)'},
        {u'count': 50, u'frame': u'test_sampler(test_sampler);testing_function(test_sampler)'},
        {u'count': 100, u'frame': u'test_sampler(test_sampler);testing_function(test_sampler)'}
    ]
}

def test_db_ctx_manager(db):
    with db.getdb() as d:
        d['foo'] = 'bar'

    with db.getdb() as d:
        assert d['foo'] == 'bar'


def test_db_path(db):
    now = datetime.now()
    expected_db_name = "{}_{}_{}".format(now.year, now.month, now.day)
    with db.getdb() as d:
        d['key'] = 'value'
    assert dbm.open(os.path.join(db.dbdir, expected_db_name))['key'] == 'value'


def test_save(db):
    with mock.patch('time.time', return_value=1):
        db.save('localhost', samples)

    with db.getdb() as d:
        assert dict([(k, d[k]) for k in d.keys()]) == {
            'test_sampler(test_sampler);testing_function(test_sampler)': '[{"count": 50, "host": "localhost", "time": 1}, {"count": 100, "host": "localhost", "time": 1}]',
            'test_sampler(test_sampler);testing_function2(test_sampler)': '[{"count": 128, "host": "localhost", "time": 1}]',
            'test_sampler(test_sampler);testing_function3(test_sampler)': '[{"count": 126, "host": "localhost", "time": 1}]'
        }

def test_load(db):
    with mock.patch('time.time', return_value=1):
        db.save('localhost', samples)

    loaded = db.load()
    assert (['test_sampler(test_sampler)', 'testing_function3(test_sampler)'], 126) in loaded
    assert (['test_sampler(test_sampler)', 'testing_function2(test_sampler)'], 128) in loaded
    # sum of 100 and 50
    assert (['test_sampler(test_sampler)', 'testing_function(test_sampler)'], 150) in loaded
{'fraction_non_alphanumeric': 0.18321366674919534, 'fraction_numerical': 0.03292894280762565, 'mean_word_length': 4.315789473684211, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7206921', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1441, 'n_words': 278}