def delete(self, **kwargs): self.session_controller = SessionController() name_or_id = kwargs.get('name_or_id') result = False try: result = self.session_controller.delete_by_name(name_or_id) except SessionDoesNotExist: pass except InvalidOperation: self.cli_helper.echo(__("error", "cli.session.delete.default")) return False try: result = self.session_controller.delete(name_or_id) except SessionDoesNotExist: pass except InvalidOperation: self.cli_helper.echo(__("error", "cli.session.delete.default")) return False if result: self.cli_helper.echo(__("info", "cli.session.delete", name_or_id)) return True else: self.cli_helper.echo( __("error", "cli.session.delete.dne", name_or_id)) return False
def ls(self, **kwargs): self.session_controller = SessionController() print_format = kwargs.get('format', "table") download = kwargs.get('download', None) download_path = kwargs.get('download_path', None) sessions = self.session_controller.list(sort_key="created_at", sort_order="descending") header_list = [ "id", "created at", "name", "selected", "tasks", "snapshots" ] item_dict_list = [] for session_obj in sessions: snapshot_count = len( self.session_controller.dal.snapshot.query({ "session_id": session_obj.id, "model_id": self.session_controller.model.id })) task_count = len( self.session_controller.dal.task.query({ "session_id": session_obj.id, "model_id": self.session_controller.model.id })) item_dict_list.append({ "id": session_obj.id, "created at": prettify_datetime(session_obj.created_at), "name": printable_object(session_obj.name), "selected": printable_object(session_obj.current), "tasks": printable_object(task_count), "snapshots": printable_object(snapshot_count) }) if download: if not download_path: # download to current working directory with timestamp current_time = datetime.utcnow() epoch_time = datetime.utcfromtimestamp(0) current_time_unix_time_ms = ( current_time - epoch_time).total_seconds() * 1000.0 download_path = os.path.join( self.session_controller.home, "session_ls_" + str(current_time_unix_time_ms)) self.cli_helper.print_items(header_list, item_dict_list, print_format=print_format, output_path=download_path) return sessions self.cli_helper.print_items(header_list, item_dict_list, print_format=print_format) return sessions
def setup_method(self): # provide mountable tmp directory for docker tempfile.tempdir = '/tmp' test_datmo_dir = os.environ.get('TEST_DATMO_DIR', tempfile.gettempdir()) self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir) self.project = ProjectController(self.temp_dir) self.project.init("test", "test description") self.session = SessionController(self.temp_dir)
class SessionCommand(ProjectCommand): def __init__(self, home, cli_helper): super(SessionCommand, self).__init__(home, cli_helper) # dest="subcommand" argument will populate a "subcommand" property with the subparsers name # example "subcommand"="create" or "subcommand"="ls" self.session_controller = SessionController(home=home) def session(self): self.parse(["--help"]) return True def create(self, **kwargs): name = kwargs.get('name') self.session_controller.create(kwargs) self.cli_helper.echo(__("info", "cli.session.create", name)) return True def delete(self, **kwargs): name = kwargs.get('name') if self.session_controller.delete_by_name(name): self.cli_helper.echo(__("info", "cli.session.delete", name)) return True def select(self, **kwargs): name = kwargs.get("name") self.cli_helper.echo(__("info", "cli.session.select", name)) return self.session_controller.select(name) def ls(self, **kwargs): sessions = self.session_controller.list(sort_key="created_at", sort_order="descending") header_list = ["name", "selected", "tasks", "snapshots"] t = prettytable.PrettyTable(header_list) for sess in sessions: snapshot_count = len( self.session_controller.dal.snapshot.query({ "session_id": sess.id, "model_id": self.session_controller.model.id })) task_count = len( self.session_controller.dal.task.query({ "session_id": sess.id, "model_id": self.session_controller.model.id })) t.add_row([sess.name, sess.current, task_count, snapshot_count]) self.cli_helper.echo(t) return True
def select(self, **kwargs): self.session_controller = SessionController() name_or_id = kwargs.get("name_or_id") result = False try: result = self.session_controller.select(name_or_id) except SessionDoesNotExist: pass if result: self.cli_helper.echo(__("info", "cli.session.select", name_or_id)) return True else: self.cli_helper.echo( __("error", "cli.session.select.dne", name_or_id)) return False
def update(self, **kwargs): self.session_controller = SessionController() id = kwargs.get('id') name = kwargs.get('name', None) result = False try: result = self.session_controller.update(id, name=name) except SessionDoesNotExist: pass if result: self.cli_helper.echo(__("info", "cli.session.update", id)) return True else: self.cli_helper.echo(__("error", "cli.session.update.dne", id)) return False
def test_init_fail_project_not_init(self): failed = False try: SessionController(self.temp_dir) except ProjectNotInitializedException: failed = True assert failed
def test_init_fail_invalid_path(self): test_home = "some_random_dir" failed = False try: SessionController(test_home) except InvalidProjectPathException: failed = True assert failed
def test_init_fail_project_not_init(self): Config().set_home(self.temp_dir) failed = False try: SessionController() except ProjectNotInitialized: failed = True assert failed
def __init__(self, home, cli_helper): super(SessionCommand, self).__init__(home, cli_helper) # dest="subcommand" argument will populate a "subcommand" property with the subparsers name # example "subcommand"="create" or "subcommand"="ls" snapshot_parser = self.subparsers.add_parser("session", help="Session module") subcommand_parsers = snapshot_parser.add_subparsers( title="subcommands", dest="subcommand") create = subcommand_parsers.add_parser("create", help="Create session") create.add_argument("--name", "-m", dest="name", default="", help="Session name") create.add_argument( "--current", dest="current", action="store_false", help="Boolean if you want to switch to this session") delete = subcommand_parsers.add_parser("delete", help="Delete a snapshot by id") delete.add_argument("--name", dest="name", help="Name of session to delete") ls = subcommand_parsers.add_parser("ls", help="List sessions") checkout = subcommand_parsers.add_parser("select", help="Select a session") checkout.add_argument("--name", dest="name", help="Name of session to select") self.session_controller = SessionController(home=home)
class TestSessionController(): def setup_method(self): # provide mountable tmp directory for docker tempfile.tempdir = "/tmp" if not platform.system( ) == "Windows" else None test_datmo_dir = os.environ.get('TEST_DATMO_DIR', tempfile.gettempdir()) self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir) def teardown_method(self): shutil.rmtree(self.temp_dir) def __setup(self): Config().set_home(self.temp_dir) self.project_controller = ProjectController() self.project_controller.init("test", "test description") self.session_controller = SessionController() def test_init_fail_project_not_init(self): Config().set_home(self.temp_dir) failed = False try: SessionController() except ProjectNotInitialized: failed = True assert failed def test_init_fail_invalid_path(self): test_home = "some_random_dir" Config().set_home(test_home) failed = False try: SessionController() except InvalidProjectPath: failed = True assert failed def test_find_default_session(self): self.__setup() sessions = self.session_controller.list() has_default = False for s in sessions: if s.name == 'default': has_default = True break assert has_default def test_create_session(self): self.__setup() test_sess = self.session_controller.create({"name": "test1"}) assert test_sess.id assert test_sess.model_id == self.project_controller.model.id def test_select(self): self.__setup() session_obj = self.session_controller.create({"name": "test2"}) # Test success with name new_current = self.session_controller.select("test2") assert new_current.current == True found_old_current = False current_was_updated = False for sess in self.session_controller.list(): if sess.current == True and sess.id != new_current.id: found_old_current = True if sess.current == True and sess.id == new_current.id: current_was_updated = True assert not found_old_current == True assert current_was_updated == True # reset to default _ = self.session_controller.select("default") # Test success with id new_current = self.session_controller.select(session_obj.id) assert new_current.current == True found_old_current = False current_was_updated = False for sess in self.session_controller.list(): if sess.current == True and sess.id != new_current.id: found_old_current = True if sess.current == True and sess.id == new_current.id: current_was_updated = True assert not found_old_current == True assert current_was_updated == True # Test failure no session failed = False try: _ = self.session_controller.select("random_name_or_id") except SessionDoesNotExist: failed = True assert failed def test_get_current(self): self.__setup() current_sess = self.session_controller.get_current() assert current_sess assert current_sess.current == True def test_list_session_sort(self): self.__setup() # Sort ascending sessions = self.session_controller.list( sort_key='created_at', sort_order='ascending') assert sessions[0].created_at <= sessions[-1].created_at # Sort descending sessions = self.session_controller.list( sort_key='created_at', sort_order='descending') assert sessions[0].created_at >= sessions[-1].created_at # Wrong order being passed in failed = False try: _ = self.session_controller.list( sort_key='created_at', sort_order='wrong_order') except InvalidArgumentType: failed = True assert failed # Wrong key and order being passed in failed = False try: _ = self.session_controller.list( sort_key='wrong_key', sort_order='wrong_order') except InvalidArgumentType: failed = True assert failed # wrong key and right order being passed in expected_sessions = self.session_controller.list( sort_key='created_at', sort_order='ascending') sessions = self.session_controller.list( sort_key='wrong_key', sort_order='ascending') expected_ids = [item.id for item in expected_sessions] ids = [item.id for item in sessions] assert set(expected_ids) == set(ids) def test_update(self): self.__setup() # Test successful update session_obj = self.session_controller.create({"name": "test3"}) new_name = "new" updated_session_obj = self.session_controller.update( session_obj.id, name=new_name) same_session_obj = self.session_controller.dal.session.findOne({ "name": new_name }) assert updated_session_obj == same_session_obj assert updated_session_obj.name == new_name # Test failure session does not exist failed = False try: self.session_controller.update("random_id") except SessionDoesNotExist: failed = True assert failed # Test failure try to update default default_session_obj = self.session_controller.dal.session.findOne({ "name": "default" }) failed = False try: self.session_controller.update(default_session_obj.id) except InvalidOperation: failed = True assert failed def test_delete(self): self.__setup() # Test successful delete session_obj = self.session_controller.create({"name": "test3"}) _ = self.session_controller.select("test3") self.session_controller.delete(session_obj.id) entity_does_not_exist = False try: _ = self.session_controller.dal.session.get_by_id(session_obj.id) except EntityNotFound: entity_does_not_exist = True assert entity_does_not_exist # current session should be "default" assert self.session_controller.get_current().name == "default" # Test failure delete default failed = False try: self.session_controller.delete( self.session_controller.get_current().id) except InvalidOperation: failed = True assert failed # Test failure does not exist failed = False try: self.session_controller.delete("random_id") except SessionDoesNotExist: failed = True assert failed def test_delete_by_name(self): self.__setup() session_obj = self.session_controller.create({"name": "test3"}) _ = self.session_controller.select("test3") self.session_controller.delete_by_name("test3") entity_does_not_exist = False try: _ = self.session_controller.dal.session.get_by_id(session_obj.id) except EntityNotFound: entity_does_not_exist = True assert entity_does_not_exist # current session should be "default" assert self.session_controller.get_current().name == "default" # Test failure delete default failed = False try: self.session_controller.delete_by_name( self.session_controller.get_current().name) except InvalidOperation: failed = True assert failed # Test failure does not exist failed = False try: self.session_controller.delete_by_name("random_name") except SessionDoesNotExist: failed = True assert failed
def __setup(self): self.project = ProjectController(self.temp_dir) self.project.init("test", "test description") self.session = SessionController(self.temp_dir)
def __init__(self, home, cli_helper): super(SessionCommand, self).__init__(home, cli_helper) # dest="subcommand" argument will populate a "subcommand" property with the subparsers name # example "subcommand"="create" or "subcommand"="ls" self.session_controller = SessionController(home=home)
class TestSessionController(): def setup_method(self): # provide mountable tmp directory for docker tempfile.tempdir = '/tmp' test_datmo_dir = os.environ.get('TEST_DATMO_DIR', tempfile.gettempdir()) self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir) self.project = ProjectController(self.temp_dir) self.project.init("test", "test description") self.session = SessionController(self.temp_dir) def teardown_method(self): shutil.rmtree(self.temp_dir) def test_find_default_session(self): sessions = self.session.list() has_default = False for s in sessions: if s.name == 'default': has_default = True break assert has_default def test_create_session(self): test_sess = self.session.create({"name": "test1"}) assert test_sess.id assert test_sess.model_id == self.project.model.id def test_select_session(self): self.session.create({"name": "test2"}) new_current = self.session.select("test2") assert new_current.current == True found_old_current = False current_was_updated = False for s in self.session.list(): if s.current == True and s.id != new_current.id: found_old_current = True if s.current == True and s.id == new_current.id: current_was_updated = True assert not found_old_current == True assert current_was_updated == True def test_get_current(self): current_sess = self.session.get_current() assert current_sess assert current_sess.current == True def test_delete_session(self): self.session.create({"name": "test3"}) new_current = self.session.select("test3") self.session.delete_by_name("test3") entity_exists = False try: session_still_exists = self.session.findOne({"name": "test3"}) entity_exists = True except: pass assert not entity_exists # current session should be "default" assert self.session.get_current().name == 'default'
def __setup(self): Config().set_home(self.temp_dir) self.project_controller = ProjectController() self.project_controller.init("test", "test description") self.session_controller = SessionController()
class SessionCommand(ProjectCommand): def __init__(self, cli_helper): super(SessionCommand, self).__init__(cli_helper) def session(self): self.parse(["session", "--help"]) return True @Helper.notify_no_project_found def create(self, **kwargs): self.session_controller = SessionController() name = kwargs.get('name') session_obj = self.session_controller.create(kwargs) self.cli_helper.echo(__("info", "cli.session.create", name)) return session_obj @Helper.notify_no_project_found def update(self, **kwargs): self.session_controller = SessionController() id = kwargs.get('id') name = kwargs.get('name', None) result = False try: result = self.session_controller.update(id, name=name) except SessionDoesNotExist: pass if result: self.cli_helper.echo(__("info", "cli.session.update", id)) return True else: self.cli_helper.echo(__("error", "cli.session.update.dne", id)) return False @Helper.notify_no_project_found def delete(self, **kwargs): self.session_controller = SessionController() name_or_id = kwargs.get('name_or_id') result = False try: result = self.session_controller.delete_by_name(name_or_id) except SessionDoesNotExist: pass except InvalidOperation: self.cli_helper.echo(__("error", "cli.session.delete.default")) return False try: result = self.session_controller.delete(name_or_id) except SessionDoesNotExist: pass except InvalidOperation: self.cli_helper.echo(__("error", "cli.session.delete.default")) return False if result: self.cli_helper.echo(__("info", "cli.session.delete", name_or_id)) return True else: self.cli_helper.echo( __("error", "cli.session.delete.dne", name_or_id)) return False @Helper.notify_no_project_found def select(self, **kwargs): self.session_controller = SessionController() name_or_id = kwargs.get("name_or_id") result = False try: result = self.session_controller.select(name_or_id) except SessionDoesNotExist: pass if result: self.cli_helper.echo(__("info", "cli.session.select", name_or_id)) return True else: self.cli_helper.echo( __("error", "cli.session.select.dne", name_or_id)) return False @Helper.notify_no_project_found def ls(self, **kwargs): self.session_controller = SessionController() print_format = kwargs.get('format', "table") download = kwargs.get('download', None) download_path = kwargs.get('download_path', None) sessions = self.session_controller.list(sort_key="created_at", sort_order="descending") header_list = [ "id", "created at", "name", "selected", "tasks", "snapshots" ] item_dict_list = [] for session_obj in sessions: snapshot_count = len( self.session_controller.dal.snapshot.query({ "session_id": session_obj.id, "model_id": self.session_controller.model.id })) task_count = len( self.session_controller.dal.task.query({ "session_id": session_obj.id, "model_id": self.session_controller.model.id })) item_dict_list.append({ "id": session_obj.id, "created at": prettify_datetime(session_obj.created_at), "name": printable_object(session_obj.name), "selected": printable_object(session_obj.current), "tasks": printable_object(task_count), "snapshots": printable_object(snapshot_count) }) if download: if not download_path: # download to current working directory with timestamp current_time = datetime.utcnow() epoch_time = datetime.utcfromtimestamp(0) current_time_unix_time_ms = ( current_time - epoch_time).total_seconds() * 1000.0 download_path = os.path.join( self.session_controller.home, "session_ls_" + str(current_time_unix_time_ms)) self.cli_helper.print_items(header_list, item_dict_list, print_format=print_format, output_path=download_path) return sessions self.cli_helper.print_items(header_list, item_dict_list, print_format=print_format) return sessions
def create(self, **kwargs): self.session_controller = SessionController() name = kwargs.get('name') session_obj = self.session_controller.create(kwargs) self.cli_helper.echo(__("info", "cli.session.create", name)) return session_obj
class SessionCommand(ProjectCommand): def __init__(self, home, cli_helper): super(SessionCommand, self).__init__(home, cli_helper) # dest="subcommand" argument will populate a "subcommand" property with the subparsers name # example "subcommand"="create" or "subcommand"="ls" snapshot_parser = self.subparsers.add_parser("session", help="Session module") subcommand_parsers = snapshot_parser.add_subparsers( title="subcommands", dest="subcommand") create = subcommand_parsers.add_parser("create", help="Create session") create.add_argument("--name", "-m", dest="name", default="", help="Session name") create.add_argument( "--current", dest="current", action="store_false", help="Boolean if you want to switch to this session") delete = subcommand_parsers.add_parser("delete", help="Delete a snapshot by id") delete.add_argument("--name", dest="name", help="Name of session to delete") ls = subcommand_parsers.add_parser("ls", help="List sessions") checkout = subcommand_parsers.add_parser("select", help="Select a session") checkout.add_argument("--name", dest="name", help="Name of session to select") self.session_controller = SessionController(home=home) def create(self, **kwargs): name = kwargs.get('name') self.session_controller.create(kwargs) self.cli_helper.echo(__("info", "cli.session.create", name)) return True def delete(self, **kwargs): name = kwargs.get('name') if self.session_controller.delete_by_name(name): self.cli_helper.echo(__("info", "cli.session.delete", name)) return True def select(self, **kwargs): name = kwargs.get("name") self.cli_helper.echo(__("info", "cli.session.select", name)) return self.session_controller.select(name) def ls(self, **kwargs): sessions = self.session_controller.list() header_list = ["name", "selected", "tasks", "snapshots"] t = prettytable.PrettyTable(header_list) for sess in sessions: snapshot_count = len( self.session_controller.dal.snapshot.query({ "session_id": sess.id, "model_id": self.session_controller.model.id })) task_count = len( self.session_controller.dal.task.query({ "session_id": sess.id, "model_id": self.session_controller.model.id })) t.add_row([sess.name, sess.current, task_count, snapshot_count]) self.cli_helper.echo(t) return True
class TestSessionController(): def setup_method(self): # provide mountable tmp directory for docker tempfile.tempdir = '/tmp' test_datmo_dir = os.environ.get('TEST_DATMO_DIR', tempfile.gettempdir()) self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir) def teardown_method(self): shutil.rmtree(self.temp_dir) def __setup(self): self.project = ProjectController(self.temp_dir) self.project.init("test", "test description") self.session = SessionController(self.temp_dir) def test_init_fail_project_not_init(self): failed = False try: SessionController(self.temp_dir) except ProjectNotInitializedException: failed = True assert failed def test_init_fail_invalid_path(self): test_home = "some_random_dir" failed = False try: SessionController(test_home) except InvalidProjectPathException: failed = True assert failed def test_find_default_session(self): self.__setup() sessions = self.session.list() has_default = False for s in sessions: if s.name == 'default': has_default = True break assert has_default def test_create_session(self): self.__setup() test_sess = self.session.create({"name": "test1"}) assert test_sess.id assert test_sess.model_id == self.project.model.id def test_select_session(self): self.__setup() self.session.create({"name": "test2"}) new_current = self.session.select("test2") assert new_current.current == True found_old_current = False current_was_updated = False for s in self.session.list(): if s.current == True and s.id != new_current.id: found_old_current = True if s.current == True and s.id == new_current.id: current_was_updated = True assert not found_old_current == True assert current_was_updated == True def test_get_current(self): self.__setup() current_sess = self.session.get_current() assert current_sess assert current_sess.current == True def test_list_session_sort(self): self.__setup() # Sort ascending sessions = self.session.list(sort_key='created_at', sort_order='ascending') assert sessions[0].created_at <= sessions[-1].created_at # Sort descending sessions = self.session.list(sort_key='created_at', sort_order='descending') assert sessions[0].created_at >= sessions[-1].created_at # Wrong order being passed in failed = False try: _ = self.session.list(sort_key='created_at', sort_order='wrong_order') except InvalidArgumentType: failed = True assert failed # Wrong key and order being passed in failed = False try: _ = self.session.list(sort_key='wrong_key', sort_order='wrong_order') except InvalidArgumentType: failed = True assert failed # wrong key and right order being passed in expected_sessions = self.session.list(sort_key='created_at', sort_order='ascending') sessions = self.session.list(sort_key='wrong_key', sort_order='ascending') expected_ids = [item.id for item in expected_sessions] ids = [item.id for item in sessions] assert set(expected_ids) == set(ids) def test_delete_session(self): self.__setup() self.session.create({"name": "test3"}) _ = self.session.select("test3") self.session.delete_by_name("test3") entity_exists = False try: _ = self.session.findOne({"name": "test3"}) entity_exists = True except: pass assert not entity_exists # current session should be "default" assert self.session.get_current().name == 'default'