# Path: ddsc/core/consistency.py
# class UploadDetails(object):
# class ProjectChecker(object):
#     def __init__(self, dds_file, remote_path):
#     def inconsistent(self):
#     def had_error(self):
#     def is_bad(self):
#     def name(self):
#     def status_str(self):
#     def file_id(self):
#     def message(self):
#     def __init__(self, config, project):
#     def files_are_ok(self):
#     def _try_fetch_project_files(self):
#     def get_bad_uploads(self):
#     def get_bad_uploads_table_data(self):
#     def delete_bad_uploads(self):
#     def print_bad_uploads_table(self):
#     def wait_for_consistency(self, wait_sec=5):
# 
# Path: ddsc/core/ddsapi.py
# class DataServiceError(Exception):
#     """
#     Error that wraps up info about it and creates an informative string.
#     """
#     def __init__(self, response, url_suffix, request_data):
#         """
#         Create exception for failed response.
#         :param response: requests.Response response that was in error
#         :param url_suffix: str url we were trying to connect to
#         :param request_data: object data we were sending to url
#         """
#         resp_json = None
#         try:
#             resp_json = response.json()
#         except:
#             resp_json = {}
#         if response.status_code == 500:
#             if resp_json and not resp_json.get('reason'):
#                 resp_json = {'reason': 'Internal Server Error', 'suggestion': 'Contact DDS support.'}
#         Exception.__init__(self, 'Error {} on {}\nReason:{}\nSuggestion:{}'.format(
#             response.status_code, url_suffix, resp_json.get('reason', resp_json.get('error', '')),
#             resp_json.get('suggestion', '')
#         ))
#         self.response = resp_json
#         self.url_suffix = url_suffix
#         self.request_data = request_data
#         self.status_code = response.status_code


from unittest import TestCase
from ddsc.core.consistency import UploadDetails, ProjectChecker, DSResourceNotConsistentError
from ddsc.core.ddsapi import DataServiceError
from mock import Mock, patch, call

    def test_files_are_ok__good(self):
        self.project.get_project_files_generator.return_value = []
        self.assertEqual(self.checker.files_are_ok(), True)

    def test_files_are_ok__error(self):
        self.project.get_project_files_generator.side_effect = DSResourceNotConsistentError(Mock(), Mock(), Mock())
        dds_file = Mock()
        dds_file.name = "file1.txt"
        dds_file.id = "123"
        dds_file.get_upload.return_value.status.is_consistent = False
        dds_file.get_upload.return_value.status.error_on = None
        dds_file.get_upload.return_value.status.initiated_on = '2021-01-01'
        self.project.get_path_to_files.return_value.items.return_value = [
            ("/data/bad/file1.txt", dds_file)
        ]
        self.assertEqual(self.checker.files_are_ok(), False)
        headers, data = self.checker.get_bad_uploads_table_data()
        self.assertEqual(headers, ['File', 'Status', 'Message', 'FileID', 'RemotePath'])
        self.assertEqual(data, [['file1.txt', 'Inconsistent', 'started upload at 2021-01-01', '123',
                                 '/data/bad/file1.txt']])

    def test_files_are_ok__DDS_400(self):
        self.project.get_project_files_generator.side_effect = DataServiceError(Mock(status_code=400), Mock(), Mock())
        dds_file = Mock()
        dds_file.name = "file1.txt"
        dds_file.id = "123"
        dds_file.get_upload.return_value.status.is_consistent = False
        dds_file.get_upload.return_value.status.error_on = None
        dds_file.get_upload.return_value.status.initiated_on = '2021-01-01'

# Path: ddsc/core/consistency.py
# class UploadDetails(object):
# class ProjectChecker(object):
#     def __init__(self, dds_file, remote_path):
#     def inconsistent(self):
#     def had_error(self):
#     def is_bad(self):
#     def name(self):
#     def status_str(self):
#     def file_id(self):
#     def message(self):
#     def __init__(self, config, project):
#     def files_are_ok(self):
#     def _try_fetch_project_files(self):
#     def get_bad_uploads(self):
#     def get_bad_uploads_table_data(self):
#     def delete_bad_uploads(self):
#     def print_bad_uploads_table(self):
#     def wait_for_consistency(self, wait_sec=5):
# 
# Path: ddsc/core/ddsapi.py
# class DataServiceError(Exception):
#     """
#     Error that wraps up info about it and creates an informative string.
#     """
#     def __init__(self, response, url_suffix, request_data):
#         """
#         Create exception for failed response.
#         :param response: requests.Response response that was in error
#         :param url_suffix: str url we were trying to connect to
#         :param request_data: object data we were sending to url
#         """
#         resp_json = None
#         try:
#             resp_json = response.json()
#         except:
#             resp_json = {}
#         if response.status_code == 500:
#             if resp_json and not resp_json.get('reason'):
#                 resp_json = {'reason': 'Internal Server Error', 'suggestion': 'Contact DDS support.'}
#         Exception.__init__(self, 'Error {} on {}\nReason:{}\nSuggestion:{}'.format(
#             response.status_code, url_suffix, resp_json.get('reason', resp_json.get('error', '')),
#             resp_json.get('suggestion', '')
#         ))
#         self.response = resp_json
#         self.url_suffix = url_suffix
#         self.request_data = request_data
#         self.status_code = response.status_code


# Path: ddsc/core/tests/test_consistency.py
from unittest import TestCase
from ddsc.core.consistency import UploadDetails, ProjectChecker, DSResourceNotConsistentError
from ddsc.core.ddsapi import DataServiceError
from mock import Mock, patch, call


    def test_files_are_ok__good(self):
        self.project.get_project_files_generator.return_value = []
        self.assertEqual(self.checker.files_are_ok(), True)

    def test_files_are_ok__error(self):
        self.project.get_project_files_generator.side_effect = DSResourceNotConsistentError(Mock(), Mock(), Mock())
        dds_file = Mock()
        dds_file.name = "file1.txt"
        dds_file.id = "123"
        dds_file.get_upload.return_value.status.is_consistent = False
        dds_file.get_upload.return_value.status.error_on = None
        dds_file.get_upload.return_value.status.initiated_on = '2021-01-01'
        self.project.get_path_to_files.return_value.items.return_value = [
            ("/data/bad/file1.txt", dds_file)
        ]
        self.assertEqual(self.checker.files_are_ok(), False)
        headers, data = self.checker.get_bad_uploads_table_data()
        self.assertEqual(headers, ['File', 'Status', 'Message', 'FileID', 'RemotePath'])
        self.assertEqual(data, [['file1.txt', 'Inconsistent', 'started upload at 2021-01-01', '123',
                                 '/data/bad/file1.txt']])

    def test_files_are_ok__DDS_400(self):
        self.project.get_project_files_generator.side_effect = DataServiceError(Mock(status_code=400), Mock(), Mock())
        dds_file = Mock()
        dds_file.name = "file1.txt"
        dds_file.id = "123"
        dds_file.get_upload.return_value.status.is_consistent = False
        dds_file.get_upload.return_value.status.error_on = None
        dds_file.get_upload.return_value.status.initiated_on = '2021-01-01'

        self.project.get_path_to_files.return_value.items.return_value = [