예제 #1
0
    def _run_command(self,
                     org,
                     other_names=None,
                     only_courses=None,
                     query_interval=None,
                     chunk_size=None):
        """Execute the management command to generate the email opt-in list.

        Arguments:
            org (unicode): The org to generate the report for.

        Keyword Arguments:
            other_names (list): List of other aliases for the org.
            only_courses (list): If provided, include only these course IDs in the report.
            query_interval (int): If provided, override the default query interval.
            chunk_size (int): If provided, overrides the default number of chunks for query iteration.

        Returns:
            list: The rows of the generated CSV report.  Each item is a dictionary.

        """
        # Create a temporary directory for the output
        # Delete it when we're finished
        temp_dir_path = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, temp_dir_path)

        # Sanitize the arguments
        if other_names is None:
            other_names = []

        output_path = os.path.join(temp_dir_path, self.OUTPUT_FILE_NAME)
        org_list = [org] + other_names
        if only_courses is not None:
            only_courses = ",".join(
                text_type(course_id) for course_id in only_courses)

        command = email_opt_in_list.Command()

        # Override the query interval to speed up the tests
        if query_interval is not None:
            command.QUERY_INTERVAL = query_interval

        # Execute the command
        kwargs = {'courses': only_courses}
        if chunk_size:
            kwargs['email_optin_chunk_size'] = chunk_size
        call_command('email_opt_in_list', output_path, *org_list, **kwargs)

        # Retrieve the output from the file
        try:
            with open(output_path) as output_file:
                reader = csv.DictReader(output_file,
                                        fieldnames=self.OUTPUT_FIELD_NAMES)
                rows = [row for row in reader]
        except IOError:
            self.fail(u"Could not find or open output file at '{path}'".format(
                path=output_path))

        # Return the output as a list of dictionaries
        return rows
예제 #2
0
 def test_not_enough_args(self, num_args):
     args = ["dummy"] * num_args
     expected_msg_regex = (
         "^Usage: <OUTPUT_FILENAME> <ORG_ALIASES> --courses=COURSE_ID_LIST --email-optin-chunk-size=CHUNK_SIZE$"
     )
     with self.assertRaisesRegexp(CommandError, expected_msg_regex):
         email_opt_in_list.Command().handle(*args)
예제 #3
0
    def test_file_already_exists(self):
        temp_file = tempfile.NamedTemporaryFile(delete=True)

        def _cleanup():  # pylint: disable=missing-docstring
            temp_file.close()

        with self.assertRaisesRegexp(CommandError, "^File already exists"):
            email_opt_in_list.Command().handle(temp_file.name, self.TEST_ORG)