CoolFace
Apppublic

FlyingNunchucks/07-tool-using-agent

sourceHugging Facemitupdated 15d agoView on Hugging Face
0likes
test_tools.py193 linesDownload Raw Back to tests
1import pytest2 3import src.tools.database as database_tool4import src.tools.external_api as external_api_tool5from src.tools.calculator import calculator6 7 8def test_calculator_basic_math():9    result = calculator("25 * 8")10 11    assert result["expression"] == "25 * 8"12    assert result["result"] == 20013 14 15def test_calculator_respects_operator_precedence():16    result = calculator("10 + 5 * 2")17 18    assert result["result"] == 2019 20 21def test_calculator_rejects_code_execution():22    with pytest.raises(ValueError):23        calculator(24            "__import__('os').system('echo hacked')"25        )26 27 28def test_calculator_rejects_division_by_zero():29    with pytest.raises(30        ValueError,31        match="Division by zero",32    ):33        calculator("10 / 0")34 35 36def test_inventory_category_search(37    tmp_path,38    monkeypatch,39):40    test_db = tmp_path / "operations.db"41 42    monkeypatch.setattr(43        database_tool,44        "DB_PATH",45        test_db,46    )47 48    result = database_tool.search_inventory(49        category="Electronics"50    )51 52    assert result["count"] == 353 54    names = {55        item["item_name"]56        for item in result["items"]57    }58 59    assert names == {60        "Laptop",61        "Monitor",62        "Keyboard",63    }64 65 66def test_inventory_item_search(67    tmp_path,68    monkeypatch,69):70    test_db = tmp_path / "operations.db"71 72    monkeypatch.setattr(73        database_tool,74        "DB_PATH",75        test_db,76    )77 78    result = database_tool.search_inventory(79        item_name="chair"80    )81 82    assert result["count"] == 183    assert (84        result["items"][0]["item_name"]85        == "Office Chair"86    )87 88 89class FakeResponse:90    def __init__(91        self,92        payload,93        status_code=200,94    ):95        self._payload = payload96        self.status_code = status_code97 98    def raise_for_status(self):99        return None100 101    def json(self):102        return self._payload103 104 105def _world_bank_payload():106    return [107        {108            "page": 1,109            "pages": 1,110            "total": 1,111        },112        [113            {114                "id": "JPN",115                "iso2Code": "JP",116                "name": "Japan",117                "region": {118                    "value": "East Asia & Pacific"119                },120                "incomeLevel": {121                    "value": "High income"122                },123                "lendingType": {124                    "value": "Not classified"125                },126                "capitalCity": "Tokyo",127                "longitude": "139.77",128                "latitude": "35.67",129            }130        ],131    ]132 133 134def test_country_lookup_success(135    monkeypatch,136):137    def fake_get(*args, **kwargs):138        return FakeResponse(139            _world_bank_payload()140        )141 142    monkeypatch.setattr(143        external_api_tool.requests,144        "get",145        fake_get,146    )147 148    result = external_api_tool.lookup_country(149        "Japan"150    )151 152    assert result["name"] == "Japan"153    assert result["iso2_code"] == "JP"154    assert result["iso3_code"] == "JPN"155    assert result["capital"] == "Tokyo"156    assert (157        result["region"]158        == "East Asia & Pacific"159    )160    assert (161        result["income_level"]162        == "High income"163    )164 165 166def test_country_lookup_not_found(167    monkeypatch,168):169    def fake_get(*args, **kwargs):170        return FakeResponse(171            [172                {173                    "page": 1,174                    "pages": 1,175                    "total": 0,176                },177                [],178            ]179        )180 181    monkeypatch.setattr(182        external_api_tool.requests,183        "get",184        fake_get,185    )186 187    with pytest.raises(188        ValueError,189        match="No country found",190    ):191        external_api_tool.lookup_country(192            "DefinitelyNotARealCountryXYZ"193        )