Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
85ba03c
1
Parent(s):
a4adf97
test: implemented demo/snippet using app code for sidebar, plus test
Browse files- the file_uploader callback is mocked by a side-effect, at the right time.
- dynamically generated elements are tested by id
- session state is tested on init, and then after processing input files
- some tests are quite fragile here (i.e. sensitive to element order).
- src/apptest/demo_elements.py +24 -0
- src/apptest/demo_input_sidebar.py +44 -0
- tests/test_demo_input_sidebar.py +235 -0
src/apptest/demo_elements.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# a small library of elements used in testing, presenting some
|
| 2 |
+
# processed data in simple ways that are easily testable via AppTest
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from input.input_handling import (
|
| 5 |
+
get_image_datetime, get_image_latlon
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
def show_uploaded_file_info():
|
| 9 |
+
if "file_uploader_data" not in st.session_state or \
|
| 10 |
+
not st.session_state.file_uploader_data:
|
| 11 |
+
|
| 12 |
+
st.write("No files uploaded yet")
|
| 13 |
+
return
|
| 14 |
+
|
| 15 |
+
st.write("the buffered files:")
|
| 16 |
+
|
| 17 |
+
uploaded_files = st.session_state.file_uploader_data
|
| 18 |
+
for ix, file in enumerate(uploaded_files):
|
| 19 |
+
image_datetime_raw = get_image_datetime(file)
|
| 20 |
+
latitude0, longitude0 = get_image_latlon(file)
|
| 21 |
+
s = f"index: {ix}, name: {file.name}, datetime: {image_datetime_raw}, lat: {latitude0}, lon:{longitude0}"
|
| 22 |
+
st.text_area(f"{file.name}", value=s, key=f"metadata_{ix}")
|
| 23 |
+
print(s)
|
| 24 |
+
|
src/apptest/demo_input_sidebar.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# a chunk of the full app, covering the elements presented on the sidebar
|
| 2 |
+
# - this includes both input and workflow items.
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# to run streamlit from this subdir, we need the the src dir on the path
|
| 6 |
+
# NOTE: pytest doesn't need this to run the tests, but to develop the test
|
| 7 |
+
# harness is hard without running streamlit
|
| 8 |
+
import sys
|
| 9 |
+
from os import path
|
| 10 |
+
# src (parent from here)
|
| 11 |
+
src_dir = path.dirname( path.dirname( path.abspath(__file__) ) )
|
| 12 |
+
sys.path.append(src_dir)
|
| 13 |
+
|
| 14 |
+
from input.input_handling import (
|
| 15 |
+
init_input_data_session_states,
|
| 16 |
+
init_input_container_states,
|
| 17 |
+
add_input_UI_elements,
|
| 18 |
+
setup_input,
|
| 19 |
+
)
|
| 20 |
+
from utils.workflow_ui import refresh_progress_display, init_workflow_viz, init_workflow_session_states
|
| 21 |
+
|
| 22 |
+
from apptest.demo_elements import show_uploaded_file_info
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
|
| 28 |
+
init_input_data_session_states()
|
| 29 |
+
init_input_container_states()
|
| 30 |
+
init_workflow_session_states()
|
| 31 |
+
|
| 32 |
+
init_workflow_viz()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
with st.sidebar:
|
| 36 |
+
refresh_progress_display()
|
| 37 |
+
# layout handling
|
| 38 |
+
add_input_UI_elements()
|
| 39 |
+
# input elements (file upload, text input, etc)
|
| 40 |
+
setup_input()
|
| 41 |
+
|
| 42 |
+
# as a debug, let's add some text_area elements to show the files (no clash
|
| 43 |
+
# with testing the prod app since we dont use text_area at all)
|
| 44 |
+
show_uploaded_file_info ()
|
tests/test_demo_input_sidebar.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
import pytest
|
| 8 |
+
from unittest.mock import MagicMock, patch
|
| 9 |
+
from streamlit.testing.v1 import AppTest
|
| 10 |
+
from datetime import datetime, timedelta
|
| 11 |
+
import time
|
| 12 |
+
|
| 13 |
+
from input.input_handling import spoof_metadata
|
| 14 |
+
from input.input_observation import InputObservation
|
| 15 |
+
from input.input_handling import buffer_uploaded_files
|
| 16 |
+
|
| 17 |
+
from streamlit.runtime.uploaded_file_manager import UploadedFile
|
| 18 |
+
|
| 19 |
+
from test_demo_multifile_upload import (
|
| 20 |
+
mock_uploadedFile_List_ImageData, mock_uploadedFile,
|
| 21 |
+
MockUploadedFile, )
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def wrapped_buffer_uploaded_files(*args, **kwargs):
|
| 26 |
+
import streamlit as st
|
| 27 |
+
#import time
|
| 28 |
+
_cprint(f"[I] buffering files in my side-effect! cool")
|
| 29 |
+
#time.sleep(1)
|
| 30 |
+
uploaded_files = st.session_state.file_uploader_data
|
| 31 |
+
_cprint(f"[I] buffering files in my side-effect! cool | {len(uploaded_files)}")
|
| 32 |
+
buffer_uploaded_files() # nowcall the real prod func
|
| 33 |
+
_cprint(f"[I] finished the real buffering ! cool | {len(uploaded_files)}")
|
| 34 |
+
# - tests for apptest/demo_input_phase
|
| 35 |
+
|
| 36 |
+
# zero test: no inputs
|
| 37 |
+
# -> empty session state
|
| 38 |
+
# -> file_uploader with no files, ready to accept input
|
| 39 |
+
# -> a couple of containers
|
| 40 |
+
# -> not much on the main tab.
|
| 41 |
+
|
| 42 |
+
# many test: list of 2 inputs
|
| 43 |
+
# -> session state with 2 files
|
| 44 |
+
# -> file_uploader with 2 files, ready to accept more
|
| 45 |
+
# -> the metadata container will have two groups inside, with several input elements
|
| 46 |
+
# -> the main tab will have a couple of text_area elements showing the uploaded file metadata
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
OKBLUE = '\033[94m'
|
| 50 |
+
OKGREEN = '\033[92m'
|
| 51 |
+
OKCYAN = '\033[96m'
|
| 52 |
+
FAIL = '\033[91m'
|
| 53 |
+
ENDC = '\033[0m'
|
| 54 |
+
|
| 55 |
+
def _cprint(msg:str, color:str=OKCYAN):
|
| 56 |
+
print(f"{color}{msg}{ENDC}")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
TIMEOUT = 10
|
| 60 |
+
#SCRIPT_UNDER_TEST = "src/main.py"
|
| 61 |
+
#SCRIPT_UNDER_TEST = "src/apptest/demo_input_phase.py"
|
| 62 |
+
SCRIPT_UNDER_TEST = "src/apptest/demo_input_sidebar.py"
|
| 63 |
+
|
| 64 |
+
def verify_initial_session_state(at:AppTest):
|
| 65 |
+
# the initialised states we expect
|
| 66 |
+
# - container_file_uploader exists
|
| 67 |
+
# - container_metadata_inputs exists
|
| 68 |
+
# - observations {}
|
| 69 |
+
# - image_hashes []
|
| 70 |
+
# - images {}
|
| 71 |
+
# - files {}
|
| 72 |
+
# - public_observations {}
|
| 73 |
+
assert at.session_state.observations == {}
|
| 74 |
+
assert at.session_state.image_hashes == []
|
| 75 |
+
assert at.session_state.images == {}
|
| 76 |
+
assert at.session_state.files == {}
|
| 77 |
+
assert at.session_state.public_observations == {}
|
| 78 |
+
assert "container_file_uploader" in at.session_state
|
| 79 |
+
assert "container_metadata_inputs" in at.session_state
|
| 80 |
+
|
| 81 |
+
def test_no_input_no_interaction():
|
| 82 |
+
|
| 83 |
+
# zero test: no inputs
|
| 84 |
+
# -> empty session state (ok many initialised, but empty data)
|
| 85 |
+
# -> file_uploader with no files, ready to accept input
|
| 86 |
+
# -> a couple of containers
|
| 87 |
+
# -> not much on the main tab.
|
| 88 |
+
|
| 89 |
+
at = AppTest.from_file(SCRIPT_UNDER_TEST, default_timeout=10).run()
|
| 90 |
+
if 1:
|
| 91 |
+
verify_initial_session_state(at)
|
| 92 |
+
else:
|
| 93 |
+
# the initialised states we expect
|
| 94 |
+
# - container_file_uploader exists
|
| 95 |
+
# - container_metadata_inputs exists
|
| 96 |
+
# - observations {}
|
| 97 |
+
# - image_hashes []
|
| 98 |
+
# - image_filenames []
|
| 99 |
+
# - images {}
|
| 100 |
+
# - files {}
|
| 101 |
+
# - public_observations {}
|
| 102 |
+
assert at.session_state.observations == {}
|
| 103 |
+
assert at.session_state.image_filenames == []
|
| 104 |
+
assert at.session_state.image_hashes == []
|
| 105 |
+
assert at.session_state.images == {}
|
| 106 |
+
assert at.session_state.files == {}
|
| 107 |
+
assert at.session_state.public_observations == {}
|
| 108 |
+
assert "container_file_uploader" in at.session_state
|
| 109 |
+
assert "container_metadata_inputs" in at.session_state
|
| 110 |
+
|
| 111 |
+
assert at.session_state.input_author_email == spoof_metadata.get("author_email")
|
| 112 |
+
|
| 113 |
+
# print (f"[I] whole tree: {at._tree}")
|
| 114 |
+
# for elem in at.sidebar.markdown:
|
| 115 |
+
# print("\t", elem.value)
|
| 116 |
+
|
| 117 |
+
# do some basic checks on what is present in the sidebar
|
| 118 |
+
assert len(at.sidebar.divider) == 1
|
| 119 |
+
|
| 120 |
+
# in the sidebar, we have the progress indicator, then the fileuploader and metadata inputs
|
| 121 |
+
# - annoyingly we can't use keys for markdown.
|
| 122 |
+
# - so we are sensitive to the order.
|
| 123 |
+
assert "Progress: 0/5" in at.sidebar.markdown[0].value
|
| 124 |
+
assert "st-key-container_file_uploader_id" in at.sidebar.markdown[1].value
|
| 125 |
+
assert "st-key-container_metadata_inputs_id" in at.sidebar.markdown[2].value
|
| 126 |
+
assert "Metadata Inputs... wait for file upload" in at.sidebar.markdown[3].value
|
| 127 |
+
|
| 128 |
+
# there should be 1 input, for the author_email, in this path (no files uploaded)
|
| 129 |
+
assert len(at.sidebar.text_input) == 1
|
| 130 |
+
|
| 131 |
+
# can't check for the presence of containers (they are st.Block elements in the tree)
|
| 132 |
+
# - no way to access the list of them, nor by key/id. nor by getter (unlike
|
| 133 |
+
# images which seem to have an undocumented accessor, "imgs")
|
| 134 |
+
# best we can do is check that the session state ids exist, which is really basic but ok
|
| 135 |
+
assert "container_file_uploader" in at.session_state
|
| 136 |
+
assert "container_metadata_inputs" in at.session_state
|
| 137 |
+
# wow, the keys defined in the constructor are not honoured in session_state, unlike with
|
| 138 |
+
# the text_input elements.
|
| 139 |
+
# code init -- st.container(border=True, key="container_file_uploader_id")
|
| 140 |
+
# so skip these ones for now.
|
| 141 |
+
# assert "container_file_uploader_id" in at.session_state
|
| 142 |
+
# assert "container_metadata_inputs_id" in at.session_state
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
@patch("streamlit.file_uploader")
|
| 146 |
+
def test_two_input_files_realdata(mock_file_rv: MagicMock, mock_uploadedFile_List_ImageData):
|
| 147 |
+
# many test: list of 2 inputs
|
| 148 |
+
# -> session state with 2 files
|
| 149 |
+
# -> file_uploader with 2 files, ready to accept more
|
| 150 |
+
# -> the metadata container will have two groups inside, with several input elements
|
| 151 |
+
# -> the main tab will have a couple of text_area elements showing the uploaded file metadata
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# Create a list of 2 mock files
|
| 155 |
+
num_files = 2
|
| 156 |
+
mock_files = mock_uploadedFile_List_ImageData(num_files=num_files)
|
| 157 |
+
|
| 158 |
+
# Set the return value of the mocked file_uploader to the list of mock files
|
| 159 |
+
mock_file_rv.return_value = mock_files
|
| 160 |
+
#mock_file_rv.side_effect = wrapped_buffer_uploaded_files # not yet!...
|
| 161 |
+
|
| 162 |
+
# Run the Streamlit app
|
| 163 |
+
at = AppTest.from_file(SCRIPT_UNDER_TEST, default_timeout=TIMEOUT).run()
|
| 164 |
+
verify_initial_session_state(at)
|
| 165 |
+
|
| 166 |
+
# put the mocked file_upload into session state, as if it were the result of a file upload, with the key 'file_uploader_data'
|
| 167 |
+
at.session_state["file_uploader_data"] = mock_files
|
| 168 |
+
# the side effect cant run until now (need file_uploader_data to be set)
|
| 169 |
+
mock_file_rv.side_effect = wrapped_buffer_uploaded_files
|
| 170 |
+
|
| 171 |
+
print(f"[I] session state: {at.session_state}")
|
| 172 |
+
at.run()
|
| 173 |
+
print(f"[I] session state: {at.session_state}")
|
| 174 |
+
print(f"full tree: {at._tree}")
|
| 175 |
+
|
| 176 |
+
# now we've processed the files and got metadata, we expect some
|
| 177 |
+
# changes in the elements in the session_state (x=same)
|
| 178 |
+
# x container_file_uploader exists
|
| 179 |
+
# x container_metadata_inputs exists
|
| 180 |
+
# - observations 2 elements, keys -> some hashes. values: InputObservation objects
|
| 181 |
+
# - image_hashes 2 elements, hashes (str) |
|
| 182 |
+
# - images {} 2 elements, keys -> hashes, values -> np.ndarray.
|
| 183 |
+
# - files {} now a LIST! check how it is read. Anyway, a list of MockUploadedFile objects
|
| 184 |
+
# x public_observations {}
|
| 185 |
+
# I think just verify the sizes and types, we could do a data integrity
|
| 186 |
+
# check on the hashes matching everywhere, but that is far from visual.
|
| 187 |
+
|
| 188 |
+
assert len(at.session_state.observations) == num_files
|
| 189 |
+
for obs in at.session_state.observations.values():
|
| 190 |
+
assert isinstance(obs, InputObservation)
|
| 191 |
+
assert len(at.session_state.image_hashes) == num_files
|
| 192 |
+
for hash in at.session_state.image_hashes:
|
| 193 |
+
assert isinstance(hash, str)
|
| 194 |
+
assert len(at.session_state.images) == num_files
|
| 195 |
+
for img in at.session_state.images.values():
|
| 196 |
+
assert isinstance(img, np.ndarray)
|
| 197 |
+
assert len(at.session_state.image_hashes) == num_files
|
| 198 |
+
for hash in at.session_state.image_hashes:
|
| 199 |
+
assert isinstance(hash, str)
|
| 200 |
+
assert len(at.session_state.files) == num_files
|
| 201 |
+
for file in at.session_state.files:
|
| 202 |
+
assert isinstance(file, MockUploadedFile)
|
| 203 |
+
assert isinstance(file, BytesIO) # cool it looks like the FileUploader.
|
| 204 |
+
#assert isinstance(file, UploadedFile) no... it isn't but bytesIO is the parent class
|
| 205 |
+
|
| 206 |
+
assert at.session_state.public_observations == {}
|
| 207 |
+
|
| 208 |
+
# and then there are plenty of visual elements, based on the image hashes.
|
| 209 |
+
for hash in at.session_state.image_hashes:
|
| 210 |
+
# check that each of the 4 inputs is present
|
| 211 |
+
assert at.sidebar.text_input(key=f"input_latitude_{hash}") is not None
|
| 212 |
+
assert at.sidebar.text_input(key=f"input_longitude_{hash}") is not None
|
| 213 |
+
assert at.sidebar.date_input(key=f"input_date_{hash}") is not None
|
| 214 |
+
assert at.sidebar.time_input(key=f"input_time_{hash}") is not None
|
| 215 |
+
|
| 216 |
+
#elem = at.get('text_input',key=f"input_latitude_{hash}")
|
| 217 |
+
#print(f"found element: {elem}")
|
| 218 |
+
#assert f"input_latitude_{hash}" in at.text_input.values()
|
| 219 |
+
|
| 220 |
+
# finally we can check the main area, where the metadata is displayed
|
| 221 |
+
# since we uplaoded num_files files, hopefully we get num_files text areas
|
| 222 |
+
assert len(at.text_area) == num_files
|
| 223 |
+
# expecting
|
| 224 |
+
exp0 = "index: 0, name: cakes.jpg, datetime: 2024:10:24 15:59:45, lat: 46.51860277777778, lon:6.562075"
|
| 225 |
+
exp1 = "index: 1, name: cakes_no_exif_datetime.jpg, datetime: None, lat: 46.51860277777778, lon:6.562075"
|
| 226 |
+
exp2 = "index: 2, name: cakes_no_exif_gps.jpg, datetime: 2024:10:24 15:59:45, lat: None, lon:None"
|
| 227 |
+
|
| 228 |
+
assert at.text_area[0].value == exp0
|
| 229 |
+
assert at.text_area[1].value == exp1
|
| 230 |
+
if num_files >= 1:
|
| 231 |
+
assert at.text_area(key='metadata_0').value == exp0
|
| 232 |
+
if num_files >= 2:
|
| 233 |
+
assert at.text_area(key='metadata_1').value == exp1
|
| 234 |
+
if num_files >= 3:
|
| 235 |
+
assert at.text_area(key='metadata_2').value == exp2
|