async def test_client_handler_new_patch(user_svc, file_svc): mock_client = MagicMock() doc = Doc() patch = doc.insert(0, "A") file_id = FileService.get_file_id("r", "test") msg = { "username": "******", "password": "******", "filename": "test", "type": "patch", "content": patch, "file_id": file_id } raw_msg = json.dumps(msg).encode("utf-8") mock_client.__aiter__.return_value = [raw_msg] mock_client.return_value.send.return_value = Future() mock_client.return_value.send.return_value.set_result("123") user_svc.auth_user.return_value = False MagicMock.__await__ = lambda x: async_magic().__await__() user_svc_instance = user_svc.return_value() user_svc_instance.auth_user.return_value = True user_svc_instance.has_access.return_value = True file_svc_instance = file_svc.return_value() file_svc_instance.register_patch.return_value = None client_handler = ClientHandler(user_svc_instance, file_svc_instance) client_handler.active_authors.append({ "connection": mock_client, "current_file": file_id }) await client_handler.handle_client(mock_client, None) response = mock_client.send.call_args.args[0] assert json.loads(response)["content"] == patch
def test_document_editor_do_copy(mock_msg_service, mock_get_app): # get sample patches doc = Doc() doc.site = 0 test_str = "Test string, quite a long one! Yeah, sure..." patches = [] for idx, c in enumerate(test_str): patches.append(doc.insert(idx, c)) msg_srv_instance = mock_msg_service.return_value() mock_msg_service.return_value.prepare_send_request.return_value = None mock_msg_service.return_value.put_message.return_value = None document_editor = DocumentEditor(msg_srv_instance) for patch in patches: document_editor.update_text(patch) document_editor.text_field.buffer.selection_state = \ SelectionState(4, SelectionType.CHARACTERS) document_editor.do_copy() # assert that document remains unchanged assert document_editor.doc.text == "Test string, quite a long one!" \ " Yeah, sure..." copied_data = mock_get_app.return_value.clipboard.set_data \ .call_args[0][0] # check if correct text was sent to clipboard assert copied_data.text == ' string, quite a long one! Yeah, sure...'
def test_docengine_insert(): """ test Doc line insertion """ test_str = "test insert of line" doc = Doc() for c in test_str: doc.insert(0, c) assert doc.text == test_str[::-1]
def __init__(self, msg_service: MessageService): self.doc = Doc() self.doc.site = int(random.getrandbits(32)) self.patch_set = [] self.msg_service = msg_service self.text_field = TextEditor( scrollbar=True, line_numbers=False, search_field=SearchToolbar(), key_bindings=self.__init_bindings(), lexer=AuthorLexer(self.doc) )
def test_author_lexer_single_author(): """ Test that lexer returns single color and original string if there is only one author """ doc = Doc() doc.site = 0 test_str = "Test string" for idx, c in enumerate(test_str): doc.insert(idx, c) lexer = AuthorLexer(doc) lex_func = lexer.lex_document(None) colors, chars = zip(*lex_func(0)) assert len(set(colors)) == 1 assert "".join(chars) == test_str
def test_author_lexer_multiple_authors(): """ Test that lexer returns three colors for three different authors. :return: """ doc = Doc() doc.site = 0 author_inserts = ["first", "second", "third"] for insert in author_inserts: for idx, c in enumerate(insert): doc.insert(idx, c) doc.site += 1 lexer = AuthorLexer(doc) lex_func = lexer.lex_document(None) colors = [item[0] for item in lex_func(0)] assert len(set(colors)) == 3
def test_document_editor_do_del(mock_msg_service): # get sample patches doc = Doc() doc.site = 0 test_str = "Test string, quite a long one! Yeah, sure..." patches = [] for idx, c in enumerate(test_str): patches.append(doc.insert(idx, c)) msg_srv_instance = mock_msg_service.return_value() mock_msg_service.return_value.prepare_send_request.return_value = None mock_msg_service.return_value.put_message.return_value = None document_editor = DocumentEditor(msg_srv_instance) for patch in patches: document_editor.update_text(patch) document_editor.text_field.buffer.selection_state = \ SelectionState(4, SelectionType.CHARACTERS) document_editor.do_delete() assert document_editor.doc.text == "Test"
def try_load_file(path) -> List[str] or None: """ Try to load file and return it as list of encoded patches :param path: path to file :type path: Path :return: list of file patches encoded to string """ try: with open(path, 'r') as file: file_doc = Doc() file_doc.site = 0 pos = 0 result = [] for line in file: for char in line: result.append(file_doc.insert(pos, char)) pos += 1 return result except (OSError, IOError, FileNotFoundError): logging.info(f"Requested [{path}] was not found!") return None
def try_save_file(path, history) -> bool: """ Try to save file of the user to the filesystem. Creates a new document, applies all the patches, then saves the resulting document's text to file. :param path: path to save :param history: patch history :type path: Path :type history: List[str] :return: True if successful, otherwise False """ file_doc = Doc() file_doc.site = 0 for patch in history: file_doc.apply_patch(patch) try: with open(path, 'w') as file: file.write(file_doc.text) return True except (OSError, IOError, FileNotFoundError): logging.info(f"Requested [{path}] was not found!") return False