Skip to content

Commit b235a1f

Browse files
committed
Updated tests
1 parent b63161c commit b235a1f

File tree

2 files changed

+73
-26
lines changed

2 files changed

+73
-26
lines changed

azure_functions_worker/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import argparse
66

77

8-
def create_args_parser():
8+
def parse_args():
99
parser = argparse.ArgumentParser(
1010
description='Python Azure Functions Worker')
1111
parser.add_argument('--host',
@@ -37,7 +37,7 @@ def create_args_parser():
3737
parser.add_argument('--functions-grpc-max-message-length', type=int,
3838
dest='functions_grpc_max_msg_len',
3939
help='Max grpc message length for Functions')
40-
return parser
40+
return parser.parse_args()
4141

4242

4343
def main():
@@ -49,8 +49,7 @@ def main():
4949
from ._thirdparty import aio_compat
5050
from .logging import error_logger, logger, format_exception
5151

52-
parser = create_args_parser()
53-
args = parser.parse_args()
52+
args = parse_args()
5453
logging.setup(log_level=args.log_level, log_destination=args.log_to)
5554

5655
logger.info('Starting Azure Functions Python Worker.')

tests/unittests/test_main.py

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,79 @@
11
import unittest
2-
3-
from azure_functions_worker.main import create_args_parser
2+
import sys
3+
from unittest.mock import patch
4+
from azure_functions_worker.main import parse_args
45

56

67
class TestMain(unittest.TestCase):
78

8-
def test_args_parser(self):
9-
parser = create_args_parser()
10-
parsed = parser.parse_args(
11-
['--host', '127.0.0.1',
12-
'--port', '50821',
13-
'--workerId', 'e9efd817-47a1-45dc-9e20-e6f975d7a025',
14-
'--requestId', 'cbef5957-cdb3-4462-9ee7-ac9f91be0a51',
15-
'--grpcMaxMessageLength', '2147483647',
16-
'--functions-uri', 'http://127.0.0.1:50821',
17-
'--functions-worker-id', 'e9efd817-47a1-45dc-9e20-e6f975d7a025',
18-
'--functions-request-id', 'cbef5957-cdb3-4462-9ee7-ac9f91be0a51',
19-
'--functions-grpc-max-message-length', '2147483647'])
20-
self.assertEqual(parsed.host, '127.0.0.1')
21-
self.assertEqual(parsed.worker_id,
9+
@patch.object(sys, 'argv',
10+
['xxx', '--host', '127.0.0.1',
11+
'--port', '50821',
12+
'--workerId', 'e9efd817-47a1-45dc-9e20-e6f975d7a025',
13+
'--requestId', 'cbef5957-cdb3-4462-9ee7-ac9f91be0a51',
14+
'--grpcMaxMessageLength', '2147483647',
15+
'--functions-uri', 'http://127.0.0.1:50821',
16+
'--functions-worker-id',
17+
'e9efd817-47a1-45dc-9e20-e6f975d7a025',
18+
'--functions-request-id',
19+
'cbef5957-cdb3-4462-9ee7-ac9f91be0a51',
20+
'--functions-grpc-max-message-length', '2147483647'])
21+
def test_all_args(self):
22+
args = parse_args()
23+
self.assertEqual(args.host, '127.0.0.1')
24+
self.assertEqual(args.port, 50821)
25+
self.assertEqual(args.worker_id,
26+
'e9efd817-47a1-45dc-9e20-e6f975d7a025')
27+
self.assertEqual(args.request_id,
28+
'cbef5957-cdb3-4462-9ee7-ac9f91be0a51')
29+
self.assertEqual(args.grpc_max_msg_len, 2147483647)
30+
self.assertEqual(args.functions_uri, 'http://127.0.0.1:50821')
31+
self.assertEqual(args.functions_worker_id,
32+
'e9efd817-47a1-45dc-9e20-e6f975d7a025')
33+
self.assertEqual(args.functions_request_id,
34+
'cbef5957-cdb3-4462-9ee7-ac9f91be0a51')
35+
self.assertEqual(args.functions_grpc_max_msg_len, 2147483647)
36+
37+
@patch.object(sys, 'argv',
38+
['xxx', '--host', '127.0.0.1',
39+
'--port', '50821',
40+
'--workerId', 'e9efd817-47a1-45dc-9e20-e6f975d7a025',
41+
'--requestId', 'cbef5957-cdb3-4462-9ee7-ac9f91be0a51',
42+
'--grpcMaxMessageLength', '2147483647'])
43+
def test_old_args(self):
44+
args = parse_args()
45+
self.assertEqual(args.host, '127.0.0.1')
46+
self.assertEqual(args.port, 50821)
47+
self.assertEqual(args.worker_id,
2248
'e9efd817-47a1-45dc-9e20-e6f975d7a025')
23-
self.assertEqual(parsed.request_id,
49+
self.assertEqual(args.request_id,
2450
'cbef5957-cdb3-4462-9ee7-ac9f91be0a51')
25-
self.assertEqual(parsed.grpc_max_msg_len, 2147483647)
26-
self.assertEqual(parsed.functions_uri, 'http://127.0.0.1:50821')
27-
self.assertEqual(parsed.functions_worker_id,
51+
self.assertEqual(args.grpc_max_msg_len, 2147483647)
52+
self.assertIsNone(args.functions_uri)
53+
self.assertIsNone(args.functions_worker_id)
54+
self.assertIsNone(args.functions_request_id)
55+
self.assertIsNone(args.functions_grpc_max_msg_len)
56+
57+
@patch.object(sys, 'argv',
58+
['xxx', '--functions-uri', 'http://127.0.0.1:50821',
59+
'--functions-worker-id',
60+
'e9efd817-47a1-45dc-9e20-e6f975d7a025',
61+
'--functions-request-id',
62+
'cbef5957-cdb3-4462-9ee7-ac9f91be0a51',
63+
'--functions-grpc-max-message-length', '2147483647'])
64+
def test_new_args(self):
65+
args = parse_args()
66+
self.assertEqual(args.functions_uri, 'http://127.0.0.1:50821')
67+
self.assertEqual(args.functions_worker_id,
2868
'e9efd817-47a1-45dc-9e20-e6f975d7a025')
29-
self.assertEqual(parsed.functions_request_id,
69+
self.assertEqual(args.functions_request_id,
3070
'cbef5957-cdb3-4462-9ee7-ac9f91be0a51')
31-
self.assertEqual(parsed.functions_grpc_max_msg_len, 2147483647)
71+
self.assertEqual(args.functions_grpc_max_msg_len, 2147483647)
72+
73+
@patch.object(sys, 'argv', ['xxx', '--host', 'dummy_host',
74+
'--port', '12345',
75+
'--invalid-arg', 'invalid_value'])
76+
def test_invalid_args(self):
77+
with self.assertRaises(SystemExit) as context:
78+
parse_args()
79+
self.assertEqual(context.exception.code, 2)

0 commit comments

Comments
 (0)