<commit_before>from django.test import TestCase
from django_dynamic_fixture import G
from manager_utils import post_bulk_operation
from mock import patch

from manager_utils.tests.models import TestModel, TestForeignKeyModel, TestPkForeignKey, TestPkChar


class SyncTest(TestCase):
    """
    Tests the sync function.
    """
    def test_no_existing_objs(self):
        """
        Tests when there are no existing objects before the sync.
        """
        TestModel.objects.sync(
            [TestModel(int_field=1), TestModel(int_field=3), TestModel(int_field=4)], ['int_field'], ['float_field'])
        self.assertEquals(TestModel.objects.count(), 3)
        self.assertTrue(TestModel.objects.filter(int_field=1).exists())
        self.assertTrue(TestModel.objects.filter(int_field=3).exists())
        self.assertTrue(TestModel.objects.filter(int_field=4).exists())

    def test_existing_objs_all_deleted(self):
        """
        Tests when there are existing objects that will all be deleted.
        """
        extant_obj1 = G(TestModel, int_field=1)
        extant_obj2 = G(TestModel, int_field=2)
        extant_obj3 = G(TestModel, int_field=3)

        TestModel.objects.sync(
            [TestModel(int_field=4), TestModel(int_field=5), TestModel(int_field=6)], ['int_field'], ['float_field'])

        self.assertEquals(TestModel.objects.count(), 3)
        self.assertTrue(TestModel.objects.filter(int_field=4).exists())
        self.assertTrue(TestModel.objects.filter(int_field=5).exists())
        self.assertTrue(TestModel.objects.filter(int_field=6).exists())<commit_msg>added sync test for different pk<commit_after>from django.test import TestCase
from django_dynamic_fixture import G
from manager_utils import post_bulk_operation
from mock import patch

from manager_utils.tests.models import TestModel, TestForeignKeyModel, TestPkForeignKey, TestPkChar


class SyncTest(TestCase):
    """
    Tests the sync function.
    """
    def test_w_char_pk(self):
        """
        Tests with a model that has a char pk.
        """
        extant_obj1 = G(TestPkChar, my_key='1', char_field='1')
        extant_obj2 = G(TestPkChar, my_key='2', char_field='1')
        extant_obj3 = G(TestPkChar, my_key='3', char_field='1')

        TestPkChar.objects.sync([
            TestPkChar(my_key='3', char_field='2'), TestPkChar(my_key='4', char_field='2'),
            TestPkChar(my_key='5', char_field='2')
        ], ['my_key'], ['char_field'])

        self.assertEquals(TestPkChar.objects.count(), 3)
        self.assertTrue(TestPkChar.objects.filter(my_key='3').exists())
        self.assertTrue(TestPkChar.objects.filter(my_key='4').exists())
        self.assertTrue(TestPkChar.objects.filter(my_key='5').exists())

        with self.assertRaises(TestPkChar.DoesNotExist):
            TestPkChar.objects.get(pk=extant_obj1.pk)
        with self.assertRaises(TestPkChar.DoesNotExist):
            TestPkChar.objects.get(pk=extant_obj2.pk)
        test_model = TestPkChar.objects.get(pk=extant_obj3.pk)
        self.assertEquals(test_model.char_field, '2')

    def test_no_existing_objs(self):
        """
        Tests when there are no existing objects before the sync.
        """
        TestModel.objects.sync(
            [TestModel(int_field=1), TestModel(int_field=3), TestModel(int_field=4)], ['int_field'], ['float_field'])
        self.assertEquals(TestModel.objects.count(), 3)
        self.assertTrue(TestModel.objects.filter(int_field=1).exists())
        self.assertTrue(TestModel.objects.filter(int_field=3).exists())
        self.assertTrue(TestModel.objects.filter(int_field=4).exists())

    def test_existing_objs_all_deleted(self):
        """
        Tests when there are existing objects that will all be deleted.
        """
        extant_obj1 = G(TestModel, int_field=1)
        extant_obj2 = G(TestModel, int_field=2)
        extant_obj3 = G(TestModel, int_field=3)

        TestModel.objects.sync(
            [TestModel(int_field=4), TestModel(int_field=5), TestModel(int_field=6)], ['int_field'], ['float_field'])

        self.assertEquals(TestModel.objects.count(), 3)
        self.assertTrue(TestModel.objects.filter(int_field=4).exists())
        self.assertTrue(TestModel.objects.filter(int_field=5).exists())
        self.assertTrue(TestModel.objects.filter(int_field=6).exists())
{'fraction_non_alphanumeric': 0.1269000460617227, 'fraction_numerical': 0.014509442653155229, 'mean_word_length': 3.586061246040127, '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': '4769522', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1369, 'n_words': 221}