@@ -15,18 +15,13 @@ def mock_valkey_ping_nop(mocker):
15
15
16
16
@pytest .fixture
17
17
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 ] = {}
22
19
23
20
mock_valkey_ping_nop .set = mocker .Mock (
24
21
side_effect = lambda key , value : _data .update ({key : value })
25
22
)
26
23
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 ])
30
25
31
26
return mock_valkey_ping_nop
32
27
@@ -81,10 +76,7 @@ def test_session_init_ping_exception(mocker, capsys):
81
76
82
77
83
78
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" : []}
88
80
89
81
mock_valkey_client = mocker .Mock ()
90
82
mocker .patch ("tenantfirstaid.session.Valkey" , return_value = mock_valkey_client )
@@ -111,30 +103,62 @@ def test_session_get_unknown_session_id(mocker, mock_environ):
111
103
}
112
104
113
105
114
- # def test_session_set_and_get(mocker, mock_environ, mock_valkey):
115
- # test_data: Dict[str, Any] = {
116
- # "city": "Test City",
117
- # "state": "Test State",
118
- # "messages": ["this is message 1", "this is message 2"],
119
- # }
120
-
121
- # mock_valkey_client = mocker.Mock()
122
- # mocker.patch("tenantfirstaid.session.Valkey", return_value=mock_valkey_client)
123
- # mock_valkey_client.ping = mocker.Mock()
124
-
125
- # tenant_session = TenantSession()
126
- # app = Flask(__name__)
127
- # app.add_url_rule(
128
- # "/api/init",
129
- # view_func=InitSessionView.as_view("init", tenant_session),
130
- # methods=["POST"],
131
- # )
132
- # app.secret_key = "test_secret_key" # Set a secret key for session management
133
-
134
- # with app.test_request_context("/api/init", method="POST", json=test_data):
135
- # response = app.full_dispatch_request()
136
- # assert response.status_code == 200 # Ensure the response is successful
137
- # session_id = response.json["session_id"]
138
-
139
- # tenant_session.set(session_id, test_data)
140
- # assert tenant_session.get() == test_data
106
+ def test_session_set_and_get (mocker , mock_environ , mock_valkey ):
107
+ test_data_obj : Dict [str , Any ] = {
108
+ "city" : "Test City" ,
109
+ "state" : "Test State" ,
110
+ "messages" : ["this is message 1" , "this is message 2" ],
111
+ }
112
+
113
+ tenant_session = TenantSession ()
114
+ app = Flask (__name__ )
115
+ app .add_url_rule (
116
+ "/api/init" ,
117
+ view_func = InitSessionView .as_view ("init" , tenant_session ),
118
+ methods = ["POST" ],
119
+ )
120
+ app .secret_key = "test_secret_key" # Set a secret key for session management
121
+
122
+ with app .test_request_context ("/api/init" , method = "POST" , json = test_data_obj ):
123
+ response = app .full_dispatch_request ()
124
+ assert response .status_code == 200 # Ensure the response is successful
125
+ session_id = response .json ["session_id" ]
126
+ assert session_id is not None # Ensure session_id is set
127
+ assert isinstance (session_id , str ) # Ensure session_id is a string
128
+
129
+ tenant_session .set (session_id , test_data_obj )
130
+ assert tenant_session .get () == test_data_obj
131
+
132
+
133
+ def test_session_set_some_and_get_none (mocker , mock_environ , mock_valkey ):
134
+ test_data_obj : Dict [str , Any ] = {
135
+ "city" : "Test City" ,
136
+ "state" : "Test State" ,
137
+ "messages" : ["this is message 1" , "this is message 2" ],
138
+ }
139
+
140
+ tenant_session = TenantSession ()
141
+ app = Flask (__name__ )
142
+ app .add_url_rule (
143
+ "/api/init" ,
144
+ view_func = InitSessionView .as_view ("init" , tenant_session ),
145
+ methods = ["POST" ],
146
+ )
147
+ app .secret_key = "test_secret_key" # Set a secret key for session management
148
+
149
+ # Simulate no data for the session (i.e. network error or similar)
150
+ mock_valkey .get .side_effect = lambda key : None
151
+
152
+ with app .test_request_context ("/api/init" , method = "POST" , json = test_data_obj ):
153
+ response = app .full_dispatch_request ()
154
+ assert response .status_code == 200 # Ensure the response is successful
155
+ session_id = response .json ["session_id" ]
156
+ assert session_id is not None # Ensure session_id is set
157
+ assert isinstance (session_id , str ) # Ensure session_id is a string
158
+
159
+ tenant_session .set (session_id , test_data_obj )
160
+ assert tenant_session .get () == {
161
+ "city" : "" ,
162
+ "state" : "" ,
163
+ "messages" : [],
164
+ }
0 commit comments