8000 Update test suite by ntoll · Pull Request #2181 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Update test suite #2181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6650809
WiP pyscript.web tests pass with upytest.
ntoll Sep 10, 2024
ec20298
Test refactoring. WiP
ntoll Sep 13, 2024
762ae19
Completed refactor of old integration tests to new Python tests.
ntoll Sep 17, 2024
b35fb8f
Added comprehensive test suite for Python based pyodide module.
ntoll Sep 19, 2024
806e574
Black.
ntoll Sep 19, 2024
177cbb9
Rebuld and check websocket attribute assignment via init and as attri…
ntoll Sep 23, 2024
091da6e
Update when tests to allow for worker round trips.
ntoll Sep 23, 2024
4a6c5fa
Post build
ntoll Sep 23, 2024
790fd26
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 23, 2024
ae66d13
Fixed config issue via polyscript update (#2182)
WebReflection Sep 24, 2024
e3c572d
WiP pyscript.web tests pass with upytest.
ntoll Sep 10, 2024
aa83e6c
Test refactoring. WiP
ntoll Sep 13, 2024
263f1fc
Completed refactor of old integration tests to new Python tests.
ntoll Sep 17, 2024
0eb8c43
Added comprehensive test suite for Python based pyodide module.
ntoll Sep 19, 2024
6c81b5f
Black.
ntoll Sep 19, 2024
43f25fd
Rebuld and check websocket attribute assignment via init and as attri…
ntoll Sep 23, 2024
344e54a
Update when tests to allow for worker round trips.
ntoll Sep 23, 2024
f24a2cf
Post build
ntoll Sep 23, 2024
6e52918
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 23, 2024
06216ac
Merge branch 'update-test-suite' of github.com:pyscript/pyscript into…
ntoll Sep 24, 2024
abc3a3b
Fix a couple of timing issues in display and web tests.
ntoll Sep 24, 2024
0d7ef9d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2024
57e023e
Black
ntoll Sep 24, 2024
58d905d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2024
7da2ba6
Add integration tests to Makefile (and CI)
ntoll Sep 24, 2024
f826d0b
Remove un-needed upload action.
ntoll Sep 24, 2024
af6cc2b
Ensure fails are properly logged as an array. Remove the explicit tes…
ntoll Sep 24, 2024
aa70295
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 24, 2024
19bc612
Directory reorg/refactor. Updated docs.
ntoll Sep 25, 2024
60622c9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
c52a695
Bump polyscript.
ntoll Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added comprehensive test suite for Python based pyodide module.
  • Loading branch information
ntoll committed Sep 23, 2024
commit b35fb8fd420b1603be9b202fb53445d2db254fe6
7 changes: 7 additions & 0 deletions pyscript.core/tests/integration/python/example_js_module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
A simple JavaScript module to test the integration with Python.
*/

export function hello() {
return "Hello from JavaScript!";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
A simple JavaScript module to test the integration with Python on a worker.
*/

export function hello() {
return "Hello from JavaScript in a web worker!";
}
22 changes: 20 additions & 2 deletions pyscript.core/tests/integration/python/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
{
"files": {
"https://raw.githubusercontent.com/ntoll/upytest/1.0.5/upytest.py": "",
"./tests/test_web.py": "tests/test_web.py",
"./tests/test_config.py": "tests/test_config.py",
"./tests/test_current_target.py": "tests/test_current_target.py",
"./tests/test_display.py": "tests/test_display.py",
"./tests/test_when.py": "tests/test_when.py"
"./tests/test_document.py": "tests/test_document.py",
"./tests/test_fetch.py": "tests/test_fetch.py",
"./tests/test_ffi.py": "tests/test_ffi.py",
"./tests/test_js_modules.py": "tests/test_js_modules.py",
"./tests/test_storage.py": "tests/test_storage.py",
"./tests/test_running_in_worker.py": "tests/test_running_in_worker.py",
"./tests/test_web.py": "tests/test_web.py",
"./tests/test_websocket.py": "tests/test_websocket.py",
"./tests/test_when.py": "tests/test_when.py",
"./tests/test_window.py": "tests/test_window.py"
},
"js_modules": {
"main": {
"./example_js_module.js": "greeting"
},
"worker": {
"./example_js_worker_module.js": "greeting_worker"
}
}
}
20 changes: 20 additions & 0 deletions pyscript.core/tests/integration/python/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Tests for the pyscript.config dictionary.
"""

from pyscript import config, fetch, document


async def test_config_reads_expected_settings_correctly():
"""
The config dictionary should read expected settings for this test suite.

Just grab the raw JSON for the settings and compare it to the config
dictionary.
"""
url = document.location.href.rsplit("/", 1)[0] + "/settings.json"
raw_config = await fetch(url).json()
for key, value in raw_config.items():
assert (
config[key] == value
), f"Expected {key} to be {value}, got {config[key]}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Ensure the pyscript.current_target function returns the expected target
element's id.
"""

from pyscript import current_target, RUNNING_IN_WORKER
from upytest import is_micropython


def test_current_target():
"""
The current_target function should return the expected target element's id.
"""
expected = "py-0"
if is_micropython:
if RUNNING_IN_WORKER:
expected = "mpy-w0-target"
else:
expected = "mpy-0"
elif RUNNING_IN_WORKER:
expected = "py-w0-target"
assert (
current_target() == expected
), f"Expected {expected} got {current_target()}"
35 changes: 0 additions & 35 deletions pyscript.core/tests/integration/python/tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import re
import upytest
import asyncio


from pyscript import display, web, HTML, RUNNING_IN_WORKER, py_import
Expand Down Expand Up @@ -33,7 +32,6 @@ async def setup():
container.replaceChildren()
target_container = web.page.find("#test-element-container")[0]
target_container.innerHTML = ""
await asyncio.sleep(0.01)


async def teardown():
Expand All @@ -45,7 +43,6 @@ async def teardown():
container.replaceChildren()
target_container = web.page.find("#test-element-container")[0]
target_container.innerHTML = ""
await asyncio.sleep(0.01)


def test_simple_display():
Expand Down Expand Up @@ -142,36 +139,6 @@ def test_tag_target_attribute():
assert target.innerText == "item 2"


@upytest.skip("CHECK: test consecutive script tags with display in JS")
def test_consecutive_display_target():
self.pyscript_run(
"""
<script type="py" id="first" async="false">
from pyscript import display
display('hello 1')
</script>
<p>hello in between 1 and 2</p>
<script type="py" id="second" async="false">
from pyscript import display
display('hello 2', target="second")
</script>
<script type="py" id="third" async="false">
from pyscript import display
display('hello 3')
</script>
"""
)
inner_text = self.page.inner_text("body")
lines = inner_text.splitlines()
lines = [line for line in filter_page_content(lines)] # remove empty lines
assert lines == [
"hello 1",
"hello in between 1 and 2",
"hello 2",
"hello 3",
]


def test_multiple_display_calls_same_tag():
"""
Multiple display calls in the same script tag should be displayed in order.
Expand Down Expand Up @@ -215,7 +182,6 @@ async def test_display_multiple_values():
Display multiple values in the same call.
"""
display("hello", "world")
await asyncio.sleep(0.01)
container = get_display_container()
assert container.innerText == "hello\nworld", container.innerText

Expand Down Expand Up @@ -260,7 +226,6 @@ async def test_display_list_dict_tuple():
d = {"B": 2, "List": l}
t = ("C", 3, "!")
display(l, d, t)
await asyncio.sleep(0.01)
container = get_display_container()
l2, d2, t2 = container.innerText.split("\n")
assert l == eval(l2)
Expand Down
17 changes: 17 additions & 0 deletions pyscript.core/tests/integration/python/tests/test_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Sanity check for the pyscript.document object.
"""

from pyscript import document


def test_document():
"""
The document object should be available and we can change its attributes
(in this case, the title).
"""
title = document.title
assert title
document.title = "A new title"
assert document.title == "A new title"
document.title = title
83 changes: 83 additions & 0 deletions pyscript.core/tests/integration/python/tests/test_fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Ensure the pyscript.test function behaves as expected.
"""

from pyscript import fetch


async def test_fetch_json():
"""
The fetch function should return the expected JSON response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
data = await response.json()
assert data["userId"] == 1
assert data["id"] == 1
assert data["title"] == "delectus aut autem"
assert data["completed"] is False


async def test_fetch_text():
"""
The fetch function should return the expected text response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
text = await response.text()
assert "delectus aut autem" in text
assert "completed" in text
assert "false" in text
assert "1" in text


async def test_fetch_bytearray():
"""
The fetch function should return the expected bytearray response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
data = await response.bytearray()
assert b"delectus aut autem" in data
assert b"completed" in data
assert b"false" in data
assert b"1" in data


async def test_fetch_array_buffer():
"""
The fetch function should return the expected array buffer response.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
data = await response.arrayBuffer()
bytes_ = bytes(data)
assert b"delectus aut autem" in bytes_
assert b"completed" in bytes_
assert b"false" in bytes_
assert b"1" in bytes_


async def test_fetch_ok():
"""
The fetch function should return a response with ok set to True for an
existing URL.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1")
assert response.ok
assert response.status == 200
data = await response.json()
assert data["userId"] == 1
assert data["id"] == 1
assert data["title"] == "delectus aut autem"
assert data["completed"] is False


async def test_fetch_not_ok():
"""
The fetch function should return a response with ok set to False for a
non-existent URL.
"""
response = await fetch("https://jsonplaceholder.typicode.com/todos/1000")
assert not response.ok
assert response.status == 404
39 changes: 39 additions & 0 deletions pyscript.core/tests/integration/python/tests/test_ffi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Exercise (as much as is possible) the pyscript.ffi namespace.
"""
import upytest
from pyscript import ffi


def test_create_proxy():
"""
The create_proxy function should return a proxy object that is callable.
"""

def func():
return 42

proxy = ffi.create_proxy(func)
assert proxy() == 42
if upytest.is_micropython:
from jsffi import JsProxy
else:
from pyodide.ffi import JsProxy
assert isinstance(proxy, JsProxy)


def test_to_js():
"""
The to_js function should convert a Python object to a JavaScript object.
In this instance, a Python dict should be converted to a JavaScript object
represented by a JsProxy object.
"""
obj = {"a": 1, "b": 2}
js_obj = ffi.to_js(obj)
assert js_obj.a == 1
assert js_obj.b == 2
if upytest.is_micropython:
from jsffi import JsProxy
else:
from pyodide.ffi import JsProxy
assert isinstance(js_obj, JsProxy)
48 changes: 48 additions & 0 deletions pyscript.core/tests/integration/python/tests/test_js_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Ensure referenced JavaScript modules are available via the pyscript.js_modules
object.
"""
import upytest
from pyscript import RUNNING_IN_WORKER


@upytest.skip("Main thread only.", skip_when=RUNNING_IN_WORKER)
def test_js_module_is_available_on_main():
"""
The "hello" function in the example_js_module.js file is available via the
js_modules object while running in the main thread. See the settings.json
file for the configuration that makes this possible.
"""
from pyscript.js_modules import greeting
assert greeting.hello() == "Hello from JavaScript!"


@upytest.skip("Worker only.", skip_when=not RUNNING_IN_WORKER)
def test_js_module_is_available_on_worker():
"""
The "hello" function in the example_js_module.js file is available via the
js_modules object while running in a worker. See the settings.json file for
the configuration that makes this possible.
"""
from pyscript.js_modules import greeting
assert greeting.hello() == "Hello from JavaScript!"


@upytest.skip("Worker only.", skip_when=not RUNNING_IN_WORKER)
def test_js_module_is_available_on_worker():
"""
The "hello" function in the example_js_worker_module.js file is available
via the js_modules object while running in a worker.
"""
from pyscript.js_modules import greeting_worker
assert greeting_worker.hello() == "Hello from JavaScript in a web worker!"


@upytest.skip("Main thread only.", skip_when=RUNNING_IN_WORKER)
def test_js_worker_module_is_not_available_on_main():
"""
The "hello" function in the example_js_worker_module.js file is not
available via the js_modules object while running in the main thread.
"""
with upytest.raises(ImportError):
from pyscript.js_modules import greeting_worker
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Ensure the pyscript.RUNNING_IN_WORKER flag is set correctly (a sanity check).
"""
import upytest
from pyscript import document, RUNNING_IN_WORKER


# In the test suite, runnint in a worker is flagged by the presence of the
# "worker" query string. We do this to avoid using RUNNING_IN_WORKER to skip
# tests that check RUNNING_IN_WORKER.
in_worker = "worker" in document.location.search.lower()


@upytest.skip("Main thread only.", skip_when=in_worker)
def test_running_in_main():
"""
The flag should be False.
"""
assert RUNNING_IN_WORKER is False


@upytest.skip("Worker only.", skip_when=not in_worker)
def test_running_in_worker():
"""
The flag should be True.
"""
assert RUNNING_IN_WORKER is True
Loading
0