def test_binary_file_serializing(binary_gif_file, binary_img_file): with open(f"tests/files/{binary_img_file['file_name']}", "rb") as f: mfile = File.from_file(f) assert File(**binary_img_file) == mfile assert (mfile.media_type == binary_img_file["data"].split( "data:", 1)[1].split(";", 1)[0]) with open(f"tests/files/{binary_gif_file['file_name']}", "rb") as f: mfile = File.from_file(f) assert File(**binary_gif_file) == mfile assert (mfile.media_type == binary_gif_file["data"].split( "data:", 1)[1].split(";", 1)[0])
def test_text_file_serializing(text_txt_file, text_json_file): with open(f"tests/files/{text_txt_file['file_name']}", "r") as f: mfile = File.from_file(f) assert File(**text_txt_file) == mfile assert (mfile.media_type == text_txt_file["data"].split("data:", 1)[1].split( ";", 1)[0]) with open(f"tests/files/{text_json_file['file_name']}", "r") as f: mfile = File.from_file(f) assert File(**text_json_file) == mfile assert (mfile.media_type == text_json_file["data"].split( "data:", 1)[1].split(";", 1)[0])
def test_creating_file_from_io_with_name( io_cls, file_data, file_name, explicit_file_name, ): created_file = io_cls(file_data) if not explicit_file_name: created_file.name = file_name assert File.from_file(created_file, filename=explicit_file_name) == File( file_name=explicit_file_name or file_name, data="data:text/plain;base64,dGVzdA==", )
async def my_handler(message: Message) -> None: with open("my_file.txt") as f: notification = SendingMessage( file=File.from_file(f), credentials=message.credentials, ) await bot.send(notification)
def sending_message() -> SendingMessage: return SendingMessage( text="text", file=File.from_string(b"data", filename="file.txt"), credentials=SendingCredentials( sync_id=uuid.uuid4(), bot_id=uuid.uuid4(), host="host", ), markup=MessageMarkup( bubbles=[[BubbleElement(command="")]], keyboard=[[KeyboardElement(command="")]], ), options=MessageOptions( recipients=[uuid.uuid4()], mentions=[ Mention(mention_data=UserMention(user_huid=uuid.uuid4())), Mention( mention_data=UserMention(user_huid=uuid.uuid4()), mention_type=MentionTypes.contact, ), Mention( mention_data=ChatMention(group_chat_id=uuid.uuid4()), mention_type=MentionTypes.chat, ), ], notifications=NotificationOptions(send=False, force_dnd=True), ), )
async def test_adding_file(bot, client, message): sending_file = File.from_string("some content", "file.txt") await bot.send_message( "some text", message.credentials, file=sending_file.file, ) command_result = client.notifications[0] assert command_result.file == sending_file
def test_file_chunks(filename, encoded_data, origin_data): file = File.construct(file_name=filename, data=encoded_data) temp_file = BytesIO() with file.file_chunks() as chunks: for chunk in chunks: temp_file.write(chunk) temp_file.seek(0) assert temp_file.read() == origin_data
async def test_answer_message_with_file_is_notification(bot, client, message): file = File.from_string("some content", "file.txt") await bot.answer_message( "some text", message, file=file, ) message = client.notifications[0] assert message.result.body == "some text" assert message.file == file
def test_file_transfer_event(): builder = MessageBuilder() builder.file = File.from_string("some data", "name.txt") builder.body = "file_transfer" builder.system_command = True
def test_accept_has_supported_extension(extension): filename = f"test{extension}" assert File.has_supported_extension(filename)
def test_retrieving_file_size(): assert File.from_string(b"file\ncontents", filename="test.txt").size_in_bytes == 13
def test_retrieving_png_media_type(): assert File.from_string(b"test", filename="test.png").media_type == "image/png"
def test_retrieving_txt_media_type(): assert File.from_string(b"test", filename="test.txt").media_type == "text/plain"
def test_message_file_from_file( self, sending_message: SendingMessage) -> None: original_file = sending_message.file sending_message.add_file(File.from_file(original_file.file)) assert sending_message.file == original_file
def test_error_with_wrong_extension(extension): with pytest.raises(ValidationError): File(file_name="tmp{0}".format(extension), data="")
def test_decline_has_supported_extension(): bad_filename = "test.bad" assert not File.has_supported_extension(bad_filename)
def sending_file(): return File.from_string("some content", "file.txt")
def test_retrieving_file_data_in_bytes(): assert File.from_string(b"test", filename="test.txt").data_in_bytes == b"test"
def test_creating_file_from_string(file_data): assert File.from_string(file_data, filename="test.txt") == File( file_name="test.txt", data="data:text/plain;base64,dGVzdA==", )
def test_retrieving_file_data_in_base64(): assert File.from_string(b"test", filename="test.txt").data_in_base64 == "dGVzdA=="
def test_file_creation_with_right_extension(extension): File(file_name=f"tmp{extension}", data="")
def test_setting_new_file_from_io(): file = File.from_string("some data", "name.txt") builder = MessageBuilder() builder.file = file.file assert builder.file == file