コード例 #1
0
def test_sync_w_char_pk():
    """
    Tests with a model that has a char pk.
    """
    extant_obj1 = ddf.G(models.TestPkChar, my_key='1', char_field='1')
    extant_obj2 = ddf.G(models.TestPkChar, my_key='2', char_field='1')
    extant_obj3 = ddf.G(models.TestPkChar, my_key='3', char_field='1')

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

    assert models.TestPkChar.objects.count() == 3
    assert models.TestPkChar.objects.filter(my_key='3').exists()
    assert models.TestPkChar.objects.filter(my_key='4').exists()
    assert models.TestPkChar.objects.filter(my_key='5').exists()

    with pytest.raises(models.TestPkChar.DoesNotExist):
        models.TestPkChar.objects.get(pk=extant_obj1.pk)
    with pytest.raises(models.TestPkChar.DoesNotExist):
        models.TestPkChar.objects.get(pk=extant_obj2.pk)
    test_model = models.TestPkChar.objects.get(pk=extant_obj3.pk)
    assert test_model.char_field == '2'
コード例 #2
0
def test_sync_existing_objs_all_deleted():
    """
    Tests when there are existing objects that will all be deleted.
    """
    extant_obj1 = ddf.G(models.TestModel, int_field=1)
    extant_obj2 = ddf.G(models.TestModel, int_field=2)
    extant_obj3 = ddf.G(models.TestModel, int_field=3)

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

    assert models.TestModel.objects.count() == 3
    assert models.TestModel.objects.filter(int_field=4).exists()
    assert models.TestModel.objects.filter(int_field=5).exists()
    assert models.TestModel.objects.filter(int_field=6).exists()

    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj1.id)
    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj2.id)
    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj3.id)
コード例 #3
0
def test_sync_existing_objs_some_deleted():
    """
    Tests when some existing objects will be deleted.
    """
    extant_obj1 = ddf.G(models.TestModel, int_field=1, float_field=1)
    extant_obj2 = ddf.G(models.TestModel, int_field=2, float_field=1)
    extant_obj3 = ddf.G(models.TestModel, int_field=3, float_field=1)

    pgbulk.sync(
        models.TestModel,
        [
            models.TestModel(int_field=3, float_field=2),
            models.TestModel(int_field=4, float_field=2),
            models.TestModel(int_field=5, float_field=2),
        ],
        ['int_field'],
        ['float_field'],
    )

    assert models.TestModel.objects.count() == 3
    assert models.TestModel.objects.filter(int_field=3).exists()
    assert models.TestModel.objects.filter(int_field=4).exists()
    assert models.TestModel.objects.filter(int_field=5).exists()

    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj1.id)
    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj2.id)
    test_model = models.TestModel.objects.get(id=extant_obj3.id)
    assert test_model.int_field == 3
コード例 #4
0
def test_sync_existing_objs_some_deleted_some_updated():
    """
    Tests when some existing objects will be deleted on a queryset. Run syncing
    with some update fields.
    """
    objs = [
        ddf.G(models.TestModel, int_field=i, float_field=i) for i in range(5)
    ]

    results = pgbulk.sync(
        models.TestModel.objects.filter(int_field__lt=4),
        [
            models.TestModel(int_field=1, float_field=2),
            models.TestModel(int_field=2, float_field=2),
            models.TestModel(int_field=3, float_field=2),
        ],
        ['int_field'],
        ['float_field'],
        returning=True,
        ignore_duplicate_updates=True,
    )

    assert len(list(results)) == 4
    assert len(list(results.deleted)) == 1
    assert len(list(results.updated)) == 2
    assert len(list(results.untouched)) == 1
    assert list(results.deleted)[0].id == objs[0].id
コード例 #5
0
def test_sync_existing_objs_some_deleted_wo_update():
    """
    Tests when some existing objects will be deleted on a queryset. Run syncing
    with no update fields and verify they are untouched in the sync
    """
    objs = [
        ddf.G(models.TestModel, int_field=i, float_field=i) for i in range(5)
    ]

    results = pgbulk.sync(
        models.TestModel.objects.filter(int_field__lt=4),
        [
            models.TestModel(int_field=1, float_field=2),
            models.TestModel(int_field=2, float_field=2),
            models.TestModel(int_field=3, float_field=2),
        ],
        ['int_field'],
        [],
        returning=True,
    )

    assert len(list(results)) == 4
    assert len(list(results.deleted)) == 1
    assert len(list(results.untouched)) == 3
    assert list(results.deleted)[0].id == objs[0].id
コード例 #6
0
def test_sync_existing_objs_all_deleted_empty_sync():
    """
    Tests when there are existing objects deleted because of an emtpy sync.
    """
    extant_obj1 = ddf.G(models.TestModel, int_field=1)
    extant_obj2 = ddf.G(models.TestModel, int_field=2)
    extant_obj3 = ddf.G(models.TestModel, int_field=3)

    pgbulk.sync(models.TestModel, [], ['int_field'], ['float_field'])

    assert not models.TestModel.objects.exists()
    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj1.id)
    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj2.id)
    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj3.id)
コード例 #7
0
def test_sync_no_existing_objs():
    """
    Tests when there are no existing objects before the sync.
    """
    pgbulk.sync(
        models.TestModel,
        [
            models.TestModel(int_field=1),
            models.TestModel(int_field=3),
            models.TestModel(int_field=4),
        ],
        ['int_field'],
        ['float_field'],
    )
    assert models.TestModel.objects.count() == 3
    assert models.TestModel.objects.filter(int_field=1).exists()
    assert models.TestModel.objects.filter(int_field=3).exists()
    assert models.TestModel.objects.filter(int_field=4).exists()
コード例 #8
0
def test_sync_existing_objs_some_deleted_w_queryset():
    """
    Tests when some existing objects will be deleted on a queryset
    """
    extant_obj0 = ddf.G(models.TestModel, int_field=0, float_field=1)
    extant_obj1 = ddf.G(models.TestModel, int_field=1, float_field=1)
    extant_obj2 = ddf.G(models.TestModel, int_field=2, float_field=1)
    extant_obj3 = ddf.G(models.TestModel, int_field=3, float_field=1)
    extant_obj4 = ddf.G(models.TestModel, int_field=4, float_field=0)

    pgbulk.sync(
        models.TestModel.objects.filter(int_field__lt=4),
        [
            models.TestModel(int_field=1, float_field=2),
            models.TestModel(int_field=2, float_field=2),
            models.TestModel(int_field=3, float_field=2),
        ],
        ['int_field'],
        ['float_field'],
    )

    assert models.TestModel.objects.count() == 4
    assert models.TestModel.objects.filter(int_field=1).exists()
    assert models.TestModel.objects.filter(int_field=2).exists()
    assert models.TestModel.objects.filter(int_field=3).exists()

    with pytest.raises(models.TestModel.DoesNotExist):
        models.TestModel.objects.get(id=extant_obj0.id)

    test_model = models.TestModel.objects.get(id=extant_obj1.id)
    assert test_model.float_field == 2
    test_model = models.TestModel.objects.get(id=extant_obj2.id)
    assert test_model.float_field == 2
    test_model = models.TestModel.objects.get(id=extant_obj3.id)
    assert test_model.float_field == 2
    test_model = models.TestModel.objects.get(id=extant_obj4.id)
    assert test_model.float_field == 0