55# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
66
77import os
8- import json
8+ import unittest
9+ from unittest .mock import MagicMock , patch
910from importlib import reload
10- from tornado .web import Application
11- from tornado .testing import AsyncHTTPTestCase
11+ from notebook .base .handlers import IPythonHandler
1212
1313import ads .config
1414import ads .aqua
@@ -20,47 +20,82 @@ class TestDataset:
2020 SERVICE_COMPARTMENT_ID = "ocid1.compartment.oc1..<OCID>"
2121
2222
23- class TestYourHandler (AsyncHTTPTestCase ):
24- def get_app (self ):
25- return Application ([(r"/hello" , CompatibilityCheckHandler )])
26-
27- def setUp (self ):
28- super ().setUp ()
29- os .environ ["ODSC_MODEL_COMPARTMENT_OCID" ] = TestDataset .SERVICE_COMPARTMENT_ID
30- reload (ads .config )
31- reload (ads .aqua )
32- reload (ads .aqua .extension .common_handler )
33-
34- def tearDown (self ):
35- super ().tearDown ()
36- os .environ .pop ("ODSC_MODEL_COMPARTMENT_OCID" , None )
37- reload (ads .config )
38- reload (ads .aqua )
39- reload (ads .aqua .extension .common_handler )
23+ class TestEvaluationHandler (unittest .TestCase ):
24+ @patch .object (IPythonHandler , "__init__" )
25+ def setUp (self , ipython_init_mock ) -> None :
26+ ipython_init_mock .return_value = None
27+ self .common_handler = CompatibilityCheckHandler (MagicMock (), MagicMock ())
28+ self .common_handler .request = MagicMock ()
4029
4130 def test_get_ok (self ):
42- response = self .fetch ("/hello" , method = "GET" )
43- assert json .loads (response .body )["status" ] == "ok"
31+ """Test to check if ok is returned when ODSC_MODEL_COMPARTMENT_OCID is set."""
32+ with patch .dict (
33+ os .environ ,
34+ {"ODSC_MODEL_COMPARTMENT_OCID" : TestDataset .SERVICE_COMPARTMENT_ID },
35+ ):
36+ reload (ads .config )
37+ reload (ads .aqua )
38+ reload (ads .aqua .extension .common_handler )
39+
40+ with patch (
41+ "ads.aqua.extension.base_handler.AquaAPIhandler.finish"
42+ ) as mock_finish :
43+ mock_finish .side_effect = lambda x : x
44+ self .common_handler .request .path = "aqua/hello"
45+ result = self .common_handler .get ()
46+ assert result ["status" ] == "ok"
4447
4548 def test_get_compatible_status (self ):
46- os .environ ["ODSC_MODEL_COMPARTMENT_OCID" ] = ""
47- os .environ ["CONDA_BUCKET_NS" ] = AQUA_GA_LIST [0 ]
48- reload (ads .common )
49- reload (ads .aqua )
50- reload (ads .aqua .extension .common_handler )
51- response = self .fetch ("/hello" , method = "GET" )
52- assert json .loads (response .body )["status" ] == "compatible"
49+ """Test to check if compatible is returned when ODSC_MODEL_COMPARTMENT_OCID is not set
50+ but CONDA_BUCKET_NS is one of the namespaces from the GA list."""
51+ with patch .dict (
52+ os .environ ,
53+ {"ODSC_MODEL_COMPARTMENT_OCID" : "" , "CONDA_BUCKET_NS" : AQUA_GA_LIST [0 ]},
54+ ):
55+ reload (ads .config )
56+ reload (ads .aqua )
57+ reload (ads .aqua .extension .common_handler )
58+ with patch (
59+ "ads.aqua.extension.base_handler.AquaAPIhandler.finish"
60+ ) as mock_finish :
61+ with patch (
62+ "ads.aqua.extension.common_handler.fetch_service_compartment"
63+ ) as mock_fetch_service_compartment :
64+ mock_fetch_service_compartment .return_value = None
65+ mock_finish .side_effect = lambda x : x
66+ self .common_handler .request .path = "aqua/hello"
67+ result = self .common_handler .get ()
68+ assert result ["status" ] == "compatible"
5369
5470 def test_raise_not_compatible_error (self ):
55- os .environ ["ODSC_MODEL_COMPARTMENT_OCID" ] = ""
56- os .environ ["CONDA_BUCKET_NS" ] = "test-namespace"
57- reload (ads .common )
58- reload (ads .aqua )
59- reload (ads .aqua .extension .common_handler )
60- response = self .fetch ("/hello" , method = "GET" )
61- body = json .loads (response .body )
62- assert body ["status" ] == 404
63- assert (
64- body ["reason" ]
65- == "The AI Quick actions extension is not compatible in the given region."
66- )
71+ """Test to check if error is returned when ODSC_MODEL_COMPARTMENT_OCID is not set
72+ and CONDA_BUCKET_NS is not one of the namespaces from the GA list."""
73+ with patch .dict (
74+ os .environ ,
75+ {"ODSC_MODEL_COMPARTMENT_OCID" : "" , "CONDA_BUCKET_NS" : "test-namespace" },
76+ ):
77+ reload (ads .config )
78+ reload (ads .aqua )
79+ reload (ads .aqua .extension .common_handler )
80+ with patch (
81+ "ads.aqua.extension.base_handler.AquaAPIhandler.finish"
82+ ) as mock_finish :
83+ with patch (
84+ "ads.aqua.extension.common_handler.fetch_service_compartment"
85+ ) as mock_fetch_service_compartment :
86+ mock_fetch_service_compartment .return_value = None
87+ mock_finish .side_effect = lambda x : x
88+ self .common_handler .write_error = MagicMock ()
89+ self .common_handler .request .path = "aqua/hello"
90+ self .common_handler .get ()
91+
92+ assert self .common_handler .write_error .call_args [1 ].get (
93+ "reason"
94+ ) == (
95+ "The AI Quick actions extension is not "
96+ "compatible in the given region."
97+ ), "Incorrect error message."
98+ assert (
99+ self .common_handler .write_error .call_args [1 ].get ("status_code" )
100+ == 404
101+ ), "Incorrect status code."
0 commit comments