<commit_before>"""Integration tests"""

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from simon import Model, connection


def skip_without_setting(setting):
    try:
        import integration_settings
    except ImportError:
        pass
    else:
        if hasattr(integration_settings, setting):
            return lambda f: f

    message = 'integration_settings.{0} was not found'.format(setting)
    return unittest.skip(message)


class TestModel(Model):
    class Meta:
        collection = 'simon-integration'
        database = 'simon-integration'


class TestConnectionIntegrations(unittest.TestCase):
    """Connection integration tests"""

    def test__get_connection_with_localhost(self):
        """Test the `_get_connection()` method with localhost."""

        connection._get_connection(host='localhost', port=None)
        self.assertIsInstance(connection._connections['localhost:27017'],
                              connection.MongoClient)

    @skip_without_setting('REMOTE_HOST')
    def test__get_connection_with_remote_host(self):
        """Test the `_get_connection()` method with a remote host."""

        # The import is safe if the method passed the skip check
        from integration_settings import REMOTE_HOST

        username = REMOTE_HOST.get('MONGODB_USERNAME')
        password = REMOTE_HOST.get('<PASSWORD>')
        host = REMOTE_HOST.get('MONGODB_HOST')
        port = REMOTE_HOST.get('MONGODB_PORT', 27017)
        dbname = REMOTE_HOST.get('MONGODB_DBNAME')

        url = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format(username, password,
                                                     host, port, dbname)
        connection._get_connection(host=url, port=None)
        self.assertIsInstance(
            connection._connections['{0}:{1}'.format(host, port)],
            connection.MongoClient)

    @skip_without_setting('REMOTE_REPLICA_SET')
    def test__get_connection_with_remote_host_replica_set(self):
        ("Test the `_get_connection()` method with a remote replica "
         "set.")

        # The import is safe if the method passed the skip check
        from integration_settings import REMOTE_REPLICA_SET

        username = REMOTE_REPLICA_SET.get('MONGODB_USERNAME')
        password = REMOTE_REPLICA_SET.get('MONGODB_PASSWORD')
        host = REMOTE_REPLICA_SET.get('MONGODB_HOST')
        dbname = REMOTE_REPLICA_SET.get('MONGODB_DBNAME')
        replica_set = REMOTE_REPLICA_SET.get('MONGODB_REPLICA_SET')

        url = 'mongodb://{0}:{1}@{2}/{3}'.format(username, password,
                                                 host, dbname)
        connection._get_connection(host=url, port=None,
                                   replica_set=replica_set)
        self.assertIsInstance(
            connection._connections['{0}:{1}'.format(url, replica_set)],
            connection.MongoReplicaSetClient)


class TestDatabaseIntegrations(unittest.TestCase):
    """Database integration tests"""

    @classmethod
    def setUpClass(cls):
        cls.connection = connection.connect(name='simon-integration')
        cls.db = cls.connection['simon-integration']
        cls.collection = cls.db['simon-integration']

    def tearDown(self):
        self.db.drop_collection('simon-integration')

    @classmethod
    def tearDownClass(cls):
        cls.connection.drop_database('simon-integration')

    def test_increment(self):
        """Test the `increment()` method."""

        _id = self.collection.insert({'a': 1})

        m = TestModel.get(_id=_id)
        m.increment('a', 2)

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(doc['a'], 3)

    def test_push(self):
        """Test the `push()` method."""

        _id = self.collection.insert({})

        m = TestModel.get(_id=_id)

        # $push

        m.push('a', 1)

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(len(doc['a']), 1)
        self.assertIn(1, doc['a'])

        m.push(a=2)

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(len(doc['a']), 2)
        self.assertIn(2, doc['a'])

        # $pushAll

        m.push(a=[3, 4])

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(len(doc['a']), 4)
        self.assertIn(3, doc['a'])
        self.assertIn(4, doc['a'])

        self.assertEqual(doc['a'], [1, 2, 3, 4])

    def test_raw_update(self):
        """Test the `raw_update()` method."""

        _id = self.collection.insert({'a': 1})

        m = TestModel.get(_id=_id)
        m.raw_update({'$set': {'b': 2}})

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(doc['b'], 2)

    def test_remove_fields(self):
        """Test the `remove_fields()` method."""

        _id = self.collection.insert({'a': 1})

        m = TestModel.get(_id=_id)
        m.remove_fields('a')<commit_msg>Add a comment<commit_after>"""Integration tests"""

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from simon import Model, connection


