33# Copyright (c) 2021, 2023 Oracle and/or its affiliates.
44# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55
6- from ads .catalog .notebook import NotebookCatalog , NotebookSummaryList
7- from ads .common import auth , oci_client
8- from ads .common .utils import random_valid_ocid
9- from ads .config import NB_SESSION_COMPARTMENT_OCID , PROJECT_OCID
6+ import os
7+ import unittest
108from collections import namedtuple
119from datetime import datetime , timezone , timedelta
12- from oci . exceptions import ServiceError
10+ from importlib import reload
1311from unittest import mock
1412from unittest .mock import MagicMock , Mock , patch
13+
1514import oci
16- import os
1715import pandas as pd
1816import pytest
19- import unittest
17+ from oci .exceptions import ServiceError
18+
19+ import ads .config
20+ import ads .catalog .notebook
21+ from ads .catalog .notebook import NotebookCatalog , NotebookSummaryList
22+ from ads .common import auth , oci_client
23+ from ads .common .utils import random_valid_ocid
24+ from ads .config import PROJECT_OCID
2025
2126
2227def generate_notebook_list (
@@ -64,33 +69,57 @@ def generate_notebook_list(
6469class NotebookCatalogTest (unittest .TestCase ):
6570 """Contains test cases for catalog.notebook"""
6671
67- with patch .object (auth , "default_signer" ):
68- with patch .object (oci_client , "OCIClientFactory" ):
69- notebook_catalog = NotebookCatalog ()
70- notebook_catalog .ds_client = MagicMock ()
71- notebook_catalog .identity_client = MagicMock ()
72+ @classmethod
73+ def setUpClass (cls ) -> None :
74+ os .environ [
75+ "NB_SESSION_COMPARTMENT_OCID"
76+ ] = "ocid1.compartment.oc1.<unique_ocid>"
77+ reload (ads .config )
78+ ads .catalog .notebook .NB_SESSION_COMPARTMENT_OCID = (
79+ ads .config .NB_SESSION_COMPARTMENT_OCID
80+ )
81+ # Initialize class properties after reloading
82+ with patch .object (auth , "default_signer" ):
83+ with patch .object (oci_client , "OCIClientFactory" ):
84+ cls .notebook_id = "ocid1.notebookcatalog.oc1.iad.<unique_ocid>"
85+ cls .comp_id = os .environ .get (
86+ "NB_SESSION_COMPARTMENT_OCID" ,
87+ "ocid1.compartment.oc1.iad.<unique_ocid>" ,
88+ )
89+ cls .date_time = datetime (
90+ 2020 , 7 , 1 , 18 , 24 , 42 , 110000 , tzinfo = timezone .utc
91+ )
92+
93+ cls .notebook_catalog = NotebookCatalog (compartment_id = cls .comp_id )
94+ cls .notebook_catalog .ds_client = MagicMock ()
95+ cls .notebook_catalog .identity_client = MagicMock ()
7296
73- notebook_id = "ocid1.notebookcatalog.oc1.iad.<unique_ocid>"
74- comp_id = os .environ .get ("NB_SESSION_COMPARTMENT_OCID" )
75- date_time = datetime (2020 , 7 , 1 , 18 , 24 , 42 , 110000 , tzinfo = timezone .utc )
97+ cls .nsl = NotebookSummaryList (generate_notebook_list ())
98+ return super ().setUpClass ()
7699
77- nsl = NotebookSummaryList (generate_notebook_list ())
100+ @classmethod
101+ def tearDownClass (cls ) -> None :
102+ os .environ .pop ("NB_SESSION_COMPARTMENT_OCID" , None )
103+ reload (ads .config )
104+ ads .catalog .notebook .NB_SESSION_COMPARTMENT_OCID = (
105+ ads .config .NB_SESSION_COMPARTMENT_OCID
106+ )
107+ return super ().tearDownClass ()
78108
79109 @staticmethod
80- def generate_notebook_response_data (self , compartment_id = None , notebook_id = None ):
110+ def generate_notebook_response_data (compartment_id = None , notebook_id = None ):
81111 entity_item = {
82112 "compartment_id" : compartment_id ,
83113 "created_by" : "mock_user" ,
84114 "defined_tags" : {},
85115 "display_name" : "my new notebook catalog" ,
86116 "freeform_tags" : {},
87117 "id" : notebook_id ,
88- "lifecycle_state" : "" ,
89118 "lifecycle_state" : "ACTIVE" ,
90119 "notebook_session_configuration_details" : "" ,
91120 "notebook_session_url" : "oci://notebook_session_url@test_namespace" ,
92121 "project_id" : PROJECT_OCID ,
93- "time_created" : self .date_time .isoformat (),
122+ "time_created" : NotebookCatalogTest .date_time .isoformat (),
94123 }
95124 notebook_response = oci .data_science .models .NotebookSession (** entity_item )
96125 return notebook_response
@@ -107,12 +136,15 @@ def test_notebook_init_with_compartment_id(self, mock_client, mock_signer):
107136 def test_notebook_init_without_compartment_id (self , mock_client , mock_signer ):
108137 """Test notebook catalog initiation without compartment_id."""
109138 test_notebook_catalog = NotebookCatalog ()
110- assert test_notebook_catalog .compartment_id == NB_SESSION_COMPARTMENT_OCID
139+ assert (
140+ test_notebook_catalog .compartment_id
141+ == ads .config .NB_SESSION_COMPARTMENT_OCID
142+ )
111143
112144 def test_decorate_notebook_session_attributes (self ):
113145 """Test NotebookCatalog._decorate_notebook_session method."""
114146 notebook = self .generate_notebook_response_data (
115- self , compartment_id = self .comp_id , notebook_id = self .notebook_id
147+ compartment_id = self .comp_id , notebook_id = self .notebook_id
116148 )
117149
118150 def generate_get_user_data (self , compartment_id = None ):
@@ -173,7 +205,6 @@ def test_get_notebook_session_with_short_id(self):
173205 def mock_get_notebook_session (notebook_id = id ):
174206 return Mock (
175207 data = self .generate_notebook_response_data (
176- self ,
177208 compartment_id = self .comp_id ,
178209 notebook_id = short_id_index [short_id ],
179210 )
@@ -298,7 +329,7 @@ def test_update_notebook_session_with_short_id(self):
298329 wrapper = namedtuple ("wrapper" , ["data" ])
299330 client_update_notebook_session_response = wrapper (
300331 data = self .generate_notebook_response_data (
301- self , compartment_id = self .comp_id , notebook_id = short_id_index [short_id ]
332+ compartment_id = self .comp_id , notebook_id = short_id_index [short_id ]
302333 )
303334 )
304335 self .notebook_catalog .ds_client .update_notebook_session = MagicMock (
@@ -345,5 +376,5 @@ def test_notebook_summary_list_filter_invalid_param(self):
345376 # selection is a notebook session instance
346377 with pytest .raises (ValueError ):
347378 self .nsl .filter (
348- selection = self .generate_notebook_response_data (self ), instance = None
379+ selection = self .generate_notebook_response_data (), instance = None
349380 )
0 commit comments