Skip to content

Commit 2eaa0d7

Browse files
committed
clean tests
yaml env pythonpath
1 parent cf05acb commit 2eaa0d7

File tree

9 files changed

+136
-137
lines changed

9 files changed

+136
-137
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,110 @@
11
name: SonarCloud
22

33
on:
4-
push:
5-
branches:
6-
- master
7-
pull_request:
8-
types: [labeled, opened, synchronize, reopened, unlabeled]
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
types: [labeled, opened, synchronize, reopened, unlabeled]
99

1010
jobs:
11-
sonarcloud:
12-
name: SonarCloud
13-
if: ${{ !contains(github.event.pull_request.labels.*.name, 'dependency') }}
14-
runs-on: ubuntu-latest
11+
sonarcloud:
12+
name: SonarCloud
13+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'dependency') }}
14+
runs-on: ubuntu-latest
1515

16-
steps:
17-
- uses: actions/checkout@v4
18-
with:
19-
fetch-depth: 0
16+
steps:
17+
- uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
2020

21-
- name: Set up AWS credentials
22-
env:
23-
AWS_ACCESS_KEY_ID: "FOOBARKEY"
24-
AWS_SECRET_ACCESS_KEY: "FOOBARSECRET"
25-
run: |
26-
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
27-
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
21+
- name: Set up AWS credentials
22+
env:
23+
AWS_ACCESS_KEY_ID: "FOOBARKEY"
24+
AWS_SECRET_ACCESS_KEY: "FOOBARSECRET"
25+
run: |
26+
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
27+
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
2828
29-
- name: Initialize Unit Test Failure Tracker
30-
run: echo "false" > test_failed.txt
29+
- name: Initialize Unit Test Failure Tracker
30+
run: echo "false" > test_failed.txt
3131

32-
# - name: Run unittest with filenameprocessor-coverage
33-
# id: filenameprocessor
34-
# continue-on-error: true
35-
# run: |
36-
# pip install poetry==1.8.4 moto==4.2.11 coverage redis botocore==1.35.49 simplejson pandas freezegun responses structlog fhir.resources jsonpath_ng pydantic==1.10.13 requests aws-lambda-typing cffi pyjwt boto3-stubs-lite[dynamodb]~=1.26.90 python-stdnum==1.20 fakeredis
37-
# poetry run coverage run --source=filenameprocessor -m unittest discover -s filenameprocessor || echo "filenameprocessor tests failed" >> failed_tests.txt
38-
# poetry run coverage xml -o sonarcloud-coverage-filenameprocessor-coverage.xml
32+
# - name: Run unittest with filenameprocessor-coverage
33+
# id: filenameprocessor
34+
# continue-on-error: true
35+
# run: |
36+
# pip install poetry==1.8.4 moto==4.2.11 coverage redis botocore==1.35.49 simplejson pandas freezegun responses structlog fhir.resources jsonpath_ng pydantic==1.10.13 requests aws-lambda-typing cffi pyjwt boto3-stubs-lite[dynamodb]~=1.26.90 python-stdnum==1.20 fakeredis
37+
# poetry run coverage run --source=filenameprocessor -m unittest discover -s filenameprocessor || echo "filenameprocessor tests failed" >> failed_tests.txt
38+
# poetry run coverage xml -o sonarcloud-coverage-filenameprocessor-coverage.xml
3939

40-
# - name: Run unittest with recordprocessor-coverage
41-
# id: recordprocessor
42-
# continue-on-error: true
43-
# run: |
44-
# poetry run coverage run --source=recordprocessor -m unittest discover -s recordprocessor || echo "recordprocessor tests failed" >> failed_tests.txt
45-
# poetry run coverage xml -o sonarcloud-coverage-recordprocessor-coverage.xml
40+
# - name: Run unittest with recordprocessor-coverage
41+
# id: recordprocessor
42+
# continue-on-error: true
43+
# run: |
44+
# poetry run coverage run --source=recordprocessor -m unittest discover -s recordprocessor || echo "recordprocessor tests failed" >> failed_tests.txt
45+
# poetry run coverage xml -o sonarcloud-coverage-recordprocessor-coverage.xml
4646

