<commit_before>from __future__ import print_function
from seedbox.db import models as api_model
from seedbox.db.sqlalchemy import models as db_model
from seedbox.db.sqlalchemy import api
from seedbox.tests import test


class SAApiTestCase(test.ConfiguredBaseTestCase):

    def setUp(self):
        super(SAApiTestCase, self).setUp()

        self.dbconn = api.Connection(self.CONF)

    def _print_tables(self, msg_key):

        for t, tab in db_model.Base.metadata.tables.items():
            print(msg_key, 'table =>', t, tab.c)

    def test_upgrade(self):
        try:
            self.dbconn.upgrade()
            self.assertTrue(True)
        except:
            self.assertTrue(False)

    def test_clear(self):
        self.dbconn.clear()
        self.assertTrue(True)

    def test_backup(self):
        self.dbconn.backup()
        self.assertTrue(True)

    def test_shrink_db(self):
        self.dbconn.shrink_db()
        self.assertTrue(True)

    def test_save(self):

        torrent = api_model.Torrent(torrent_id=None, name='fake1.torrent')
        self.assertIsInstance(torrent, api_model.Torrent)

        _saved_torrent = self.dbconn.save(torrent)
        self.assertIsInstance(_saved_torrent, api_model.Torrent)

        self.assertNotEqual(torrent, _saved_torrent)

        _saved_torrent.state = 'active'
        torrent = self.dbconn.save(_saved_torrent)
        self.assertNotEqual(torrent, _saved_torrent)

    def test_bulk_create(self):

        torrent = api_model.Torrent(torrent_id=None, name='fake2.torrent')
        torrent = self.dbconn.save(torrent)

        _medias = []
        for i in range(1, 11):
            media = api_model.MediaFile.make_empty()
            media.filename = 'movie-{0}.mp4'.format(i)
            media.file_ext = '.mp4'
            media.file_path = '/tmp/media'
            media.torrent_id = torrent.torrent_id
            _medias.append(media)
        _results = self.dbconn.bulk_create(_medias)
        self.assertEqual(len(list(_results)), 10)

    def test_bulk_update(self):

        torrent = api_model.Torrent(torrent_id=None, name='fake3.torrent')
        torrent = self.dbconn.save(torrent)

        _medias = []
        for i in range(1, 11):
            media = api_model.MediaFile.make_empty()
            media.filename = 'movie-{0}.mp4'.format(i)
            media.file_ext = '.mp4'
            media.file_path = '/tmp/media'
            media.torrent_id = torrent.torrent_id
            _medias.append(media)
        self.dbconn.bulk_create(_medias)<commit_msg>fixed db save that fails randomly when two instances with different values not successful when asserting not equal<commit_after>from __future__ import print_function
from seedbox.db import models as api_model
from seedbox.db.sqlalchemy import models as db_model
from seedbox.db.sqlalchemy import api
from seedbox.tests import test


class SAApiTestCase(test.ConfiguredBaseTestCase):

    def setUp(self):
        super(SAApiTestCase, self).setUp()

        self.dbconn = api.Connection(self.CONF)

    def _print_tables(self, msg_key):

        for t, tab in db_model.Base.metadata.tables.items():
            print(msg_key, 'table =>', t, tab.c)

    def test_upgrade(self):
        try:
            self.dbconn.upgrade()
            self.assertTrue(True)
        except:
            self.assertTrue(False)

    def test_clear(self):
        self.dbconn.clear()
        self.assertTrue(True)

    def test_backup(self):
        self.dbconn.backup()
        self.assertTrue(True)

    def test_shrink_db(self):
        self.dbconn.shrink_db()
        self.assertTrue(True)

    def test_save(self):

        torrent = api_model.Torrent(torrent_id=None, name='fake1.torrent')
        self.assertIsInstance(torrent, api_model.Torrent)

        _saved_torrent = self.dbconn.save(torrent)
        self.assertIsInstance(_saved_torrent, api_model.Torrent)

        self.assertIsNone(torrent.created_at)
        self.assertIsNotNone(_saved_torrent.created_at)

        _saved_torrent.state = 'active'
        torrent = self.dbconn.save(_saved_torrent)
        self.assertIsNotNone(torrent.updated_at)
        self.assertIsNone(_saved_torrent.updated_at)

    def test_bulk_create(self):

        torrent = api_model.Torrent(torrent_id=None, name='fake2.torrent')
        torrent = self.dbconn.save(torrent)

        _medias = []
        for i in range(1, 11):
            media = api_model.MediaFile.make_empty()
            media.filename = 'movie-{0}.mp4'.format(i)
            media.file_ext = '.mp4'
            media.file_path = '/tmp/media'
            media.torrent_id = torrent.torrent_id
            _medias.append(media)
        _results = self.dbconn.bulk_create(_medias)
        self.assertEqual(len(list(_results)), 10)

    def test_bulk_update(self):

        torrent = api_model.Torrent(torrent_id=None, name='fake3.torrent')
        torrent = self.dbconn.save(torrent)

        _medias = []
        for i in range(1, 11):
            media = api_model.MediaFile.make_empty()
            media.filename = 'movie-{0}.mp4'.format(i)
            media.file_ext = '.mp4'
            media.file_path = '/tmp/media'
            media.torrent_id = torrent.torrent_id
            _medias.append(media)
        self.dbconn.bulk_create(_medias)
{'fraction_non_alphanumeric': 0.10478532248912427, 'fraction_numerical': 0.006430868167202572, 'mean_word_length': 2.5206391478029295, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2219974', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1670, 'n_words': 286}