Skip to content

Commit

Permalink
refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
stat committed Jan 17, 2025
1 parent f070650 commit de5979b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cdp_agentkit_core.actions.cdp_action import CdpAction
from cdp_agentkit_core.actions.cdp_action import CdpAction # noqa: I001

from cdp_agentkit_core.actions.address_reputation import AddressReputationAction
from cdp_agentkit_core.actions.deploy_nft import DeployNftAction
from cdp_agentkit_core.actions.deploy_token import DeployTokenAction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re
from collections.abc import Callable

from cdp import Address
from pydantic import BaseModel, Field, field_validator
import re

from cdp_agentkit_core.actions import CdpAction

Expand Down Expand Up @@ -31,7 +31,20 @@ class AddressReputationInput(BaseModel):
)

@field_validator("address")
@classmethod
def validate_address(cls, v: str) -> str:
"""Validate that the provided address is a valid Ethereum address.
Args:
v (str): The address string to validate
Returns:
str: The validated address string
Raises:
ValueError: If the address format is invalid
"""
if not re.match(r"^0x[a-fA-F0-9]{40}$", v):
raise ValueError("Invalid Ethereum address format")
return v
Expand All @@ -46,6 +59,7 @@ def check_address_reputation(address: str, network: str) -> str:
Returns:
str: A string containing the reputation json data or error message
"""
try:
address = Address(network, address)
Expand Down
27 changes: 15 additions & 12 deletions cdp-agentkit-core/python/tests/actions/test_address_reputation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from unittest.mock import patch

import pytest
from cdp.address_reputation import (
AddressReputation,
AddressReputationMetadata,
AddressReputationModel,
)

from cdp.address_reputation import AddressReputation, AddressReputationMetadata, AddressReputationModel
from cdp_agentkit_core.actions.address_reputation import (
AddressReputationAction,
AddressReputationInput,
Expand Down Expand Up @@ -47,9 +51,8 @@ def test_address_reputation_input_model_invalid_address():
)


def test_address_reputation_success(address_factory):
def test_address_reputation_success():
"""Test successful address reputation check."""
# Create the model and reputation instances
mock_model = AddressReputationModel(
score=85,
metadata=AddressReputationMetadata(
Expand All @@ -67,27 +70,27 @@ def test_address_reputation_success(address_factory):
)
mock_reputation = AddressReputation(model=mock_model)

with patch('cdp_agentkit_core.actions.address_reputation.Address') as MockAddress:
mock_address_instance = MockAddress.return_value
with patch('cdp_agentkit_core.actions.address_reputation.Address') as mock_address:
mock_address_instance = mock_address.return_value
mock_address_instance.reputation.return_value = mock_reputation

action_response = check_address_reputation(MOCK_ADDRESS, MOCK_NETWORK)
expected_response = str(mock_reputation)

MockAddress.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS)
mock_address.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS)
mock_address_instance.reputation.assert_called_once()
assert action_response == expected_response


def test_address_reputation_failure(address_factory):
def test_address_reputation_failure():
"""Test address reputation check failure."""
with patch('cdp_agentkit_core.actions.address_reputation.Address') as MockAddress:
mock_address_instance = MockAddress.return_value
with patch('cdp_agentkit_core.actions.address_reputation.Address') as mock_address:
mock_address_instance = mock_address.return_value
mock_address_instance.reputation.side_effect = Exception("API error")

action_response = check_address_reputation(MOCK_ADDRESS, MOCK_NETWORK)
expected_response = "Error checking address reputation: API error"

MockAddress.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS)
mock_address.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS)
mock_address_instance.reputation.assert_called_once()
assert action_response == expected_response
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Wallet, Address } from "@coinbase/coinbase-sdk";
import { AddressReputationAction, AddressReputationInput } from "../actions/cdp/address_reputation";
import { Address } from "@coinbase/coinbase-sdk";
import { AddressReputationAction } from "../actions/cdp/address_reputation";

const MOCK_ADDRESS = "0x1234567890123456789012345678901234567890";
const MOCK_NETWORK = "base-sepolia";

jest.mock("@coinbase/coinbase-sdk", () => ({
Address: jest.fn()
Address: jest.fn(),
}));

describe("Address Reputation Input", () => {
Expand Down Expand Up @@ -53,6 +53,7 @@ describe("Address Reputation Action", () => {
});

it("should successfully check address reputation", async () => {
// TODO: ask John if there is a better way...
const mockReputation = {
score: 85,
metadata: {
Expand All @@ -65,12 +66,12 @@ describe("Address Reputation Action", () => {
bridge_transactions_performed: 5,
lend_borrow_stake_transactions: 10,
ens_contract_interactions: 2,
smart_contract_deployments: 1
smart_contract_deployments: 1,
},
toString: () => "Address Reputation: (score=85, metadata=(...))"
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as unknown as jest.Mocked<any>;

mockAddress.reputation.mockResolvedValue(mockReputation as any);
mockAddress.reputation.mockResolvedValue(mockReputation);

const args = {
network: MOCK_NETWORK,
Expand All @@ -80,6 +81,7 @@ describe("Address Reputation Action", () => {
const action = new AddressReputationAction();
const response = await action.func(args);

console.log(mockReputation.toString());
expect(response).toBe(mockReputation.toString());
});

Expand Down

0 comments on commit de5979b

Please sign in to comment.