Ejemplo n.º 1
0
def test_delete(mocker):
    mock_session = mocker.Mock(sqlalchemy.orm.Session)

    base_dao = BaseDao(Customer, mock_session)
    actual_result = base_dao.delete("ce05ae37-75ae-4b2c-b4e2-4384e5be0c73")

    mock_session.delete.assert_called_once()
    assert actual_result is True
Ejemplo n.º 2
0
def test_delete_exception(mocker, general_test_customer):
    mock_session = mocker.patch.object(sqlalchemy.orm.Session,
                                       'delete',
                                       name="mock_session")
    mock_session.delete.side_effect = RuntimeError("Error deleting customer")

    base_dao = BaseDao(Customer, mock_session)

    try:
        base_dao.delete(general_test_customer.get_identifier())
    except RuntimeError as e:
        assert str(e) == "Error deleting customer"
Ejemplo n.º 3
0
def test_update_exception(mocker, general_test_customer):
    mock_session = mocker.patch.object(sqlalchemy.orm.Session,
                                       'add',
                                       name="mock_session")
    mock_session.add.side_effect = RuntimeError("Error updating customer")

    base_dao = BaseDao(Customer, mock_session)

    try:
        base_dao.update(general_test_customer)
    except RuntimeError as e:
        assert str(e) == "Error updating customer"
Ejemplo n.º 4
0
def test_update(mocker, expected_update_result):
    mock_session = mocker.Mock(sqlalchemy.orm.Session)

    base_dao = BaseDao(Customer, mock_session)
    actual_result = base_dao.update(expected_update_result)

    mock_session.merge.assert_called_once()
    mock_session.add.assert_called_once()

    assert actual_result is not None
    assert actual_result.get_identifier(
    ) == expected_update_result.get_identifier()
Ejemplo n.º 5
0
def test_save(mocker, expected_save_result):
    mock_session = mocker.patch.object(sqlalchemy.orm.Session,
                                       'add',
                                       name="mock_session")
    mock_session.add.return_value = expected_save_result

    base_dao = BaseDao(Customer, mock_session)
    _actual_result = base_dao.save(expected_save_result)

    mock_session.add.assert_called_once()

    assert _actual_result is not None
    assert _actual_result.get_identifier(
    ) == expected_save_result.get_identifier()
Ejemplo n.º 6
0
def test_find_all(mocker, expected_find_all_result):

    mock_query = mocker.patch.object(sqlalchemy.orm.Query,
                                     'all',
                                     name="mock_result_proxy")
    mock_query.all.return_value = expected_find_all_result

    mock_session = mocker.patch.object(sqlalchemy.orm.Session,
                                       'query',
                                       name="mock_session")
    mock_session.query.return_value = mock_query

    base_dao = BaseDao(Customer, mock_session)
    _results = base_dao.find_all()

    mock_session.query.assert_called_once()
    mock_query.all.assert_called_once()

    assert _results is not None
    assert len(_results) == 1
Ejemplo n.º 7
0
def test_find_by_id(mocker, expected_find_by_id_result):
    mock_query = mocker.patch.object(sqlalchemy.orm.Query,
                                     'get',
                                     name="mock_result_proxy")
    mock_query.get.return_value = expected_find_by_id_result

    mock_session = mocker.patch.object(sqlalchemy.orm.Session,
                                       'query',
                                       name="mock_session")
    mock_session.query.return_value = mock_query

    base_dao = BaseDao(Customer, mock_session)
    _actual_result = base_dao.find_by_id(
        "7d0e2324-7057-435f-8510-f05a6bb2d7f5")

    mock_query.get.assert_called_once()
    mock_session.query.assert_called_once()

    assert _actual_result is not None
    assert _actual_result.get_identifier(
    ) == expected_find_by_id_result.get_identifier()