47-
# - name: Run unittest with recordforwarder-coverage
48-
# id: recordforwarder
49-
# continue-on-error: true
50-
# run: |
51-
# PYTHONPATH=$(pwd)/backend:$(pwd)/backend/tests poetry run coverage run --source=backend -m unittest discover -s backend/tests -p "*batch*.py" || echo "recordforwarder tests failed" >> failed_tests.txt
52-
# poetry run coverage xml -o sonarcloud-coverage-recordforwarder-coverage.xml
47+
# - name: Run unittest with recordforwarder-coverage
48+
# id: recordforwarder
49+
# continue-on-error: true
50+
# run: |
51+
# PYTHONPATH=$(pwd)/backend:$(pwd)/backend/tests poetry run coverage run --source=backend -m unittest discover -s backend/tests -p "*batch*.py" || echo "recordforwarder tests failed" >> failed_tests.txt
52+
# poetry run coverage xml -o sonarcloud-coverage-recordforwarder-coverage.xml
5353

54-
# - name: Run unittest with coverage-ack-lambda
55-
# id: acklambda
56-
# continue-on-error: true
57-
# run: |
58-
# pip install poetry==1.8.4 coverage moto==4.2.11 freezegun
59-
# poetry run coverage run --source=ack_backend -m unittest discover -s ack_backend || echo "ack-lambda tests failed" >> failed_tests.txt
60-
# poetry run coverage xml -o sonarcloud-coverage-ack-lambda.xml
54+
# - name: Run unittest with coverage-ack-lambda
55+
# id: acklambda
56+
# continue-on-error: true
57+
# run: |
58+
# pip install poetry==1.8.4 coverage moto==4.2.11 freezegun
59+
# poetry run coverage run --source=ack_backend -m unittest discover -s ack_backend || echo "ack-lambda tests failed" >> failed_tests.txt
60+
# poetry run coverage xml -o sonarcloud-coverage-ack-lambda.xml
6161

62-
- name: Run unittest with coverage-delta
63-
id: delta
64-
env:
65-
PYTHONPATH: delta_backend/src:delta_backend/tests:./src:./tests
66-
continue-on-error: true
67-
run: |
68-
echo ">>>> SAW: use delta.firehose_log:"
69-
pip install poetry==1.8.4 moto==4.2.11 mypy-boto3-dynamodb==1.35.54 boto3==1.26.165 coverage botocore==1.29.165 jmespath==1.0.1 python-dateutil==2.9.0 urllib3==1.26.20 s3transfer==0.6.2 typing-extensions==4.12.2
70-
export PYTHONPATH=src:tests
71-
PYTHONPATH=delta_backend/src:delta_backend/tests:./src:./tests poetry run coverage run --source=delta_backend -m unittest discover -s delta_backend/tests || echo "delta tests failed" >> failed_tests.txt
72-
poetry run coverage xml -o sonarcloud-coverage-delta.xml
62+
- name: Run unittest with coverage-delta
63+
id: delta
64+
env:
65+
PYTHONPATH: delta_backend/src:delta_backend/tests
66+
continue-on-error: true
67+
run: |
68+
echo ">>>> SAW: use delta.firehose_log:"
69+
pip install poetry==1.8.4 moto==4.2.11 mypy-boto3-dynamodb==1.35.54 boto3==1.26.165 coverage botocore==1.29.165 jmespath==1.0.1 python-dateutil==2.9.0 urllib3==1.26.20 s3transfer==0.6.2 typing-extensions==4.12.2
70+
poetry run coverage run --source=delta_backend -m unittest discover -s delta_backend/tests || echo "delta tests failed" >> failed_tests.txt
71+
poetry run coverage run --source=delta_backend -m unittest discover -s delta_backend/tests || echo "delta tests failed" >> failed_tests.txt
72+
poetry run coverage xml -o sonarcloud-coverage-delta.xml
7373
74-
# - name: Run unittest with coverage-fhir-api
75-
# id: fhirapi
76-
# continue-on-error: true
77-
# run: |
78-
# pip install poetry==1.8.4 moto==4.2.11 coverage redis botocore==1.35.49 simplejson responses structlog fhir.resources jsonpath_ng pydantic==1.10.13 requests aws-lambda-typing cffi pyjwt boto3-stubs-lite[dynamodb]~=1.26.90 python-stdnum==1.20
79-
# poetry run coverage run --source=backend -m unittest discover -s backend || echo "fhir-api tests failed" >> failed_tests.txt
80-
# poetry run coverage xml -o sonarcloud-coverage.xml
74+
# - name: Run unittest with coverage-fhir-api
75+
# id: fhirapi
76+
# continue-on-error: true
77+
# run: |
78+
# pip install poetry==1.8.4 moto==4.2.11 coverage redis botocore==1.35.49 simplejson responses structlog fhir.resources jsonpath_ng pydantic==1.10.13 requests aws-lambda-typing cffi pyjwt boto3-stubs-lite[dynamodb]~=1.26.90 python-stdnum==1.20
79+
# poetry run coverage run --source=backend -m unittest discover -s backend || echo "fhir-api tests failed" >> failed_tests.txt
80+
# poetry run coverage xml -o sonarcloud-coverage.xml
8181

