8000 Add tests for snippets in docs (#1264) · katv7/pyscript@371b5ea · GitHub
[go: up one dir, main page]

Skip to content

Commit 371b5ea

Browse files
authored
Add tests for snippets in docs (pyscript#1264)
1 parent 5319bd1 commit 371b5ea

File tree

2 files changed

+216
-2
lines changed

2 files changed

+216
-2
lines changed

docs/tutorials/requests.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Now that we have installed the dependencies, we need to patch the Requests libra
7272

7373
<py-script>
7474
import pyodide_http
75-
pyodide_http.patch()
75+
pyodide_http.patch_all()
7676
</py-script>
7777
</body>
7878
</html
@@ -104,7 +104,7 @@ Finally, let's make a request to the JSON Placeholder API to confirm that everyt
104104
import pyodide_http
105105
106106
# Patch the Requests library so it works with Pyscript
107-
pyodide_http.patch()
107+
pyodide_http.patch_all()
108108
109109
# Make a request to the JSON Placeholder API
110110
response = requests.get("https://jsonplaceholder.typicode.com/todos")
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import re
2+
3+
from .support import PyScriptTest
4+
5+
6+
class TestDocsSnippets(PyScriptTest):
7+
def test_tutorials_py_click(self):
8+
self.pyscript_run(
9+
"""
10+
<button
11+
py-click="current_time()"
12+
id="get-time" class="py-button">
13+
Get current time
14+
</button>
15+
<p id="current-time"></p>
16+
17+
<py-script>
18+
from pyscript import Element
19+
import datetime
20+
21+
def current_time():
22+
now = datetime.datetime.now()
23+
24+
# Get paragraph element by id
25+
paragraph = Element("current-time")
26+
27+
# Add current time to the paragraph element
28+
paragraph.write(now.strftime("%Y-%m-%d %H:%M:%S"))
29+
</py-script>
30+
"""
31+
)
32+
33+
assert self.console.log.lines[0] == self.PY_COMPLETE
34+
btn = self.page.wait_for_selector("#get-time")
35+
btn.click()
36+
37+
current_time = self.page.wait_for_selector("#current-time")
38+
39+
pattern = "\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+" # e.g. 08-09-2022 15:57:32
40+
assert re.search(pattern, current_time.inner_text())
41+
self.assert_no_banners()
42+
43+
def test_tutorials_requests(self):
44+
self.pyscript_run(
45+
"""
46+
<py-config>
47+
packages = ["requests", "pyodide-http"]
48+
</py-config>
49+
50+
<py-script>
51+
import requests
52+
import pyodide_http
53+
54+
# Patch the Requests library so it works with Pyscript
55+
pyodide_http.patch_all()
56+
57+
# Make a request to the JSON Placeholder API
58+
response = requests.get("https://jsonplaceholder.typicode.com/todos")
59+
print(response.json())
60+
</py-script>
61+
"""
62+
)
63+
64+
assert self.console.log.lines[0] == self.PY_COMPLETE
65+
py_terminal = self.page.wait_for_selector("py-terminal")
66+
# Just a small check to confirm that the response was received
67+
assert "userId" in py_terminal.inner_text()
68+
self.assert_no_banners()
69+
70+
def test_tutorials_py_config_fetch(self):
71+
# flake8: noqa
72+
self.pyscript_run(
73+
"""
74+
<py-config>
75+
[[fetch]]
76+
from = "https://pyscript.net/examples/"
77+
files = ["utils.py"]
78+
[[fetch]]
79+
from = "https://gist.githubusercontent.com/FabioRosado/faba0b7f6ad4438b07c9ac567c73b864/raw/37603b76dc7ef7997bf36781ea0116150f727f44/"
80+
files = ["todo.py"]
81+
</py-config>
82+
<py-script>
83+
from todo import add_task, add_task_event
84+
</py-script>
85+
<section>
86+
<div class="text-center w-full mb-8">
87+
<h1 class="text-3xl font-bold text-gray-800 uppercase tracking-tight">
88+
To Do List
89+
</h1>
90+
</div>
91+
<div>
92+
<input id="new-task-content" class=& F438 quot;py-input" type="text">
93+
<button id="new-task-btn" class="py-button" type="submit" py-click="add_task()">
94+
Add task
95+
</button>
96+
</div>
97+
<div id="list-tasks-container" class="flex flex-col-reverse mt-4"></div>
98+
<template id="task-template">
99+
<section class="task py-li-element">
100+
<label for="flex items-center p-2 ">
101+
<input class="mr-2" type="checkbox">
102+
<p class="m-0 inline"></p>
103+
</label>
104+
</section>
105+
</template
106+
"""
107+
)
108+
109+
assert self.console.log.lines[0] == self.PY_COMPLETE
110+
todo_input = self.page.locator("input")
111+
submit_task_button = self.page.locator("button")
112+
113+
todo_input.type("Fold laundry")
114+
submit_task_button.click()
115+
116+
first_task = self.page.locator("#task-0")
117+
assert "Fold laundry" in first_task.inner_text()
118+
119+
task_checkbox = first_task.locator("input")
120+
# Confirm that the new task isn't checked
121+
assert not task_checkbox.is_checked()
122+
123+
# Let's mark it as done now
124+
task_checkbox.check()
125+
126+
# Basic check that the task has the line-through class
127+
assert (
128+
'<p class="m-0 inline line-through">Fold laundry</p>'
129+
in first_task.inner_html()
130+
)
131+
self.assert_no_banners()
132+
133+
def test_tutorials_py_config_interpreter(self):
134+
self.pyscript_run(
135+
"""
136+
<py-config>
137+
[[interpreters]]
138+
src = "https://cdn.jsdelivr.net/pyodide/v0.22.0a3/full/pyodide.js"
139+
name = "pyodide-0.22.0a3"
140+
lang = "python"
141+
</py-config>
142+
<py-script>
143+
import pyodide
144+
print(pyodide.__version__)
145+
</py-script>
146+
"""
147+
)
148+
149+
assert self.console.log.lines[0] == self.PY_COMPLETE
150+
py_terminal = self.page.wait_for_selector("py-terminal")
151+
assert "0.22.0a3" in py_terminal.inner_text()
152+
self.assert_no_banners()
153+
154+
def test_tutorials_writing_to_page(self):
155+
self.pyscript_run(
156+
"""
157+
<div id="manual-write"></div>
158+
<button py-click="write_to_page()" id="manual">Say Hello</button>
159+
<div id="display-write"></div>
160+
<button py-click="display_to_div()" id="display">Say Things!</button>
161+
<div>
162+
<py-terminal>
163+
</div>
164+
<button py-click="print_to_page()" id="print">Print Things!</button>
165+
166+
<py-script>
167+
def write_to_page():
168+
manual_div = Element("manual-write")
169+
manual_div.element.innerHTML = "<p><b>Hello World</b></p>"
170+
171+
def display_to_div():
172+
display("I display things!", target="display-write")
173+
174+
def print_to_page():
175+
print("I print things!")
176+
</py-script>
177+
"""
178+
)
179+
assert self.console.log.lines[0] == self.PY_COMPLETE
180+
btn_manual = self.page.wait_for_selector("#manual")
181+
btn_display = self.page.wait_for_selector("#display")
182+
btn_print = self.page.wait_for_selector("#print")
183+
184+
btn_manual.click()
185+
manual_write_div = self.page.wait_for_selector("#manual-write")
186+
assert "<p><b>Hello World</b></p>" in manual_write_div.inner_html()
187+
188+
btn_display.click()
189+
display_write_div = self.page.wait_for_selector("#display-write")
190+
assert "I display things!" in display_write_div.inner_text()
191+
192+
btn_print.click()
193+
py_terminal = self.page.wait_for_selector("py-terminal")
194+
assert "I print things!" in py_terminal.inner_text()
195+
self.assert_no_banners()
196+
197+
def test_guides_asyncio(self):
198+
self.pyscript_run(
199+
"""
200+
<py-script>
201+
import asyncio
202+
203+
async def main():
204+
for i in range(3):
205+
print(i)
206+
207+
asyncio.ensure_future(main())
208+
</py-script>
209+
"""
210+
)
211+
assert self.console.log.lines[0] == self.PY_COMPLETE
212+
py_terminal = self.page.wait_for_selector("py-terminal")
213+
214+
assert "0\n1\n2\n" in py_terminal.inner_text()

0 commit comments

Comments
 (0)
0