File size: 3,850 Bytes
6bedc74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import pytest

import src.tools.database as database_tool
import src.tools.external_api as external_api_tool
from src.tools.calculator import calculator


def test_calculator_basic_math():
    result = calculator("25 * 8")

    assert result["expression"] == "25 * 8"
    assert result["result"] == 200


def test_calculator_respects_operator_precedence():
    result = calculator("10 + 5 * 2")

    assert result["result"] == 20


def test_calculator_rejects_code_execution():
    with pytest.raises(ValueError):
        calculator(
            "__import__('os').system('echo hacked')"
        )


def test_calculator_rejects_division_by_zero():
    with pytest.raises(
        ValueError,
        match="Division by zero",
    ):
        calculator("10 / 0")


def test_inventory_category_search(
    tmp_path,
    monkeypatch,
):
    test_db = tmp_path / "operations.db"

    monkeypatch.setattr(
        database_tool,
        "DB_PATH",
        test_db,
    )

    result = database_tool.search_inventory(
        category="Electronics"
    )

    assert result["count"] == 3

    names = {
        item["item_name"]
        for item in result["items"]
    }

    assert names == {
        "Laptop",
        "Monitor",
        "Keyboard",
    }


def test_inventory_item_search(
    tmp_path,
    monkeypatch,
):
    test_db = tmp_path / "operations.db"

    monkeypatch.setattr(
        database_tool,
        "DB_PATH",
        test_db,
    )

    result = database_tool.search_inventory(
        item_name="chair"
    )

    assert result["count"] == 1
    assert (
        result["items"][0]["item_name"]
        == "Office Chair"
    )


class FakeResponse:
    def __init__(
        self,
        payload,
        status_code=200,
    ):
        self._payload = payload
        self.status_code = status_code

    def raise_for_status(self):
        return None

    def json(self):
        return self._payload


def _world_bank_payload():
    return [
        {
            "page": 1,
            "pages": 1,
            "total": 1,
        },
        [
            {
                "id": "JPN",
                "iso2Code": "JP",
                "name": "Japan",
                "region": {
                    "value": "East Asia & Pacific"
                },
                "incomeLevel": {
                    "value": "High income"
                },
                "lendingType": {
                    "value": "Not classified"
                },
                "capitalCity": "Tokyo",
                "longitude": "139.77",
                "latitude": "35.67",
            }
        ],
    ]


def test_country_lookup_success(
    monkeypatch,
):
    def fake_get(*args, **kwargs):
        return FakeResponse(
            _world_bank_payload()
        )

    monkeypatch.setattr(
        external_api_tool.requests,
        "get",
        fake_get,
    )

    result = external_api_tool.lookup_country(
        "Japan"
    )

    assert result["name"] == "Japan"
    assert result["iso2_code"] == "JP"
    assert result["iso3_code"] == "JPN"
    assert result["capital"] == "Tokyo"
    assert (
        result["region"]
        == "East Asia & Pacific"
    )
    assert (
        result["income_level"]
        == "High income"
    )


def test_country_lookup_not_found(
    monkeypatch,
):
    def fake_get(*args, **kwargs):
        return FakeResponse(
            [
                {
                    "page": 1,
                    "pages": 1,
                    "total": 0,
                },
                [],
            ]
        )

    monkeypatch.setattr(
        external_api_tool.requests,
        "get",
        fake_get,
    )

    with pytest.raises(
        ValueError,
        match="No country found",
    ):
        external_api_tool.lookup_country(
            "DefinitelyNotARealCountryXYZ"
        )