Skip to content

Commit e74db2a

Browse files
committed
Add more tests
1 parent fb23dfc commit e74db2a

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

django_project/cplus_api/tests/test_sync_default_layers.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
import os
2+
import tempfile
23
import time
3-
from rasterio.errors import RasterioIOError
4-
from unittest.mock import patch
4+
from datetime import timedelta
55
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
611

712
from core.settings.utils import absolute_path
813
from cplus_api.models.layer import (
914
InputLayer,
1015
COMMON_LAYERS_DIR
1116
)
12-
from cplus_api.tests.factories import InputLayerF
1317
from cplus_api.tasks.sync_default_layers import (
1418
sync_default_layers,
1519
ProcessFile
1620
)
1721
from cplus_api.tests.common import BaseAPIViewTransactionTest
22+
from cplus_api.tests.factories import InputLayerF
1823

1924

2025
class TestSyncDefaultLayer(BaseAPIViewTransactionTest):
2126
def setUp(self, *args, **kwargs):
2227
super().setUp(*args, **kwargs)
23-
# print(help(self))
24-
# breakpoint()
2528
self.superuser.username = os.getenv('ADMIN_USERNAME')
2629
self.superuser.save()
2730

@@ -176,3 +179,47 @@ def test_invalid_input_layers_not_deleted(self):
176179
# Check modified_on is updated
177180
input_layer.refresh_from_db()
178181
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

Comments
 (0)