Skip to content

Commit

Permalink
Implement utils unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegYurchik committed Apr 12, 2024
1 parent 236cc02 commit 2c0a7ec
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ app.run()

## TODO

1. More unit tests
2. Add documentation
1. Add documentation
6 changes: 1 addition & 5 deletions fast_grpc/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .middleware import FastGRPCMiddleware


class GRPCMethod:
class grpc_method: # pylint: disable=invalid-name
def __init__(
self,
name: str | None = None,
Expand Down Expand Up @@ -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)
Expand Down
62 changes: 52 additions & 10 deletions tests/proto/test_utils.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 2c0a7ec

Please sign in to comment.