autogen/test/io/test_console.py
Davor Runje 78aa0eb220
Introducing IOStream protocol and adding support for websockets (#1551)
* Introducing IOStream

* bug fixing

* polishing

* refactoring

* refactoring

* refactoring

* wip: async tests

* websockets added

* wip

* merge with main

* notebook added

* FastAPI example added

* wip

* merge

* getter/setter to iostream added

* website/blog/2024-03-03-AutoGen-Update/img/dalle_gpt4v.png: convert to Git LFS

* website/blog/2024-03-03-AutoGen-Update/img/gaia.png: convert to Git LFS

* website/blog/2024-03-03-AutoGen-Update/img/teach.png: convert to Git LFS

* add SSL support

* wip

* wip

* exception handling added to on_connect()

* refactoring: default iostream is being set in a context manager

* test fix

* polishing

* polishing

* polishing

* fixed bug with new thread

* polishing

* a bit of refactoring and docs added

* notebook added to docs

* type checking added to CI

* CI fix

* CI fix

* CI fix

* polishing

* obsolete todo comment removed

* fixed precommit error

---------

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
2024-03-26 22:39:55 +00:00

39 lines
1.4 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from autogen.io import IOConsole
class TestConsoleIO:
def setup_method(self) -> None:
self.console_io = IOConsole()
@patch("builtins.print")
def test_print(self, mock_print: MagicMock) -> None:
# calling the print method should call the mock of the builtin print function
self.console_io.print("Hello, World!", flush=True)
mock_print.assert_called_once_with("Hello, World!", end="\n", sep=" ", flush=True)
@patch("builtins.input")
def test_input(self, mock_input: MagicMock) -> None:
# calling the input method should call the mock of the builtin input function
mock_input.return_value = "Hello, World!"
actual = self.console_io.input("Hi!")
assert actual == "Hello, World!"
mock_input.assert_called_once_with("Hi!")
@pytest.mark.parametrize("prompt", ["", "Password: ", "Enter you password:"])
def test_input_password(self, monkeypatch: pytest.MonkeyPatch, prompt: str) -> None:
mock_getpass = MagicMock()
mock_getpass.return_value = "123456"
monkeypatch.setattr("getpass.getpass", mock_getpass)
actual = self.console_io.input(prompt, password=True)
assert actual == "123456"
if prompt == "":
mock_getpass.assert_called_once_with("Password: ")
else:
mock_getpass.assert_called_once_with(prompt)