|
1 | 1 | from datetime import datetime |
2 | 2 |
|
3 | | -from ..local_changes import LocalChange, LocalChanges |
| 3 | +from ..local_changes import LocalChange, LocalChanges, MAX_UPLOAD_CHANGES |
4 | 4 |
|
5 | 5 |
|
6 | 6 | def test_local_changes_from_dict(): |
@@ -120,60 +120,91 @@ def test_local_changes_get_upload_changes(): |
120 | 120 | assert upload_changes[1].path == "file2.txt" # Second change is from updated |
121 | 121 |
|
122 | 122 |
|
123 | | -def test_local_changes_get_media_upload_size(): |
124 | | - """Test the get_media_upload_size method of LocalChanges.""" |
| 123 | +def test_local_changes_get_media_upload_over_size(): |
| 124 | + """Test the get_media_upload_file method of LocalChanges.""" |
| 125 | + # Define constants |
| 126 | + SIZE_LIMIT_MB = 10 |
| 127 | + SIZE_LIMIT_BYTES = SIZE_LIMIT_MB * 1024 * 1024 |
| 128 | + SMALL_FILE_SIZE = 1024 |
| 129 | + LARGE_FILE_SIZE = 15 * 1024 * 1024 |
| 130 | + |
125 | 131 | # Create sample LocalChange instances |
126 | 132 | added = [ |
127 | | - LocalChange(path="file1.txt", checksum="abc123", size=1024, mtime=datetime.now()), |
128 | | - LocalChange(path="file2.jpg", checksum="xyz789", size=2048, mtime=datetime.now()), |
| 133 | + LocalChange(path="file1.txt", checksum="abc123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
| 134 | + LocalChange(path="file2.jpg", checksum="xyz789", size=LARGE_FILE_SIZE, mtime=datetime.now()), # Over limit |
129 | 135 | ] |
130 | 136 | updated = [ |
131 | | - LocalChange(path="file3.mp4", checksum="lmn456", size=5120, mtime=datetime.now()), |
132 | | - LocalChange(path="file4.gpkg", checksum="opq123", size=1024, mtime=datetime.now()), |
| 137 | + LocalChange(path="file3.mp4", checksum="lmn456", size=5 * 1024 * 1024, mtime=datetime.now()), |
| 138 | + LocalChange(path="file4.gpkg", checksum="opq123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
133 | 139 | ] |
134 | 140 |
|
135 | 141 | # Initialize LocalChanges |
136 | 142 | local_changes = LocalChanges(added=added, updated=updated) |
137 | 143 |
|
138 | | - # Call get_media_upload_size |
139 | | - media_size = local_changes.get_media_upload_size() |
| 144 | + # Call get_media_upload_file with a size limit |
| 145 | + media_file = local_changes.get_media_upload_over_size(SIZE_LIMIT_BYTES) |
140 | 146 |
|
141 | 147 | # Assertions |
142 | | - assert media_size == 8192 # Only non-versioned files (txt, jpg, mp4) are included |
| 148 | + assert media_file is not None |
| 149 | + assert media_file.path == "file2.jpg" # The first file over the limit |
| 150 | + assert media_file.size == LARGE_FILE_SIZE |
| 151 | + |
143 | 152 |
|
| 153 | +def test_local_changes_get_gpgk_upload_over_size(): |
| 154 | + """Test the get_gpgk_upload_file method of LocalChanges.""" |
| 155 | + # Define constants |
| 156 | + SIZE_LIMIT_MB = 10 |
| 157 | + SIZE_LIMIT_BYTES = SIZE_LIMIT_MB * 1024 * 1024 |
| 158 | + SMALL_FILE_SIZE = 1024 |
| 159 | + LARGE_FILE_SIZE = 15 * 1024 * 1024 |
144 | 160 |
|
145 | | -def test_local_changes_get_gpgk_upload_size(): |
146 | | - """Test the get_gpgk_upload_size method of LocalChanges.""" |
147 | 161 | # Create sample LocalChange instances |
148 | 162 | added = [ |
149 | | - LocalChange(path="file1.gpkg", checksum="abc123", size=1024, mtime=datetime.now()), |
150 | | - LocalChange(path="file2.gpkg", checksum="xyz789", size=2048, mtime=datetime.now(), diff={"path": "diff1"}), |
| 163 | + LocalChange(path="file1.gpkg", checksum="abc123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
| 164 | + LocalChange( |
| 165 | + path="file2.gpkg", checksum="xyz789", size=LARGE_FILE_SIZE, mtime=datetime.now(), diff=None |
| 166 | + ), # Over limit |
151 | 167 | ] |
152 | 168 | updated = [ |
153 | | - LocalChange(path="file3.gpkg", checksum="lmn456", size=5120, mtime=datetime.now()), |
154 | | - LocalChange(path="file4.txt", checksum="opq123", size=1024, mtime=datetime.now()), |
| 169 | + LocalChange(path="file3.gpkg", checksum="lmn456", size=5 * 1024 * 1024, mtime=datetime.now()), |
| 170 | + LocalChange(path="file4.txt", checksum="opq123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
155 | 171 | ] |
156 | 172 |
|
157 | 173 | # Initialize LocalChanges |
158 | 174 | local_changes = LocalChanges(added=added, updated=updated) |
159 | 175 |
|
160 | | - # Call get_gpgk_upload_size |
161 | | - gpkg_size = local_changes.get_gpgk_upload_size() |
| 176 | + # Call get_gpgk_upload_file with a size limit |
| 177 | + gpkg_file = local_changes.get_gpgk_upload_over_size(SIZE_LIMIT_BYTES) |
162 | 178 |
|
163 | 179 | # Assertions |
164 | | - assert gpkg_size == 6144 # Only GPKG files without diffs are included |
| 180 | + assert gpkg_file is not None |
| 181 | + assert gpkg_file.path == "file2.gpkg" # The first GPKG file over the limit |
| 182 | + assert gpkg_file.size == LARGE_FILE_SIZE |
| 183 | + assert gpkg_file.diff is None # Ensure it doesn't include diffs |
165 | 184 |
|
166 | 185 |
|
167 | 186 | def test_local_changes_post_init(): |
168 | 187 | """Test the __post_init__ method of LocalChanges.""" |
| 188 | + # Define constants |
| 189 | + ADDED_COUNT = 80 |
| 190 | + UPDATED_COUNT = 21 |
| 191 | + SMALL_FILE_SIZE = 1024 |
| 192 | + LARGE_FILE_SIZE = 2048 |
| 193 | + |
169 | 194 | # Create more than MAX_UPLOAD_CHANGES changes |
170 | | - added = [LocalChange(path=f"file{i}.txt", checksum="abc123", size=1024, mtime=datetime.now()) for i in range(80)] |
171 | | - updated = [LocalChange(path=f"file{i}.txt", checksum="xyz789", size=2048, mtime=datetime.now()) for i in range(21)] |
| 195 | + added = [ |
| 196 | + LocalChange(path=f"file{i}.txt", checksum="abc123", size=SMALL_FILE_SIZE, mtime=datetime.now()) |
| 197 | + for i in range(ADDED_COUNT) |
| 198 | + ] |
| 199 | + updated = [ |
| 200 | + LocalChange(path=f"file{i}.txt", checksum="xyz789", size=LARGE_FILE_SIZE, mtime=datetime.now()) |
| 201 | + for i in range(UPDATED_COUNT) |
| 202 | + ] |
172 | 203 |
|
173 | 204 | # Initialize LocalChanges |
174 | 205 | local_changes = LocalChanges(added=added, updated=updated) |
175 | 206 |
|
176 | 207 | # Assertions |
177 | | - assert len(local_changes.added) == 80 # All 80 added changes are included |
178 | | - assert len(local_changes.updated) == 20 # Only 20 updated changes are included to respect the limit |
179 | | - assert len(local_changes.added) + len(local_changes.updated) == 100 # Total is limited to MAX_UPLOAD_CHANGES |
| 208 | + assert len(local_changes.added) == ADDED_COUNT # All added changes are included |
| 209 | + assert len(local_changes.updated) == MAX_UPLOAD_CHANGES - ADDED_COUNT # Only enough updated changes are included |
| 210 | + assert len(local_changes.added) + len(local_changes.updated) == MAX_UPLOAD_CHANGES # Total is limited |
0 commit comments