22import builtins
33import pytest
44from unittest .mock import patch , MagicMock
5- from agentops .logging .instrument_logging import setup_print_logger , upload_logfile , LOGFILE_NAME
5+ from agentops .logging .instrument_logging import setup_print_logger , upload_logfile
66import logging
7-
87@pytest .fixture
9- def cleanup_log_file ():
10- """Fixture to clean up the log file before and after tests"""
11- log_file = os .path .join (os .getcwd (), LOGFILE_NAME )
12- if os .path .exists (log_file ):
13- os .remove (log_file )
8+ def reset_print ():
9+ """Fixture to reset the print function after tests"""
10+ original_print = builtins .print
1411 yield
15- if os .path .exists (log_file ):
16- os .remove (log_file )
12+ builtins .print = original_print
1713
18- def test_setup_print_logger_creates_log_file ( cleanup_log_file ):
19- """Test that setup_print_logger creates a log file """
14+ def test_setup_print_logger_creates_buffer_logger_and_handler ( ):
15+ """Test that setup_print_logger creates a buffer logger with a StreamHandler. """
2016 setup_print_logger ()
21- log_file = os .path .join (os .getcwd (), LOGFILE_NAME )
22- assert os .path .exists (log_file )
17+ buffer_logger = logging .getLogger ('agentops_buffer_logger' )
18+ assert buffer_logger .level == logging .DEBUG
19+ assert len (buffer_logger .handlers ) == 1
20+ assert isinstance (buffer_logger .handlers [0 ], logging .StreamHandler )
2321
24- def test_print_logger_writes_to_file ( cleanup_log_file ):
25- """Test that the monkeypatched print function writes to the log file """
22+ def test_print_logger_writes_message_to_stringio_buffer ( reset_print ):
23+ """Test that the monkeypatched print function writes messages to the StringIO buffer. """
2624 setup_print_logger ()
2725 test_message = "Test log message"
2826 print (test_message )
29-
30- log_file = os .path .join (os .getcwd (), LOGFILE_NAME )
31- with open (log_file , 'r' ) as f :
32- log_content = f .read ()
33- assert test_message in log_content
27+ buffer_logger = logging .getLogger ('agentops_buffer_logger' )
28+ log_content = buffer_logger .handlers [0 ].stream .getvalue ()
29+ assert test_message in log_content
3430
35- def test_print_logger_preserves_original_print (cleanup_log_file ):
36- """Test that the original print function is preserved"""
31+ def test_print_logger_replaces_and_restores_builtin_print (reset_print ):
32+ """Test that setup_print_logger replaces builtins.print and the fixture restores it after the test."""
33+ import agentops .logging .instrument_logging as il
34+ builtins .print = il ._original_print
3735 original_print = builtins .print
3836 setup_print_logger ()
3937 assert builtins .print != original_print
40-
41- # Cleanup should restore original print
42- for handler in logging .getLogger ('agentops_file_logger' ).handlers [:]:
43- handler .close ()
44- logging .getLogger ('agentops_file_logger' ).removeHandler (handler )
45- builtins .print = original_print
38+ # The reset_print fixture will restore print after the test
4639
4740@patch ('agentops.get_client' )
48- def test_upload_logfile (mock_get_client , cleanup_log_file ):
49- """Test that upload_logfile reads and uploads log content"""
50- # Setup
41+ def test_upload_logfile_sends_buffer_content_and_clears_buffer (mock_get_client ):
42+ """Test that upload_logfile uploads the buffer content and clears the buffer after upload."""
5143 setup_print_logger ()
5244 test_message = "Test upload message"
5345 print (test_message )
54-
55- # Mock the client
5646 mock_client = MagicMock ()
5747 mock_get_client .return_value = mock_client
58-
59- # Test upload
6048 upload_logfile (trace_id = 123 )
61-
62- # Verify
6349 mock_client .api .v4 .upload_logfile .assert_called_once ()
64- assert not os .path .exists (os .path .join (os .getcwd (), LOGFILE_NAME ))
50+ buffer_logger = logging .getLogger ('agentops_buffer_logger' )
51+ assert buffer_logger .handlers [0 ].stream .getvalue () == ""
6552
66- def test_upload_logfile_nonexistent_file ():
67- """Test that upload_logfile handles nonexistent log file gracefully """
53+ def test_upload_logfile_does_nothing_when_buffer_is_empty ():
54+ """Test that upload_logfile does nothing and does not call the client when the buffer is empty. """
6855 with patch ('agentops.get_client' ) as mock_get_client :
6956 upload_logfile (trace_id = 123 )
7057 mock_get_client .assert_not_called ()
0 commit comments