Mohammed Thameem commited on
Commit
9506837
·
1 Parent(s): 23ded75

WIP before rebase

Browse files
Files changed (1) hide show
  1. tests/test_app.py +24 -20
tests/test_app.py CHANGED
@@ -1,33 +1,38 @@
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,
@@ -39,7 +44,6 @@ def test_calculate_footprint():
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
 
1
  import sys, os
 
2
  import pytest
3
+ from unittest.mock import MagicMock
4
 
5
+ class DummyTokenizer:
6
+ pad_token_id = 0
7
+ eos_token_id = 1
8
+ chat_template = "{% for message in messages %}{{ message['role'] }}: {{ message['content'] }}{% endfor %}"
9
 
10
+ def apply_chat_template(self, conv, tokenize=False, add_generation_prompt=True):
11
+ return "mock_prompt"
 
 
 
 
 
 
12
 
13
+ def __call__(self, prompt, return_tensors=None):
14
+ return {"input_ids": [[0, 1]]}
15
 
16
+ def decode(self, tokens, skip_special_tokens=True):
17
+ return "• Easy switch"
 
18
 
19
+
20
+ class DummyModel:
21
+ def to(self, device): return self
22
+ def eval(self): return self
23
+ def generate(self, **kwargs): return [[0, 1, 2, 3]]
24
+
25
+
26
+ sys.modules["transformers"] = MagicMock(
27
+ AutoTokenizer=MagicMock(from_pretrained=lambda *a, **kw: DummyTokenizer()),
28
+ AutoModelForCausalLM=MagicMock(from_pretrained=lambda *a, **kw: DummyModel())
29
+ )
30
 
31
  import app
32
 
33
 
34
+ # ================== Tests ==================
35
+
36
  def test_calculate_footprint():
37
  total, stats = app.calculate_footprint(
38
  car_km=10, bus_km=5, train_km=2, air_km_week=50,
 
44
 
45
 
46
  def test_chat_mock_runs():
 
47
  out = app.chat(messages=[{"role": "user", "content": "Hello"}], history=[])
48
  assert isinstance(out, str)
49
  assert "Easy switch" in out or "•" in out