82-
# - name: Run unittest with coverage-mesh-processor
83-
# id: meshprocessor
84-
# continue-on-error: true
85-
# run: |
86-
# pip install poetry==1.8.4 moto==4.2.11 coverage redis botocore==1.35.49 simplejson responses structlog fhir.resources jsonpath_ng pydantic==1.10.13 requests aws-lambda-typing cffi pyjwt boto3-stubs-lite[dynamodb]~=1.26.90 python-stdnum==1.20
87-
# poetry run coverage run --source=mesh_processor -m unittest discover -s mesh_processor || echo "mesh_processor tests failed" >> failed_tests.txt
88-
# poetry run coverage xml -o sonarcloud-mesh_processor-coverage.xml
82+
# - name: Run unittest with coverage-mesh-processor
83+
# id: meshprocessor
84+
# continue-on-error: true
85+
# run: |
86+
# pip install poetry==1.8.4 moto==4.2.11 coverage redis botocore==1.35.49 simplejson responses structlog fhir.resources jsonpath_ng pydantic==1.10.13 requests aws-lambda-typing cffi pyjwt boto3-stubs-lite[dynamodb]~=1.26.90 python-stdnum==1.20
87+
# poetry run coverage run --source=mesh_processor -m unittest discover -s mesh_processor || echo "mesh_processor tests failed" >> failed_tests.txt
88+
# poetry run coverage xml -o sonarcloud-mesh_processor-coverage.xml
8989

90-
- name: Run Test Failure Summary
91-
id: check_failure
92-
run: |
93-
if [ -s failed_tests.txt ]; then
94-
echo "The following tests failed:"
95-
cat failed_tests.txt
90+
- name: Run Test Failure Summary
91+
id: check_failure
92+
run: |
93+
if [ -s failed_tests.txt ]; then
94+
echo "The following tests failed:"
95+
cat failed_tests.txt
9696
97-
while IFS= read -r line; do
98-
echo "##[error]Test Failures: $line"
99-
done < failed_tests.txt
100-
101-
exit 1
102-
else
103-
echo "All tests passed."
104-
fi
97+
while IFS= read -r line; do
98+
echo "##[error]Test Failures: $line"
99+
done < failed_tests.txt
100+
101+
exit 1
102+
else
103+
echo "All tests passed."
104+
fi
105105
106-
- name: SonarCloud Scan
107-
uses: SonarSource/sonarqube-scan-action@master
108-
env:
109-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
110-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
106+
- name: SonarCloud Scan
107+
uses: SonarSource/sonarqube-scan-action@master
108+
env:
109+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
110+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

