Exemplo n.º 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
Exemplo n.º 2
0
def test_batch_create_no_reduce(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)
    batches = batch.batched_create(
        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() == 0
    for batched in batches:
        assert len(batched)
        assert len(batched) <= 2
        assert all(isinstance(b, Suggestion) for b in batched)
        assert all(b.target_f == b.unit.source_f for b in batched)
    assert new_suggs.count() == store0.units.count()
    new_suggs.delete()
    created = batch.create(
        store0.units.values_list("id", "source_f", "mtime"),
        _create_method,
        reduces=False)
    new_suggs = Suggestion.objects.filter(pk__gt=last_sugg_pk)
    assert created == new_suggs.count() == store0.units.count()
    assert (
        list(new_suggs.values_list("unit"))
        == list(store0.units.values_list("id")))