From 2c0a7ec9b68c55731c9e064e5db0568304afe57f Mon Sep 17 00:00:00 2001 From: Oleg Yurchik Date: Fri, 12 Apr 2024 17:05:06 +0300 Subject: [PATCH] Implement utils unit tests --- .github/workflows/integration.yaml | 4 +- README.md | 3 +- fast_grpc/service.py | 6 +-- tests/proto/test_utils.py | 62 +++++++++++++++++++++++++----- tests/test_service.py | 6 +++ 5 files changed, 62 insertions(+), 19 deletions(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index a9f97ee..2b75341 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -6,7 +6,7 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - - uses: snok/install-poetry@1 + - uses: snok/install-poetry@v1 - name: Install requirements run: poetry install - name: Check lint @@ -16,7 +16,7 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - - uses: snok/install-poetry@1 + - uses: snok/install-poetry@v1 - name: Install requirements run: poetry install - name: Check lint diff --git a/README.md b/README.md index 4bca1b0..2b1d865 100644 --- a/README.md +++ b/README.md @@ -37,5 +37,4 @@ app.run() ## TODO -1. More unit tests -2. Add documentation +1. Add documentation diff --git a/fast_grpc/service.py b/fast_grpc/service.py index a5f0557..bf90b3b 100644 --- a/fast_grpc/service.py +++ b/fast_grpc/service.py @@ -12,7 +12,7 @@ from .middleware import FastGRPCMiddleware -class GRPCMethod: +class grpc_method: # pylint: disable=invalid-name def __init__( self, name: str | None = None, @@ -84,10 +84,6 @@ def get_response_model_from_signature(signature) -> Type[BaseModel]: return signature.return_annotation -def grpc_method(*args, **kwargs) -> GRPCMethod: - return GRPCMethod(*args, **kwargs) - - class FastGRPCServiceMeta(type): def __init__(cls, name: str, bases: tuple[type], attributes: dict[str, Any]): super().__init__(name, bases, attributes) diff --git a/tests/proto/test_utils.py b/tests/proto/test_utils.py index d87970a..eacab0d 100644 --- a/tests/proto/test_utils.py +++ b/tests/proto/test_utils.py @@ -1,23 +1,65 @@ import pathlib -import pytest +from faker import Faker +from fast_grpc.proto import Service, compile_proto, delete_proto, render_proto, write_proto -@pytest.mark.skip("Need implement test") -def test_render_proto(): - pass + +def test_render_proto(faker: Faker): + name = faker.first_name() + service = Service(name=name, methods={}, messages={}) + + proto_content = render_proto(service=service).strip() + expected_proto_content = ( + "syntax = \"proto3\";\n" + f"package {name.lower()};\n" + "\n" + f'service {name} {{\n' + "}" + ) + + assert proto_content == expected_proto_content -@pytest.mark.skip("Need implement test") def test_write_proto(tmp_path: pathlib.Path): - pass + service = Service(name="Test", methods={}, messages={}) + content = """ + syntax = "proto3"; + package test; + + service Test {} + """ + + write_proto(service=service, proto_path=tmp_path, content=content) + + assert (tmp_path / "test.proto").read_text().strip() == content.strip() -@pytest.mark.skip("Need implement test") def test_compile_proto(tmp_path: pathlib.Path): - pass + service = Service(name="Test", methods={}, messages={}) + proto_content = """ + syntax = "proto3"; + package test; + + service Test {} + """ + (tmp_path / "test.proto").write_text(data=proto_content) + + compile_proto(service=service, proto_path=tmp_path, grpc_path=tmp_path) + + assert (tmp_path / "test_pb2.py").is_file() and (tmp_path / "test_pb2_grpc.py").is_file() -@pytest.mark.skip("Need implement test") def test_delete_proto(tmp_path: pathlib.Path): - pass + service = Service(name="Test", methods={}, messages={}) + proto_content = """ + syntax = "proto3"; + package test; + + service Test {} + """ + (tmp_path / "test.proto").write_text(data=proto_content) + + delete_proto(service=service, proto_path=tmp_path) + + assert not (tmp_path / "test.proto").is_file() diff --git a/tests/test_service.py b/tests/test_service.py index 9b98d9b..578d433 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -65,3 +65,9 @@ def test_get_proto(): ] assert service.get_proto().strip() in expected_variants + + +def test_get_service_name(): + service = PyTestService() + + assert service.get_service_name() == "pytestservice.PyTestService"