def test_basic_getall_filter():
    fake_db = {}
    adapter = FakeAdapter(fake_db)
    make_context(adapter)
    adapter.filter = MagicMock(return_value=[mock_entity('siclano')])

    params = dict(filter_field='nome', filter_value='siclano')
    request = BasicGetAllRequestModel(params)
    interactor = BasicGetAllInteractor(request, adapter_instance=adapter)
    resultado = interactor.run()

    assert len(resultado.object_list) == 1
    assert resultado.object_list[0]['nome'] == 'siclano'
def test_basic_getall_sort_desc():
    fake_db = {}
    adapter = FakeAdapter(fake_db)
    make_context(adapter)
    adapter.filter = MagicMock(return_value=[
        mock_entity('siclano'),
        mock_entity('fulano'),
        mock_entity('beltrano')
    ])

    params = dict(sort_field='nome', sort_order='DESC')
    request = BasicGetAllRequestModel(params)
    interactor = BasicGetAllInteractor(request, adapter_instance=adapter)
    resultado = interactor.run()

    assert len(resultado.object_list) == 3
    assert resultado.object_list[0]['nome'] == 'yankee'
def test_basic_getall():
    fake_db = {}
    adapter = FakeAdapter(fake_db)
    entidades = make_context(adapter)

    request = BasicGetAllRequestModel(None)
    interactor = BasicGetAllInteractor(request, adapter_instance=adapter)
    resultado = interactor.run()

    assert len(resultado.object_list) == 3
    assert resultado.object_list[0]['nome'] in [x[0] for x in entidades]
Exemplo n.º 4
0
def test_basic_post_raises(save):
    fake_db = {}
    adapter = FakeAdapter(fake_db)

    post_data = {'nome': 'novo nome', 'idade': 42}

    request = BasicPostRequestModel(post_data)
    interactor = BasicPostInteractor(request, adapter, FakeEntity)
    with pytest.raises(SaveEntityException) as excinfo:
        interactor.run()
    assert str(excinfo.value) == 'Erro salvando entidade: oops'
Exemplo n.º 5
0
def test_basic_delete():
    fake_db = {}
    adapter = FakeAdapter(fake_db)
    make_context(adapter)

    entity_to_delete = list(fake_db.values())[0]['entity_id']
    deleted_name = fake_db[entity_to_delete]['nome']

    request = BasicDeleteRequestModel(entity_to_delete)
    interactor = BasicDeleteInteractor(request, adapter)

    assert len(fake_db) == 3

    response = interactor.run()

    assert len(fake_db) == 2
    assert deleted_name not in [x['nome'] for x in fake_db.values()]
    assert response == entity_to_delete
Exemplo n.º 6
0
def test_basic_post():
    fake_db = {}
    adapter = FakeAdapter(fake_db)
    make_context(adapter)

    post_data = {'nome': 'novo nome', 'idade': 42}

    request = BasicPostRequestModel(post_data)
    interactor = BasicPostInteractor(request, adapter, FakeEntity)

    assert len(fake_db) == 3

    result = interactor.run()

    assert result['nome'] == 'novo nome'
    assert result['idade'] == 42
    assert result['entity_id']

    assert len(fake_db) == 4
    assert 'novo nome' in [x['nome'] for x in fake_db.values()]
def test_basic_getall_paginated():
    fake_db = {}
    adapter = FakeAdapter(fake_db)
    entidades = make_context(adapter, 26)

    params = dict(pagination_perPage=5, pagination_page=1)
    request = BasicGetAllRequestModel(params)
    interactor = BasicGetAllInteractor(request, adapter_instance=adapter)
    resultado = interactor.run()

    assert len(resultado.object_list) == 5
    assert resultado.object_list[0]['nome'] == entidades[0][0]
    assert resultado.object_list[4]['nome'] == entidades[4][0]

    params = dict(pagination_perPage=5, pagination_page=2)
    request2 = BasicGetAllRequestModel(params)
    interactor2 = BasicGetAllInteractor(request2, adapter_instance=adapter)
    resultado2 = interactor2.run()

    assert len(resultado.object_list) == 5
    assert resultado2.object_list[0]['nome'] == entidades[5][0]
    assert resultado2.object_list[4]['nome'] == entidades[9][0]
def test_basic_put():
    fake_db = {}
    adapter = FakeAdapter(fake_db)
    make_context(adapter)

    entity_to_update = list(fake_db.values())[0]['entity_id']
    put_data = {
        'entity_id': entity_to_update,
        'nome': 'outro nome',
        'idade': 42
    }

    request = BasicPutRequestModel(entity_to_update, put_data)
    interactor = BasicPutInteractor(request, adapter, FakeEntity)

    result = interactor.run()

    assert result == dict(nome='outro nome',
                          idade=42,
                          entity_id=entity_to_update)

    assert fake_db[entity_to_update]['nome'] == 'outro nome'
    assert fake_db[entity_to_update]['idade'] == 42