Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test_sessions.py #144

Merged
merged 8 commits into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,30 @@ class TestSessions:
def setup_method(self):
self.api_key = "random_api_key"
self.event_type = 'test_event_type'
agentops.init(api_key=self.api_key, max_wait_time=5, auto_start_session=False)
self.config = agentops.Configuration(api_key=self.api_key, max_wait_time=50)

def test_session(self, mock_req):
# Arrange
agentops.start_session()
agentops.start_session(config=self.config)

# Act
agentops.record(ActionEvent(self.event_type))
agentops.record(ActionEvent(self.event_type))

# Assert the session has been initiated and the id has been created on backend.
# We should have 1 requests (session start).
assert len(mock_req.request_history) == 1
time.sleep(0.15)

# Act
agentops.record(ActionEvent(self.event_type))
time.sleep(0.1)

# Assert an event has been added
# We should have 2 requests (session and 2 events combined into 1)
print(mock_req.last_request.json())
assert len(mock_req.request_history) == 2
assert mock_req.last_request.headers['X-Agentops-Auth'] == self.api_key
request_json = mock_req.last_request.json()
assert request_json['events'][0]['event_type'] == self.event_type

# Act
end_state = 'Success'
agentops.end_session(end_state)
time.sleep(0.1)
time.sleep(0.15)

# Since a session has ended, no more events should be recorded, but end_session should be called
# We should have 3 requests (additional end session)
assert len(mock_req.request_history) == 3
assert mock_req.last_request.headers['X-Agentops-Auth'] == self.api_key
request_json = mock_req.last_request.json()
Expand All @@ -55,13 +51,14 @@ def test_session(self, mock_req):
def test_tags(self, mock_req):
# Arrange
tags = ['GPT-4']
agentops.start_session(tags=tags)
agentops.start_session(tags=tags, config=self.config)

# Act
agentops.record(ActionEvent(self.event_type))
time.sleep(0.1)
time.sleep(0.15)

# Assert 2 requests - 1 for session init, 1 for event
print(mock_req.last_request.json())
assert len(mock_req.request_history) == 2
assert mock_req.last_request.headers['X-Agentops-Auth'] == self.api_key
request_json = mock_req.last_request.json()
Expand All @@ -70,7 +67,7 @@ def test_tags(self, mock_req):
# Act
end_state = 'Success'
agentops.end_session(end_state)
time.sleep(0.1)
time.sleep(0.15)

# Assert 3 requets, 1 for session init, 1 for event, 1 for end session
assert len(mock_req.request_history) == 3
Expand Down
Loading