Repository navigation
Make test classes available - #21
Conversation
llucax
left a comment
There was a problem hiding this comment.
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.
I generally agree with that concern, but as long as the implementation is bellow 300 lines, I felt it was a non-issue. |
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>
30248d2 to
f8e3a10
Compare
|
llucax
left a comment
There was a problem hiding this comment.
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 😄
| from .generator import DispatchGenerator, to_create_params | ||
| from .service import FakeClient, FakeService | ||
|
|
||
| __all__ = ["DispatchGenerator", "FakeClient", "FakeService", "to_create_params"] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Ah, I went the other way now and removed all from init
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- Consistency, other projects are using that approach.
- The user of this library will import
from ...test.service import FakeClient. It is a bit weird to haveservicein there.
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>
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>
Also export the basic typeDispatchtestpackage