-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_insert_mocked.py
More file actions
239 lines (209 loc) · 8.24 KB
/
test_insert_mocked.py
File metadata and controls
239 lines (209 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'''
Copyright (c) 2022 Skyflow, Inc.
'''
import json
import unittest
from unittest.mock import Mock, patch, ANY
import os
from dotenv import dotenv_values
import requests
from requests.models import Response
from skyflow.errors._skyflow_errors import SkyflowError
from skyflow.vault._client import Client
from skyflow.vault._config import Configuration, InsertOptions, BYOT
class TestInsertWithMocks(unittest.TestCase):
def setUp(self) -> None:
self.envValues = dotenv_values(".env")
self.dataPath = os.path.join(os.getcwd(), 'tests/vault/data/')
self.valid_token = self.envValues["MOCK_TOKEN"]
self.record = {
"table": "pii_fields",
"fields": {
"cardNumber": "4111-1111-1111-1111",
"cvv": "234"
}
}
self.data = {"records": [self.record, self.record]}
# Mock API response data
self.mock_success_response = {
"responses": [
{
"records": [
{
"skyflow_id": "123",
"tokens": {
"cardNumber": "card_number_token",
"cvv": "cvv_token"
}
}
]
},
{
"records": [
{
"skyflow_id": "456",
"tokens": {
"cardNumber": "card_number_token",
"cvv": "cvv_token"
}
}
]
},
],
"requestId": "test-request-id"
}
self.mock_error_response = {
"error": {
"grpc_code": 3,
"http_code": 400,
"message": "Insert failed due to error.",
"http_status": "Bad Request"
}
}
# Create configurations for testing with different token scenarios
self.valid_config = Configuration(
'test-vault-id',
'https://test-vault.skyflow.com',
lambda: self.valid_token
)
self.invalid_config = Configuration(
'test-vault-id',
'https://test-vault.skyflow.com',
lambda: 'invalid-token'
)
@patch('requests.post')
def test_successful_insert(self, mock_post):
# Setup mock response
mock_response = Mock(spec=Response)
mock_response.status_code = 200
mock_response.content = json.dumps(self.mock_success_response).encode('utf-8')
mock_response.headers = {'x-request-id': 'test-request-id'}
mock_post.return_value = mock_response # Create client and perform insert
client = Client(self.valid_config)
options = InsertOptions(tokens=True)
result = client.insert(self.data, options)
# Verify the result
self.assertIn("records", result)
self.assertEqual(len(result["records"]), 2)
self.assertEqual(result["records"][0]["fields"]["cardNumber"], "card_number_token")
# Verify the API was called with correct parameters
mock_post.assert_called_once()
called_url = mock_post.call_args[0][0]
self.assertTrue(called_url.endswith("/v1/vaults/test-vault-id"))
@patch('requests.post')
def test_insert_api_error(self, mock_post):
# Setup mock error response
mock_response = Mock(spec=Response)
mock_response.status_code = 400
mock_response.content = json.dumps(self.mock_error_response).encode('utf-8')
mock_response.headers = {'x-request-id': 'test-request-id'}
# Mock raise_for_status to raise HTTPError
def raise_for_status():
raise requests.exceptions.HTTPError("400 Client Error")
mock_response.raise_for_status = raise_for_status
mock_post.return_value = mock_response
# Create client and attempt insert
client = Client(self.valid_config)
options = InsertOptions(tokens=True)
# This should raise a SkyflowError
with self.assertRaises(SkyflowError) as context:
client.insert(self.data, options)
# Verify the error details
self.assertEqual(context.exception.code, 400)
self.assertIn("Insert failed due to error", context.exception.message)
@patch('requests.post')
def test_insert_network_error(self, mock_post):
# Setup mock to simulate network error
mock_post.side_effect = Exception("Network error")
# Create client and attempt insert
client = Client(self.valid_config)
options = InsertOptions(tokens=True)
# Assert that the insert raises an error
with self.assertRaises(SkyflowError) as context:
client.insert(self.data, options)
@patch('requests.post')
def test_insert_with_continue_on_error_partial_sucess(self, mock_post):
# Setup mock response with partial success
partial_response = {
"responses": [
{
"Body": {
"records": [
{
"skyflow_id": "123",
"tokens": {"cardNumber": "token1"}
}
]
},
"Status": 200
},
{
"Body": {
"error": "Unique constraint violation"
},
"Status": 400
}
],
"requestId": "test-request-id"
}
mock_response = Mock(spec=Response)
mock_response.status_code = 207
mock_response.content = json.dumps(partial_response).encode('utf-8')
mock_response.headers = {'x-request-id': 'test-request-id'}
mock_post.return_value = mock_response
# Create client and perform insert with continueOnError
client = Client(self.valid_config)
options = InsertOptions(tokens=True, continueOnError=True)
# Create test data with two records
test_data = {
"records": [
self.record,
self.record # Duplicate record that will cause error
]
}
result = client.insert(test_data, options)
# Verify partial success results
self.assertIn("records", result)
self.assertIn("errors", result)
self.assertEqual(len(result["records"]), 1)
self.assertEqual(len(result["errors"]), 1)
@patch('requests.post')
def test_insert_with_continue_on_error_complete_failure(self, mock_post):
# Setup mock response with complete failure
complete_failure_response = {
"responses": [
{
"Body": {
"error": "Unique constraint violation"
},
"Status": 400
},
{
"Body": {
"error": "Unique constraint violation"
},
"Status": 400
}
],
"requestId": "test-request-id"
}
mock_response = Mock(spec=Response)
mock_response.status_code = 207
mock_response.content = json.dumps(complete_failure_response).encode('utf-8')
mock_response.headers = {'x-request-id': 'test-request-id'}
mock_post.return_value = mock_response
# Create client and perform insert with continueOnError
client = Client(self.valid_config)
options = InsertOptions(tokens=True, continueOnError=True)
# Create test data with two records
test_data = {
"records": [
self.record,
self.record # Duplicate record that will cause error
]
}
result = client.insert(test_data, options)
# Verify complete failure results
self.assertIn("errors", result)
self.assertNotIn("records", result)
self.assertEqual(len(result["errors"]), 2)