@@ -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 )
@@ -110,30 +102,62 @@ def test_session_get_unknown_session_id(mocker, mock_environ):
110
102
}
111
103
112
104
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