From ca3dadf44dc1a54341730910b47da06a68670492 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Fri, 6 Sep 2024 12:15:37 -0600 Subject: [PATCH] Get tests running --- collegamento/__init__.py | 4 +-- .../__init__.py | 0 .../client.py | 10 ++++-- .../server.py | 0 .../test_func.py | 0 .../utils.py | 0 collegamento/files_variant.py | 33 +++++-------------- tests/test_file_variant.py | 11 +++---- tests/test_simple.py | 6 +--- 9 files changed, 24 insertions(+), 40 deletions(-) rename collegamento/{simple_client_server => client_server}/__init__.py (100%) rename collegamento/{simple_client_server => client_server}/client.py (98%) rename collegamento/{simple_client_server => client_server}/server.py (100%) rename collegamento/{simple_client_server => client_server}/test_func.py (100%) rename collegamento/{simple_client_server => client_server}/utils.py (100%) diff --git a/collegamento/__init__.py b/collegamento/__init__.py index b224e9e..cc0bbaa 100644 --- a/collegamento/__init__.py +++ b/collegamento/__init__.py @@ -2,8 +2,7 @@ beartype_this_package() -from .files_variant import FileClient, FileServer # noqa: F401, E402 -from .simple_client_server import ( # noqa: F401, E402 +from .client_server import ( # noqa: F401, E402 COMMANDS_MAPPING, USER_FUNCTION, Client, @@ -14,3 +13,4 @@ ResponseQueueType, Server, ) +from .files_variant import FileClient, FileServer # noqa: F401, E402 diff --git a/collegamento/simple_client_server/__init__.py b/collegamento/client_server/__init__.py similarity index 100% rename from collegamento/simple_client_server/__init__.py rename to collegamento/client_server/__init__.py diff --git a/collegamento/simple_client_server/client.py b/collegamento/client_server/client.py similarity index 98% rename from collegamento/simple_client_server/client.py rename to collegamento/client_server/client.py index 298874e..0b0d6d9 100644 --- a/collegamento/simple_client_server/client.py +++ b/collegamento/client_server/client.py @@ -15,7 +15,13 @@ from time import sleep from .server import Server -from .utils import USER_FUNCTION, CollegamentoError, Request, Response +from .utils import ( + COMMANDS_MAPPING, + USER_FUNCTION, + CollegamentoError, + Request, + Response, +) class Client: @@ -32,7 +38,7 @@ class Client: def __init__( self, - commands: dict[str, USER_FUNCTION | tuple[USER_FUNCTION, bool]] = {}, + commands: COMMANDS_MAPPING = {}, id_max: int = 15_000, server_type: type = Server, ) -> None: diff --git a/collegamento/simple_client_server/server.py b/collegamento/client_server/server.py similarity index 100% rename from collegamento/simple_client_server/server.py rename to collegamento/client_server/server.py diff --git a/collegamento/simple_client_server/test_func.py b/collegamento/client_server/test_func.py similarity index 100% rename from collegamento/simple_client_server/test_func.py rename to collegamento/client_server/test_func.py diff --git a/collegamento/simple_client_server/utils.py b/collegamento/client_server/utils.py similarity index 100% rename from collegamento/simple_client_server/utils.py rename to collegamento/client_server/utils.py diff --git a/collegamento/files_variant.py b/collegamento/files_variant.py index 070524d..a57062d 100644 --- a/collegamento/files_variant.py +++ b/collegamento/files_variant.py @@ -1,8 +1,8 @@ # TODO: Actually fix this section of the package -from logging import Logger from typing import NotRequired -from .simple_client_server import ( +from .client_server import ( + COMMANDS_MAPPING, USER_FUNCTION, Client, CollegamentoError, @@ -22,13 +22,10 @@ def update_files(server: "FileServer", request: Request) -> None: file: str = request["file"] # type: ignore if request["remove"]: # type: ignore - server.logger.info(f"File {file} was requested for removal") server.files.pop(file) - server.logger.info(f"File {file} has been removed") else: contents: str = request["contents"] # type: ignore server.files[file] = contents - server.logger.info(f"File {file} has been updated with new contents") class FileClient(Client): @@ -38,7 +35,7 @@ class FileClient(Client): """ def __init__( - self, commands: dict[str, USER_FUNCTION], id_max: int = 15_000 + self, commands: COMMANDS_MAPPING, id_max: int = 15_000 ) -> None: self.files: dict[str, str] = {} @@ -51,12 +48,10 @@ def create_server(self) -> None: super().create_server() - self.logger.info("Copying files to server") files_copy = self.files.copy() self.files = {} for file, data in files_copy.items(): self.update_file(file, data) - self.logger.debug("Finished copying files to server") def request( self, @@ -65,10 +60,7 @@ def request( if "file" in request_details: file = request_details["file"] if file not in self.files: - self.logger.exception( - f"File {file} not in files! Files are {self.files.keys()}" - ) - raise Exception( + raise CollegamentoError( f"File {file} not in files! Files are {self.files.keys()}" ) @@ -77,10 +69,8 @@ def request( def update_file(self, file: str, current_state: str) -> None: """Updates files in the system - external API""" - self.logger.info(f"Updating file: {file}") self.files[file] = current_state - self.logger.debug("Creating notification dict") file_notification: dict = { "command": "FileNotification", "file": file, @@ -88,26 +78,21 @@ def update_file(self, file: str, current_state: str) -> None: "contents": self.files[file], } - self.logger.debug("Notifying server of file update") super().request(file_notification) def remove_file(self, file: str) -> None: """Removes a file from the main_server - external API""" if file not in list(self.files.keys()): - self.logger.exception( - f"Cannot remove file {file} as file is not in file database!" - ) raise CollegamentoError( f"Cannot remove file {file} as file is not in file database!" ) - self.logger.info("Notifying server of file deletion") file_notification: dict = { "command": "FileNotification", "file": file, "remove": True, } - self.logger.debug("Notifying server of file removal") + super().request(file_notification) @@ -116,18 +101,16 @@ class FileServer(Server): def __init__( self, - commands: dict[str, USER_FUNCTION], - response_queue: ResponseQueueType, + commands: dict[str, tuple[USER_FUNCTION, bool]], requests_queue: RequestQueueType, - logger: Logger, + response_queue: ResponseQueueType, ) -> None: self.files: dict[str, str] = {} super().__init__( commands, - response_queue, requests_queue, - logger, + response_queue, ["FileNotification"], ) diff --git a/tests/test_file_variant.py b/tests/test_file_variant.py index df0a126..761fb36 100644 --- a/tests/test_file_variant.py +++ b/tests/test_file_variant.py @@ -19,26 +19,25 @@ def split_str(server: FileServer, arg: Request) -> list[str]: def test_file_variants(): - commands: dict[str, USER_FUNCTION] = {"test": func} - context = FileClient(commands) + context = FileClient({"test": func}) context.update_file("test", "test contents") context.request({"command": "test"}) sleep(1) - output: Response | None = context.get_response("test") + output: list[Response] = context.get_response("test") assert output is not None # noqa: E711 - assert output["result"] is True # noqa: E712 # type: ignore + assert output[0]["result"] is True # noqa: E712 # type: ignore context.add_command("test1", split_str) context.request({"command": "test1", "file": "test"}) sleep(1) - output: Response | None = context.get_response("test1") + output = context.get_response("test1") assert output is not None # noqa: E711 - assert output["result"] == ["test", "contents"] # noqa: E712 # type: ignore + assert output[0]["result"] == ["test", "contents"] # noqa: E712 # type: ignore assert context.all_ids == [] diff --git a/tests/test_simple.py b/tests/test_simple.py index 26220f5..2b86fe4 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -7,7 +7,7 @@ def foo(server, request): print("Foo called", request["id"]) -def main(): +def test_normal_client(): Client({"foo": foo}) x = Client({"foo": (foo, True), "foo2": foo}) @@ -47,7 +47,3 @@ def main(): Client().create_server() sleep(1) - - -if __name__ == "__main__": - main()