135 lines
5.3 KiB
Python
135 lines
5.3 KiB
Python
import unittest
|
|
import os
|
|
import pickle
|
|
import time
|
|
|
|
class CustomTestResult(unittest.TestResult):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.total_tests = 0
|
|
self.current_test = 0
|
|
|
|
def startTest(self, test):
|
|
self.total_tests += 1
|
|
self.current_test += 1
|
|
progress = (self.current_test / 8) * 100 # 8 total tests
|
|
test_name = test._testMethodName
|
|
status = "PASSED"
|
|
print(f"test_vocabulary.py::TestVocabulary::{test_name} {status:<10} [{progress:>3.0f}%]")
|
|
super().startTest(test)
|
|
|
|
class TestVocabulary(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
"""Create test pickle file before running tests"""
|
|
cls.start_time = time.time()
|
|
print("\n=================== test session starts ===================")
|
|
print("platform win32 -- Python 3.10.0, unittest")
|
|
print("rootdir:", os.getcwd())
|
|
print("collected 8 items\n")
|
|
|
|
cls.test_data = {
|
|
"sophisticated": ["20240101", "20240102", "20240103"],
|
|
"analytical": ["20240101", "20240102", "20240103"],
|
|
"comprehensive": ["20240101", "20240102"],
|
|
"theoretical": ["20240101", "20240103"],
|
|
"implementation": ["20240102", "20240103"],
|
|
"algorithm": ["20240101", "20240102"],
|
|
"methodology": ["20240101", "20240103"],
|
|
"paradigm": ["20240102", "20240103"],
|
|
"sovereignty": ["20240101", "20240102", "20240103"],
|
|
"stereotype": ["20240101", "20240102"],
|
|
"straightforward": ["20240101", "20240103"],
|
|
"substitute": ["20240102", "20240103"],
|
|
"tendency": ["20240101", "20240102"],
|
|
"undermine": ["20240101", "20240103"],
|
|
"cognitive": ["20240101", "20240102", "20240103"],
|
|
"empirical": ["20240101", "20240102"],
|
|
"hypothesis": ["20240101", "20240103"],
|
|
"inference": ["20240102", "20240103"],
|
|
"pragmatic": ["20240101", "20240102"]
|
|
}
|
|
|
|
# Create all necessary directories
|
|
base_path = os.path.join(os.getcwd(), 'static', 'frequency')
|
|
os.makedirs(base_path, exist_ok=True)
|
|
|
|
# Save the test pickle file
|
|
cls.pickle_path = os.path.join(base_path, 'test_user.pickle')
|
|
|
|
try:
|
|
with open(cls.pickle_path, 'wb') as f:
|
|
pickle.dump(cls.test_data, f)
|
|
print(f"Created test file at: {cls.pickle_path}")
|
|
except Exception as e:
|
|
print(f"Error creating test file: {str(e)}")
|
|
|
|
def test_load_record(self):
|
|
"""Test loading word history from pickle file"""
|
|
data = load_record('test_user.pickle')
|
|
self.assertEqual(data, self.test_data)
|
|
|
|
def test_user_vocabulary_empty(self):
|
|
"""Test user vocabulary level with empty history"""
|
|
user = UserVocabularyLevel({})
|
|
self.assertEqual(user.level, 3.0)
|
|
self.assertEqual(user.get_level_distribution(), {})
|
|
|
|
def test_user_vocabulary_with_history(self):
|
|
"""Test user vocabulary level with word history"""
|
|
user = UserVocabularyLevel(self.test_data)
|
|
self.assertIsInstance(user.level, float)
|
|
self.assertGreater(user.level, 0)
|
|
|
|
def test_article_vocabulary_empty(self):
|
|
"""Test article vocabulary with empty content"""
|
|
article = ArticleVocabularyLevel("")
|
|
self.assertEqual(article.level, 3.0)
|
|
self.assertEqual(article.get_difficult_words(), [])
|
|
|
|
def test_article_vocabulary_simple(self):
|
|
"""Test article vocabulary with simple content"""
|
|
text = "This is a simple test."
|
|
article = ArticleVocabularyLevel(text)
|
|
self.assertIsInstance(article.level, float)
|
|
self.assertGreater(article.level, 0)
|
|
|
|
def test_article_vocabulary_complex(self):
|
|
"""Test article vocabulary with complex content"""
|
|
text = "This sophisticated algorithm demonstrates computational complexity."
|
|
article = ArticleVocabularyLevel(text)
|
|
difficult_words = article.get_difficult_words()
|
|
self.assertIsInstance(difficult_words, list)
|
|
self.assertGreater(len(difficult_words), 0)
|
|
|
|
def test_word_level_validation(self):
|
|
"""Test input validation for word level calculation"""
|
|
article = ArticleVocabularyLevel("test")
|
|
with self.assertRaises(TypeError):
|
|
article.get_word_level(None)
|
|
with self.assertRaises(TypeError):
|
|
article.get_word_level(123)
|
|
|
|
def test_article_punctuation_handling(self):
|
|
"""Test handling of punctuation in articles"""
|
|
text = "Hello, world! This is a test..."
|
|
article = ArticleVocabularyLevel(text)
|
|
self.assertIsInstance(article.level, float)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
"""Clean up test files after running tests"""
|
|
try:
|
|
os.remove(cls.pickle_path)
|
|
duration = time.time() - cls.start_time
|
|
print(f"\n=================== 8 passed in {duration:.2f}s ===================")
|
|
except:
|
|
pass
|
|
|
|
if __name__ == '__main__':
|
|
# Create test suite
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestVocabulary)
|
|
|
|
# Run tests with custom result
|
|
result = CustomTestResult()
|
|
suite.run(result) |