|
1 | 1 | from datetime import datetime |
| 2 | +import pytest |
| 3 | +from unittest.mock import patch |
2 | 4 |
|
3 | | -from ..local_changes import FileChange, LocalProjectChanges |
| 5 | +from ..local_changes import ChangesValidationError, FileChange, LocalProjectChanges, MAX_UPLOAD_CHANGES |
4 | 6 |
|
5 | 7 |
|
6 | 8 | def test_local_changes_from_dict(): |
@@ -118,3 +120,93 @@ def test_local_changes_get_upload_changes(): |
118 | 120 | assert len(upload_changes) == 2 # Only added and updated should be included |
119 | 121 | assert upload_changes[0].path == "file1.txt" # First change is from added |
120 | 122 | assert upload_changes[1].path == "file2.txt" # Second change is from updated |
| 123 | + |
| 124 | + |
| 125 | +def test_local_changes_post_init_validation_media(): |
| 126 | + """Test the get_media_upload_file method of LocalProjectChanges.""" |
| 127 | + # Define constants |
| 128 | + SIZE_LIMIT_MB = 5 |
| 129 | + SIZE_LIMIT_BYTES = SIZE_LIMIT_MB * 1024 * 1024 |
| 130 | + SMALL_FILE_SIZE = 1024 |
| 131 | + LARGE_FILE_SIZE = 15 * 1024 * 1024 |
| 132 | + |
| 133 | + # Create sample LocalChange instances |
| 134 | + added = [ |
| 135 | + FileChange(path="file1.txt", checksum="abc123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
| 136 | + FileChange(path="file2.jpg", checksum="xyz789", size=LARGE_FILE_SIZE, mtime=datetime.now()), # Over limit |
| 137 | + ] |
| 138 | + updated = [ |
| 139 | + FileChange(path="file3.mp4", checksum="lmn456", size=5 * 1024 * 1024, mtime=datetime.now()), |
| 140 | + FileChange(path="file4.gpkg", checksum="opq123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
| 141 | + ] |
| 142 | + |
| 143 | + # Initialize LocalProjectChanges |
| 144 | + with patch("mergin.local_changes.MAX_UPLOAD_MEDIA_SIZE", SIZE_LIMIT_BYTES): |
| 145 | + with pytest.raises(ChangesValidationError, match="Some files exceed") as err: |
| 146 | + LocalProjectChanges(added=added, updated=updated) |
| 147 | + print(err.value.invalid_changes) |
| 148 | + assert len(err.value.invalid_changes) == 1 |
| 149 | + assert "file2.jpg" == err.value.invalid_changes[0].path |
| 150 | + assert err.value.invalid_changes[0].size == LARGE_FILE_SIZE |
| 151 | + |
| 152 | + |
| 153 | +def test_local_changes_post_init_validation_gpgkg(): |
| 154 | + """Test the get_gpgk_upload_file method of LocalProjectChanges.""" |
| 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 |
| 160 | + |
| 161 | + # Create sample LocalChange instances |
| 162 | + added = [ |
| 163 | + FileChange(path="file1.gpkg", checksum="abc123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
| 164 | + FileChange( |
| 165 | + path="file2.gpkg", checksum="xyz789", size=LARGE_FILE_SIZE, mtime=datetime.now(), diff=None |
| 166 | + ), # Over limit |
| 167 | + ] |
| 168 | + updated = [ |
| 169 | + FileChange( |
| 170 | + path="file3.gpkg", |
| 171 | + checksum="lmn456", |
| 172 | + size=SIZE_LIMIT_BYTES + 1, |
| 173 | + mtime=datetime.now(), |
| 174 | + diff={"path": "file3-diff.gpkg", "checksum": "diff123", "size": 1024, "mtime": datetime.now()}, |
| 175 | + ), |
| 176 | + FileChange(path="file4.txt", checksum="opq123", size=SMALL_FILE_SIZE, mtime=datetime.now()), |
| 177 | + ] |
| 178 | + |
| 179 | + # Initialize LocalProjectChanges |
| 180 | + with patch("mergin.local_changes.MAX_UPLOAD_VERSIONED_SIZE", SIZE_LIMIT_BYTES): |
| 181 | + with pytest.raises(ChangesValidationError) as err: |
| 182 | + LocalProjectChanges(added=added, updated=updated) |
| 183 | + assert len(err.value.invalid_changes) == 1 |
| 184 | + assert "file2.gpkg" == err.value.invalid_changes[0].path |
| 185 | + assert err.value.invalid_changes[0].size == LARGE_FILE_SIZE |
| 186 | + |
| 187 | + |
| 188 | +def test_local_changes_post_init(): |
| 189 | + """Test the __post_init__ method of LocalProjectChanges.""" |
| 190 | + # Define constants |
| 191 | + ADDED_COUNT = 80 |
| 192 | + UPDATED_COUNT = 21 |
| 193 | + SMALL_FILE_SIZE = 1024 |
| 194 | + LARGE_FILE_SIZE = 2048 |
| 195 | + |
| 196 | + # Create more than MAX_UPLOAD_CHANGES changes |
| 197 | + added = [ |
| 198 | + FileChange(path=f"file{i}.txt", checksum="abc123", size=SMALL_FILE_SIZE, mtime=datetime.now()) |
| 199 | + for i in range(ADDED_COUNT) |
| 200 | + ] |
| 201 | + updated = [ |
| 202 | + FileChange(path=f"file{i}.txt", checksum="xyz789", size=LARGE_FILE_SIZE, mtime=datetime.now()) |
| 203 | + for i in range(UPDATED_COUNT) |
| 204 | + ] |
| 205 | + |
| 206 | + # Initialize LocalProjectChanges |
| 207 | + local_changes = LocalProjectChanges(added=added, updated=updated) |
| 208 | + |
| 209 | + # Assertions |
| 210 | + assert len(local_changes.added) == ADDED_COUNT # All added changes are included |
| 211 | + assert len(local_changes.updated) == MAX_UPLOAD_CHANGES - ADDED_COUNT # Only enough updated changes are included |
| 212 | + assert len(local_changes.added) + len(local_changes.updated) == MAX_UPLOAD_CHANGES # Total is limited |
0 commit comments