Skip to content

Commit

Permalink
fix:check runs error
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravleen-Solulab committed Oct 22, 2024
1 parent 5934b36 commit c9c0467
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

"""This package contains the tests for Decision Maker"""

from typing import Any, Dict, List

import pytest

from packages.valory.skills.abstract_round_abci.base import (
AbciAppDB,
BaseSynchronizedData,
DegenerateRound,
)
Expand All @@ -43,7 +45,7 @@
class MockSynchronizedData(BaseSynchronizedData):
"""A mock class for SynchronizedData."""

def __init__(self, db=None) -> None:
def __init__(self, db: AbciAppDB) -> None:
"""Mock function"""
super().__init__(db) # Pass db to the parent class

Expand All @@ -62,9 +64,9 @@ class TestFinalStates:
@pytest.fixture
def setup_round(self) -> tuple[MockSynchronizedData, MockContext]:
"""Fixture to set up a round instance."""
synchronized_data = MockSynchronizedData(
db="mock_db"
) # Provide a mock db value
setup_data: Dict[str, List[Any]] = {}
mock_db = AbciAppDB(setup_data)
synchronized_data = MockSynchronizedData(db=mock_db) # Provide a mock db value
context = MockContext()
return synchronized_data, context

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023-2024 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 package contains the tests for Decision Maker"""

from unittest.mock import MagicMock
Expand All @@ -35,12 +28,10 @@ def redeem_round():
synchronized_data = MagicMock(spec=BaseSynchronizedData)
context = MagicMock()
redeem_instance = RedeemRound(synchronized_data, context)

# Set initial properties
redeem_instance.block_confirmations = 0
synchronized_data.period_count = 0
synchronized_data.db = MagicMock()

return redeem_instance


Expand All @@ -54,30 +45,25 @@ def test_end_block_no_update(redeem_round):
# This ensures that block_confirmations and period_count are 0
redeem_round.block_confirmations = 0
redeem_round.synchronized_data.period_count = 0

# Mock the superclass's end_block to simulate behavior
redeem_round.synchronized_data.db.get = MagicMock(return_value="mock_value")

# Call the actual end_block method
result = redeem_round.end_block()

# Assert the result is a tuple and check for specific event
assert isinstance(result, tuple)
assert result[1] == Event.NO_REDEEMING # Adjust based on expected output


def test_end_block_with_update(redeem_round):
"""Test the end_block behavior when an update occurs."""
# Mock the super class's end_block to return a valid update
# Mock the superclass's end_block to return a valid update
update_result = (
redeem_round.synchronized_data,
Event.NO_REDEEMING,
) # Use an actual event from your enum
RedeemRound.end_block = MagicMock(return_value=update_result)

result = redeem_round.end_block()
assert result == update_result

# Ensure no database update was attempted
redeem_round.synchronized_data.db.update.assert_not_called()

Expand All @@ -86,18 +72,21 @@ def test_end_block_with_period_count_update(redeem_round):
"""Test the behavior when period_count is greater than zero."""
# Set up the necessary attributes
redeem_round.synchronized_data.period_count = 1

# Directly set nb_participants as an integer within the synchronized_data mock
redeem_round.synchronized_data.nb_participants = 3

# Set up mock return values for db.get as needed
mock_keys = RedeemRound.selection_key
for _key in mock_keys:
redeem_round.synchronized_data.db.get = MagicMock(return_value="mock_value")

# Call the actual end_block method
# Debug prints to trace the issue
print("Before calling end_block")
result = redeem_round.end_block()

# Assertions to check the result
assert isinstance(result, tuple) # Ensure it returns a tuple
assert result[1] == Event.NO_REDEEMING # Adjust based on expected behavior
print(f"After calling end_block, result: {result}")

# Add additional checks
assert result is not None, "end_block returned None"
assert isinstance(
result, tuple
), f"end_block returned {type(result)} instead of tuple"
assert result[1] == Event.NO_REDEEMING
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023-2024 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 package contains the tests for Decision Maker"""

