File size: 1,726 Bytes
456f698
 
246f918
 
456f698
246f918
 
456f698
 
 
 
 
 
 
 
 
246f918
 
 
 
 
456f698
246f918
456f698
246f918
 
 
 
 
 
 
456f698
246f918
 
 
 
 
 
456f698
246f918
 
 
 
456f698
246f918
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
import pytest
from unittest.mock import MagicMock
import singletons
from verification.verifier import verify_idiom_exists, WiktionaryClient


class FakeWiktionaryClient:
    def __init__(self, return_value: bool):
        self.return_value = return_value
        self.called_with = None

    def exists(self, idiom: str) -> bool:
        self.called_with = idiom
        return self.return_value
    

def test_returns_true_if_in_chid():
    # Pick an idiom guaranteed to be in CHID_SET
    idiom = next(iter(singletons.CHID_SET))
    fake_client = FakeWiktionaryClient(False)  # should not be called
    result = verify_idiom_exists(idiom, wiktionary_client=fake_client)
    assert result is True
    assert fake_client.called_with is None  # Wiktionary not used

def test_returns_true_if_in_cc_dict(monkeypatch):
    idiom = "临危不乱"  # example idiom not in CHID_SET
    monkeypatch.setattr("singletons.CC_DICT.get_definitions", lambda x: ["fake def"])
    fake_client = FakeWiktionaryClient(False)  # should not be called
    result = verify_idiom_exists(idiom, wiktionary_client=fake_client)
    assert result is True
    assert fake_client.called_with is None  # Wiktionary not used

def test_returns_true_if_only_in_wiktionary():
    idiom = "不存在的成语"
    fake_client = FakeWiktionaryClient(True)
    result = verify_idiom_exists(idiom, wiktionary_client=fake_client)
    assert result is True
    assert fake_client.called_with == idiom

def test_returns_false_if_nowhere():
    idiom = "完全不存在的成语"
    fake_client = FakeWiktionaryClient(False)
    result = verify_idiom_exists(idiom, wiktionary_client=fake_client)
    assert result is False
    assert fake_client.called_with == idiom