1
0
Fork 0
mirror of https://github.com/cosmo-sims/cosmICweb-music.git synced 2024-09-19 16:53:43 +02:00

run tests the proper way

This commit is contained in:
Lukas Winkler 2024-04-30 13:48:15 +02:00
parent 515f2693e4
commit c48fba1cc3
Signed by: lukas
GPG key ID: 54DE4D798D244853
2 changed files with 53 additions and 42 deletions

View file

@ -3,6 +3,9 @@ from __future__ import annotations
import os import os
import sys import sys
from typing import Any from typing import Any
from click import Context
from .data_types import Ellipsoid, Args, DownloadConfig from .data_types import Ellipsoid, Args, DownloadConfig
import click import click
@ -245,7 +248,7 @@ def call_music() -> None:
pass pass
def process_config(config: DownloadConfig, args: Args, store: bool) -> None: def process_config(config: DownloadConfig, args: Args) -> None:
ellipsoids = [] ellipsoids = []
for halo_name, url in zip(config.halo_names, config.halo_urls): for halo_name, url in zip(config.halo_names, config.halo_urls):
logging.info("Fetching ellipsoids from halo " + halo_name) logging.info("Fetching ellipsoids from halo " + halo_name)
@ -262,7 +265,7 @@ def process_config(config: DownloadConfig, args: Args, store: bool) -> None:
music_template = music_config_to_template(config) music_template = music_config_to_template(config)
output = [] output = []
if store and click.confirm( if click.confirm(
"Do you want to edit the MUSIC template before creating the IC files?\n" "Do you want to edit the MUSIC template before creating the IC files?\n"
"(changing zstart, levelmin, levelmax, etc.)", "(changing zstart, levelmin, levelmax, etc.)",
default=False, default=False,
@ -292,27 +295,21 @@ def process_config(config: DownloadConfig, args: Args, store: bool) -> None:
halo_name, output_file halo_name, output_file
) )
) )
if store: write_music_file(output_file, music_config)
write_music_file(output_file, music_config)
else:
output.append((output_file, music_config))
return output
# TODO: Execute MUSIC? # TODO: Execute MUSIC?
def downloadstore_mode(args: Args, target: str, store=True) -> None | str: def downloadstore_mode(args: Args, target: str) -> None | str:
logging.info("Fetching download configuration from the cosmICweb server") logging.info("Fetching download configuration from the cosmICweb server")
config = fetch_downloadstore(args.url, target) config = fetch_downloadstore(args.url, target)
if args.output_path == "./": if args.output_path == "./":
args = args._replace(output_path=f"./cosmICweb-zooms-{config.simulation_name}") args = args._replace(output_path=f"./cosmICweb-zooms-{config.simulation_name}")
logging.debug("Output directory set to " + args.output_path) logging.debug("Output directory set to " + args.output_path)
logging.info("Download configuration successfully fetched") logging.info("Download configuration successfully fetched")
return process_config(config, args, store) return process_config(config, args)
def publication_mode( def publication_mode(args: Args, publication_name: str, traceback_radius) -> None | str:
args: Args, publication_name: str, traceback_radius, store=True
) -> None | str:
logging.info(f"Fetching publication {publication_name} from the cosmICweb server") logging.info(f"Fetching publication {publication_name} from the cosmICweb server")
config = fetch_multiple( config = fetch_multiple(
args.url, traceback_radius, publication_name=publication_name args.url, traceback_radius, publication_name=publication_name
@ -320,12 +317,10 @@ def publication_mode(
args = args._replace(output_path=os.path.join(args.output_path, publication_name)) args = args._replace(output_path=os.path.join(args.output_path, publication_name))
logging.debug("Output directory set to " + args.output_path) logging.debug("Output directory set to " + args.output_path)
logging.info("Publication successfully fetched") logging.info("Publication successfully fetched")
return process_config(config, args, store) return process_config(config, args)
def collection_mode( def collection_mode(args: Args, collection_uuid: str, traceback_radius) -> None | str:
args: Args, collection_uuid: str, traceback_radius, store=True
) -> None | str:
logging.info(f"Fetching collection {collection_uuid} from the cosmICweb server") logging.info(f"Fetching collection {collection_uuid} from the cosmICweb server")
config = fetch_multiple(args.url, traceback_radius, collection_uuid=collection_uuid) config = fetch_multiple(args.url, traceback_radius, collection_uuid=collection_uuid)
args = args._replace( args = args._replace(
@ -334,7 +329,7 @@ def collection_mode(
logging.debug("Output directory set to " + args.output_path) logging.debug("Output directory set to " + args.output_path)
logging.info("Publication successfully fetched") logging.info("Publication successfully fetched")
print(config) print(config)
return process_config(config, args, store) return process_config(config, args)
def dir_path(p: str) -> str: def dir_path(p: str) -> str:

View file

@ -1,10 +1,17 @@
import re import re
from pathlib import Path
import click
from _pytest.monkeypatch import MonkeyPatch
from click.testing import CliRunner
from cosmicweb_music.cosmICweb import ( from cosmicweb_music.cosmICweb import (
DEFAULT_URL, DEFAULT_URL,
downloadstore_mode, downloadstore_mode,
publication_mode, publication_mode,
collection_mode, collection_mode,
get,
cli,
) )
from cosmicweb_music.data_types import Args from cosmicweb_music.data_types import Args
@ -129,7 +136,7 @@ laplace_order = 6
# TODO: add output options # TODO: add output options
""".lstrip() """.lstrip()
reference_output_collection=""" reference_output_collection = """
# Zoom Initial Conditions for halo 20770177 (20770177) in simulation RefL0100N1504 (EAGLE Simulations project) # Zoom Initial Conditions for halo 20770177 (20770177) in simulation RefL0100N1504 (EAGLE Simulations project)
# Details on this halo can be found on https://cosmicweb.eu/simulation/RefL0100N1504/halo/20770177 # Details on this halo can be found on https://cosmicweb.eu/simulation/RefL0100N1504/halo/20770177
# This file has been generated by CosmICweb @2024-04-27T19:00:39.574652 # This file has been generated by CosmICweb @2024-04-27T19:00:39.574652
@ -184,40 +191,49 @@ laplace_order = 6
time_fix_regex = re.compile(r"@[\d\-T:.]+") time_fix_regex = re.compile(r"@[\d\-T:.]+")
def test_single_saved(): def test_single_saved(tmp_path: Path, monkeypatch: MonkeyPatch):
monkeypatch.chdir(tmp_path)
id = "50e20c16-45b8-4d0f-b6cd-44e050b76afb" id = "50e20c16-45b8-4d0f-b6cd-44e050b76afb"
args = Args(output_path="./", url=DEFAULT_URL, common_directory=True, attempts=1) runner = CliRunner()
output = downloadstore_mode(args, id, store=False) result = runner.invoke(cli, ["get", id])
assert len(output) == 1 assert result.exit_code == 0
output = output[0] subdir = tmp_path / "cosmICweb-zooms-AGORA"
assert output[0] == "./cosmICweb-zooms-AGORA/ics_halo_25401888.cfg" assert subdir.exists()
assert len(list(subdir.glob("*"))) == 1
assert time_fix_regex.sub("TIME", output[1]) == time_fix_regex.sub( output_file = subdir / "ics_halo_25401888.cfg"
assert output_file.exists()
assert time_fix_regex.sub("TIME", output_file.read_text()) == time_fix_regex.sub(
"TIME", reference_output "TIME", reference_output
) )
def test_publication(): def test_publication(tmp_path: Path, monkeypatch: MonkeyPatch):
monkeypatch.chdir(tmp_path)
id = "agora-halos" id = "agora-halos"
args = Args(output_path="./", url=DEFAULT_URL, common_directory=True, attempts=1) runner = CliRunner()
output = publication_mode(args, id, store=False, traceback_radius=2.0) result = runner.invoke(cli, ["publication", id])
assert len(output) == 6 assert result.exit_code == 0
output = output[0] subdir = tmp_path / "agora-halos"
assert output[0] == "./agora-halos/1e11v/ics.cfg" assert subdir.exists()
assert len(list(subdir.glob("*"))) == 6
assert time_fix_regex.sub("TIME", output[1]) == time_fix_regex.sub( output_file = subdir / "ics_1e11v.cfg"
assert output_file.exists()
assert time_fix_regex.sub("TIME", output_file.read_text()) == time_fix_regex.sub(
"TIME", reference_output_publication "TIME", reference_output_publication
) )
def test_collection(): def test_collection(tmp_path: Path, monkeypatch: MonkeyPatch):
monkeypatch.chdir(tmp_path)
id = "c30de0f3-ab4d-48ad-aa26-f20bb4b70bbd" id = "c30de0f3-ab4d-48ad-aa26-f20bb4b70bbd"
args = Args(output_path="./", url=DEFAULT_URL, common_directory=True, attempts=1) runner = CliRunner()
output = collection_mode(args, id, store=False, traceback_radius=2.0) result = runner.invoke(cli, ["collection", id])
assert len(output) == 1 assert result.exit_code == 0
output = output[0] subdir = tmp_path / "RefL0100N1504"
assert output[0] == "./RefL0100N1504/ics_20770177.cfg" assert subdir.exists()
assert len(list(subdir.glob("*"))) == 1
assert time_fix_regex.sub("TIME", output[1]) == time_fix_regex.sub( output_file = subdir / "ics_20770177.cfg"
assert output_file.exists()
assert time_fix_regex.sub("TIME", output_file.read_text()) == time_fix_regex.sub(
"TIME", reference_output_collection "TIME", reference_output_collection
) )