Skip to content

Commit

Permalink
Change printing error messages to raising typer aborts
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielZhangDP committed Sep 13, 2024
1 parent 4c9f9ee commit f26edef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions cfmtoolbox/plugins/one_wise_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from collections import defaultdict
from dataclasses import asdict

import typer

from cfmtoolbox import app
from cfmtoolbox.models import CFM, Cardinality, Feature, FeatureNode

Expand All @@ -12,12 +14,10 @@ def one_wise_sampling(
model: CFM | None,
) -> CFM | None:
if model is None:
print("No model loaded.")
return None
raise typer.Abort("No model loaded.")

if model.is_unbound():
print("Model is unbound. Please apply big-m global bound first.")
return model
raise typer.Abort("Model is unbound. Please apply big-m global bound first.")

print(
json.dumps(
Expand Down
11 changes: 7 additions & 4 deletions tests/plugins/test_one_wise_sampling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
import typer

import cfmtoolbox.plugins.one_wise_sampling as one_wise_sampling_plugin
from cfmtoolbox import app
Expand Down Expand Up @@ -29,13 +30,15 @@ def test_plugin_can_be_loaded():


def test_one_wise_sampling_without_loaded_model():
assert one_wise_sampling(None) is None
with pytest.raises(typer.Abort, match="No model loaded."):
one_wise_sampling(None)


def test_one_wise_sampling_with_unbound_model(unbound_model: CFM, capsys):
one_wise_sampling(unbound_model) is unbound_model
captured = capsys.readouterr()
assert "Model is unbound. Please apply big-m global bound first." in captured.out
with pytest.raises(
typer.Abort, match="Model is unbound. Please apply big-m global bound first."
):
one_wise_sampling(unbound_model)


def test_plugin_passes_though_model(model: CFM):
Expand Down

0 comments on commit f26edef

Please sign in to comment.