def skip_without_setting(setting):
    try:
        import integration_settings
    except ImportError:
        pass
    else:
        if hasattr(integration_settings, setting):
            return lambda f: f

    message = 'integration_settings.{0} was not found'.format(setting)
    return unittest.skip(message)


class TestModel(Model):
    class Meta:
        collection = 'simon-integration'
        database = 'simon-integration'


class TestConnectionIntegrations(unittest.TestCase):
    """Connection integration tests"""

    def test__get_connection_with_localhost(self):
        """Test the `_get_connection()` method with localhost."""

        connection._get_connection(host='localhost', port=None)
        self.assertIsInstance(connection._connections['localhost:27017'],
                              connection.MongoClient)

    @skip_without_setting('REMOTE_HOST')
    def test__get_connection_with_remote_host(self):
        """Test the `_get_connection()` method with a remote host."""

        # The import is safe if the method passed the skip check
        from integration_settings import REMOTE_HOST

        username = REMOTE_HOST.get('MONGODB_USERNAME')
        password = REMOTE_HOST.get('MONGODB_PASSWORD')
        host = REMOTE_HOST.get('MONGODB_HOST')
        port = REMOTE_HOST.get('MONGODB_PORT', 27017)
        dbname = REMOTE_HOST.get('MONGODB_DBNAME')

        url = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format(username, password,
                                                     host, port, dbname)
        connection._get_connection(host=url, port=None)
        self.assertIsInstance(
            connection._connections['{0}:{1}'.format(host, port)],
            connection.MongoClient)

    @skip_without_setting('REMOTE_REPLICA_SET')
    def test__get_connection_with_remote_host_replica_set(self):
        ("Test the `_get_connection()` method with a remote replica "
         "set.")

        # The import is safe if the method passed the skip check
        from integration_settings import REMOTE_REPLICA_SET

        username = REMOTE_REPLICA_SET.get('MONGODB_USERNAME')
        password = <PASSWORD>('MONGODB_PASSWORD')
        host = REMOTE_REPLICA_SET.get('MONGODB_HOST')
        dbname = REMOTE_REPLICA_SET.get('MONGODB_DBNAME')
        replica_set = REMOTE_REPLICA_SET.get('MONGODB_REPLICA_SET')

        url = 'mongodb://{0}:{1}@{2}/{3}'.format(username, password,
                                                 host, dbname)
        connection._get_connection(host=url, port=None,
                                   replica_set=replica_set)
        self.assertIsInstance(
            connection._connections['{0}:{1}'.format(url, replica_set)],
            connection.MongoReplicaSetClient)


class TestDatabaseIntegrations(unittest.TestCase):
    """Database integration tests"""

    @classmethod
    def setUpClass(cls):
        cls.connection = connection.connect(name='simon-integration')
        cls.db = cls.connection['simon-integration']
        cls.collection = cls.db['simon-integration']

    def tearDown(self):
        self.db.drop_collection('simon-integration')

    @classmethod
    def tearDownClass(cls):
        cls.connection.drop_database('simon-integration')

    def test_increment(self):
        """Test the `increment()` method."""

        _id = self.collection.insert({'a': 1})

        m = TestModel.get(_id=_id)
        m.increment('a', 2)

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(doc['a'], 3)

    def test_push(self):
        """Test the `push()` method."""

        _id = self.collection.insert({})

        m = TestModel.get(_id=_id)

        # $push

        m.push('a', 1)

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(len(doc['a']), 1)
        self.assertIn(1, doc['a'])

        m.push(a=2)

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(len(doc['a']), 2)
        self.assertIn(2, doc['a'])

        # $pushAll

        m.push(a=[3, 4])

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(len(doc['a']), 4)
        self.assertIn(3, doc['a'])
        self.assertIn(4, doc['a'])

        # Make sure all of the values have been inserted in order
        self.assertEqual(doc['a'], [1, 2, 3, 4])

    def test_raw_update(self):
        """Test the `raw_update()` method."""

        _id = self.collection.insert({'a': 1})

        m = TestModel.get(_id=_id)
        m.raw_update({'$set': {'b': 2}})

        doc = self.collection.find_one({'_id': _id})

        self.assertEqual(doc['b'], 2)

    def test_remove_fields(self):
        """Test the `remove_fields()` method."""

        _id = self.collection.insert({'a': 1})

        m = TestModel.get(_id=_id)
        m.remove_fields('a')
{'fraction_non_alphanumeric': 0.13232323232323231, 'fraction_numerical': 0.009494949494949495, 'mean_word_length': 2.6240849194729137, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2717960', 'n_tokens_mistral': 3200, 'n_tokens_neox': 2962, 'n_words': 566}