3
3
# Copyright (c) 2021, 2023 Oracle and/or its affiliates.
4
4
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
5
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
10
8
from collections import namedtuple
11
9
from datetime import datetime , timezone , timedelta
12
- from oci . exceptions import ServiceError
10
+ from importlib import reload
13
11
from unittest import mock
14
12
from unittest .mock import MagicMock , Mock , patch
13
+
15
14
import oci
16
- import os
17
15
import pandas as pd
18
16
import 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
20
25
21
26
22
27
def generate_notebook_list (
@@ -64,33 +69,57 @@ def generate_notebook_list(
64
69
class NotebookCatalogTest (unittest .TestCase ):
65
70
"""Contains test cases for catalog.notebook"""
66
71
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 ()
72
96
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 ()
76
99
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 ()
78
108
79
109
@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 ):
81
111
entity_item = {
82
112
"compartment_id" : compartment_id ,
83
113
"created_by" : "mock_user" ,
84
114
"defined_tags" : {},
85
115
"display_name" : "my new notebook catalog" ,
86
116
"freeform_tags" : {},
87
117
"id" : notebook_id ,
88
- "lifecycle_state" : "" ,
89
118
"lifecycle_state" : "ACTIVE" ,
90
119
"notebook_session_configuration_details" : "" ,
91
120
"notebook_session_url" : "oci://notebook_session_url@test_namespace" ,
92
121
"project_id" : PROJECT_OCID ,
93
- "time_created" : self .date_time .isoformat (),
122
+ "time_created" : NotebookCatalogTest .date_time .isoformat (),
94
123
}
95
124
notebook_response = oci .data_science .models .NotebookSession (** entity_item )
96
125
return notebook_response
@@ -107,12 +136,15 @@ def test_notebook_init_with_compartment_id(self, mock_client, mock_signer):
107
136
def test_notebook_init_without_compartment_id (self , mock_client , mock_signer ):
108
137
"""Test notebook catalog initiation without compartment_id."""
109
138
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
+ )
111
143
112
144
def test_decorate_notebook_session_attributes (self ):
113
145
"""Test NotebookCatalog._decorate_notebook_session method."""
114
146
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
116
148
)
117
149
118
150
def generate_get_user_data (self , compartment_id = None ):
@@ -173,7 +205,6 @@ def test_get_notebook_session_with_short_id(self):
173
205
def mock_get_notebook_session (notebook_id = id ):
174
206
return Mock (
175
207
data = self .generate_notebook_response_data (
176
- self ,
177
208
compartment_id = self .comp_id ,
178
209
notebook_id = short_id_index [short_id ],
179
210
)
@@ -298,7 +329,7 @@ def test_update_notebook_session_with_short_id(self):
298
329
wrapper = namedtuple ("wrapper" , ["data" ])
299
330
client_update_notebook_session_response = wrapper (
300
331
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 ]
302
333
)
303
334
)
304
335
self .notebook_catalog .ds_client .update_notebook_session = MagicMock (
@@ -345,5 +376,5 @@ def test_notebook_summary_list_filter_invalid_param(self):
345
376
# selection is a notebook session instance
346
377
with pytest .raises (ValueError ):
347
378
self .nsl .filter (
348
- selection = self .generate_notebook_response_data (self ), instance = None
379
+ selection = self .generate_notebook_response_data (), instance = None
349
380
)
0 commit comments