<commit_before>import pytest
import random

import pymongo

from logster.db import Model, get_db
from logster import conf


class TempCollection:
    def __init__(self, db, name, initial_data=None):
        self.db = db
        self.name = name
        self.initial_data = initial_data

    def __enter__(self):
        self.col = self.db[self.name]
        if self.initial_data is not None:
            self.col.insert(self.initial_data)

        return self.col

    def __exit__(self, type, value, traceback):
        self.col.drop()


class DbTests:
    def __init__(self):
        self.db = get_db(conf.DB_CONN_STRING, conf.TEST_DB_NAME)

    def test_collection(self, initial_data=None):
        name = 'test%d' % random.randint(10000, 99999)
        if initial_data is not None:
            pass

        return TempCollection(self.db, name, initial_data)


@pytest.fixture(scope='module')
def db():
    return DbTests()


def test_db_fixture(db):
    data = [{'test': 42}, {'test': 43}]

    with db.test_collection(initial_data=data) as col:
        assert isinstance(col, pymongo.collection.Collection)
        doc = col.find_one({'test': 42})

        assert doc is not None
        assert doc['test'] == 42


def test_find_in_collection(db):
    data = [
        {'name': 'Ginger', 'age': 2},
        {'name': 'Kleo', 'age': 4},
        {'name': 'Fluffy', 'age': 7}
    ]

    with db.test_collection(initial_data=data) as col:<commit_msg>Change fixture scope to session<commit_after>import pytest
import random

import pymongo

from logster.db import Model, get_db
from logster import conf


class TempCollection:
    def __init__(self, db, name, initial_data=None):
        self.db = db
        self.name = name
        self.initial_data = initial_data

    def __enter__(self):
        self.col = self.db[self.name]
        if self.initial_data is not None:
            self.col.insert(self.initial_data)

        return self.col

    def __exit__(self, type, value, traceback):
        self.col.drop()


class DbTests:
    def __init__(self):
        self.db = get_db(conf.DB_CONN_STRING, conf.TEST_DB_NAME)

    def test_collection(self, initial_data=None):
        name = 'test%d' % random.randint(10000, 99999)
        if initial_data is not None:
            pass

        return TempCollection(self.db, name, initial_data)


@pytest.fixture(scope='session')
def db():
    return DbTests()


def test_db_fixture(db):
    data = [{'test': 42}, {'test': 43}]

    with db.test_collection(initial_data=data) as col:
        assert isinstance(col, pymongo.collection.Collection)
        doc = col.find_one({'test': 42})

        assert doc is not None
        assert doc['test'] == 42


def test_find_in_collection(db):
    data = [
        {'name': 'Ginger', 'age': 2},
        {'name': 'Kleo', 'age': 4},
        {'name': 'Fluffy', 'age': 7}
    ]

    with db.test_collection(initial_data=data) as col:
{'fraction_non_alphanumeric': 0.12119137281752825, 'fraction_numerical': 0.01437863745292708, 'mean_word_length': 2.7413572343149806, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4457236', 'n_tokens_mistral': 1005, 'n_tokens_neox': 940, 'n_words': 241}