7
7
import os
8
8
import logging
9
9
import subprocess
10
- import pytest
11
10
from unittest import TestCase
12
11
from unittest .mock import patch
13
12
from importlib import reload
21
20
class TestAquaCLI (TestCase ):
22
21
"""Tests the AQUA CLI."""
23
22
24
- DEFAUL_AQUA_CLI_LOGGING_LEVEL = "ERROR"
23
+ DEFAULT_AQUA_CLI_LOGGING_LEVEL = "ERROR"
25
24
logger = logging .getLogger (__name__ )
26
25
logging .basicConfig (
27
26
format = "%(asctime)s %(module)s %(levelname)s: %(message)s" ,
@@ -30,11 +29,6 @@ class TestAquaCLI(TestCase):
30
29
)
31
30
SERVICE_COMPARTMENT_ID = "ocid1.compartment.oc1..<OCID>"
32
31
33
- def setUp (self ):
34
- os .environ ["ODSC_MODEL_COMPARTMENT_OCID" ] = TestAquaCLI .SERVICE_COMPARTMENT_ID
35
- reload (ads .aqua )
36
- reload (ads .aqua .cli )
37
-
38
32
def test_entrypoint (self ):
39
33
"""Tests CLI entrypoint."""
40
34
result = subprocess .run (["ads" , "aqua" , "--help" ], capture_output = True )
@@ -43,23 +37,55 @@ def test_entrypoint(self):
43
37
44
38
@parameterized .expand (
45
39
[
46
- ("default" , None , DEFAUL_AQUA_CLI_LOGGING_LEVEL ),
40
+ ("default" , None , DEFAULT_AQUA_CLI_LOGGING_LEVEL ),
47
41
("set logging level" , "info" , "info" ),
48
42
]
49
43
)
50
- @patch ("ads.aqua.cli.set_log_level" )
51
- def test_aquacommand (self , name , arg , expected , mock_setting_log ):
52
- """Tests aqua command initailzation."""
53
- if arg :
54
- AquaCommand (arg )
55
- else :
56
- AquaCommand ()
57
- mock_setting_log .assert_called_with (expected )
44
+ def test_aquacommand (self , name , arg , expected ):
45
+ """Tests aqua command initialization."""
46
+ with patch .dict (
47
+ os .environ ,
48
+ {"ODSC_MODEL_COMPARTMENT_OCID" : TestAquaCLI .SERVICE_COMPARTMENT_ID },
49
+ ):
50
+ reload (ads .config )
51
+ reload (ads .aqua )
52
+ reload (ads .aqua .cli )
53
+ with patch ("ads.aqua.cli.set_log_level" ) as mock_setting_log :
54
+ if arg :
55
+ AquaCommand (arg )
56
+ else :
57
+ AquaCommand ()
58
+ mock_setting_log .assert_called_with (expected )
59
+
60
+ @parameterized .expand (
61
+ [
62
+ ("default" , None ),
63
+ ("using jupyter instance" , "nb-session-ocid" ),
64
+ ]
65
+ )
66
+ def test_aqua_command_without_compartment_env_var (self , name , session_ocid ):
67
+ """Test whether exit is called when ODSC_MODEL_COMPARTMENT_OCID is not set. Also check if NB_SESSION_OCID is
68
+ set then log the appropriate message."""
58
69
59
- @patch ("sys.exit" )
60
- def test_aqua_command_without_compartment_env_var (self , mock_exit ):
61
- os .environ .pop ("ODSC_MODEL_COMPARTMENT_OCID" , None )
62
- reload (ads .aqua )
63
- reload (ads .aqua .cli )
64
- AquaCommand ()
65
- mock_exit .assert_called_with (0 )
70
+ with patch ("sys.exit" ) as mock_exit :
71
+ env_dict = {"ODSC_MODEL_COMPARTMENT_OCID" : "" }
72
+ if session_ocid :
73
+ env_dict .update ({"NB_SESSION_OCID" : session_ocid })
74
+ with patch .dict (os .environ , env_dict ):
75
+ reload (ads .config )
76
+ reload (ads .aqua )
77
+ reload (ads .aqua .cli )
78
+ with patch ("ads.aqua.cli.set_log_level" ) as mock_setting_log :
79
+ with patch ("ads.aqua.logger.error" ) as mock_logger_error :
80
+ AquaCommand ()
81
+ mock_setting_log .assert_called_with (
82
+ TestAquaCLI .DEFAULT_AQUA_CLI_LOGGING_LEVEL
83
+ )
84
+ mock_logger_error .assert_any_call (
85
+ "ODSC_MODEL_COMPARTMENT_OCID environment variable is not set for Aqua."
86
+ )
87
+ if session_ocid :
88
+ mock_logger_error .assert_any_call (
89
+ f"Aqua is not available for the notebook session { session_ocid } ."
90
+ )
91
+ mock_exit .assert_called_with (1 )
0 commit comments