0

Hello i am new on nicegui, i create a simple app where i need to upload the files and store this files on ftp server. I start to make some tests with nicegui.testing.User (i use this package because i don't need to install the selisium package) i want to know how to simulate a test of uploading files.

# simple function for handle_uplaod 
 # file_handler.py 
def handle_file_upload(file_name : str ) : 
    # some logic for store the file on the ftp server 

# main.py
# imports  ... 


ui.page("/")
def main(): 
    ui.upload(on_upload = lambda file_name :handle_file_upload(file_name), auto_upload = True)
# tests/test_upload_file.py
ui.run() @pytest.mark.asyncio
async def test_upload_file(user: User) -> None:
    """Test step 2 page elements"""
    # loading the real page

    await user.open("/")
    
    upload = user.find(ui.upload).elements.pop()
upload.handle_uploads([ui.upload.SmallFileUpload("file.psd", "application/pdf", b"Test Service Terms File Hello")])
    # How to apply tests xD
    asserts ... 
    asserts ...

1 Answer 1

0

As I already answered on GitHub, there is an example for simulating ui.upload in the documentation:

async def receive_file(e: events.UploadEventArguments):
    content = await e.file.text()
    reader = csv.DictReader(content.splitlines())
    ui.table(
        columns=[{
            'name': h,
            'label': h.capitalize(),
            'field': h,
        } for h in reader.fieldnames or []],
        rows=list(reader),
    )

ui.upload(on_upload=receive_file)
from nicegui import ui

upload = user.find(ui.upload).elements.pop()
await upload.handle_uploads([
    ui.upload.SmallFileUpload('data.csv', 'text/csv', b'name,age\nAlice,30\nBob,28')
])
await user.should_see(ui.table)
table = user.find(ui.table).elements.pop()
assert table.columns == [
    {'name': 'name', 'label': 'Name', 'field': 'name'},
    {'name': 'age', 'label': 'Age', 'field': 'age'},
]
assert table.rows == [
    {'name': 'Alice', 'age': '30'},
    {'name': 'Bob', 'age': '28'},
]
Sign up to request clarification or add additional context in comments.

1 Comment

I am using patch from unitytest.mock ``` with patch("project.my_file.handle_file_upload") as handle_file: await upload.handle_uploads([ ui.upload.SmallFileUpload( "data.csv", "text/csv", b"name,age\nAlice,30\nBob,28" ) ]) handle_file.assert_called_once() ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.