-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_validators.py
91 lines (72 loc) · 4.38 KB
/
test_validators.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (C) 2024. Hao Zheng
# All rights reserved.
import unittest
from openlrc.validators import ChunkedTranslateValidator, AtomicTranslateValidator, ContextReviewerValidateValidator
class TestChunkedTranslateValidator(unittest.TestCase):
# Validate correctly extracts original text and translation
def test_validate_correctly_extracts_original_and_translation(self):
user_input = "Original>\nThis is a test.\nTranslation>\nEsto es una prueba."
generated_content = "<summary>Test Summary</summary><scene>Test Scene</scene>\nOriginal>\nThis is a test.\nTranslation>\nEsto es una prueba."
validator = ChunkedTranslateValidator(target_lang='es')
result = validator.validate(user_input, generated_content)
self.assertTrue(result)
# Validate handles empty user input and generated content
def test_validate_handles_empty_input_and_content(self):
user_input = ""
generated_content = ""
validator = ChunkedTranslateValidator(target_lang='es')
result = validator.validate(user_input, generated_content)
self.assertFalse(result)
# Validate ensures translation is in the target language
def test_translation_not_in_target_language(self):
user_input = "Original>\nThis is a test.\nTranslation>\nEsto es una prueba."
generated_content = "<summary>Test Summary</summary><scene>Test Scene</scene>\nOriginal>\nThis is a test.\nTranslation>\n这是一个测试。"
validator = ChunkedTranslateValidator(target_lang='es')
result = validator.validate(user_input, generated_content)
self.assertFalse(result)
# Validate handles content with summary and scene tags
def test_handles_summary_and_scene_tags(self):
user_input = "Original>\nThis is a test.\nTranslation>\nEsto es una prueba."
generated_content = "<summary>Test Summary</summary><scene>Test Scene</scene>\nOriginal>\nThis is a test.\nTranslation>\nEsto es una prueba."
validator = ChunkedTranslateValidator(target_lang='es')
result = validator.validate(user_input, generated_content)
self.assertTrue(result)
# Validate returns True for matching original and translation lengths
def test_mismatched_original_and_translation_lengths(self):
user_input = "Original>\nThis is a test.\nTranslation>\nEsto es una prueba."
generated_content = "<summary>Test Summary</summary><scene>Test Scene</scene>\nOriginal>\nThis is a test.\nTranslation>\nEsto es una prueba.\nOriginal>\nThis is a test!\nTranslation>\nEsto es una prueba!"
validator = ChunkedTranslateValidator(target_lang='es')
result = validator.validate(user_input, generated_content)
self.assertFalse(result)
class TestAtomicTranslateValidator(unittest.TestCase):
def test_validate_returns_true_when_generated_content_matches_target_language(self):
validator = AtomicTranslateValidator(target_lang='en')
user_input = "你有什么问题?"
generated_content = "What's your problem?"
result = validator.validate(user_input, generated_content)
self.assertTrue(result)
def test_validate_returns_false_when_generated_content_not_matches_target_language(self):
validator = AtomicTranslateValidator(target_lang='ja')
user_input = "Hello"
generated_content = "你好"
result = validator.validate(user_input, generated_content)
self.assertFalse(result)
class TestContextReviewerValidateValidator(unittest.TestCase):
def test_validate_returns_true_when_true_in_generated_content(self):
validator = ContextReviewerValidateValidator()
user_input = "Some input"
generated_content = "This is true"
result = validator.validate(user_input, generated_content)
self.assertTrue(result)
def test_validate_returns_true_when_false_in_generated_content(self):
validator = ContextReviewerValidateValidator()
user_input = "Some input"
generated_content = "FALse!"
result = validator.validate(user_input, generated_content)
self.assertTrue(result)
def test_validate_returns_false_when_no_true_or_false_in_generated_content(self):
validator = ContextReviewerValidateValidator()
user_input = "Some input"
generated_content = "I dont know!"
result = validator.validate(user_input, generated_content)
self.assertFalse(result)