Skip to content

Commit 1e02c9a

Browse files
committed
Add a couple unit tests
1 parent de0c141 commit 1e02c9a

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

test/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import contextlib
44
import os
5+
import sys
56
import unittest
67
from importlib import reload
78

@@ -31,6 +32,13 @@ def without_import(self, library, module):
3132
yield
3233
reload(module)
3334

35+
@contextlib.contextmanager
36+
def replace_stdin(self, target):
37+
orig = sys.stdin
38+
sys.stdin = target
39+
yield
40+
sys.stdin = orig
41+
3442
# recipe from https://stackoverflow.com/a/34333710
3543
@contextlib.contextmanager
3644
def modified_environ(self, *remove, **update):

test/test_utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import numpy as np
2020
import pandas as pd
2121
import pytest
22-
from testfixtures import LogCapture
22+
from testfixtures import LogCapture, StringComparison
2323

2424
from cmdstanpy import _DOT_CMDSTAN, _TMPDIR
2525
from cmdstanpy.model import CmdStanModel
@@ -36,6 +36,7 @@
3636
do_command,
3737
flatten_chains,
3838
get_latest_cmdstan,
39+
install_cmdstan,
3940
parse_method_vars,
4041
parse_rdump_value,
4142
parse_stan_vars,
@@ -850,6 +851,34 @@ def test_bad(self):
850851
flatten_chains(array_2d)
851852

852853

854+
class InstallCmdstanFunctionTest(CustomTestCase):
855+
def test_bad_version(self):
856+
with LogCapture() as log:
857+
res = install_cmdstan(version="0.00.0")
858+
log.check_present(
859+
(
860+
"cmdstanpy",
861+
"WARNING",
862+
StringComparison("CmdStan installation failed.\nVersion*"),
863+
)
864+
)
865+
self.assertFalse(res)
866+
867+
def test_interactive_extra_args(self):
868+
with LogCapture() as log:
869+
with self.replace_stdin(io.StringIO("9.99.9\n")):
870+
res = install_cmdstan(version="2.29.2", interactive=True)
871+
log.check_present(
872+
(
873+
"cmdstanpy",
874+
"WARNING",
875+
"Interactive installation requested but other arguments"
876+
" were used.\n\tThese values will be ignored!",
877+
)
878+
)
879+
self.assertFalse(res)
880+
881+
853882
@pytest.mark.order(-1)
854883
class ShowProgressTest(unittest.TestCase):
855884
# this test must run after any tests that check tqdm progress bars

0 commit comments

Comments
 (0)