Skip to content

Commit 2ae0062

Browse files
authored
Merge pull request #36 from TaskarCenterAtUW/stage
Stage to Prod
2 parents 91c2c55 + 963b493 commit 2ae0062

File tree

7 files changed

+348
-480
lines changed

7 files changed

+348
-480
lines changed

.github/workflows/unit_tests.yaml

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,42 @@
1-
---
21
name: Unit Tests
3-
4-
#############################
5-
# Start the job on all push #
6-
#############################
72
on:
3+
workflow_dispatch:
84
push:
95
branches-ignore:
106
- '**'
11-
# Remove the line above to run when pushing to master
127
pull_request:
13-
branches: [master, dev, stage]
8+
branches: [main, dev, stage]
149

15-
###############
16-
# Set the Job #
17-
###############
1810
jobs:
1911
UnitTest:
20-
name: Unit Test Cases
21-
# Set the agent to run on
2212
runs-on: ubuntu-latest
13+
14+
env:
15+
DATABASE_NAME: test_database
16+
2317
steps:
24-
- name: Checkout code
25-
uses: actions/checkout@v3
26-
27-
- name: Set up Python
28-
uses: actions/setup-python@v4
29-
with:
30-
python-version: "3.10" # Use the appropriate Python version
31-
32-
- name: Install dependencies
33-
run: |
34-
pip install -r requirements.txt
35-
36-
- name: Run unit tests
37-
run: |
38-
python test_report.py
39-
coverage run --source=src -m unittest discover -s tests/
40-
coverage report -m
41-
exit_status=$?
42-
43-
# Set the exit status as an output for later use
44-
echo "::set-output name=exit_status::$exit_status"
45-
46-
- name: Archive Coverage Report
47-
if: ${{ always() }} # Upload the coverage report even if tests fail
48-
uses: actions/upload-artifact@v2
49-
with:
50-
name: htmlcov
51-
path: htmlcov
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
21+
- name: Set up Python
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: '3.10'
25+
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install -r requirements.txt
30+
31+
- name: Run tests with coverage
32+
run: |
33+
coverage run --source=src -m unittest discover -s tests/unit_tests
34+
coverage xml
35+
36+
- name: Check coverage
37+
run: |
38+
coverage report --fail-under=85
39+
40+
41+
42+

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pydantic==1.10.4
33
python-ms-core==0.0.22
44
uvicorn==0.20.0
55
html_testRunner==1.2.1
6-
python-osw-validation==0.2.4
6+
geopandas==0.14.4
7+
python-osw-validation==0.2.7

src/osw_validator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import uuid
1+
import gc
22
import logging
3-
import datetime
43
import urllib.parse
54
from typing import List
65
from python_ms_core import Core
@@ -58,7 +57,7 @@ def validate(self, received_message: Upload):
5857
if self.has_permission(roles=['tdei-admin', 'poc', 'osw_data_generator'],
5958
queue_message=received_message) is None:
6059
error_msg = 'Unauthorized request !'
61-
logger.error(tdei_record_id, error_msg, received_message)
60+
logger.error(f'{tdei_record_id}, {error_msg}, {received_message}')
6261
raise Exception(error_msg)
6362

6463
file_upload_path = urllib.parse.unquote(received_message.data.file_upload_path)
@@ -89,6 +88,8 @@ def send_status(self, result: ValidationResult, upload_message: Upload):
8988
logger.info(f'Publishing message for : {upload_message.message_id}')
9089
except Exception as e:
9190
logger.error(f'Error occurred while publishing message for : {upload_message.message_id} with error: {e}')
91+
finally:
92+
gc.collect()
9293

9394

9495
def has_permission(self, roles: List[str], queue_message: Upload) -> bool:

src/validation.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import gc
12
import os
3+
import time
24
import shutil
35
import logging
46
import traceback
@@ -10,7 +12,7 @@
1012

1113
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
1214
# Path used for download file generation.
13-
DOWNLOAD_FILE_PATH = f'{Path.cwd()}/downloads'
15+
DOWNLOAD_DIR = f'{Path.cwd()}/downloads'
1416

1517
logging.basicConfig()
1618
logger = logging.getLogger('OSW_VALIDATION')
@@ -25,46 +27,55 @@ def __init__(self, file_path=None, storage_client=None):
2527
self.file_path = file_path
2628
self.file_relative_path = file_path.split('/')[-1]
2729
self.client = self.storage_client.get_container(container_name=self.container_name)
30+
is_exists = os.path.exists(DOWNLOAD_DIR)
31+
unique_id = self.get_unique_id()
32+
if not is_exists:
33+
os.makedirs(DOWNLOAD_DIR)
34+
self.unique_dir_path = os.path.join(DOWNLOAD_DIR, unique_id)
35+
if not os.path.exists(self.unique_dir_path):
36+
os.makedirs(self.unique_dir_path)
2837

