def test_process_syrx_file_path_no_bad_records(self, move, remove, open_mock, path_join, named_temporary_file, uow):
        syrx_file_handler = SyrxFileHandler()
        syrx_file_handler.logger = Mock()
        syrx_file_handler.syrx_upload_folder = Mock()
        syrx_file_handler.date_str = Mock()

        summary = ProcessSyrxRecordsReturn()
        summary.num_bad_records = 0
        syrx_file_handler.process_syrx_file = Mock(return_value=summary)

        file_path = MagicMock()

        bad_record_file_mock = named_temporary_file.return_value
        read_file_mock = MagicMock()

        def open_mock_side_effect(path, mode):
            if path == file_path and mode == "r":
                return read_file_mock
            else:
                return None
        open_mock.side_effect = open_mock_side_effect

        syrx_file_handler.process_syrx_file_path(file_path)

        named_temporary_file.assert_called_with(delete=False)
        syrx_file_handler.process_syrx_file.assert_called_with(read_file_mock.__enter__.return_value,
                                                               bad_record_file_mock)

        remove.assert_has_calls([call(file_path), call(bad_record_file_mock.name)])
        bad_record_file_mock.close.assert_called_with()
    def test_process_syrx_file_mass_records(self, uow):
        syrx_file_handler = SyrxFileHandler()
        syrx_file_handler.logger = Mock()
        syrx_file_handler.date_str = Mock()

        bad_record_file_mock = MagicMock()

        summary = ProcessSyrxRecordsReturn()
        summary.num_good_records = 200
        summary.num_bad_records = 0
        syrx_file_handler.process_syrx_records = Mock(return_value=summary)

        read_file_stream = io.StringIO()
        for i in range(400):
            read_file_stream.write(u'{"name": "record"}\n')

        # add extra record to force small batch for good measure
        read_file_stream.write(u'{"name": "record"}')
        read_file_stream.seek(0)

        parsed_list = [{"name": "record"} for i in range(200)]

        rv = syrx_file_handler.process_syrx_file(read_file_stream, bad_record_file_mock)

        syrx_file_handler.process_syrx_records.assert_has_calls([call(parsed_list,  bad_record_file_mock),
                                                                 call(parsed_list, bad_record_file_mock),
                                                                 call([{"name": "record"}], bad_record_file_mock)])
        assert rv.num_good_records == 600
    def test_process_syrx_file(self, uow):
        syrx_file_handler = SyrxFileHandler()
        syrx_file_handler.logger = Mock()
        syrx_file_handler.syrx_upload_folder = Mock()
        syrx_file_handler.date_str = Mock()

        bad_record_file_mock = MagicMock()

        summary = ProcessSyrxRecordsReturn()
        summary.num_good_records = 3
        summary.num_bad_records = 1
        syrx_file_handler.process_syrx_records = Mock(return_value=summary)

        read_file_stream = io.StringIO()
        read_file_stream.write(u'{"name": "record0"}\n'
                               u'{"name": "record1"}\n'
                               u'{"name": "record2"}\n'
                               u'{"name": "record3"}')
        read_file_stream.seek(0)

        parsed_list = [{"name": "record0"}, {"name": "record1"}, {"name": "record2"}, {"name": "record3"}]

        rv = syrx_file_handler.process_syrx_file(read_file_stream, bad_record_file_mock)

        syrx_file_handler.process_syrx_records.assert_called_with(parsed_list, bad_record_file_mock)
        assert rv.num_good_records == 3 and rv.num_bad_records == 1
    def test_process_syrx_filename(self, path_join, uow):
        syrx_file_handler = SyrxFileHandler()
        syrx_file_handler.logger = Mock()
        syrx_file_handler.syrx_upload_folder = Mock()
        syrx_file_handler.process_syrx_file_path = Mock()

        file_name = Mock()

        syrx_file_handler.process_syrx_filename(0, file_name, 1)
        path_join.assert_called_with(syrx_file_handler.syrx_upload_folder, file_name)
        syrx_file_handler.process_syrx_file_path.assert_called_with(path_join.return_value)
        assert syrx_file_handler.process_syrx_file_path.call_count == 1
    def test_process_johnson_filename_exception(self, path_join, uow):
        syrx_file_handler = SyrxFileHandler()
        syrx_file_handler.logger = Mock()
        syrx_file_handler.syrx_upload_folder = Mock()
        syrx_file_handler.date_str = Mock()
        syrx_file_handler.process_syrx_file_path = Mock(side_effect=Exception())

        file_name = Mock()

        syrx_file_handler.process_syrx_filename(0, file_name, 1)
        path_join.assert_called_with(syrx_file_handler.syrx_upload_folder, file_name)
        syrx_file_handler.process_syrx_file_path.assert_called_with(path_join.return_value)
        assert syrx_file_handler.logger.exception.call_count == 1
    def test_process_all_syrx_files(self, listdir, uow):
        syrx_file_handler = SyrxFileHandler()
        syrx_file_handler.logger = Mock()
        syrx_file_handler.syrx_upload_folder = Mock()
        syrx_file_handler.process_syrx_filename = Mock()
        syrx_file_handler.process_syrx_filename.return_value = []
        syrx_file_handler.compile_equipment_point_records = Mock()
        listdir.return_value = ["file1", "file2", "file3", "file4", "file5", "file6"]

        syrx_file_handler.process_all_syrx_files()
        syrx_file_handler.process_syrx_filename.assert_has_calls([call(0, "file1", 6),
                                                                  call(1, "file2", 6),
                                                                  call(2, "file3", 6),
                                                                  call(3, "file4", 6),
                                                                  call(4, "file5", 6),
                                                                  call(5, "file6", 6)])