Explorar o código

test: add misc test case

wangweimin %!s(int64=4) %!d(string=hai) anos
pai
achega
38c8bde400
Modificáronse 3 ficheiros con 127 adicións e 9 borrados
  1. 1 1
      test/12.cors.py
  2. 115 0
      test/13.misc.py
  3. 11 8
      test/template.py

+ 1 - 1
test/12.cors.py

@@ -23,7 +23,7 @@ def test_once(browser: Chrome, output_file, process_func):
     template.test_output(browser)
     template.test_input(browser)
     time.sleep(1)
-    return template.save_output(browser, output_file, process_func)
+    return template.save_output(browser, output_file, process_func)[0]
 
 
 def test(server_proc: subprocess.Popen, browser: Chrome):

+ 115 - 0
test/13.misc.py

@@ -0,0 +1,115 @@
+import asyncio
+import subprocess
+from functools import partial
+
+from percy import percySnapshot
+from selenium.webdriver import Chrome
+
+import pywebio
+import template
+import util
+from pywebio import start_server
+from pywebio.output import *
+from pywebio.session import *
+from pywebio.utils import *
+
+
+def target():
+    set_auto_scroll_bottom(False)
+
+    # test session data
+    g = data()
+    assert g.none is None
+    g.one = 1
+    g.one += 1
+    assert g.one == 2
+
+    # test pywebio.utils
+    async def corofunc(**kwargs):
+        pass
+
+    def genfunc(**kwargs):
+        yield
+
+    corofunc = partial(corofunc, a=1)
+    genfunc = partial(genfunc, a=1)
+
+    assert isgeneratorfunction(genfunc)
+    assert iscoroutinefunction(corofunc)
+    assert get_function_name(corofunc) == 'corofunc'
+
+    get_free_port()
+
+    # test pywebio.output
+    set_output_fixed_height(False)
+
+    try:
+        yield put_buttons([{'label': 'must not be shown'}], onclick=[lambda: None])
+    except Exception:
+        pass
+
+    put_table([
+        ['Idx', 'Actions'],
+        ['1', table_cell_buttons(['edit', 'delete'], onclick=lambda _: None)],
+    ])
+
+    popup('title', 'html content')
+    popup('title2', 'html content')
+    close_popup()
+
+    with use_scope() as name:
+        put_text('no show')
+    remove(name)
+
+    with use_scope('test') as name:
+        put_text('current scope name:%s' % name)
+
+    with use_scope('test', clear=True):
+        put_text('clear previous scope content')
+
+    @use_scope('test')
+    def scoped_func(text):
+        put_text(text)
+
+    scoped_func('text1 from `scoped_func`')
+    scoped_func('text2 from `scoped_func`')
+
+    try:
+        put_column([put_text('A'), 'error'])
+    except Exception:
+        pass
+
+    yield hold()
+
+
+async def corobased():
+    await to_coroutine(target())
+
+
+def threadbased():
+    run_as_function(target())
+
+
+def test(server_proc: subprocess.Popen, browser: Chrome):
+    asyncio.run(wait_host_port(port=8080, host='127.0.0.1'))
+
+    time.sleep(2)
+    percySnapshot(browser=browser, name='misc output')
+
+    coro_out = template.save_output(browser)[-1]
+
+    browser.get('http://localhost:8080/?app=thread')
+    time.sleep(2)
+
+    thread_out = template.save_output(browser)[-1]
+
+    assert coro_out == thread_out
+
+
+def start_test_server():
+    pywebio.enable_debug()
+    start_server({'coro': corobased, 'thread': threadbased}, port=8080, host='127.0.0.1', debug=True)
+
+
+if __name__ == '__main__':
+    util.run_test(start_test_server, test, address='http://localhost:8080/?app=coro')

+ 11 - 8
test/template.py

@@ -136,7 +136,7 @@ def basic_output():
         ], size=PopupSize.NORMAL)
 
     with use_scope('popup_btn'):
-        put_buttons(['popup()'], onclick=lambda _: show_popup())
+        put_buttons(['popup()'], onclick=[show_popup])
 
     def edit_row(choice, row):
         put_text("You click %s button at row %s" % (choice, row), scope='table_cell_buttons')
@@ -154,7 +154,9 @@ def basic_output():
 
     put_markdown('### Image')
     put_image(img_data)
-    put_image(img_data, width="30px")
+    from PIL.Image import open as pil_open
+    pil_img = pil_open(path.join(here_dir, 'assets', 'img.png'))
+    put_image(pil_img, width="30px")
     put_image('https://cdn.jsdelivr.net/gh/wang0618/pywebio/test/assets/img.png', height="50px")
 
     put_file('hello_word.txt', b'hello word!')
@@ -253,7 +255,7 @@ def basic_output():
     put_markdown('### Span')
     cell = lambda text: style(put_code(text), 'margin-right:10px;')
     put_grid([
-        [span(cell('A'), col=2), cell('B'), ],
+        [span(cell('A'), col=2), None],
         [span(cell('C'), row=2, col=2), span(cell('D'), row=2)],
         [],
     ], cell_width='1fr', cell_height='1fr')
@@ -661,18 +663,19 @@ def test_defer_call():
     os.remove('test_defer.tmp')
 
 
-def save_output(browser: Chrome, filename, process_func=None):
+def save_output(browser: Chrome, filename=None, process_func=None):
     """获取输出区html源码,并去除随机元素,供之后diff比较
 
     :param browser:
-    :param filename:
+    :param filename: 保存文件名, 为 None 时,不保存为文件
     :param process_func: 自定义数据处理函数
-    :return: 原始html文本
+    :return: 处理前后的html文本
     """
     raw_html = browser.find_element_by_id('markdown-body').get_attribute('innerHTML')
     html = re.sub(r"WebIO.DisplayAreaButtonOnClick\(.*?\)", '', raw_html)
     html = re.sub(r"</(.*?)>", r'</\g<1>>\n', html)  # 进行断行方便后续的diff判断
     if process_func:
         html = process_func(html)
-    open(path.join(here_dir, 'output', filename), 'w').write(html)
-    return raw_html
+    if filename:
+        open(path.join(here_dir, 'output', filename), 'w').write(html)
+    return raw_html, html