Skip to content

Commit 4514046

Browse files
Add simple pytests (#310)
A couple of simple tests that build and inspect the generated encoderfiles using the python API have been added. They still need to be added, together with the stubtest checks, into the overall pre-commit action.
1 parent aa69438 commit 4514046

7 files changed

Lines changed: 87 additions & 8 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
encoderfile:
2+
name: my-model-2
3+
path: models/token_classification
4+
model_type: token_classification
5+
output_path: ./test-model.encoderfile
6+
transform: |
7+
--- Applies a softmax across token classification logits.
8+
--- Each token classification is normalized independently.
9+
---
10+
--- Args:
11+
--- arr (Tensor): A tensor of shape [batch_size, n_tokens, n_labels].
12+
--- The softmax is applied along the third axis (n_labels).
13+
---
14+
--- Returns:
15+
--- Tensor: The input tensor with softmax-normalized embeddings.
16+
---@param arr Tensor
17+
---@return Tensor
18+
function Postprocess(arr)
19+
return arr:softmax(3)
20+
end

test_config_lua.yml renamed to encoderfile-py/python/tests/assets/test_config_lua.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
encoderfile:
22
name: my-model-2-lua
3-
path: models/dummy_electra_token_classifier
3+
path: models/token_classification
44
model_type: token_classification
5-
output_path: ./test-model-lua.encoderfile
5+
output_path: ./test-model.encoderfile
66
lua_libs:
77
- table
88
- math
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
3+
import yaml
4+
5+
6+
def asset_path(filename: str) -> str:
7+
"""
8+
Returns the absolute path to an asset file in the assets directory,
9+
regardless of the current working directory.
10+
"""
11+
base_dir = os.path.dirname(os.path.abspath(__file__))
12+
return os.path.join(base_dir, "assets", filename)
13+
14+
15+
def load_yaml_asset(filename):
16+
"""
17+
Loads a yaml asset file from the assets directory.
18+
"""
19+
path = asset_path(filename)
20+
if filename.endswith((".yml", ".yaml")):
21+
with open(path, "r", encoding="utf-8") as f:
22+
return yaml.safe_load(f)
23+
else:
24+
raise ValueError("Only yaml files are supported for this fixture.")

encoderfile-py/python/tests/test_all.py

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
from encoderfile import (
3+
EncoderfileBuilder,
4+
ModelConfig,
5+
EncoderfileConfig,
6+
InspectInfo,
7+
inspect,
8+
)
9+
from conftest import asset_path, load_yaml_asset
10+
11+
12+
def test_encoderfilebuilder_from_config_returns_builder():
13+
config_path = asset_path("test_config.yml")
14+
builder = EncoderfileBuilder.from_config(config_path)
15+
assert isinstance(builder, EncoderfileBuilder)
16+
17+
18+
@pytest.mark.parametrize("config_filename", ["test_config.yml", "test_config_lua.yml"])
19+
def test_encoderfilebuilder_build_runs(config_filename):
20+
config_path = asset_path(config_filename)
21+
config_info = load_yaml_asset(config_filename)
22+
builder = EncoderfileBuilder.from_config(config_path)
23+
# Should not raise
24+
builder.build(working_dir=None, version=None, no_download=True)
25+
result = inspect(config_info["encoderfile"]["output_path"])
26+
assert isinstance(result, InspectInfo)
27+
assert isinstance(result.model_config, ModelConfig)
28+
assert isinstance(result.encoderfile_config, EncoderfileConfig)
29+
print(result.model_config)
30+
print(result.encoderfile_config)
31+
assert result.encoderfile_config.name == config_info["encoderfile"]["name"]
32+
assert (
33+
result.encoderfile_config.transform.strip()
34+
== config_info["encoderfile"].get("transform").strip()
35+
)
36+
assert result.encoderfile_config.lua_libs == config_info["encoderfile"].get(
37+
"lua_libs"
38+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ requires-python = ">=3.13"
77
dependencies = [ "pip>=25.2", "protobuf>=6.33.0"]
88

99
[dependency-groups]
10-
dev = [ "pre-commit>=4.3.0", "ruff>=0.14.2", "mypy>=1.19"]
10+
dev = [ "pre-commit>=4.3.0", "ruff>=0.14.2", "mypy>=1.19", "pytest"]
1111
docs = [ "mkdocs>=1.6.1", "mkdocs-include-markdown-plugin>=7.2.0", "mkdocs-material>=9.7.0",]
1212
setup = [ "onnxruntime>=1.23.1", "optimum[onnxruntime]>=2.0.0", "transformers>=4.55.0",]
1313
models = [ "torch>=2.9.0", "click", {include-group = "setup"} ]

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)