delta_backend/failed_tests.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

delta_backend/pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ version = "0.1.0"
44
description = ""
55
authors = ["Your Name <you@example.com>"]
66
readme = "README.md"
7-
packages = [{include = "src"}]
7+
packages = [
8+
{include = "src"},
9+
{include = "tests"},
10+
{include = "tests/sample_data"}
11+
]
812

913
[tool.poetry.dependencies]
1014
python = "~3.10"

delta_backend/src/Converter.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414
get_valid_address,
1515
)
1616

17-
# Converter variables
18-
FHIRData = ""
19-
SchemaFile = {}
20-
imms = []
21-
Converted = {}
22-
ErrorRecords = []
23-
24-
2517
# Converter
2618
class Converter:
2719

2820
def __init__(self, fhir_data):
21+
#Converter variables
22+
self.FHIRData = ""
23+
self.SchemaFile = {}
24+
self.imms = []
25+
self.Converted = {}
26+
self.ErrorRecords = []
27+
2928
self.FHIRData = fhir_data # Store JSON data directly
3029
self.SchemaFile = ConversionLayout.ConvertLayout
3130

@@ -56,7 +55,7 @@ def _convertData(self, ConversionValidate, expression, dataParser, json_data):
5655
except Exception as e:
5756
message = "Data get value Unexpected exception [%s]: %s" % (e.__class__.__name__, e)
5857
p = {"code": ExceptionMessages.PARSING_ERROR, "message": message}
59-
ErrorRecords.append(p)
58+
self.ErrorRecords.append(p)
6059
return p
6160

6261
for conversionValue in conversionValues:
@@ -66,7 +65,7 @@ def _convertData(self, ConversionValidate, expression, dataParser, json_data):
6665
if "address" in FHIRFieldName or "performer" in FHIRFieldName or "name" in FHIRFieldName:
6766
convertedData = self.extract_patient_details(json_data, FlatFieldName)
6867
if convertedData is not None:
69-
Converted[FlatFieldName] = convertedData
68+
self.Converted[FlatFieldName] = convertedData
7069

