Exemplo n.º 1
0
    def test_big_insert_update(self):
        # Expected to fail in SQLite (too many variables)
        set1 = [TestModelA(a="Test", b=i, c=1) for i in range(1000)]

        insert_many(TestModelA, set1)
        self.assertEqual(1000, TestModelA.objects.all().count())

        set2 = [TestModelA(a="Test", b=i, c=2) for i in range(500, 2000)]
        insert_or_update_many(TestModelA, set2, keys=['b'])
        self.assertEqual(2000, TestModelA.objects.all().count())
Exemplo n.º 2
0
    def test_multi_insert(self):
        n = TestModelA(a="Test", b=1, c=2)
        insert_many(TestModelA, [n, n, n])
        self.assertEqual(3, TestModelA.objects.all().count())

        a = TestModelA.objects.all()[0]
        b = TestModelA.objects.all()[1]
        self.assertEqual(a.a, b.a)
        self.assertEqual(a.b, b.b)
        self.assertEqual(a.c, b.c)
Exemplo n.º 3
0
    def test_insert_with_auto_now_add(self):
        n = TestModelAutoCreated(a="Test", b=1)

        insert_many(TestModelAutoCreated, [n])
        self.assertEqual(1, TestModelAutoCreated.objects.all().count())

        n = TestModelAutoCreated.objects.all()[0]
        self.assertEqual(n.a, "Test")
        self.assertEqual(n.b, 1)
        self.assertTrue(n.created)
Exemplo n.º 4
0
    def test_basic_insert(self):
        n = TestModelA(a="Test", b=1, c=2)

        self.assertEqual(0, TestModelA.objects.all().count())
        insert_many(TestModelA, [n])
        self.assertEqual(1, TestModelA.objects.all().count())

        n = TestModelA.objects.all()[0]
        self.assertEqual(n.a, "Test")
        self.assertEqual(n.b, 1)
        self.assertEqual(n.c, 2)
Exemplo n.º 5
0
    def test_returned_results_insert_update(self):
        # Expected to fail in SQLite (too many variables)
        set1 = [TestModelA(a="Test", b=i, c=1) for i in range(1000)]
        insert_many(TestModelA, set1)

        set2 = [TestModelA(a="Test", b=i, c=2) for i in range(500, 2000)]
        inserted, updated = insert_or_update_many(TestModelA, set2, keys=['b'])
        self.assertEqual(1000, len(inserted))
        self.assertEqual(500, len(updated))
        self.assertTrue(isinstance(inserted[0], dict))
        self.assertTrue(isinstance(updated[0], dict))
Exemplo n.º 6
0
    def test_multikey_insert_update(self):
        # Expected to fail in SQLite (no tuple comparison)
        set1 = [
            TestModelA(a="Test1", b=1, c=1),
            TestModelA(a="Test2", b=2, c=2),
            ]

        insert_many(TestModelA, set1)
        self.assertEqual(2, TestModelA.objects.all().count())

        set2 = [
            TestModelA(a="Test1", b=1, c=3),
            TestModelA(a="Test2", b=3, c=4),
            TestModelA(a="Test3", b=3, c=3),
            ]

        insert_or_update_many(TestModelA, set2, keys=['a', 'b'])
        self.assertEqual(4, TestModelA.objects.all().count())
        self.assertEqual(2, TestModelA.objects.filter(a="Test2").count())

        self.assertEqual(3, TestModelA.objects.get(a="Test1", b=1).c)
        self.assertEqual(3, TestModelA.objects.get(a="Test3", b=3).c)
Exemplo n.º 7
0
    def test_multi_update(self):
        set1 = [
            TestModelA(a="Test1", b=1, c=2),
            TestModelA(a="Test2", b=3, c=4),
            TestModelA(a="Test3", b=5, c=6),
            ]

        insert_many(TestModelA, set1)
        self.assertEqual(3, TestModelA.objects.all().count())

        set2 = [
            TestModelA(a="Test1", b=7, c=8),
            TestModelA(a="Test2", b=9, c=10),
            TestModelA(a="Test3", b=11, c=12),
            ]

        update_many(TestModelA, set2, keys=['a'])
        self.assertEqual(3, TestModelA.objects.all().count())

        for i in set2:
            n = TestModelA.objects.get(a=i.a)
            self.assertEqual(n.b, i.b)
            self.assertEqual(n.c, i.c)
Exemplo n.º 8
0
    def test_multi_insert(self):
        n = TestModelA(a="Test", b=1, c=2)
        entries = insert_many(TestModelA, [n, n, n], skip_result=False)
        self.assertEqual(3, TestModelA.objects.all().count())

        a = TestModelA.objects.all()[0]
        b = TestModelA.objects.all()[1]
        self.assertEqual(a.a, b.a)
        self.assertEqual(a.b, b.b)
        self.assertEqual(a.c, b.c)

        # Test Returned data
        self.assertTrue(isinstance(entries[0]['b'], int))
        self.assertEqual(entries[0]['a'], 'Test')
Exemplo n.º 9
0
    def test_multikey_update(self):
        set1 = [
            TestModelA(a="Test1", b=1, c=1),
            TestModelA(a="Test1", b=1, c=2),
            TestModelA(a="Test2", b=1, c=3),
            TestModelA(a="Test1", b=2, c=4),
            ]

        insert_many(TestModelA, set1)
        self.assertEqual(4, TestModelA.objects.all().count())

        set2 = [
            TestModelA(a="Test1", b=1, c=5),
            ]

        update_many(TestModelA, set2, keys=['a', 'b'])
        self.assertEqual(4, TestModelA.objects.all().count())
        self.assertEqual(2, TestModelA.objects.filter(a="Test1", b=1).count())

        for i in TestModelA.objects.filter(a="Test1", b=1):
            self.assertEqual(5, i.c)

        self.assertEqual(3, TestModelA.objects.get(a="Test2", b=1).c)
        self.assertEqual(4, TestModelA.objects.get(a="Test1", b=2).c)
Exemplo n.º 10
0
 def test_presave_called(self):
     m = TestModelPreSave()
     m.a = 3
     insert_many(TestModelPreSave, [m])
     self.assertEquals(m.a, 5)
Exemplo n.º 11
0
 def bulk_insert(self, records):
     """Bulk insert translations."""
     # TODO Maybe use COPY instead?
     insert_many(Translation, records)
Exemplo n.º 12
0
 def bulk_insert(self, records):
     """Bulk insert records to the database."""
     # TODO Maybe use COPY instead?
     insert_many(SourceEntity, records)