-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_upload_manifests.py
More file actions
179 lines (153 loc) · 7.01 KB
/
test_upload_manifests.py
File metadata and controls
179 lines (153 loc) · 7.01 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
import os
import tempfile
import unittest
from unittest.mock import Mock, patch, mock_open
from socketdev.uploadmanifests import UploadManifests
class TestUploadManifests(unittest.TestCase):
def setUp(self):
self.mock_api = Mock()
self.upload_manifests = UploadManifests(self.mock_api)
def test_calculate_key_name_with_base_path(self):
"""Test that key names are calculated correctly with base_path."""
# Test with base_path
key = self.upload_manifests._calculate_key_name(
"/project/frontend/package.json",
base_path="/project"
)
self.assertEqual(key, "frontend/package.json")
def test_calculate_key_name_with_workspace(self):
"""Test that key names are calculated correctly with workspace."""
# Test with workspace
key = self.upload_manifests._calculate_key_name(
"/project/frontend/package.json",
workspace="/project/"
)
self.assertEqual(key, "frontend/package.json")
def test_calculate_key_name_with_base_paths(self):
"""Test that key names are calculated correctly with base_paths."""
# Test with base_paths (takes precedence over base_path)
key = self.upload_manifests._calculate_key_name(
"/project/frontend/package.json",
base_path="/project",
base_paths=["/different", "/project"]
)
self.assertEqual(key, "frontend/package.json")
def test_calculate_key_name_no_stripping(self):
"""Test that key names default to basename when no stripping options provided."""
# Test without any path stripping - should preserve relative path structure
key = self.upload_manifests._calculate_key_name(
"frontend/package.json"
)
self.assertEqual(key, "frontend/package.json")
def test_calculate_key_name_absolute_path_no_stripping(self):
"""Test that absolute paths get cleaned up when no stripping options provided."""
key = self.upload_manifests._calculate_key_name(
"/absolute/path/frontend/package.json"
)
self.assertEqual(key, "absolute/path/frontend/package.json")
def test_calculate_key_name_windows_paths(self):
"""Test that Windows paths are handled correctly."""
key = self.upload_manifests._calculate_key_name(
"C:\\project\\frontend\\package.json",
base_path="C:\\project"
)
self.assertEqual(key, "frontend/package.json")
@patch('socketdev.uploadmanifests.Utils.load_files_for_sending_lazy')
@patch('os.path.exists')
@patch('os.path.isfile')
def test_upload_manifest_files_lazy_loading(self, mock_isfile, mock_exists, mock_lazy_load):
"""Test that lazy loading preserves key names correctly."""
# Setup mocks
mock_exists.return_value = True
mock_isfile.return_value = True
mock_lazy_load.return_value = [
('frontend/package.json', ('frontend/package.json', Mock()))
]
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {'tarHash': 'test_hash'}
self.mock_api.do_request.return_value = mock_response
# Test lazy loading
result = self.upload_manifests.upload_manifest_files(
"test_org",
["/project/frontend/package.json"],
workspace="/project",
use_lazy_loading=True
)
self.assertEqual(result, 'test_hash')
mock_lazy_load.assert_called_once_with(
["/project/frontend/package.json"],
workspace="/project",
base_path=None,
base_paths=None
)
@patch('builtins.open', new_callable=mock_open, read_data=b'{"name": "test"}')
@patch('os.path.exists')
@patch('os.path.isfile')
def test_upload_manifest_files_non_lazy_loading(self, mock_isfile, mock_exists, mock_file):
"""Test that non-lazy loading produces same key names as lazy loading."""
# Setup mocks
mock_exists.return_value = True
mock_isfile.return_value = True
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {'tarHash': 'test_hash'}
self.mock_api.do_request.return_value = mock_response
# Test non-lazy loading with workspace
result = self.upload_manifests.upload_manifest_files(
"test_org",
["frontend/package.json"],
workspace="/project",
use_lazy_loading=False
)
self.assertEqual(result, 'test_hash')
# Verify the API was called with the correct file structure
call_args = self.mock_api.do_request.call_args
files_arg = call_args[1]['files']
self.assertEqual(len(files_arg), 1)
# The key should be 'frontend/package.json' not just 'package.json'
key, (filename, content) = files_arg[0]
self.assertEqual(key, "frontend/package.json")
self.assertEqual(filename, "frontend/package.json")
@patch('builtins.open', new_callable=mock_open, read_data=b'{"name": "test"}')
@patch('os.path.exists')
@patch('os.path.isfile')
def test_upload_manifest_files_consistency_between_modes(self, mock_isfile, mock_exists, mock_file):
"""Test that lazy and non-lazy loading produce identical key names."""
# Setup mocks
mock_exists.return_value = True
mock_isfile.return_value = True
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {'tarHash': 'test_hash'}
self.mock_api.do_request.return_value = mock_response
test_files = ["frontend/package.json", "backend/package.json"]
# Test non-lazy loading
with patch('socketdev.uploadmanifests.Utils.load_files_for_sending_lazy') as mock_lazy_load:
mock_lazy_load.return_value = [
('frontend/package.json', ('frontend/package.json', Mock())),
('backend/package.json', ('backend/package.json', Mock()))
]
# Get lazy loading result
self.upload_manifests.upload_manifest_files(
"test_org",
test_files,
use_lazy_loading=True
)
lazy_call_args = self.mock_api.do_request.call_args[1]['files']
# Reset mock
self.mock_api.reset_mock()
# Get non-lazy loading result
self.upload_manifests.upload_manifest_files(
"test_org",
test_files,
use_lazy_loading=False
)
non_lazy_call_args = self.mock_api.do_request.call_args[1]['files']
# Compare key names - they should be identical
lazy_keys = [item[0] for item in lazy_call_args]
non_lazy_keys = [item[0] for item in non_lazy_call_args]
self.assertEqual(lazy_keys, non_lazy_keys)
self.assertEqual(non_lazy_keys, ['frontend/package.json', 'backend/package.json'])
if __name__ == '__main__':
unittest.main()