Coverage for tests/unit/test_server.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-06 15:09 -0600

1# tests/unit/test_server.py 

2from unittest import mock 

3 

4from pattern_lens.server import main as server_main 

5 

6 

7def test_server_main(): 

8 """Test the server main function.""" 

9 # Mock the TCPServer and other components 

10 with ( 

11 mock.patch("pattern_lens.server.socketserver.TCPServer") as mock_server, 

12 mock.patch( 

13 "pattern_lens.server.http.server.SimpleHTTPRequestHandler", 

14 ) as mock_handler, 

15 mock.patch("pattern_lens.server.os.chdir") as mock_chdir, 

16 mock.patch("pattern_lens.server.sys.exit") as mock_exit, 

17 ): 

18 # Setup the server to raise KeyboardInterrupt after being called 

19 mock_server_instance = mock_server.return_value.__enter__.return_value 

20 mock_server_instance.serve_forever.side_effect = KeyboardInterrupt() 

21 

22 # Call the server main function 

23 server_main("test_path", 8080) 

24 

25 # Check that the function changed to the right directory 

26 mock_chdir.assert_called_once_with("test_path") 

27 

28 # Check that the server was initialized with the right parameters 

29 mock_server.assert_called_once_with(("", 8080), mock_handler) 

30 

31 # Check that serve_forever was called 

32 mock_server_instance.serve_forever.assert_called_once() 

33 

34 # Check that sys.exit was called with 0 (clean exit) 

35 mock_exit.assert_called_once_with(0)