import math import pytest from unittest.mock import MagicMock, patch, mock_open from AAA_VocabularyLevelEstimator import VocabularyLevelEstimator # 模拟加载记录函数返回的数据 mock_words_and_tests = {'word1': ['CET4'], 'word2': ['OXFORD3000']} mock_word_vectors_path = './wiki-news-300d-1M.vec' @pytest.fixture def estimator(): with patch('gensim.models.KeyedVectors.load_word2vec_format', return_value=MagicMock()): return VocabularyLevelEstimator(word_vectors_path=mock_word_vectors_path) @pytest.fixture def estimator_with_failed_load(): with patch('gensim.models.KeyedVectors.load_word2vec_format', side_effect=Exception("Failed to load word vectors")): return VocabularyLevelEstimator(word_vectors_path=mock_word_vectors_path) def test_estimate_text_difficulty_normal(estimator): text = "This is a simple sentence with common words." difficulty = estimator.estimate_text_difficulty(text) assert isinstance(difficulty, float) and not math.isnan(difficulty) def test_estimate_user_vocabulary_level_normal(estimator): user_unknown_words = ['abandon', 'pursuit', 'knowledge'] level = estimator.estimate_user_vocabulary_level(user_unknown_words) assert isinstance(level, float) and not math.isnan(level) def test_estimate_text_difficulty_empty_string(estimator): text = "" difficulty = estimator.estimate_text_difficulty(text) assert difficulty == 0.0 def test_estimate_user_vocabulary_level_empty_list(estimator): user_unknown_words = [] level = estimator.estimate_user_vocabulary_level(user_unknown_words) assert level == 0.0 def test_estimate_text_difficulty_with_stopwords_only(estimator): text = "the and of to in" difficulty = estimator.estimate_text_difficulty(text) assert difficulty == 0.0 def test_estimate_word_difficulty_cache(estimator): word = "example" first_call = estimator.estimate_word_difficulty(word) second_call = estimator.estimate_word_difficulty(word) assert first_call == second_call def test_estimate_text_difficulty_non_english_characters(estimator): text = "你好,世界!" difficulty = estimator.estimate_text_difficulty(text) assert difficulty == 0.0 def test_estimate_user_vocabulary_level_non_english_characters(estimator): user_unknown_words = ['你好', '世界'] level = estimator.estimate_user_vocabulary_level(user_unknown_words) assert level == 0.0 def test_estimate_text_difficulty_exception_handling(estimator): original_method = estimator.estimate_word_difficulty estimator.estimate_word_difficulty = MagicMock(side_effect=Exception("Simulated error")) text = "This should trigger exception handling." difficulty = estimator.estimate_text_difficulty(text) assert isinstance(difficulty, float) and not math.isnan(difficulty) estimator.estimate_word_difficulty = original_method def test_estimator_initialization_with_failed_word_vector_load(estimator_with_failed_load): """确保即使加载词向量失败,类也能正常初始化""" assert estimator_with_failed_load.word_vectors is None if __name__ == '__main__': pytest.main()