Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional param for passing an unique_id to call method. #438

Conversation

santiagosalamandri
Copy link
Contributor

This PR adds:
-An additional optional parameter to the call method.

This is needed for requirement E-13 on OCPP2.0.1 when the CS needs to retry sending a transaction message with the same ID.
Currently, when the call method is called, a Call object is created with a new random ID.

Copy link
Collaborator

@OrangeTux OrangeTux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Can you add or modify a unit test to cover the change?

@santiagosalamandri santiagosalamandri force-pushed the feat_add_optional_param_for_transaction_id branch from 081ffae to c0eea1a Compare April 17, 2023 10:06
@santiagosalamandri
Copy link
Contributor Author

@OrangeTux I added some tests that cover this new functionality. To maintain consistency with the existing codebase, I followed the same approach used in the repository and duplicated the same tests for the different OCPP versions.
However, I'm currently receiving an error during code analysis regarding duplicated code, do you have any advice for this?

@tropxy
Copy link
Collaborator

tropxy commented Apr 17, 2023

@santiagosalamandri not sure this is a SonarCloud bug (it happened in the past) but can you try to reduce duplication by using parametrized python testing?

For example, this code:

@pytest.mark.asyncio
async def test_call_with_unique_id_should_return_same_id(
    mock_boot_request, mock_base_central_system
):

    expected_unique_id = "12345"
    # Call the method being tested with a unique_id as a parameter
    await mock_base_central_system.call(mock_boot_request, unique_id=expected_unique_id)
    (
        actual_unique_id,
        _,
    ) = mock_base_central_system._get_specific_response.call_args_list[0][0]

    # Check the actual unique id is equals to the one passed to the call method
    assert actual_unique_id == expected_unique_id


@pytest.mark.asyncio
async def test_call_without_unique_id_should_return_a_random_value(
    mock_boot_request, mock_base_central_system
):

    expected_unique_id = str(mock_base_central_system._unique_id_generator())

    # Call the method being tested without passing a unique_id as a parameter
    await mock_base_central_system.call(mock_boot_request)

    (
        actual_unique_id,
        _,
    ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
    # Check the actual unique id is equals to the one internally generated
    assert actual_unique_id == expected_unique_id

could be reduced to one test with the unique_id being parametrized as argument for the test.

@santiagosalamandri
Copy link
Contributor Author

@santiagosalamandri not sure this is a SonarCloud bug (it happened in the past) but can you try to reduce duplication by using parametrized python testing?

For example, this code:

@pytest.mark.asyncio
async def test_call_with_unique_id_should_return_same_id(
    mock_boot_request, mock_base_central_system
):

    expected_unique_id = "12345"
    # Call the method being tested with a unique_id as a parameter
    await mock_base_central_system.call(mock_boot_request, unique_id=expected_unique_id)
    (
        actual_unique_id,
        _,
    ) = mock_base_central_system._get_specific_response.call_args_list[0][0]

    # Check the actual unique id is equals to the one passed to the call method
    assert actual_unique_id == expected_unique_id


@pytest.mark.asyncio
async def test_call_without_unique_id_should_return_a_random_value(
    mock_boot_request, mock_base_central_system
):

    expected_unique_id = str(mock_base_central_system._unique_id_generator())

    # Call the method being tested without passing a unique_id as a parameter
    await mock_base_central_system.call(mock_boot_request)

    (
        actual_unique_id,
        _,
    ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
    # Check the actual unique id is equals to the one internally generated
    assert actual_unique_id == expected_unique_id

could be reduced to one test with the unique_id being parametrized as argument for the test.

Hi @tropxy , yes, that will reduce the code duplication but it won't reduce the percentage duplication(which has to be less than 3% to pass), as it is calculated based on new code among the files that have changed. So, to overcome this, I could create different PRs for each test, what do you think @OrangeTux ?

@tropxy
Copy link
Collaborator

tropxy commented Apr 19, 2023

@santiagosalamandri not sure this is a SonarCloud bug (it happened in the past) but can you try to reduce duplication by using parametrized python testing?
For example, this code:

@pytest.mark.asyncio
async def test_call_with_unique_id_should_return_same_id(
    mock_boot_request, mock_base_central_system
):

    expected_unique_id = "12345"
    # Call the method being tested with a unique_id as a parameter
    await mock_base_central_system.call(mock_boot_request, unique_id=expected_unique_id)
    (
        actual_unique_id,
        _,
    ) = mock_base_central_system._get_specific_response.call_args_list[0][0]

    # Check the actual unique id is equals to the one passed to the call method
    assert actual_unique_id == expected_unique_id


@pytest.mark.asyncio
async def test_call_without_unique_id_should_return_a_random_value(
    mock_boot_request, mock_base_central_system
):

    expected_unique_id = str(mock_base_central_system._unique_id_generator())

    # Call the method being tested without passing a unique_id as a parameter
    await mock_base_central_system.call(mock_boot_request)

    (
        actual_unique_id,
        _,
    ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
    # Check the actual unique id is equals to the one internally generated
    assert actual_unique_id == expected_unique_id

could be reduced to one test with the unique_id being parametrized as argument for the test.

Hi @tropxy , yes, that will reduce the code duplication but it won't reduce the percentage duplication(which has to be less than 3% to pass), as it is calculated based on new code among the files that have changed. So, to overcome this, I could create different PRs for each test, what do you think @OrangeTux ?

Yup, understood, on the light of this I think we can just ignore the sound cloud analysis. All tests pass, which is good.

@tropxy tropxy merged commit b49cbe7 into mobilityhouse:master Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants