예제 #1
0
    def test_reload_config_not_load_settings_if_config_is_missing(self):
        os.remove(CLIMan.config_path())

        self.climan.reload_config()
        self.assertEqual(self.climan.DEFAULT_DOWNLOAD_DIR,
                         CLIMan.DEFAULT_DOWNLOAD_DIR)
        self.assertEqual(self.climan.DEFAULT_MAX_RECENT_THREADS,
                         CLIMan.DEFAULT_MAX_RECENT_THREADS)
        self.assertEqual(self.climan.recent_threads, [])
예제 #2
0
class CLIManSyncRecentTestCase(unittest.TestCase):
    """Test sync-recent sub-command"""
    def setUp(self):
        self.dead_thread_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        self.alive_thread_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        self.config_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        CLIMan.CONFIG_DIR = self.config_dir.name
        CLIMan.DEBUG = True
        self.climan = CLIMan()
        self.input_format = '{subcmd} {dir}'
        self.cli_input = CLIMan.COMMAND_SYNC_RECENT

        create_test_environment(
            self.dead_thread_dir.name,
            url=TEST_THREAD_FILENAME)  # Creates a dead thread
        # Pretend that the dead thread is still alive
        create_test_environment(self.alive_thread_dir.name,
                                url=STICKY_THREAD_URL)
        self.climan.recent_threads += [
            self.dead_thread_dir.name, self.dead_thread_dir.name,
            self.dead_thread_dir.name, self.dead_thread_dir.name,
            self.alive_thread_dir.name
        ]
        self.climan.save_config()  # For loading purposes later

    def test_correct_default_function(self):
        args = self.climan.parse_string(self.cli_input)
        self.assertEqual(args.func, self.climan.cli_synchronise_recent_threads)

    def test_sync_recent(self):
        cli_input = CLIMan.COMMAND_SYNC_RECENT
        args = self.climan.parse_string(self.cli_input)
        args.func(args)
        self.assertNotIn(self.dead_thread_dir.name, self.climan.recent_threads)
        self.assertIn(os.path.abspath(self.alive_thread_dir.name),
                      self.climan.recent_threads)

    def test_sync_recent_update_config(self):
        args = self.climan.parse_string(self.cli_input)
        args.func(args)
        config_dict = utilities.json_from_path(self.climan.config_path())
        self.assertNotIn(self.dead_thread_dir.name,
                         config_dict[CLIMan.OPTION_RECENT_THREADS])
        self.assertIn(os.path.abspath(self.alive_thread_dir.name),
                      config_dict[CLIMan.OPTION_RECENT_THREADS])

    def test_recent_list_with_missing_threads(self):
        """For when the recent_list contains a thread folder that has already been deleted from the hard drive."""
        dead = self.dead_thread_dir.name
        del self.dead_thread_dir
        self.assertFalse(os.path.exists(dead))
        args = self.climan.parse_string(CLIMan.COMMAND_SYNC_RECENT)
        args.func(args)
        self.assertNotIn(dead, self.climan.recent_threads)
        '''All thread folders that no longer exist should be removed from the list'''
예제 #3
0
class CLIManDownloadSubCommandTestCase(unittest.TestCase):
    def setUp(self):
        self.download_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        self.config_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        CLIMan.CONFIG_DIR = self.config_dir.name
        CLIMan.DEBUG = True
        self.climan = CLIMan()
        self.input_format = '{subcmd} {url} -d {dir}'
        self.cli_input = self.input_format.format(
            subcmd=CLIMan.COMMAND_DOWNLOAD,
            url=THREAD_URL,
            dir=self.download_dir.name)
        self.climan.save_config()

    def test_download_to_directory_sub_command(self):
        args = self.climan.parse_string(self.cli_input)
        # print('type of args:', type(args))

        self.assertEqual(args.func, self.climan.cli_download_to_directory)
        downloaded_dir = args.func(args)

        thread_details_path = os.path.join(
            downloaded_dir, BatchDownloader.THREAD_DETAILS_FILENAME)
        self.assertTrue(os.path.exists(thread_details_path))

    def test_download_to_directory_added_to_recent_threads(self):
        args = self.climan.parse_string(self.cli_input)
        thread_dir = args.func(args)
        self.assertIn(thread_dir, self.climan.recent_threads)

    def test_dead_thread_added_to_recent(self):
        """Dead threads should not be added to recent threads variable"""
        cli_input = self.input_format.format(subcmd=CLIMan.COMMAND_DOWNLOAD,
                                             url=TEST_THREAD_FILENAME,
                                             dir=self.download_dir.name)
        args = self.climan.parse_string(cli_input)
        thread_dir = args.func(args)
        self.assertNotIn(thread_dir, self.climan.recent_threads)

    def test_config_updated_after_download(self):
        cli_input = self.input_format.format(subcmd=CLIMan.COMMAND_DOWNLOAD,
                                             url=STICKY_THREAD_URL,
                                             dir=self.download_dir.name)
        args = self.climan.parse_string(cli_input)
        thread_dir = args.func(args)
        config_dict = utilities.json_from_path(self.climan.config_path())
        self.assertIn(thread_dir, config_dict[CLIMan.OPTION_RECENT_THREADS])
예제 #4
0
class CLIManSyncDirSubCommandTestCase(unittest.TestCase):
    def setUp(self):
        self.thread_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        self.config_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        CLIMan.CONFIG_DIR = self.config_dir.name
        CLIMan.DEBUG = True
        self.climan = CLIMan()
        self.input_format = '{subcmd} {dir}'
        self.cli_input = self.input_format.format(
            subcmd=CLIMan.COMMAND_SYNC_DIR, dir=self.thread_dir.name)
        self.climan.save_config()

        create_test_environment(self.thread_dir.name)

    def test_synchronise_directory(self):
        args = self.climan.parse_string(self.cli_input)
        self.assertEqual(args.func, self.climan.cli_synchronise_to_directory)
        downloaded_dir = args.func(args)

        # Check details pickle existence
        thread_details_path = os.path.join(
            downloaded_dir, BatchDownloader.THREAD_DETAILS_FILENAME)
        self.assertTrue(os.path.exists(thread_details_path))

    def test_synchronise_directory_added_to_recent_threads(self):
        create_test_environment(self.thread_dir.name, url=STICKY_THREAD_URL)
        args = self.climan.parse_string(self.cli_input)
        thread_dir = args.func(args)
        self.assertIn(thread_dir, self.climan.recent_threads)

    def test_dead_thread_added_to_recent(self):
        """Dead threads should not be added to recent threads variable"""
        args = self.climan.parse_string(self.cli_input)
        thread_dir = args.func(args)
        self.assertNotIn(thread_dir, self.climan.recent_threads)

    def test_config_updated_after_download(self):
        create_test_environment(self.thread_dir.name, url=STICKY_THREAD_URL)

        args = self.climan.parse_string(self.cli_input)
        thread_dir = args.func(args)
        config_dict = utilities.json_from_path(self.climan.config_path())
        self.assertIn(thread_dir, config_dict[CLIMan.OPTION_RECENT_THREADS])
예제 #5
0
class CLIManLoadSetSaveConfig(unittest.TestCase):
    def setUp(self):
        self.temp_config_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        self.temp_download_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        CLIMan.CONFIG_DIR = self.temp_config_dir.name
        CLIMan.DEFAULT_DOWNLOAD_DIR = self.temp_download_dir.name
        self.climan = CLIMan()

        # Create a config file at temp_config_dir
        self.climan.save_config()

    def test_config_path(self):
        self.assertEqual(CLIMan.config_path(),
                         os.path.join(CLIMan.CONFIG_DIR, CLIMan.CONFIG_NAME))

    def test_save_config_file_exists(self):
        self.climan.save_config()
        self.assertTrue(os.path.exists(self.climan.config_path()))

    def test_load_json_dict(self):
        """Check if load_json has the right contents stored in it"""
        config_dict = utilities.json_from_path(CLIMan.config_path())
        self.assertIsNotNone(config_dict)
        self.assertEqual(set(CLIMan.SETTINGS), set(config_dict.keys()))

    def test_set_config(self):
        # Constants to replace climan instance's option variables to demonstrate set_config
        ddir = '~/Downloads/t0/'
        maxthreads = 123
        recent_threads = ['~/Downloads/t1', '~/Downloads/t2', '~/Downloads/t3']

        self.climan.DEFAULT_DOWNLOAD_DIR = ddir
        self.climan.DEFAULT_MAX_RECENT_THREADS = maxthreads
        self.climan.recent_threads = recent_threads

        self.climan.configure(
            utilities.json_from_path(self.climan.config_path()))

        self.assertIsNotNone(self.climan.DEFAULT_DOWNLOAD_DIR)
        self.assertIsNotNone(self.climan.DEFAULT_MAX_RECENT_THREADS)
        self.assertIsNotNone(self.climan.recent_threads)

        self.assertNotEqual(self.climan.DEFAULT_DOWNLOAD_DIR, ddir)
        self.assertNotEqual(self.climan.DEFAULT_MAX_RECENT_THREADS, maxthreads)
        self.assertNotEqual(self.climan.recent_threads, recent_threads)

    def test_set_config_with_missing_keys(self):
        """Make sure that CLIMan's configure function raises KeyError when there are missing keys"""
        config_dict = {'ABC': 123, 'DEF': 'GHI'}
        with self.assertRaises(KeyError):
            self.climan.configure(config_dict)

    def test_reload_config_not_load_settings_if_config_is_missing(self):
        os.remove(CLIMan.config_path())

        self.climan.reload_config()
        self.assertEqual(self.climan.DEFAULT_DOWNLOAD_DIR,
                         CLIMan.DEFAULT_DOWNLOAD_DIR)
        self.assertEqual(self.climan.DEFAULT_MAX_RECENT_THREADS,
                         CLIMan.DEFAULT_MAX_RECENT_THREADS)
        self.assertEqual(self.climan.recent_threads, [])
예제 #6
0
class CLIManSettingSubCommandTestCase(unittest.TestCase):
    def setUp(self):
        self.config_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        CLIMan.CONFIG_DIR = self.config_dir.name
        CLIMan.DEBUG = True
        self.climan = CLIMan()
        self.set_format = '{sub} {opt} {val}'
        self.get_format = '{sub} {opt}'

    def test_subcommand_with_option_name_and_value(self):
        '''This test has already been done before'''
        option = CLIMan.OPTION_MAX_RECENT_THREADS
        new_value = 100
        cli_input = self.set_format.format(sub=CLIMan.COMMAND_SETTING,
                                           opt=option,
                                           val=new_value)
        args = self.climan.parse_string(cli_input)
        self.assertEqual(args.option_name, CLIMan.OPTION_MAX_RECENT_THREADS)
        self.assertEqual(int(args.option_value), new_value)

    def test_correct_function_returned(self):
        cli_input = self.set_format.format(
            sub=CLIMan.COMMAND_SETTING,
            opt=CLIMan.OPTION_MAX_RECENT_THREADS,
            val=100)
        args = self.climan.parse_string(cli_input)
        self.assertEqual(args.func, self.climan.cli_settings)

    def test_option_set_max_recent_threads(self):
        cli_input = self.set_format.format(
            sub=CLIMan.COMMAND_SETTING,
            opt=CLIMan.OPTION_MAX_RECENT_THREADS,
            val=100)
        args = self.climan.parse_string(cli_input)
        args.func(args)
        self.assertEqual(self.climan.DEFAULT_MAX_RECENT_THREADS,
                         int(args.option_value))

    def test_option_get_max_recent_threads(self):
        cli_input = self.get_format.format(
            sub=CLIMan.COMMAND_SETTING, opt=CLIMan.OPTION_MAX_RECENT_THREADS)
        args = self.climan.parse_string(cli_input)
        new_max = args.func(args)
        self.assertEqual(new_max, self.climan.DEFAULT_MAX_RECENT_THREADS)

    def test_option_set_default_download_dir(self):
        new_download_dir = TemporaryDirectory(dir=TMP_DIRECTORY)
        cli_input = self.set_format.format(sub=CLIMan.COMMAND_SETTING,
                                           opt=CLIMan.OPTION_DOWNLOAD_DIR,
                                           val=new_download_dir.name)
        args = self.climan.parse_string(cli_input)
        args.func(args)
        self.assertEqual(self.climan.DEFAULT_DOWNLOAD_DIR,
                         new_download_dir.name)

    def test_option_get_default_download_dir(self):
        cli_input = self.get_format.format(sub=CLIMan.COMMAND_SETTING,
                                           opt=CLIMan.OPTION_DOWNLOAD_DIR)
        args = self.climan.parse_string(cli_input)
        download_dir = args.func(args)
        self.assertEqual(download_dir, self.climan.DEFAULT_DOWNLOAD_DIR)

    def test_new_settings_get_saved(self):
        self.climan.save_config()  # Force create a new config

        cli_input = self.set_format.format(
            sub=CLIMan.COMMAND_SETTING,
            opt=CLIMan.OPTION_MAX_RECENT_THREADS,
            val=100)
        args = self.climan.parse_string(cli_input)
        args.func(args)  # internally, change some properties

        # Load the settings and compare
        config_dict = utilities.json_from_path(self.climan.config_path())
        self.assertEqual(config_dict[CLIMan.OPTION_MAX_RECENT_THREADS],
                         int(args.option_value))
예제 #7
0
 def test_load_json_dict(self):
     """Check if load_json has the right contents stored in it"""
     config_dict = utilities.json_from_path(CLIMan.config_path())
     self.assertIsNotNone(config_dict)
     self.assertEqual(set(CLIMan.SETTINGS), set(config_dict.keys()))
예제 #8
0
 def test_config_path(self):
     self.assertEqual(CLIMan.config_path(),
                      os.path.join(CLIMan.CONFIG_DIR, CLIMan.CONFIG_NAME))