import pytest

from packages.valory.skills.abstract_round_abci.base import get_name
from packages.valory.skills.abstract_round_abci.base import AbciAppDB, get_name
from packages.valory.skills.decision_maker_abci.payloads import SamplingPayload
from packages.valory.skills.decision_maker_abci.rounds import SamplingRound
from packages.valory.skills.decision_maker_abci.states.base import (
Expand All @@ -38,11 +31,15 @@ class MockSynchronizedData(SynchronizedData):

sampled_bet_index = 0 # Default value for sampled_bet_index

def __init__(self, db: AbciAppDB):
"""Initialize MockSynchronizedData with the given db."""
super().__init__(db)


class MockContext:
"""A mock class for context used in AbstractRound."""

def __init__(self):
def __init__(self) -> None:
"""Mock function"""
self.sender = "mock_sender"
self.bets_hash = "mock_bets_hash"
Expand All @@ -52,27 +49,27 @@ class TestSamplingRound:
"""The class for testing Sampling Round"""

@pytest.fixture
def setup_sampling_round(self):
def setup_sampling_round(self) -> SamplingRound:
"""Fixture to set up a SamplingRound instance."""
context = MockContext()
synchronized_data = MockSynchronizedData(
db=dict()
) # Passing a mock dictionary for 'db'
db=AbciAppDB({})
) # Passing a mock AbciAppDB instance
return SamplingRound(context=context, synchronized_data=synchronized_data)

def test_sampling_round_properties(self, setup_sampling_round):
def test_sampling_round_properties(
self, setup_sampling_round: SamplingRound
) -> None:
"""Test the properties of the SamplingRound class."""
sampling_round = setup_sampling_round

assert sampling_round.payload_class == SamplingPayload
assert sampling_round.done_event == Event.DONE
assert sampling_round.none_event == Event.NONE
assert sampling_round.no_majority_event == Event.NO_MAJORITY
assert sampling_round.selection_key is not None

def test_sampling_payload_initialization(self):
def test_sampling_payload_initialization(self) -> None:
"""Test the initialization of the SamplingPayload."""
# Adjust according to the actual initialization parameters
payload = SamplingPayload(
sender="mock_sender", bets_hash="mock_bets_hash", index=0
) # Added index
Expand All @@ -81,11 +78,13 @@ def test_sampling_payload_initialization(self):
assert payload.bets_hash == "mock_bets_hash"
assert payload.index == 0 # Check that the index is correctly initialized

def test_sampling_round_inherits_update_bets_round(self):
def test_sampling_round_inherits_update_bets_round(self) -> None:
"""Test that SamplingRound inherits from UpdateBetsRound."""
assert issubclass(SamplingRound, UpdateBetsRound)

def test_sampling_round_selection_key(self, setup_sampling_round):
def test_sampling_round_selection_key(
self, setup_sampling_round: SamplingRound
) -> None:
"""Test the selection key property of SamplingRound."""
sampling_round = setup_sampling_round
expected_selection_key = (
Expand All @@ -96,14 +95,11 @@ def test_sampling_round_selection_key(self, setup_sampling_round):
)
assert sampling_round.selection_key == expected_selection_key

def test_sampling_round_event_handling(self, setup_sampling_round):
def test_sampling_round_event_handling(
self, setup_sampling_round: SamplingRound
) -> None:
"""Test event handling in SamplingRound."""
sampling_round = setup_sampling_round
sampling_round.current_event = None # Simulating an initial state

# Assuming the event changes the round's state
sampling_round.current_event = Event.DONE
assert sampling_round.current_event == Event.DONE

sampling_round.current_event = Event.NO_MAJORITY
assert sampling_round.current_event == Event.NO_MAJORITY
# Simulate event handling through method calls or appropriate property checks
assert sampling_round.done_event == Event.DONE
assert sampling_round.no_majority_event == Event.NO_MAJORITY

0 comments on commit c9c0467

Please sign in to comment.