Skip to content

Commit

Permalink
testing main directly so that codecov get check coverage of main
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Nov 23, 2024
1 parent d886fe1 commit 67be9a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
6 changes: 1 addition & 5 deletions src/acom_music_box/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
import os
import subprocess
import sys
import tempfile
import matplotlib.pyplot as plt
import mplcursors
Expand Down Expand Up @@ -187,9 +186,8 @@ def main():

if not musicBoxConfigFile:
error = "Configuration file is required."
print(error)
logger.error(error)
sys.exit(1)
raise RuntimeError(error)

# Create and load a MusicBox object
myBox = MusicBox()
Expand All @@ -212,8 +210,6 @@ def main():
logger.info(f"End time: {end}")
logger.info(f"Elapsed time: {end - start} seconds")

sys.exit(0)


if __name__ == "__main__":
main()
36 changes: 24 additions & 12 deletions tests/integration/test_executable_data_output.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,72 @@
import subprocess
import os
import glob
import pytest
import tempfile
from acom_music_box.main import main
from unittest.mock import patch, MagicMock


@pytest.fixture
def temp_dir():
with tempfile.TemporaryDirectory() as tmpdirname:
with tempfile.TemporaryDirectory(delete=False) as tmpdirname:
yield tmpdirname


def run_main_with_args(args, temp_dir):
# Change the working directory temporarily
with patch('sys.argv', ['music_box'] + args), patch('os.getcwd', return_value=temp_dir):
original_cwd = os.getcwd()
try:
os.chdir(temp_dir)
main()
finally:
os.chdir(original_cwd)


def test_print_results_to_terminal(temp_dir):
result = subprocess.run(['music_box', '-e', 'Analytical'], capture_output=True, text=True, cwd=temp_dir)
assert len(result.stdout) > 0
run_main_with_args(['-e', 'Analytical'], temp_dir)
# Add assertions to check the output


def test_create_netcdf_with_timestamp(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'netcdf'], temp_dir)
assert glob.glob(os.path.join(temp_dir, "music_box_*.nc"))


def test_create_csv_with_timestamp(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'csv'], temp_dir)
assert glob.glob(os.path.join(temp_dir, "music_box_*.csv"))


def test_create_named_csv(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv', '-o', 'out.csv'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'csv', '-o', 'out.csv'], temp_dir)
assert os.path.exists(os.path.join(temp_dir, "out.csv"))


def test_create_named_netcdf(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf', '-o', 'out.nc'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'netcdf', '-o', 'out.nc'], temp_dir)
assert os.path.exists(os.path.join(temp_dir, "out.nc"))


def test_create_directory_and_named_netcdf(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf', '-o', 'results/out.nc'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'netcdf', '-o', 'results/out.nc'], temp_dir)
assert os.path.exists(os.path.join(temp_dir, "results/out.nc"))


def test_create_directory_and_named_csv(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv', '-o', 'results/out.csv'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'csv', '-o', 'results/out.csv'], temp_dir)
assert os.path.exists(os.path.join(temp_dir, "results/out.csv"))


def test_create_directory_and_timestamped_csv(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv', '-o', 'results/'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'csv', '-o', 'results/'], temp_dir)
assert glob.glob(os.path.join(temp_dir, "results/music_box_*.csv"))


def test_create_directory_and_timestamped_netcdf(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf', '-o', 'results/'], cwd=temp_dir)
run_main_with_args(['-e', 'Analytical', '--output-format', 'netcdf', '-o', 'results/'], temp_dir)
assert glob.glob(os.path.join(temp_dir, "results/music_box_*.nc"))

0 comments on commit 67be9a4

Please sign in to comment.