Skip to content

Commit 762281f

Browse files
committed
put all modules under web_modules + more test coverage
1 parent 73b2f56 commit 762281f

File tree

14 files changed

+70
-73
lines changed

14 files changed

+70
-73
lines changed

.pre-commit-config.yaml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
repos:
2-
- repo: https://github.com/ambv/black
2+
- repo: https://github.com/ambv/black
33
rev: stable
44
hooks:
5-
- id: black
6-
language_version: python3.6
7-
- repo: https://github.com/PyCQA/flake8
5+
- id: black
6+
language_version: python3.6
7+
- repo: https://github.com/PyCQA/flake8
88
rev: 3.7.9
99
hooks:
10-
- id: flake8
11-
- repo: https://github.com/pre-commit/mirrors-mypy
10+
- id: flake8
11+
- repo: https://github.com/pre-commit/mirrors-mypy
1212
rev: v0.761
1313
hooks:
14-
- id: mypy
14+
- id: mypy
1515
additional_dependencies: [pyalect]
16+
- repo: https://github.com/kynan/nbstripout
17+
rev: master
18+
hooks:
19+
- id: nbstripout
20+
files: ".ipynb"

docs/source/extras.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ While ``your_module.py`` should contain the following:
105105
106106
# dialect=html
107107
from idom import html
108-
assert html("<div/>") == {"tagName": "div"}
108+
assert html(f"<div/>") == {"tagName": "div"}
109109
110110
And that's it!
111111

examples/chart.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import React from "../web_modules/react.js";
2-
import {
3-
VictoryBar,
4-
VictoryChart,
5-
VictoryTheme,
6-
Bar,
7-
} from "../web_modules/victory.js";
8-
import htm from "../web_modules/htm.js";
1+
import React from "./react.js";
2+
import { VictoryBar, VictoryChart, VictoryTheme, Bar } from "./victory.js";
3+
import htm from "./htm.js";
94

105
const html = htm.bind(React.createElement);
116

idom/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
from .tools import Var, html_to_vdom
1717

18+
# try to automatically setup the dialect's import hook
1819
try:
1920
import pyalect
2021
import tagged
2122
import htm
22-
except ImportError:
23+
except ImportError: # pragma: no cover
2324
pass
2425
else:
2526
from . import dialect

idom/client/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
node_modules
22
web_modules
3-
user_modules

idom/client/__init__.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,31 @@
1212
CORE_MODULES = CLIENT_DIR / "core_modules"
1313
NODE_MODULES = CLIENT_DIR / "node_modules"
1414
WEB_MODULES = CLIENT_DIR / "web_modules"
15-
USER_MODULES = CLIENT_DIR / "user_modules"
1615

1716

18-
def import_path(prefix: str, name: str) -> str:
19-
path = f"../{prefix}/{name}.js"
20-
if not module_exists(prefix, name):
17+
def import_path(name: str) -> str:
18+
path = f"../{WEB_MODULES.name}/{name}.js"
19+
if not module_exists(name):
2120
raise ValueError(f"Module '{path}' does not exist.")
2221
return path
2322

2423

2524
def define_module(name: str, source: str) -> str:
26-
path = _create_module_os_path(USER_MODULES, name)
25+
path = _create_web_module_os_path(name)
2726
with path.open("w+") as f:
2827
f.write(source)
29-
return import_path("user_modules", name)
28+
return import_path(name)
3029

3130

32-
def delete_module(prefix: str, name: str) -> None:
33-
path = _find_module_os_path(prefix, name)
31+
def delete_module(name: str) -> None:
32+
path = _find_web_module_os_path(name)
3433
if path is None:
35-
raise ValueError(f"Module '{import_path(prefix, name)}' does not exist.")
34+
raise ValueError(f"Module '{name}' does not exist.")
3635
_delete_os_paths(path)
3736

3837

39-
def module_exists(prefix: Union[str, Path], name: str) -> bool:
40-
return _find_module_os_path(prefix, name) is not None
38+
def module_exists(name: str) -> bool:
39+
return _find_web_module_os_path(name) is not None
4140

