Skip to content

Commit 8d8a5b1

Browse files
authored
Merge pull request #56 from TaskarCenterAtUW/stage
2 parents 3e6bbb9 + cb8a640 commit 8d8a5b1

File tree

4 files changed

+71
-32
lines changed

4 files changed

+71
-32
lines changed

.github/workflows/unit_tests.yaml

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ on:
1010
jobs:
1111
UnitTest:
1212
runs-on: ubuntu-latest
13-
1413
env:
1514
DATABASE_NAME: test_database
1615

1716
steps:
1817
- name: Checkout code
19-
uses: actions/checkout@v2
18+
uses: actions/checkout@v4
2019

2120
- name: Set up Python
22-
uses: actions/setup-python@v2
21+
uses: actions/setup-python@v5
2322
with:
2423
python-version: '3.10'
2524

@@ -30,46 +29,60 @@ jobs:
3029
3130
- name: Determine output folder
3231
id: set_output_folder
32+
shell: bash
3333
run: |
34-
if [[ $GITHUB_EVENT_NAME == "pull_request" ]]; then
35-
branch_name=$GITHUB_BASE_REF
36-
else
37-
branch_name=$GITHUB_REF_NAME
38-
fi
39-
40-
if [[ $branch_name == "main" ]]; then
41-
echo "output_folder=prod" >> $GITHUB_ENV
42-
elif [[ $branch_name == "stage" ]]; then
43-
echo "output_folder=stage" >> $GITHUB_ENV
44-
elif [[ $branch_name == "dev" ]]; then
45-
echo "output_folder=dev" >> $GITHUB_ENV
34+
if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
35+
branch_name="$GITHUB_BASE_REF"
4636
else
47-
echo "Unknown branch: $branch_name"
48-
exit 1
37+
branch_name="$GITHUB_REF_NAME"
4938
fi
5039
51-
- name: Run tests with coverage
40+
case "$branch_name" in
41+
main) echo "output_folder=prod" >> "$GITHUB_ENV" ;;
42+
stage) echo "output_folder=stage" >> "$GITHUB_ENV" ;;
43+
dev) echo "output_folder=dev" >> "$GITHUB_ENV" ;;
44+
*) echo "Unknown branch: $branch_name"; exit 1 ;;
45+
esac
46+
47+
- name: Run tests with coverage (show failures in logs)
48+
shell: bash
5249
run: |
53-
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
50+
set -o pipefail
51+
timestamp="$(date '+%Y-%m-%d_%H-%M-%S')"
5452
mkdir -p test_results
5553
log_file="test_results/${timestamp}_report.log"
56-
echo -e "\nTest Cases Report Report\n" >> $log_file
57-
# Run the tests and append output to the log file
58-
python -m coverage run --source=src -m unittest discover -s tests/unit_tests >> $log_file 2>&1
59-
echo -e "\nCoverage Report\n" >> $log_file
60-
coverage report >> $log_file
54+
55+
{
56+
echo
57+
echo "Test Cases Report"
58+
echo
59+
} | tee -a "$log_file"
60+
61+
# Run unittest in verbose mode; mirror output to console and file
62+
python -m coverage run --source=src -m unittest discover -s tests/unit_tests -v 2>&1 | tee -a "$log_file"
63+
test_status=${PIPESTATUS[0]}
64+
65+
echo -e "\nCoverage Report\n" | tee -a "$log_file"
66+
coverage report 2>&1 | tee -a "$log_file"
67+
68+
exit $test_status
6169
6270
- name: Check coverage
63-
run: |
64-
coverage report --fail-under=85
71+
run: coverage report --fail-under=85
72+
73+
# Optional: keep the log as a build artifact for easy download
74+
- name: Upload test log artifact
75+
uses: actions/upload-artifact@v4
76+
with:
77+
name: unit-test-log
78+
path: test_results/
6579

6680
- name: Upload report to Azure
6781
uses: LanceMcCarthy/Action-AzureBlobUpload@v2
6882
with:
6983
source_folder: 'test_results'
7084
destination_folder: '${{ env.output_folder }}'
7185
connection_string: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
72-
container_name: 'osw-validation-service '
86+
container_name: 'osw-validation-service'
7387
clean_destination_folder: false
7488
delete_if_exists: false
75-

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ python-ms-core==0.0.23
44
uvicorn==0.20.0
55
html_testRunner==1.2.1
66
geopandas==0.14.4
7-
python-osw-validation==0.2.11
7+
python-osw-validation==0.2.13

src/validation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from python_osw_validation import OSWValidation
1010
from .models.queue_message_content import ValidationResult
1111
import uuid
12+
import json
1213

1314
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
1415
# Path used for download file generation.
@@ -55,8 +56,8 @@ def is_osw_valid(self, max_errors) -> ValidationResult:
5556
validation_result = validator.validate(max_errors)
5657
result.is_valid = validation_result.is_valid
5758
if not result.is_valid:
58-
result.validation_message = validation_result.errors
59-
logger.error(f' Error While Validating File: {str(validation_result.errors)}')
59+
result.validation_message = json.dumps(validation_result.issues)
60+
logger.error(f' Error While Validating File: {json.dumps(validation_result.issues)}')
6061
Validation.clean_up(downloaded_file_path)
6162
else:
6263
result.validation_message = 'Failed to validate because unknown file format'

tests/unit_tests/test_validation.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import json
23
import unittest
34
from pathlib import Path
45
from src.validation import Validation
@@ -75,12 +76,36 @@ def test_validate_invalid_file(self, mock_download_file, mock_clean_up):
7576

7677
# Assert that validation is marked as valid
7778
self.assertFalse(result.is_valid)
78-
self.assertIn('Validation error', ' '.join(result.validation_message))
79+
errors = json.loads(result.validation_message)
80+
self.assertNotEqual(len(errors), 0)
7981

8082

8183
# Ensure clean_up is called twice (once for the file, once for the folder)
8284
self.assertEqual(mock_clean_up.call_count, 2)
8385

86+
@patch('src.validation.Validation.clean_up')
87+
@patch('src.validation.Validation.download_single_file')
88+
def test_validate_invalid_file_with_errors(self, mock_download_file, mock_clean_up):
89+
"""Test the validate method for a invalid file."""
90+
mock_download_file.return_value = f'{SAVED_FILE_PATH}/{FAILURE_FILE_NAME}'
91+
error_in_file = 'wa.microsoft.graph.edges.OSW.geojson'
92+
feature_indexes = [3, 6, 8, 25]
93+
error_message = "Additional properties are not allowed ('crossing' was unexpected)"
94+
# Act
95+
result = self.validation.validate(max_errors=10)
96+
97+
# Assert that validation is marked as valid
98+
self.assertFalse(result.is_valid)
99+
errors = json.loads(result.validation_message)
100+
count = 0
101+
for error in errors:
102+
self.assertEqual(error['filename'], error_in_file)
103+
self.assertEqual(error['error_message'][0], error_message)
104+
self.assertEqual(error['feature_index'], feature_indexes[count])
105+
count += 1
106+
# Ensure clean_up is called twice (once for the file, once for the folder)
107+
self.assertEqual(mock_clean_up.call_count, 2)
108+
84109
@patch('src.validation.OSWValidation')
85110
@patch('src.validation.Validation.clean_up')
86111
def test_validate_invalid_zip(self, mock_clean_up, mock_osw_validation):

0 commit comments

Comments
 (0)