|
1 | 1 | import os
|
| 2 | +import tempfile |
2 | 3 | import time
|
3 |
| -from rasterio.errors import RasterioIOError |
4 |
| -from unittest.mock import patch |
| 4 | +from datetime import timedelta |
5 | 5 | from shutil import copyfile
|
| 6 | +from unittest.mock import patch, MagicMock |
| 7 | + |
| 8 | +from django.utils import timezone |
| 9 | +from rasterio.errors import RasterioIOError |
| 10 | +from storages.backends.s3 import S3Storage |
6 | 11 |
|
7 | 12 | from core.settings.utils import absolute_path
|
8 | 13 | from cplus_api.models.layer import (
|
9 | 14 | InputLayer,
|
10 | 15 | COMMON_LAYERS_DIR
|
11 | 16 | )
|
12 |
| -from cplus_api.tests.factories import InputLayerF |
13 | 17 | from cplus_api.tasks.sync_default_layers import (
|
14 | 18 | sync_default_layers,
|
15 | 19 | ProcessFile
|
16 | 20 | )
|
17 | 21 | from cplus_api.tests.common import BaseAPIViewTransactionTest
|
| 22 | +from cplus_api.tests.factories import InputLayerF |
18 | 23 |
|
19 | 24 |
|
20 | 25 | class TestSyncDefaultLayer(BaseAPIViewTransactionTest):
|
21 | 26 | def setUp(self, *args, **kwargs):
|
22 | 27 | super().setUp(*args, **kwargs)
|
23 |
| - # print(help(self)) |
24 |
| - # breakpoint() |
25 | 28 | self.superuser.username = os.getenv('ADMIN_USERNAME')
|
26 | 29 | self.superuser.save()
|
27 | 30 |
|
@@ -176,3 +179,47 @@ def test_invalid_input_layers_not_deleted(self):
|
176 | 179 | # Check modified_on is updated
|
177 | 180 | input_layer.refresh_from_db()
|
178 | 181 | self.assertNotEquals(input_layer.modified_on, first_modified_on)
|
| 182 | + |
| 183 | + def run_s3(self, mock_storage, mock_named_tmp_file=None): |
| 184 | + source_path = absolute_path( |
| 185 | + 'cplus_api', 'tests', 'data', |
| 186 | + 'pathways', 'test_pathway_2.tif' |
| 187 | + ) |
| 188 | + dest_path = ( |
| 189 | + f'/home/web/media/minio_test/{COMMON_LAYERS_DIR}/' |
| 190 | + f'{InputLayer.ComponentTypes.NCS_PATHWAY}/test_pathway_2.tif' |
| 191 | + ) |
| 192 | + os.makedirs(os.path.dirname(dest_path), exist_ok=True) |
| 193 | + copyfile(source_path, dest_path) |
| 194 | + |
| 195 | + storage = S3Storage(bucket_name='test-bucket') |
| 196 | + s3_client = MagicMock() |
| 197 | + s3_client.list_objects.return_value = { |
| 198 | + 'Contents': [ |
| 199 | + { |
| 200 | + 'Key': 'common_layers/ncs_pathway/test_pathway_2.tif', |
| 201 | + 'LastModified': timezone.now() + timedelta(days=1) |
| 202 | + } |
| 203 | + ] |
| 204 | + } |
| 205 | + storage.connection.meta.client = s3_client |
| 206 | + mock_storage.return_value = storage |
| 207 | + if mock_named_tmp_file: |
| 208 | + (mock_named_tmp_file.return_value. |
| 209 | + __enter__.return_value).name = dest_path |
| 210 | + sync_default_layers() |
| 211 | + |
| 212 | + @patch('cplus_api.tasks.sync_default_layers.select_input_layer_storage') |
| 213 | + def test_invalid_input_layers_not_created_s3(self, mock_storage): |
| 214 | + self.run_s3(mock_storage) |
| 215 | + self.assertFalse(InputLayer.objects.exists()) |
| 216 | + |
| 217 | + @patch('cplus_api.tasks.sync_default_layers.select_input_layer_storage') |
| 218 | + @patch.object(tempfile, 'NamedTemporaryFile') |
| 219 | + def test_invalid_input_layers_created_s3( |
| 220 | + self, |
| 221 | + mock_named_tmp_file, |
| 222 | + mock_storage |
| 223 | + ): |
| 224 | + self.run_s3(mock_storage, mock_named_tmp_file) |
| 225 | + self.assertTrue(InputLayer.objects.exists()) |
0 commit comments