4241

4342
def install(dependencies: Dict[str, str]) -> None:
@@ -65,7 +64,7 @@ def install(dependencies: Dict[str, str]) -> None:
6564

6665

6766
def restore() -> None:
68-
_delete_os_paths(WEB_MODULES, NODE_MODULES, USER_MODULES)
67+
_delete_os_paths(WEB_MODULES, NODE_MODULES)
6968
_run_subprocess(["npm", "install"], CLIENT_DIR)
7069
_run_subprocess(["npm", "run", "snowpack"], CLIENT_DIR)
7170

@@ -99,11 +98,8 @@ def _run_subprocess(args: List[str], cwd: Union[str, Path]):
9998
raise
10099

101100

102-
def _find_module_os_path(prefix: Union[str, Path], name: str) -> Optional[Path]:
103-
if isinstance(prefix, str):
104-
path = CLIENT_DIR / prefix
105-
else:
106-
path = prefix
101+
def _find_web_module_os_path(name: str) -> Optional[Path]:
102+
path = WEB_MODULES
107103
for name_part in name.split("/"):
108104
if not path.is_dir():
109105
return None
@@ -114,11 +110,8 @@ def _find_module_os_path(prefix: Union[str, Path], name: str) -> Optional[Path]:
114110
return full_path
115111

116112

117-
def _create_module_os_path(prefix: Union[str, Path], name: str) -> Path:
118-
if isinstance(prefix, str):
119-
path = CLIENT_DIR / prefix
120-
else:
121-
path = prefix
113+
def _create_web_module_os_path(name: str) -> Path:
114+
path = WEB_MODULES
122115
for n in name.split("/"):
123116
if not path.exists():
124117
path.mkdir()

idom/core/element.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818

1919

20-
if TYPE_CHECKING:
20+
if TYPE_CHECKING: # pragma: no cover
2121
from .layout import AbstractLayout
2222
from .vdom import VdomDict # noqa
2323

idom/server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
for name in ["sanic"]:
1414
try:
1515
import_module(name)
16-
except ImportError:
16+
except ImportError: # pragma: no cover
1717
pass
1818
else:
1919
import_module(__name__ + "." + name)

idom/widgets/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ def __init__(
6363
install: Union[str, bool] = False,
6464
) -> None:
6565
if install:
66-
if not client.module_exists("web_modules", package):
66+
if not client.module_exists(package):
6767
if isinstance(install, str):
6868
client.install({install: package})
6969
else:
7070
client.install({package: package})
71-
new_import_path = client.import_path("web_modules", package)
71+
new_import_path = client.import_path(package)
7272
if new_import_path is None:
7373
raise ValueError(f"Unexpectedly failed to find install of {package}")
7474
package = new_import_path

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ xfail_strict = True
2929
addopts = --cov=idom
3030

3131
[coverage:report]
32-
fail_under = 86
32+
fail_under = 87
3333
show_missing = True
3434
skip_covered = True
3535
sort = Miss
3636
exclude_lines =
3737
pragma: no cover
3838
\.\.\.
39+
raise NotImplementedError

tests/test_browser/conftest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from selenium.webdriver.chrome.options import Options
77

88
import idom
9+
from idom.server import imperative_server_mount
910
from idom.server.sanic import PerClientState
1011

1112

@@ -48,17 +49,17 @@ def display(_display):
4849

4950
@pytest.fixture(scope="session")
5051
def _display(driver):
51-
_display, element = idom.hotswap()
52-
server = ServerWithErrorCatch(element)
53-
host, port = "127.0.0.1", 5555
54-
server.daemon(host, port, debug=True)
52+
host, port = "127.0.0.1", 5000
53+
server, mount = imperative_server_mount(
54+
ServerWithErrorCatch, host, port, run_options={"debug": True}
55+
)
5556

5657
def display(element):
57-
_display(element)
58+
mount(element)
5859
driver.get(f"http://{host}:{port}/client/index.html")
5960

