<commit_before>import logging
import pytest

from . import db
from ... import utils


logger = logging.getLogger(__name__)

@pytest.mark.asyncio
async def test_store_restore_user(event_loop):
    db_interface = db.MongodbInterface(uri='mongo', db_name='test')
    await db_interface.connect(loop=event_loop)
    user = utils.build_fake_user()

    user_id = await db_interface.set_user(user._response)
    restored_user = await db_interface.get_user(user_id)

    assert user.items() == restored_user.items()

@pytest.mark.asyncio
async def test_store_restore_session(event_loop):
    db_interface = db.MongodbInterface(uri='mongo', db_name='test')
    await db_interface.connect(loop=event_loop)
    session = utils.build_fake_session()
    user_id = '123456789'

    session_id = await db_interface.set_session(user_id, session._response)
    logger.debug('session_id')
    logger.debug(session_id)
    restored_session = await db_interface.get_session(session_id=session_id)
    logger.debug('restored_session')
    logger.debug(restored_session)
    restored_session = await db_interface.get_session(user_id=user_id)

    assert list(session.items()) == [(key, value) for (key, value) in restored_session.items() if key not in ['_id']]<commit_msg>open/close mongo session  between classes<commit_after>import logging
import pytest

from . import db
from ... import utils

logger = logging.getLogger(__name__)


@pytest.fixture
@pytest.mark.asyncio
def open_db(event_loop):
    class AsyncDBConnection:
        def __init__(self):
            self.db_interface = db.MongodbInterface(uri='mongo', db_name='test')

        async def __aenter__(self):
            await self.db_interface.connect(loop=event_loop)
            return self.db_interface

        async def __aexit__(self, exc_type, exc_val, exc_tb):
            self.db_interface = None

    return AsyncDBConnection


@pytest.mark.asyncio
async def test_store_restore_user(open_db):
    async with open_db() as db_interface:
        user = utils.build_fake_user()

        user_id = await db_interface.set_user(user._response)
        restored_user = await db_interface.get_user(user_id)

        assert user.items() == restored_user.items()


@pytest.mark.asyncio
async def test_store_restore_session(open_db):
    async with open_db() as db_interface:
        session = utils.build_fake_session()
        user_id = '123456789'

        session_id = await db_interface.set_session(user_id, session._response)
        logger.debug('session_id')
        logger.debug(session_id)
        restored_session = await db_interface.get_session(session_id=session_id)
        logger.debug('restored_session')
        logger.debug(restored_session)
        restored_session = await db_interface.get_session(user_id=user_id)

        assert list(session.items()) == [(key, value) for (key, value) in restored_session.items() if
                                         key not in ['_id']]
{'fraction_non_alphanumeric': 0.09792587555253315, 'fraction_numerical': 0.006120367222033322, 'mean_word_length': 3.8150572831423895, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4998386', 'n_tokens_mistral': 927, 'n_tokens_neox': 915, 'n_words': 177}