Ignore invalid model sort inputs (#1653)
This commit is contained in:
@@ -14,8 +14,12 @@ function _compareText(a, b) {
|
||||
});
|
||||
}
|
||||
|
||||
function _arrayOrEmpty(models) {
|
||||
return Array.isArray(models) ? models : [];
|
||||
}
|
||||
|
||||
export function sortModelIds(models) {
|
||||
return (models || []).slice().sort(_compareText);
|
||||
return _arrayOrEmpty(models).slice().sort(_compareText);
|
||||
}
|
||||
|
||||
export function compareModelObjects(a, b) {
|
||||
@@ -25,5 +29,5 @@ export function compareModelObjects(a, b) {
|
||||
}
|
||||
|
||||
export function sortModelObjects(models) {
|
||||
return (models || []).slice().sort(compareModelObjects);
|
||||
return _arrayOrEmpty(models).slice().sort(compareModelObjects);
|
||||
}
|
||||
|
||||
59
tests/test_model_sort_js.py
Normal file
59
tests/test_model_sort_js.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import json
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
pytestmark = pytest.mark.skipif(not shutil.which("node"), reason="node binary not on PATH")
|
||||
|
||||
|
||||
def _node_eval(source: str):
|
||||
result = subprocess.run(
|
||||
["node", "--input-type=module", "-e", source],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
return json.loads(result.stdout)
|
||||
|
||||
|
||||
def test_model_sort_helpers_ignore_non_arrays():
|
||||
values = _node_eval(
|
||||
"""
|
||||
import { sortModelIds, sortModelObjects } from './static/js/modelSort.js';
|
||||
console.log(JSON.stringify({
|
||||
idsObject: sortModelIds({bad: true}),
|
||||
idsString: sortModelIds('llama'),
|
||||
objectsNull: sortModelObjects(null),
|
||||
objectsObject: sortModelObjects({bad: true})
|
||||
}));
|
||||
"""
|
||||
)
|
||||
|
||||
assert values == {
|
||||
"idsObject": [],
|
||||
"idsString": [],
|
||||
"objectsNull": [],
|
||||
"objectsObject": [],
|
||||
}
|
||||
|
||||
|
||||
def test_model_sort_helpers_keep_valid_arrays():
|
||||
values = _node_eval(
|
||||
"""
|
||||
import { sortModelIds, sortModelObjects } from './static/js/modelSort.js';
|
||||
console.log(JSON.stringify({
|
||||
ids: sortModelIds(['zeta/10', 'alpha/2', 'alpha/11']),
|
||||
objects: sortModelObjects([{id: 'zeta/10'}, {id: 'alpha/2'}]).map(m => m.id)
|
||||
}));
|
||||
"""
|
||||
)
|
||||
|
||||
assert values == {
|
||||
"ids": ["alpha/2", "zeta/10", "alpha/11"],
|
||||
"objects": ["alpha/2", "zeta/10"],
|
||||
}
|
||||
Reference in New Issue
Block a user