7170
# run the conversion against the data
7271
def runConversion(self, json_data, summarise=False, report_unexpected_exception=True):
@@ -76,7 +75,7 @@ def runConversion(self, json_data, summarise=False, report_unexpected_exception=
7675
if report_unexpected_exception:
7776
message = "FHIR Parser Unexpected exception [%s]: %s" % (e.__class__.__name__, e)
7877
p = {"code": 0, "message": message}
79-
ErrorRecords.append(p)
78+
self.ErrorRecords.append(p)
8079
return p
8180

8281
try:
@@ -85,7 +84,7 @@ def runConversion(self, json_data, summarise=False, report_unexpected_exception=
8584
if report_unexpected_exception:
8685
message = "Schema Parser Unexpected exception [%s]: %s" % (e.__class__.__name__, e)
8786
p = {"code": 0, "message": message}
88-
ErrorRecords.append(p)
87+
self.ErrorRecords.append(p)
8988
return p
9089

9190
try:
@@ -94,7 +93,7 @@ def runConversion(self, json_data, summarise=False, report_unexpected_exception=
9493
if report_unexpected_exception:
9594
message = "Expression Checker Unexpected exception [%s]: %s" % (e.__class__.__name__, e)
9695
p = {"code": 0, "message": message}
97-
ErrorRecords.append(p)
96+
self.ErrorRecords.append(p)
9897
return p
9998

10099
# get list of expressions
@@ -104,17 +103,17 @@ def runConversion(self, json_data, summarise=False, report_unexpected_exception=
104103
if report_unexpected_exception:
105104
message = "Expression Getter Unexpected exception [%s]: %s" % (e.__class__.__name__, e)
106105
p = {"code": 0, "message": message}
107-
ErrorRecords.append(p)
106+
self.ErrorRecords.append(p)
108107
return p
109108

110109
for conversion in conversions:
111110
rows = self._convertData(ConversionValidate, conversion, dataParser, json_data)
112111

113-
imms.append(Converted)
114-
return imms
112+
self.imms.append(self.Converted)
113+
return self.imms
115114

116115
def getErrorRecords(self):
117-
return ErrorRecords
116+
return self.ErrorRecords
118117

119118
def extract_patient_details(self, json_data, FlatFieldName):
120119
if not hasattr(self, "_cached_values"):

delta_backend/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
import sys
33

44
sys.path.append(f"{os.path.dirname(os.path.abspath(__file__))}/../src")
5+

delta_backend/tests/sample_data/__init__.py

Whitespace-only changes.

delta_backend/tests/test_convert_to_flat_json.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"SOURCE": "test-source",
2323
}
2424
request_json_data = ValuesForTests.json_data
25-
with patch.dict("os.environ", MOCK_ENV_VARS):
25+
with patch.dict("os.environ", MOCK_ENV_VARS, clear=True):
2626
from delta import handler, Converter
27-
from Converter import imms, ErrorRecords
27+
# from Converter import imms, ErrorRecords
2828

2929

3030
@patch.dict("os.environ", MOCK_ENV_VARS, clear=True)
@@ -111,7 +111,7 @@ def assert_dynamodb_record(self, operation_flag, items, expected_values, expecte
111111

112112
def test_fhir_converter_json_direct_data(self):
113113
"""it should convert fhir json data to flat json"""
114-
imms.clear()
114+
# imms.clear()
115115
json_data = json.dumps(ValuesForTests.json_data)
116116

117117
start = time.time()
@@ -135,12 +135,13 @@ def test_fhir_converter_json_direct_data(self):
135135
end = time.time()
136136
print(end - start)
137137

138+
# These tests fail
138139
def test_fhir_converter_json_error_scenario(self):
139140
"""it should convert fhir json data to flat json - error scenarios"""
140141
error_test_cases = [ErrorValuesForTests.missing_json, ErrorValuesForTests.json_dob_error]
141142

142143
for test_case in error_test_cases:
143-
imms.clear()
144+
# imms.clear()
144145
json_data = json.dumps(test_case)
145146

146147
start = time.time()
@@ -177,7 +178,7 @@ def test_handler_imms_convert_to_flat_json(self):
177178

178179
for test_case in expected_action_flags:
179180
with self.subTest(test_case["Operation"]):
180-
imms.clear()
181+
# imms.clear()
181182

182183
event = self.get_event(operation=test_case["Operation"])
183184

@@ -257,6 +258,9 @@ def test_conversion_checker_exception(self, mock_conversion_checker):
257258
)
258259
self.assertEqual(converter.getErrorRecords()[0]["code"], 0)
259260

261+
262+
263+
260264
@patch("Converter.SchemaParser.getConversions")
261265
def test_get_conversions_exception(self, mock_get_conversions):
262266
# Mock getConversions to raise an exception
@@ -266,7 +270,7 @@ def test_get_conversions_exception(self, mock_get_conversions):
266270
response = converter.runConversion(ValuesForTests.json_data)
267271

268272
# Check if the error message was added to ErrorRecords
269-
self.assertEqual(len(converter.getErrorRecords()), 3)
273+
self.assertEqual(len(converter.getErrorRecords()), 1)
270274
self.assertIn(
271275
"FHIR Parser Unexpected exception [JSONDecodeError]: Expecting value: line 1 column 1 (char 0)",
272276
converter.getErrorRecords()[0]["message"],
@@ -278,7 +282,7 @@ def test_get_conversions_exception(self, mock_get_conversions):
278282
def test_conversion_exceptions(self, mock_get_key_value, mock_get_conversions):
279283
mock_get_conversions.side_effect = Exception("Error while getting conversions")
280284
mock_get_key_value.side_effect = Exception("Key value retrieval failed")
281-
ErrorRecords.clear()
285+
# ErrorRecords.clear()
282286
converter = Converter(fhir_data="some_data")
283287

284288
schema = {

0 commit comments

Comments
 (0)