move check_single_value

This commit is contained in:
montezdesousa
2024-02-14 12:17:09 +00:00
parent ca480aa80e
commit 62507d0052
5 changed files with 31 additions and 39 deletions

View File

@@ -2,8 +2,7 @@
from typing import Dict, Optional, Set
from openbb_core.app.utils import convert_to_basemodel
from openbb_core.provider.utils.validators import check_single_value
from openbb_core.app.utils import check_single_value, convert_to_basemodel
def filter_inputs(

View File

@@ -3,6 +3,7 @@ import json
from typing import Dict, Iterable, List, Optional, Union
import numpy as np
from openbb_core.app.model.abstract.error import OpenBBError
import pandas as pd
from pydantic import ValidationError
@@ -140,3 +141,12 @@ def get_user_cache_directory() -> str:
else Preferences().cache_directory
)
return cache_dir
def check_single_value(
value: Optional[str], message: Optional[str] = None
) -> Optional[str]:
"""Check that string is a single value."""
if value and ("," in value or ";" in value):
raise OpenBBError(message if message else "multiple values not allowed")
return value

View File

@@ -1,14 +0,0 @@
"""Field validators"""
from typing import Optional
from openbb_core.app.model.abstract.error import OpenBBError
def check_single_value(
value: Optional[str], message: Optional[str] = None
) -> Optional[str]:
"""Check that string is a single value."""
if value and ("," in value or ";" in value):
raise OpenBBError(message if message else "multiple values not allowed")
return value

View File

@@ -1,8 +1,10 @@
import numpy as np
from openbb_core.app.model.abstract.error import OpenBBError
import pandas as pd
import pytest
from openbb_core.app.utils import (
basemodel_to_df,
check_single_value,
df_to_basemodel,
dict_to_basemodel,
get_target_column,
@@ -124,3 +126,21 @@ def test_ndarray_to_basemodel(array, expected):
result = ndarray_to_basemodel(array)
for r, e in zip(result, expected):
assert r.model_dump() == e.model_dump()
@pytest.mark.parametrize(
"value, expected",
[
("SYMBOL", "SYMBOL"),
(None, None),
("", ""),
("SYMBOL1,SYMBOL2", OpenBBError),
("SYMBOL1;SYMBOL2", OpenBBError),
],
)
def test_check_single_value(value, expected):
if expected is OpenBBError:
with pytest.raises(OpenBBError):
check_single_value(value)
else:
assert check_single_value(value) == expected

View File

@@ -1,23 +0,0 @@
"""Test the provider validators."""
import pytest
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.provider.utils.validators import check_single_value
@pytest.mark.parametrize(
"value, expected",
[
("SYMBOL", "SYMBOL"),
(None, None),
("", ""),
("SYMBOL1,SYMBOL2", OpenBBError),
("SYMBOL1;SYMBOL2", OpenBBError),
],
)
def test_check_single_value(value, expected):
if expected is OpenBBError:
with pytest.raises(OpenBBError):
check_single_value(value)
else:
assert check_single_value(value) == expected