6061
time.sleep(1) # wait for server start
61-
return display
62+
yield display
6263

6364

6465
@pytest.fixture(scope="session")

tests/test_browser/my_chart.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import React from '../web_modules/react.js';
2-
import { VictoryBar, VictoryChart, VictoryAxis } from '../web_modules/victory.js';
3-
import htm from "../web_modules/htm.js";
1+
import React from "../react.js";
2+
import { VictoryBar, VictoryChart, VictoryAxis } from "../victory.js";
3+
import htm from "../htm.js";
44

55
const html = htm.bind(React.createElement);
66

77
const data = [
88
{ quarter: 1, earnings: 13000 },
99
{ quarter: 2, earnings: 16500 },
1010
{ quarter: 3, earnings: 14250 },
11-
{ quarter: 4, earnings: 19000 }
11+
{ quarter: 4, earnings: 19000 },
1212
];
1313

1414
function Chart() {
@@ -20,15 +20,15 @@ function Chart() {
2020
/>
2121
<${VictoryAxis}
2222
dependentAxis
23-
tickFormat=${(x) => (`$${x / 1000}k`)}
23+
tickFormat=${(x) => `$${x / 1000}k`}
2424
/>
2525
<${VictoryBar}
2626
data=${data}
2727
x="quarter"
2828
y="earnings"
2929
/>
3030
</${VictoryChart}>
31-
`
31+
`;
3232
}
3333

34-
export default { Chart: Chart }
34+
export default { Chart: Chart };

tests/test_browser/test_client.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,32 @@ def test_install(driver, display, victory):
2020

2121
driver.find_element_by_class_name("VictoryContainer")
2222

23-
assert client.module_exists("web_modules", "victory")
24-
assert client.import_path("web_modules", "victory") == "../web_modules/victory.js"
23+
assert client.module_exists("victory")
24+
assert client.import_path("victory") == "../web_modules/victory.js"
2525

2626

2727
def test_raise_on_missing_import_path():
2828
with pytest.raises(ValueError, match="does not exist"):
29-
client.import_path("web_modules", "module/that/does/not/exist")
29+
client.import_path("module/that/does/not/exist")
3030

3131

3232
def test_custom_module(driver, display, victory):
33-
my_chart = define_module("my_chart", HERE / "my_chart.js")
33+
my_chart = define_module("my/chart", HERE / "my_chart.js")
34+
35+
assert client.module_exists("my/chart")
36+
assert client.import_path("my/chart") == "../web_modules/my/chart.js"
3437

3538
display(my_chart.Chart)
3639

3740
driver.find_element_by_class_name("VictoryContainer")
3841

39-
assert client.module_exists("user_modules", "my_chart")
40-
assert (
41-
client.import_path("user_modules", "my_chart") == "../user_modules/my_chart.js"
42-
)
43-
4442

4543
def test_delete_module(victory):
46-
client.delete_module("web_modules", "victory")
47-
assert not client.module_exists("web_modules", "victory")
44+
client.delete_module("victory")
45+
assert not client.module_exists("victory")
4846

4947
with pytest.raises(ValueError, match="does not exist"):
50-
client.delete_module("web_modules", "victory")
48+
client.delete_module("victory")
5149

5250

5351
called_process_error = CalledProcessError(1, "failing-cmd")

tests/test_idom/test_tools.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66

77
def test_var_equivalence():
8-
r1 = idom.Var([1, 2, 3])
9-
r2 = idom.Var([1, 2, 3])
10-
assert r1 == r2
8+
assert idom.Var([1, 2, 3]) == idom.Var([1, 2, 3])
9+
assert idom.Var([1, 2, 3]) != idom.Var([1, 2])
10+
assert idom.Var([1, 2, 3]) != [1, 2, 3]
11+
12+
13+
def test_var_repr():
14+
assert repr(idom.Var([1, 2, 3])) == "Var([1, 2, 3])"
1115

1216

1317
def test_var_set():

0 commit comments

Comments
 (0)