Skip to content

Make test classes available - #21

Merged
Marenz merged 10 commits into
frequenz-floss:v0.x.xfrom
Marenz:test_package
Mar 7, 2024
Merged

Marenz merged 10 commits into
frequenz-floss:v0.x.xfrom
Marenz:test_package

Conversation

@Marenz

@Marenz Marenz commented Feb 29, 2024 •

Copy link
Copy Markdown
Contributor
  • Move conftest outside of dispatch namespace
  • Move Dispatch.id attribute to the top
  • Update dependency to use to/from datetime functions
  • Also export the basic type Dispatch
  • Move MockServices, MockClient and Sampler to test package

@Marenz
Marenz requested a review from a team as a code owner February 29, 2024 15:22
@github-actions github-actions Bot added part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) labels Feb 29, 2024
@github-actions github-actions Bot added the part:docs Affects the documentation label Feb 29, 2024

@llucax llucax left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In general I like this very much, and was planning to go on a similar direction for the microgrid API.

I'm a bit concerned about trying to sort of simulate the server instead of mocking it (for example, implementing the filtering logic), as it could get out of hand as the server becomes more complex. We shouldn't encourage to write unit tests that involves so high level of "simulation", but just mocks whatever it is using at the dumbest level possible. So instead I would probably just purely mock the return of the function. But I guess that is a broader discussion to have elsewhere.

I would also like to [register a pytest fixture[(https://docs.pytest.org/en/7.1.x/how-to/writing_plugins.html#making-your-plugin-installable-by-others) so users can just write tests like:

def test_my_app(microgrid_client: MockMicrogridClient) -> None:
    micorgrid_client.mock_whatever()

Like we do with time_machine and other plugins.

Comment thread src/frequenz/client/conftest.py Resolved
@Marenz

Marenz commented Mar 3, 2024

Copy link
Copy Markdown
Contributor Author

I'm a bit concerned about trying to sort of simulate the server instead of mocking

I generally agree with that concern, but as long as the implementation is bellow 300 lines, I felt it was a non-issue.

Comment thread tests/test_dispatch_client.py Resolved
Marenz added 2 commits March 4, 2024 18:55
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
It's the most important one, defining the identity,
it should be first.
`microgrid_id` just has to get over it, it is now second after all.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
@Marenz
Marenz force-pushed the test_package branch 4 times, most recently from 30248d2 to f8e3a10 Compare March 5, 2024 11:02
@Marenz
Marenz requested a review from llucax March 5, 2024 11:03
@Marenz

Marenz commented Mar 5, 2024

Copy link
Copy Markdown
Contributor Author
  • Named client/service Fake*
  • Made service available through client .service
  • Used that to add new tests that directly set or get the expected dispatches

@llucax llucax left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a few last comments, mainly about modules structure.

Also you have a trailing dot in the commit updating release notes. Please train your ChatGPT accordingly 😄

Comment thread pyproject.toml Resolved
Comment on lines +6 to +9
from .generator import DispatchGenerator, to_create_params
from .service import FakeClient, FakeService

__all__ = ["DispatchGenerator", "FakeClient", "FakeService", "to_create_params"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wouldn't do this for public modules, it is confusing to be able to find the same symbol in multiple places. I'd just make the sub-modules private (start with _) or leave this __init__ empty. The advantage or having stuff more scattered is that you don't need to import unnecessary stuff if you want to only use, say, the DispatchGenerator. The disadvantage is that you need to add more imports if you want to use the whole thing. I guess I would go with the later approach for simplicity, because it should be pretty unlikely that you want to use one but not the other.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am a bit confused for which approach you're arguing now.
The arguments you say sound to me like "keep commonly used things together" no?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Together how? (same file? Same module/package?) 😆

But no, my main argument is against exposing symbols publicly in different locations and being consistent. The rest have different trade-offs and might be valid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Specifically in this case I'm suggesting to make all modules in test private: test/_service.py, test/_generator.py, etc. and then keep the test/__init__.py as it is now, except for FakeService` that I think it shouldn't be added because downstream users of this project don't need it and shouldn't deal with protobuf stuff.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Then a separate issue is if you want to split those private modules (test/_service.py, etc.) into more or less files. Under this scheme, where the sub-module structure is just an implementation detail, it's not all that important how you split the stuff, as it is not part of the public interface. I would still try to keep modules small so they are manageable, and to groups very closely related stuff in the same module (specially stuff that needs to know about the implementation details of other stuff or access private data).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, I went the other way now and removed all from init

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OK, I think the repo is still having some mixed style, for example now we have stuff in types.py that shouldn't be really public (the functions to convert to/from protobuf, but we are also exposing this as methods, so it is a different refactor that we need there, and something more for the future as we need to coordinate with other APIs). Long term I think it would be simpler to have a flat structure for the client, so users don't need to import stuff from different nested modules. In terms of performance or dependencies, when you import __init__ you are importing _client which is importing types anyway, so is not like you are saving anything, and I guess it would be pretty unlikely that users import only the types, but maybe I'm wrong.

Anyway, out of scope for this PR, so approving.

Comment on lines +221 to +235
class FakeClient(Client):
"""Fake client for testing.

This client uses a fake service to simulate the dispatch api.
"""

def __init__(self) -> None:
"""Initialize the mock client."""
super().__init__(grpc.aio.insecure_channel("mock"), "mock")
self._stub = FakeService() # type: ignore

@property
def service(self) -> FakeService:
"""Get the fake service."""
return self._stub # type: ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would move this to client.py and make service.py private (_service.py), so you can use it in the tests for the client itself (in this repo) but it is still hidden to users of the client, which shouldn't do anything related to protobuf.

And if possible (and not very annoying for the tests of this repo), I would not make the service property public, if you are doing this only to provide access to the service.dispatches list, then add a dispatches property that returns self._stub.dispatches, assuming these dispatches are the wrappers and not the protobuf types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OK, I see now that you are using __all__ to hide the FakeService. I'm not sure if this will hide it from the generated docs too (you can try), but still I would recommend going for the splitting approach for 2 reasons:

  1. Consistency, other projects are using that approach.
  2. The user of this library will import from ...test.service import FakeClient. It is a bit weird to have service in there.

Comment thread tests/test_dispatch_client.py Resolved
Marenz added 3 commits March 6, 2024 13:55
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
Moves `FakeClient`, `FakeService`, the dispatch generator and the
utility function `to_create_params` to the test module.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
This allows users to directly manipulate what `FakeClient` calls return.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
Marenz added 5 commits March 6, 2024 17:21
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
See pytest-dev/pytest#12069

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
@Marenz
Marenz requested a review from llucax March 6, 2024 16:26
@Marenz
Marenz added this pull request to the merge queue Mar 7, 2024
Merged via the queue into frequenz-floss:v0.x.x with commit bfbe12a Mar 7, 2024
@Marenz
Marenz deleted the test_package branch March 7, 2024 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants