def test_generate_sr_completion_csv_success(
            self, mock_generate_completion_report):
        mock_generate_completion_report.return_value = None
        output = os.path.join(self.__tmp_dir, 'stureg_stat.csv')
        queries = {QueryType.QUERY: 'q1'}
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE:
            ExtractionDataType.SR_COMPLETION,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.ACADEMIC_YEAR: 2016,
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.TASK_QUERIES: queries,
            TaskConstants.CSV_HEADERS: 'completion_csv_headers'
        }

        result = generate_extract_file.apply(
            args=[self._tenant, 'request_id', task])
        result.get()

        task_info = {
            Constants.TASK_ID: task[TaskConstants.TASK_TASK_ID],
            Constants.CELERY_TASK_ID: result.id,
            Constants.REQUEST_GUID: 'request_id'
        }

        mock_generate_completion_report.assert_called_with(
            self._tenant, output, task_info, task)
    def test_generate_json(self):
        with UnittestEdcoreDBConnection() as connection:
            dim_asmt = connection.get_table('dim_asmt')
            query = select([dim_asmt.c.asmt_guid], from_obj=[dim_asmt])
            query = query.where(
                dim_asmt.c.asmt_guid == '7d10d26b-b013-4cdd-a916-5d577e895cff')
            output = os.path.join(self.__tmp_dir, 'asmt.json')
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE: ExtractionDataType.QUERY_JSON,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.TASK_QUERIES: {
                QueryType.QUERY: query
            }
        }

        results = generate_extract_file.apply(
            args=[self._tenant, 'request_id', task])  # @UndefinedVariable
        results.get()

        self.assertTrue(os.path.exists(output))
        with open(output) as out:
            data = json.load(out)
        self.assertEqual(data['asmt_guid'],
                         '7d10d26b-b013-4cdd-a916-5d577e895cff')
    def test_generate_csv(self):
        with UnittestEdcoreDBConnection() as connection:
            dim_asmt = connection.get_table('dim_asmt')
            query = select([dim_asmt.c.asmt_guid, dim_asmt.c.asmt_period],
                           from_obj=[dim_asmt])
            query = query.where(
                dim_asmt.c.asmt_guid == '7d10d26b-b013-4cdd-a916-5d577e895cff')
        output = os.path.join(self.__tmp_dir, 'asmt.csv')
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE: ExtractionDataType.QUERY_CSV,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.TASK_QUERIES: {
                QueryType.QUERY: query
            }
        }

        result = generate_extract_file.apply(
            args=[self._tenant, 'request_id', task])  # @UndefinedVariable
        result.get()

        self.assertTrue(os.path.exists(output))
        csv_data = []
        with open(output) as out:
            data = csv.reader(out)
            for row in data:
                csv_data.append(row)
        self.assertEqual(len(csv_data), 2)
        self.assertEqual(csv_data[0], ['asmt_guid', 'asmt_period'])
        self.assertEqual(
            csv_data[1],
            ['7d10d26b-b013-4cdd-a916-5d577e895cff', 'Spring 2016'])
    def test_generate_csv_no_tenant(self):
        output = '/tmp/unittest.csv.gz.pgp'
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE: ExtractionDataType.QUERY_CSV,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.TASK_QUERIES: None
        }

        result = generate_extract_file.apply(args=[None, '0',
                                                   task])  # @UndefinedVariable
        result.get()

        self.assertFalse(os.path.exists(output))
    def test_generate_sr_statistics_csv_no_tenant(self):
        output = os.path.join(self.__tmp_dir, 'stureg_stat.csv')
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE:
            ExtractionDataType.SR_STATISTICS,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.ACADEMIC_YEAR: 2014
        }

        result = generate_extract_file.apply(args=[None, 'request_id', task])
        result.get()

        self.assertFalse(os.path.exists(output))
    def test_generate_sr_statistics_csv_bad_file(self):
        output = 'C:'
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE:
            ExtractionDataType.SR_STATISTICS,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.ACADEMIC_YEAR: 2014
        }

        result = generate_extract_file.apply(
            args=[self._tenant, 'request_id', task])

        self.assertRaises(
            ExtractionError,
            result.get,
        )
    def test_generate_json_with_bad_file(self):
        with UnittestEdcoreDBConnection() as connection:
            dim_asmt = connection.get_table('dim_asmt')
            query = select([dim_asmt.c.asmt_guid], from_obj=[dim_asmt])
            query = query.where(dim_asmt.c.asmt_guid == '2123122')
        output = 'C:'
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE: ExtractionDataType.QUERY_JSON,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.TASK_QUERIES: {
                QueryType.QUERY: query
            }
        }

        results = generate_extract_file.apply(
            args=[self._tenant, 'request_id', task])  # @UndefinedVariable

        self.assertRaises(ExtractionError, results.get)
    def test_generate_json_not_writable(self):
        with UnittestEdcoreDBConnection() as connection:
            dim_asmt = connection.get_table('dim_asmt')
            query = select([dim_asmt.c.asmt_guid], from_obj=[dim_asmt])
            query = query.where(
                dim_asmt.c.asmt_guid == '7d10d26b-b013-4cdd-a916-5d577e895cff')
        output = os.path.join('', 'asmt.json')
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE: ExtractionDataType.QUERY_JSON,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.TASK_QUERIES: {
                QueryType.QUERY: query
            }
        }

        results = generate_extract_file.apply(
            args=[self._tenant, 'request_id', task])  # @UndefinedVariable

        self.assertRaises(ExtractionError, results.get)
    def test_generate_json_with_no_results(self):
        with UnittestEdcoreDBConnection() as connection:
            dim_asmt = connection.get_table('dim_asmt')
            query = select([dim_asmt.c.asmt_guid], from_obj=[dim_asmt])
            query = query.where(dim_asmt.c.asmt_guid == '2123122')
        output = os.path.join(self.__tmp_dir, 'asmt.json')
        task = {
            TaskConstants.EXTRACTION_DATA_TYPE: ExtractionDataType.QUERY_JSON,
            TaskConstants.TASK_TASK_ID: 'task_id',
            TaskConstants.TASK_FILE_NAME: output,
            TaskConstants.TASK_QUERIES: {
                QueryType.QUERY: query
            }
        }

        results = generate_extract_file.apply(
            args=[self._tenant, 'request_id', task])  # @UndefinedVariable
        results.get()

        self.assertTrue(os.path.exists(output))
        statinfo = os.stat(output)
        self.assertEqual(0, statinfo.st_size)