Skip to content

Commit

Permalink
Merge pull request #3 from VemundFredriksen/integration-test-in-ci-pi…
Browse files Browse the repository at this point in the history
…peline

Integration test in ci pipeline
  • Loading branch information
VemundFredriksen authored Oct 16, 2023
2 parents 08da925 + 9a1ad98 commit a26c1d4
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 6 deletions.
23 changes: 21 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@ on:
workflow_dispatch:

jobs:
sonarcloud:
name: SonarCloud
test_and_analyze:
name: Test & Analyze
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'

- uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ hashFiles('setup.py') }}-${{ hashFiles('requirements.txt') }}

- name: Install dependencies
run: pip install -r requirements.txt

- name: Install tox
run: pip install tox
- name: Run tox
run: tox -e py

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/out/*
/synthlung.egg*/
/build/*
*__pycache__*
*__pycache__*
.coverage
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
numpy
torch
monai
tqdm
pytest
pytest-cov
3 changes: 3 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
sonar.projectKey=VemundFredriksen_SynthLung
sonar.organization=vemundfredriksen

sonar.python.coverage.reportPaths=coverage.xml
sonar.sources = synthlung/
sonar.tests = tests/
# This is the name and version displayed in the SonarCloud UI.
#sonar.projectName=SynthLung
#sonar.projectVersion=1.0
Expand Down
6 changes: 3 additions & 3 deletions synthlung/utils/dataset_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def generate_json(self) -> None:
pass

class MSDImageSourceFormatter(ImageSourceFormatter, JSONGenerator):
def __init__(self) -> None:
self.target_directory = "./assets/source/msd/"
self.source_directory = "./assets/Task06_Lung/"
def __init__(self, source_directory: str = "./assets/Task06_Lung/", target_directory: str = "./assets/source/msd/") -> None:
self.target_directory = target_directory
self.source_directory = source_directory

def format(self) -> None:
if not os.path.exists(self.target_directory):
Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file.
72 changes: 72 additions & 0 deletions tests/integration_tests/test_dataset_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest
from synthlung.utils.dataset_formatter import MSDImageSourceFormatter
import shutil
from pathlib import Path
import json

def test_msd_formatter_format():
# Arrange
source_dir = './tests/source'
target_dir = './tests/target'

shutil.os.makedirs(f'{source_dir}/imagesTr/')
shutil.os.makedirs(f'{source_dir}/labelsTr/')
dummy_images = ["lung_001.nii.gz", "lung_002.nii.gz", "lung_003.nii.gz"]
for image in dummy_images:
image_path = Path(f"{source_dir}/imagesTr/{image}")
label_path = Path(f"{source_dir}/labelsTr/{image}")
Path.touch(image_path)
Path.touch(label_path)


msd_image_source_formatter = MSDImageSourceFormatter(source_dir, target_dir)

# Act
msd_image_source_formatter.format()

# Assert
expected_files = ["source_msd_lung_001_image.nii.gz", "source_msd_lung_001_label.nii.gz", "source_msd_lung_002_image.nii.gz", "source_msd_lung_002_label.nii.gz", "source_msd_lung_003_image.nii.gz", "source_msd_lung_003_label.nii.gz"]
actual_files = sorted(shutil.os.listdir(target_dir))

assert expected_files == actual_files

# Cleanup
shutil.rmtree(source_dir)
shutil.rmtree(target_dir)

def test_msd_formatter_generate_json():
# Arrange
target_dir = './tests/target/'
shutil.os.makedirs(target_dir)

dummy_images = ["source_msd_lung_001_image.nii.gz", "source_msd_lung_001_label.nii.gz", "source_msd_lung_002_image.nii.gz", "source_msd_lung_002_label.nii.gz"]
for image in dummy_images:
image_path = Path(f"{target_dir}/{image}")
Path.touch(image_path)


msd_image_source_formatter = MSDImageSourceFormatter(target_dir, target_dir)

# Act
msd_image_source_formatter.generate_json()

# Assert
expected_json = [
{
"image": f"{target_dir}source_msd_lung_001_image.nii.gz",
"label": f"{target_dir}source_msd_lung_001_label.nii.gz"
},
{
"image": f"{target_dir}source_msd_lung_002_image.nii.gz",
"label": f"{target_dir}source_msd_lung_002_label.nii.gz"
}
]

actual_json = None
with open(f"{target_dir}/dataset.json", 'r') as json_file:
actual_json = json.load(json_file)

assert expected_json == actual_json

# Cleanup
shutil.rmtree(target_dir)
14 changes: 14 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tox]
envlist = py39
skipsdist = True

[testenv]
deps =
pytest
pytest-cov
commands = pytest --cov=synthlung --cov-report=xml --cov-config=tox.ini --cov-branch

[coverage:run]
relative_files = True
source = synthlung/
branch = True

0 comments on commit a26c1d4

Please sign in to comment.