Ejemplo n.º 1
0
def test_progress(capsys):
    remove_file('author.dump')

    Author.objects.create(id=2, name='2')
    call_command(COMMAND_NAME, '--cqrs-id=author', '--progress')

    captured = capsys.readouterr()
    assert 'Processing 1 records with batch size 10000' in captured.err
    assert '1 of 1 processed - 100% with rate' in captured.err
Ejemplo n.º 2
0
def test_error(capsys, mocker):
    remove_file('author.dump')
    Author.objects.create(id=2, name='2')

    mocker.patch('tests.dj_master.models.Author.to_cqrs_dict', side_effect=db_error)
    call_command(COMMAND_NAME, '--cqrs-id=author')

    captured = capsys.readouterr()
    assert 'Dump record failed for pk=2' in captured.err
    assert '1 instance(s) processed.' in captured.err
    assert '0 instance(s) saved.' in captured.err
Ejemplo n.º 3
0
def test_dumps_no_rows(capsys):
    remove_file('author.dump')

    call_command(COMMAND_NAME, '--cqrs-id=author')

    with open('author.dump', 'r') as f:
        lines = f.readlines()
        assert len(lines) == 1
        assert lines[0] == 'author'

    captured = capsys.readouterr()
    assert 'Done!\n0 instance(s) saved.\n0 instance(s) processed.' in captured.err
Ejemplo n.º 4
0
def tests_dumps_more_than_batch(capsys):
    remove_file('author.dump')

    Author.objects.bulk_create(
        (Author(id=index, name='n') for index in range(1, 150)), )

    call_command(COMMAND_NAME, '--cqrs-id=author')

    with open('author.dump', 'r') as f:
        lines = f.readlines()
        assert len(lines) == 150

    captured = capsys.readouterr()
    assert 'Done!\n149 instance(s) saved.\n149 instance(s) processed.' in captured.err
Ejemplo n.º 5
0
def tests_dumps_several_rows(capsys):
    remove_file('author.dump')

    Author.objects.create(id=2, name='2')

    with transaction.atomic():
        publisher = Publisher.objects.create(id=1, name='publisher')
        author = Author.objects.create(id=1, name='1', publisher=publisher)

    call_command(COMMAND_NAME, '--cqrs-id=author')

    with open('author.dump', 'r') as f:
        lines = f.readlines()
        assert len(lines) == 3
        assert lines[0].strip() == 'author'

        line_with_publisher = next(ln for ln in lines[1:] if '"name":"publisher"' in ln)
        assert author.to_cqrs_dict() == ujson.loads(line_with_publisher)

    captured = capsys.readouterr()
    assert 'Done!\n2 instance(s) saved.\n2 instance(s) processed.' in captured.err
Ejemplo n.º 6
0
def tests_dumps_several_rows(mocker):
    mocker.patch('dj_cqrs.controller.producer.produce')
    remove_file('bulk_flow.dump')

    master_models.Author.objects.create(id=2, name='2')

    with transaction.atomic():
        publisher = master_models.Publisher.objects.create(id=1,
                                                           name='publisher')
        master_models.Author.objects.create(id=1,
                                            name='1',
                                            publisher=publisher)

    assert replica_models.AuthorRef.objects.count() == 0
    assert replica_models.Publisher.objects.count() == 0

    call_command('cqrs_bulk_dump', '--cqrs-id=author', '-o=bulk_flow.dump')
    call_command('cqrs_bulk_load', '-i=bulk_flow.dump')

    assert replica_models.AuthorRef.objects.count() == 2
    assert replica_models.Publisher.objects.count() == 1