Skip to content
Snippets Groups Projects
Commit e8770af6 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Removed tests that interact with _Session class, since no longer trying to use singleton

parent fa7ebaab
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ from base.models import Model
logger = logging.getLogger(__name__)
class Singleton(type):
_instances = {}
......@@ -37,7 +38,7 @@ class CsvTable(object):
self.empty = False
return True
class Session(object, metaclass=Singleton):
class _Session(object, metaclass=Singleton):
"""
Singleton class for a server session that persists data between API calls
"""
......@@ -99,7 +100,7 @@ class Session(object, metaclass=Singleton):
root_path = Path(conf.defaults.root)
else:
root_path = Path(root)
sid = Session.create_session_id(root_path)
sid = _Session.create_session_id(root_path)
paths = {'root': root_path}
for pk in ['inbound_images', 'outbound_images', 'logs', 'tables']:
pa = root_path / sid / conf.defaults.subdirectories[pk]
......@@ -194,7 +195,7 @@ class Session(object, metaclass=Singleton):
# create singleton instance
session = Session()
session = _Session()
class Error(Exception):
......
......@@ -3,31 +3,20 @@ import pathlib
from pydantic import BaseModel
import unittest
import base.session
from base.models import DummySemanticSegmentationModel
from base.session import session
class TestGetSessionObject(unittest.TestCase):
def setUp(self) -> None:
session.restart()
self.sesh = session
def tearDown(self) -> None:
print('Tearing down...')
session.__class__._instances = {}
def test_session_is_singleton(self):
session.__class__._instances = {}
self.assertEqual(len(session.__class__._instances), 0)
s = session.__class__()
self.assertEqual(len(session.__class__._instances), 1)
self.assertIs(s, session.__class__())
self.assertEqual(len(session.__class__._instances), 1)
def test_session_logfile_is_valid(self):
self.assertTrue(exists(self.sesh.logfile), 'Session did not create a log file in the correct place')
def test_changing_session_root_creates_new_directory(self):
from model_server.conf.defaults import root
from shutil import rmtree
old_paths = self.sesh.get_paths()
newroot = root / 'subdir'
......@@ -36,12 +25,6 @@ class TestGetSessionObject(unittest.TestCase):
for k in old_paths.keys():
self.assertTrue(new_paths[k].__str__().startswith(newroot.__str__()))
# this is necessary because logger itself is a singleton class
self.tearDown()
self.setUp()
rmtree(newroot)
self.assertFalse(newroot.exists(), 'Could not clean up temporary test subdirectory')
def test_change_session_subdirectory(self):
old_paths = self.sesh.get_paths()
print(old_paths)
......@@ -56,6 +39,15 @@ class TestGetSessionObject(unittest.TestCase):
self.assertTrue(logfile2.exists())
self.assertNotEqual(logfile1, logfile2, 'Restarting session does not generate new logfile')
def test_reimporting_session_uses_same_logfile(self):
logfile1 = self.sesh.logfile
self.assertTrue(logfile1.exists())
session2 = base.session.session
logfile2 = session2.logfile
self.assertTrue(logfile2.exists())
self.assertEqual(logfile1, logfile2, 'Reimporting session incorrectly creates new logfile')
def test_log_warning(self):
msg = 'A test warning'
self.sesh.log_info(msg)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment