Mocking a function in a process started with multiprocessing.Process

2024-08-27 Thread Norman Robins via Python-list
Is it possible to mock out a function in a process started with
multiprocessing.Process? AI tells me it is, but I have not gotten it to
work.

The use case is that I have a server I want to write tests for. I want the
tests to start up the server, but I need to mock out the JWT based auth
functions in the server.

If it is possible, perhaps I am doing something wrong.

The function I want to mock out is in foo.bar.baz.validate_jwt`.

The server imports it as from .baz import validate_jwt. I have tried to
mock it many ways, here is the latest attempt:

@pytest.fixture(scope="module")
def mock_get_public_key():
return PUBLIC_KEY

@patch('foo.bar.baz.validate_jwt')
def test_fn(mock_validate_jwt, mock_get_public_key):

I also tried `@patch('foo.bar.main.validate_jwt')`

But no matter what I tried the real functions get called.

Here is an example:

In directory foo/bar file baz.py

def get_public_key(token: str) -> str:
print("in get_public_key)

def validate_jwt(token: str) -> dict
public_key = get_public_key(token)
print("in validate_jwt)

In directory foo/bar file main.py:

from .baz import validate_jwt

def server(token):
valid = validate_jwt(token)

In my test code, which is in test/test_main.py (test and foo are at the
same level)

@pytest.fixture(scope="module")def mock_get_public_key():
return PUBLIC_KEY
def run_server(port):
from foo.bar.main import start
start(is_prod=False, port=port)
@pytest.fixture(scope="module")def mock_validate_jwt(token):
return {"user": "test_user"}
@pytest.fixture(scope="module")def start_server():
port = 8002
server_process = multiprocessing.Process(target=run_server, args=(port,))
server_process.start()

max_retries = 10
for i in range(max_retries):
try:
response =
requests.get(f"http://127.0.0.1:{port}/echo?message=test";)
if response.status_code == 200:
break
except requests.ConnectionError:
time.sleep(1)
else:
pytest.fail("Server did not start in time")
@pytest.mark.usefixtures("start_server")@patch('dispatcher.main.validate_jwt')def
test_function(mock_validate_jwt, mock_get_public_key):
,
.
.





When test_function runs the real validate_jwt runs, not my mock.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Triggered By Mediocre Code (Posting On Python-List Prohibited)

2024-08-27 Thread Piergiorgio Sartor via Python-list

On 25/08/2024 23.53, Lawrence D'Oliveiro wrote:

Looking at this article about the top three languages for getting
programming jobs
,
naturally I couldn’t help noticing the code in the screenshot at the
top (my transcription):

 bufferedNumber = str(doc.GetTime().GetFrame(docFps))
 if len(bufferedNumber)<4:
 for x in range(len(bufferedNumber),4):
 bufferedNumber = "0" + bufferedNumber

I mean, really? Four lines to do what could be done in a single
expression?

Was that written by a PHP programmer, do you think?


That the more correct question would be:
What is easier to read? And to debug?
The four line version or the one liner?

To paraphrase someone:
"If the length of a program would be
measured by the time needed to understand
it, some program would be too short to
be short."

Because the world is plenty of one liner
nobody (almost) doesn't understand.

There is even a category in the OCC
(https://www.ioccc.org/).

Don't get me wrong, I like and dislike
one liner.
Namely, I like mine and dislike the others :-)

bye,

--

piergiorgio

--
https://mail.python.org/mailman/listinfo/python-list


Re: Formatting a str as a number

2024-08-27 Thread Grant Edwards via Python-list
On 2024-08-27, Gilmeh Serda via Python-list  wrote:
> On 25 Aug 2024 15:46:25 GMT, Stefan Ram wrote:
>
>> f"{int(number):>20,}"
>
> Great. Thanks. Do you have a link to where that's documented?
>
> I did web search, found nothing.

https://docs.python.org/3/library/string.html#formatspec
https://docs.python.org/3/reference/lexical_analysis.html#f-strings

-- 
https://mail.python.org/mailman/listinfo/python-list