Skip to content

Commit 9268462

Browse files
authored
fix missing/broken test and add another test to hit missing line (#146)
1 parent ebdb754 commit 9268462

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

backend/tests/test_session.py

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@ def mock_valkey_ping_nop(mocker):
1515

1616
@pytest.fixture
1717
def mock_valkey(mock_valkey_ping_nop, mocker):
18-
# mock_valkey_dbcon_client = mocker.Mock()
19-
# mocker.patch("tenantfirstaid.session.Valkey", mock_valkey_dbcon_client)
20-
21-
_data: Dict[str, Any] = {}
18+
_data: Dict[str, str] = {}
2219

2320
mock_valkey_ping_nop.set = mocker.Mock(
2421
side_effect=lambda key, value: _data.update({key: value})
2522
)
2623

27-
mock_valkey_ping_nop.get = mocker.Mock(
28-
return_value=lambda key: _data.get(key, None)
29-
)
24+
mock_valkey_ping_nop.get = mocker.Mock(side_effect=lambda key: _data[key])
3025

3126
return mock_valkey_ping_nop
3227

@@ -81,10 +76,7 @@ def test_session_init_ping_exception(mocker, capsys):
8176

8277

8378
def test_session_get_unknown_session_id(mocker, mock_environ):
84-
test_data = {
85-
"city": "Test City",
86-
"state": "Test State",
87-
}
79+
test_data = {"city": "Test City", "state": "Test State", "messages": []}
8880

8981
mock_valkey_client = mocker.Mock()
9082
mocker.patch("tenantfirstaid.session.Valkey", return_value=mock_valkey_client)
@@ -110,30 +102,62 @@ def test_session_get_unknown_session_id(mocker, mock_environ):
110102
}
111103

112104

113-
# def test_session_set_and_get(mocker, mock_environ, mock_valkey):
114-
# test_data: Dict[str, Any] = {
115-
# "city": "Test City",
116-
# "state": "Test State",
117-
# "messages": ["this is message 1", "this is message 2"],
118-
# }
119-
120-
# mock_valkey_client = mocker.Mock()
121-
# mocker.patch("tenantfirstaid.session.Valkey", return_value=mock_valkey_client)
122-
# mock_valkey_client.ping = mocker.Mock()
123-
124-
# tenant_session = TenantSession()
125-
# app = Flask(__name__)
126-
# app.add_url_rule(
127-
# "/api/init",
128-
# view_func=InitSessionView.as_view("init", tenant_session),
129-
# methods=["POST"],
130-
# )
131-
# app.secret_key = "test_secret_key" # Set a secret key for session management
132-
133-
# with app.test_request_context("/api/init", method="POST", json=test_data):
134-
# response = app.full_dispatch_request()
135-
# assert response.status_code == 200 # Ensure the response is successful
136-
# session_id = response.json["session_id"]
137-
138-
# tenant_session.set(session_id, test_data)
139-
# assert tenant_session.get() == test_data
105+
def test_session_set_and_get(mocker, mock_environ, mock_valkey):
106+
test_data_obj: Dict[str, Any] = {
107+
"city": "Test City",
108+
"state": "Test State",
109+
"messages": ["this is message 1", "this is message 2"],
110+
}
111+
112+
tenant_session = TenantSession()
113+
app = Flask(__name__)
114+
app.add_url_rule(
115+
"/api/init",
116+
view_func=InitSessionView.as_view("init", tenant_session),
117+
methods=["POST"],
118+
)
119+
app.secret_key = "test_secret_key" # Set a secret key for session management
120+
121+
with app.test_request_context("/api/init", method="POST", json=test_data_obj):
122+
response = app.full_dispatch_request()
123+
assert response.status_code == 200 # Ensure the response is successful
124+
session_id = response.json["session_id"]
125+
assert session_id is not None # Ensure session_id is set
126+
assert isinstance(session_id, str) # Ensure session_id is a string
127+
128+
tenant_session.set(session_id, test_data_obj)
129+
assert tenant_session.get() == test_data_obj
130+
131+
132+
def test_session_set_some_and_get_none(mocker, mock_environ, mock_valkey):
133+
test_data_obj: Dict[str, Any] = {
134+
"city": "Test City",
135+
"state": "Test State",
136+
"messages": ["this is message 1", "this is message 2"],
137+
}
138+
139+
tenant_session = TenantSession()
140+
app = Flask(__name__)
141+
app.add_url_rule(
142+
"/api/init",
143+
view_func=InitSessionView.as_view("init", tenant_session),
144+
methods=["POST"],
145+
)
146+
app.secret_key = "test_secret_key" # Set a secret key for session management
147+
148+
# Simulate no data for the session (i.e. network error or similar)
149+
mock_valkey.get.side_effect = lambda key: None
150+
151+
with app.test_request_context("/api/init", method="POST", json=test_data_obj):
152+
response = app.full_dispatch_request()
153+
assert response.status_code == 200 # Ensure the response is successful
154+
session_id = response.json["session_id"]
155+
assert session_id is not None # Ensure session_id is set
156+
assert isinstance(session_id, str) # Ensure session_id is a string
157+
158+
tenant_session.set(session_id, test_data_obj)
159+
assert tenant_session.get() == {
160+
"city": "",
161+
"state": "",
162+
"messages": [],
163+
}

0 commit comments

Comments
 (0)