Beispiel #1
0
def test_batch_lists(store0, member):
    batch = Batch(Suggestion.objects, batch_size=2)
    assert batch.target == Suggestion.objects
    assert batch.batch_size == 2
    last_sugg_pk = Suggestion.objects.order_by("-pk").values_list(
        "pk", flat=True).first()

    def _create_method(unit, source, mtime):
        return dict(unit_id=unit,
                    creation_time=mtime,
                    target_f=source,
                    user_id=member.id)

    batch.create(list(store0.units.values_list("id", "source_f", "mtime")),
                 _create_method,
                 reduces=False)
    new_suggs = Suggestion.objects.filter(pk__gt=last_sugg_pk)
    assert new_suggs.count() == store0.units.count()
    updated_suggs = []
    for suggestion in new_suggs:
        suggestion.target_f = "suggestion %s" % suggestion.id
        updated_suggs.append(suggestion)
    batch.update(updated_suggs, update_fields=["target_f"], reduces=False)
    for suggestion in Suggestion.objects.filter(pk__gt=last_sugg_pk):
        assert suggestion.target_f == "suggestion %s" % suggestion.id
Beispiel #2
0
def test_batch_update_reduce(store0, member):
    """The source queryset reduces as batches are created"""
    batch = Batch(Unit, batch_size=2)
    store0.units.exclude(pk=store0.units.first().pk).update(target_f="FOO")

    def _update_method(unit):
        unit.target_f = "BAR"
        return unit

    count = batch.update(store0.units.filter(target_f="FOO"), _update_method)
    assert count == store0.units.count() - 1
    assert count == store0.units.filter(target_f="BAR").count()

    store0.units.update(target_f="BAZ")
    newcount = batch.update(store0.units.filter(target_f="FOO"),
                            _update_method,
                            update_fields=["source_f"])
    # units not updated, only source in update_fields
    assert newcount == 0
    assert (store0.units.count() == store0.units.filter(
        target_f="BAZ").count())