2938
def validate(self, max_errors=20) -> ValidationResult:
30-
return self.is_osw_valid(max_errors)
39+
try:
40+
return self.is_osw_valid(max_errors)
41+
finally:
42+
Validation.clean_up(self.unique_dir_path)
3143

3244
def is_osw_valid(self, max_errors) -> ValidationResult:
45+
start_time = time.time()
3346
result = ValidationResult()
3447
result.is_valid = False
3548
result.validation_message = ''
3649
root, ext = os.path.splitext(self.file_relative_path)
3750
if ext and ext.lower() == '.zip':
3851
downloaded_file_path = self.download_single_file(self.file_path)
39-
logger.info(f' Downloaded file path: {downloaded_file_path}')
40-
validator = OSWValidation(zipfile_path=downloaded_file_path)
41-
validation_result = validator.validate(max_errors)
42-
result.is_valid = validation_result.is_valid
43-
if not result.is_valid:
44-
result.validation_message = validation_result.errors
45-
logger.error(f' Error While Validating File: {str(validation_result.errors)}')
46-
Validation.clean_up(downloaded_file_path)
52+
if downloaded_file_path:
53+
logger.info(f' Downloaded file path: {downloaded_file_path}')
54+
validator = OSWValidation(zipfile_path=downloaded_file_path)
55+
validation_result = validator.validate(max_errors)
56+
result.is_valid = validation_result.is_valid
57+
if not result.is_valid:
58+
result.validation_message = validation_result.errors
59+
logger.error(f' Error While Validating File: {str(validation_result.errors)}')
60+
Validation.clean_up(downloaded_file_path)
61+
else:
62+
result.validation_message = 'Failed to validate because unknown file format'
4763
else:
4864
result.validation_message = 'Failed to validate because unknown file format'
4965
logger.error(f' Failed to validate because unknown file format')
50-
66+
end_time = time.time()
67+
time_taken = end_time - start_time
68+
logger.info(f'Validation completed in {time_taken} seconds')
69+
gc.collect()
5170
return result
5271

5372
# Downloads the single file into a unique directory
5473
def download_single_file(self, file_upload_path=None) -> str:
55-
is_exists = os.path.exists(DOWNLOAD_FILE_PATH)
56-
unique_id = self.get_unique_id()
57-
if not is_exists:
58-
os.makedirs(DOWNLOAD_FILE_PATH)
59-
unique_directory = os.path.join(DOWNLOAD_FILE_PATH,unique_id)
60-
if not os.path.exists(unique_directory):
61-
os.makedirs(unique_directory)
62-
6374
file = self.storage_client.get_file_from_url(self.container_name, file_upload_path)
6475
try:
6576
if file.file_path:
6677
file_path = os.path.basename(file.file_path)
67-
local_download_path = os.path.join(unique_directory,file_path)
78+
local_download_path = os.path.join(self.unique_dir_path, file_path)
6879
with open(local_download_path, 'wb') as blob:
6980
blob.write(file.get_stream())
7081
logger.info(f' File downloaded to location: {local_download_path}')
@@ -74,14 +85,14 @@ def download_single_file(self, file_upload_path=None) -> str:
7485
except Exception as e:
7586
traceback.print_exc()
7687
logger.error(e)
88+
finally:
89+
gc.collect()
7790

7891
# Generates a unique string for directory
7992
def get_unique_id(self) -> str:
8093
unique_id = uuid.uuid1().hex[0:24]
8194
return unique_id
8295

83-
84-
8596
@staticmethod
8697
def clean_up(path):
8798
if os.path.isfile(path):
@@ -91,3 +102,4 @@ def clean_up(path):
91102
# folder = os.path.join(DOWNLOAD_FILE_PATH, path)
92103
logger.info(f' Removing Folder: {path}')
93104
shutil.rmtree(path, ignore_errors=False)
105+
gc.collect()

tests/unit_tests/test_queue_message_content.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def setUp(self):
1919
data = TEST_DATA
2020
self.upload = Upload(data)
2121

22+
def test_message(self):
23+
self.upload.message = 'New message'
24+
self.assertEqual(self.upload.message, 'New message')
25+
2226
def test_message_type(self):
2327
self.assertEqual(self.upload.message_type, 'workflow_identifier')
2428
self.upload.message_type = 'New messageType'

0 commit comments

Comments
 (0)