-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement test for
remove_fraction_wei
- Loading branch information
1 parent
5a7fcd9
commit 73567ce
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/valory/skills/decision_maker_abci/tests/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2021-2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This module contains the tests for valory/decision_maker_abci.""" | ||
|
||
from aea.configurations.base import PublicId | ||
|
||
|
||
PUBLIC_ID = PublicId.from_str("valory/decision_maker_abci:0.1.0") |
20 changes: 20 additions & 0 deletions
20
packages/valory/skills/decision_maker_abci/tests/behaviours/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2021-2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This module contains the tests for valory/decision_maker_abci's behaviours.""" |
68 changes: 68 additions & 0 deletions
68
packages/valory/skills/decision_maker_abci/tests/behaviours/test_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2021-2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This module contains the tests for valory/decision_maker_abci's base behaviour.""" | ||
|
||
import re | ||
from typing import Tuple | ||
|
||
import pytest | ||
from hypothesis import given, settings | ||
from hypothesis import strategies as st | ||
from hypothesis.strategies import composite | ||
|
||
from packages.valory.skills.decision_maker_abci.behaviours.base import ( | ||
remove_fraction_wei, | ||
) | ||
from packages.valory.skills.decision_maker_abci.tests.conftest import profile_name | ||
|
||
|
||
settings.load_profile(profile_name) | ||
FRACTION_REMOVAL_PRECISION = 2 | ||
|
||
|
||
@composite | ||
def remove_fraction_args(draw: st.DrawFn) -> Tuple[int, float, int]: | ||
"""A strategy for building the values of the `test_remove_fraction_wei` with the desired constraints.""" | ||
amount = draw(st.integers()) | ||
fraction = draw(st.floats(min_value=0, max_value=1)) | ||
keep_percentage = 1 - fraction | ||
assert 0 <= keep_percentage <= 1 | ||
expected = int(amount * keep_percentage) | ||
return amount, fraction, expected | ||
|
||
|
||
@given(remove_fraction_args()) | ||
def test_remove_fraction_wei(strategy: Tuple[int, float, int]) -> None: | ||
"""Test the `remove_fraction_wei` function.""" | ||
amount, fraction, expected = strategy | ||
assert remove_fraction_wei(amount, fraction) == expected | ||
|
||
|
||
@given( | ||
amount=st.integers(), | ||
fraction=st.floats().filter(lambda x: x < 0 or x > 1), | ||
) | ||
def test_remove_fraction_wei_incorrect_fraction(amount: int, fraction: float) -> None: | ||
"""Test the `remove_fraction_wei` function.""" | ||
with pytest.raises( | ||
ValueError, | ||
match=re.escape(f"The given fraction {fraction!r} is not in the range [0, 1]."), | ||
): | ||
remove_fraction_wei(amount, fraction) |
34 changes: 34 additions & 0 deletions
34
packages/valory/skills/decision_maker_abci/tests/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2021-2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""Conftest module for decision maker tests.""" | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
from hypothesis import settings | ||
|
||
|
||
# pylint: skip-file | ||
|
||
|
||
CI = "CI" | ||
PACKAGE_DIR = Path(__file__).parent.parent | ||
settings.register_profile(CI, deadline=5000) | ||
profile_name = ("default", "CI")[bool(os.getenv("CI"))] |