rmm commited on
Commit
35f3306
·
1 Parent(s): 998f2aa

test: debugging the mock / side-effect usage

Browse files

- the rerun exec model of streamlit causes the mock's side effect to be
called multiple times, and this invalidates the data beyond the first
call. The true object doesn't behave in the same way (because the
callback is only triggered when the input files are changed). New
wrapper here gives stable behaviour

Files changed (1) hide show
  1. tests/test_demo_input_sidebar.py +39 -8
tests/test_demo_input_sidebar.py CHANGED
@@ -21,17 +21,52 @@ from test_demo_multifile_upload import (
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
@@ -222,10 +257,6 @@ def test_two_input_files_realdata(mock_file_rv: MagicMock, mock_uploadedFile_Lis
222
  assert at.sidebar.text_input(key=f"input_longitude_{hash}") is not None
223
  assert at.sidebar.date_input(key=f"input_date_{hash}") is not None
224
  assert at.sidebar.time_input(key=f"input_time_{hash}") is not None
225
-
226
- #elem = at.get('text_input',key=f"input_latitude_{hash}")
227
- #print(f"found element: {elem}")
228
- #assert f"input_latitude_{hash}" in at.text_input.values()
229
 
230
  if 'demo_input_sidebar' in SCRIPT_UNDER_TEST:
231
  verify_metadata_in_demo_display(at, num_files)
 
21
  MockUploadedFile, )
22
 
23
 
24
+ # decorator that counts the number of times a function is called
25
+ def count_calls(func):
26
+ def wrapper(*args, **kwargs):
27
+ wrapper.called += 1
28
+ return func(*args, **kwargs)
29
+ wrapper.called = 0
30
+ return wrapper
31
 
32
+
33
+ @count_calls
34
  def wrapped_buffer_uploaded_files(*args, **kwargs):
35
  import streamlit as st
 
 
 
36
  uploaded_files = st.session_state.file_uploader_data
37
  _cprint(f"[I] buffering files in my side-effect! cool | {len(uploaded_files)}")
38
+ for i, (key, img) in enumerate(st.session_state.images.items()):
39
+ _cprint(f" - image {i}: {type(img)} [{key}]")
40
+
41
+ buffer_uploaded_files() # nowcall the real prod func
42
+ _cprint(f"[I] finished the real buffering ! cool | {len(uploaded_files)}")
43
+
44
+
45
+ @count_calls
46
+ def wrapped_buffer_uploaded_files_allowed_once(*args, **kwargs):
47
+ # this is a wrapper that only allows the real function to be called once
48
+ # - this is to prevent the side-effect from being called multiple times
49
+ # - the callback is only invoked when the input data is changed for the
50
+ # real file_uploader object; but due to the 're-run script on interaction'
51
+ # model, the side-effect is called each time.
52
+ import streamlit as st
53
+ uploaded_files = st.session_state.file_uploader_data
54
+ if len(st.session_state.images) != 0:
55
+ _cprint(f"[I] buffering already called before, side-effect! not rerun inner func | {len(uploaded_files)} | {len(st.session_state.images)}")
56
+ for i, (key, img) in enumerate(st.session_state.images.items()):
57
+ _cprint(f" - image {i}: {type(img)} [{key}]")
58
+ return
59
+
60
+ _cprint(f"[I] buffering files in my side-effect! cool | {len(uploaded_files)}")
61
+ for i, (key, img) in enumerate(st.session_state.images.items()):
62
+ _cprint(f" - image {i}: {type(img)} [{key}]")
63
+
64
  buffer_uploaded_files() # nowcall the real prod func
65
  _cprint(f"[I] finished the real buffering ! cool | {len(uploaded_files)}")
66
+
67
+
68
+
69
+ # - tests for apptest/demo_input_sidebar
70
 
71
  # zero test: no inputs
72
  # -> empty session state
 
257
  assert at.sidebar.text_input(key=f"input_longitude_{hash}") is not None
258
  assert at.sidebar.date_input(key=f"input_date_{hash}") is not None
259
  assert at.sidebar.time_input(key=f"input_time_{hash}") is not None
 
 
 
 
260
 
261
  if 'demo_input_sidebar' in SCRIPT_UNDER_TEST:
262
  verify_metadata_in_demo_display(at, num_files)