Spaces:
Runtime error
Runtime error
Mohammed Thameem commited on
Commit ·
23ded75
1
Parent(s): e4b1e43
WIP before rebase
Browse files- tests/test_app.py +32 -2
tests/test_app.py
CHANGED
|
@@ -1,15 +1,45 @@
|
|
| 1 |
import sys, os
|
| 2 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
import app
|
| 5 |
|
|
|
|
| 6 |
def test_calculate_footprint():
|
| 7 |
-
"""Check CO2 footprint calculation."""
|
| 8 |
total, stats = app.calculate_footprint(
|
| 9 |
car_km=10, bus_km=5, train_km=2, air_km_week=50,
|
| 10 |
meat_meals=3, vegetarian_meals=2, vegan_meals=1,
|
| 11 |
)
|
| 12 |
-
|
| 13 |
assert total > 0
|
| 14 |
assert "trees" in stats
|
| 15 |
assert isinstance(stats["trees"], int)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sys, os
|
| 2 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 3 |
+
import pytest
|
| 4 |
+
from unittest.mock import patch, MagicMock
|
| 5 |
+
|
| 6 |
+
@pytest.fixture(autouse=True, scope="session")
|
| 7 |
+
def patch_hf_classes():
|
| 8 |
+
with patch("app.AutoTokenizer") as mock_tokenizer_cls, \
|
| 9 |
+
patch("app.AutoModelForCausalLM") as mock_model_cls:
|
| 10 |
+
|
| 11 |
+
# Fake tokenizer
|
| 12 |
+
mock_tokenizer = MagicMock()
|
| 13 |
+
mock_tokenizer.pad_token_id = 0
|
| 14 |
+
mock_tokenizer.eos_token_id = 1
|
| 15 |
+
mock_tokenizer.chat_template = "{% for message in messages %}{{ message['role'] }}: {{ message['content'] }}{% endfor %}"
|
| 16 |
+
mock_tokenizer.apply_chat_template.return_value = "mock_prompt"
|
| 17 |
+
mock_tokenizer.__call__.return_value = {"input_ids": [[0, 1]]}
|
| 18 |
+
mock_tokenizer.decode.return_value = "• Easy switch"
|
| 19 |
+
|
| 20 |
+
mock_tokenizer_cls.from_pretrained.return_value = mock_tokenizer
|
| 21 |
+
|
| 22 |
+
mock_model = MagicMock()
|
| 23 |
+
mock_model.generate.return_value = [[0, 1, 2, 3]]
|
| 24 |
+
mock_model_cls.from_pretrained.return_value = mock_model
|
| 25 |
+
|
| 26 |
+
yield
|
| 27 |
|
| 28 |
import app
|
| 29 |
|
| 30 |
+
|
| 31 |
def test_calculate_footprint():
|
|
|
|
| 32 |
total, stats = app.calculate_footprint(
|
| 33 |
car_km=10, bus_km=5, train_km=2, air_km_week=50,
|
| 34 |
meat_meals=3, vegetarian_meals=2, vegan_meals=1,
|
| 35 |
)
|
|
|
|
| 36 |
assert total > 0
|
| 37 |
assert "trees" in stats
|
| 38 |
assert isinstance(stats["trees"], int)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_chat_mock_runs():
|
| 42 |
+
"""chat() should return mocked text without loading real model."""
|
| 43 |
+
out = app.chat(messages=[{"role": "user", "content": "Hello"}], history=[])
|
| 44 |
+
assert isinstance(out, str)
|
| 45 |
+
assert "Easy switch" in out or "•" in out
|