Skip to content

Commit

Permalink
test: add unittests for SyntheticQAPairs model
Browse files Browse the repository at this point in the history
  • Loading branch information
jamnicki committed May 15, 2024
1 parent f7cdee8 commit 6f269c7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions juddges/data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __len__(self) -> int:
def test_empty(self) -> None:
if not any(self.questions):
raise GeneratedQAPairsEmptyError("At least one question should be generated")
else:
if not any(self.answers):
raise GeneratedQAPairsEmptyError("At least one answer should be generated")

def test_equal_length(self) -> None:
assertion_msg = "Number of questions and answers should be equal"
Expand Down
79 changes: 79 additions & 0 deletions tests/test_synthetic_qa_pairs_dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import unittest

from juddges.data.models import LangCode, SyntheticQAPairs
from juddges.exception import (
GeneratedQAPairsEmptyError,
GeneratedQAPairsLenghtMismatchError,
GeneratedQAPairsNotUniqueError,
LanguageMismatchError,
)


class TestSyntheticQAPairs(unittest.TestCase):
def test_len(self):
scenarios = [
{"q": [], "a": [], "expected": 0},
{"q": ["1"], "a": [], "expected": 1},
{"q": ["1"], "a": ["2"], "expected": 1},
{"q": ["1", "2"], "a": ["1", "2"], "expected": 2},
{"q": ["1"], "a": ["1", "2"], "expected": 1},
]
for scenario in scenarios:
dto = SyntheticQAPairs(questions=scenario["q"], answers=scenario["a"])
self.assertEqual(len(dto), scenario["expected"])

def test_raises_on_empty(self):
scenarios = [
{"q": [], "a": []},
{"q": [], "a": [""]},
{"q": [""], "a": []},
]
for scenario in scenarios:
dto = SyntheticQAPairs(questions=scenario["q"], answers=scenario["a"])
with self.assertRaises(GeneratedQAPairsEmptyError):
dto.test_empty()

def test_raises_on_length_mismatch(self):
scenarios = [
{"q": ["1"], "a": []},
{"q": [], "a": ["1"]},
{"q": ["1"], "a": ["1", "2"]},
{"q": ["1", "2"], "a": ["1"]},
]
for scenario in scenarios:
dto = SyntheticQAPairs(questions=scenario["q"], answers=scenario["a"])
with self.assertRaises(GeneratedQAPairsLenghtMismatchError):
dto.test_equal_length()

def test_raises_on_not_unique(self):
dto = SyntheticQAPairs(questions=["1", "1"], answers=["1", "2"])
with self.assertRaises(GeneratedQAPairsNotUniqueError):
dto.test_unique_questions()

def test_raises_on_language_mismatch(self):
scenarios = [
{
"q": ["Jakie podstawowe prawo stosuje sąd w tej sprawie?"],
"a": ["The court applies the Code of Civil Procedure."],
"expected_lang": LangCode.POLISH,
},
{
"q": ["What basic law does the court apply in this case?"],
"a": ["Sąd stosuje Kodeks postępowania cywilnego."],
"expected_lang": LangCode.POLISH,
},
{
"q": ["What is the name of the accused?"],
"a": ["Jędrzej Brzęczyszczykiewicz"],
"expected_lang": LangCode.ENGLISH,
},
{
"q": ["Jakie jest imię i nazwisko oskarżonego?"],
"a": ["Helga Müller-Fäßler"],
"expected_lang": LangCode.POLISH,
},
]
for scenario in scenarios:
dto = SyntheticQAPairs(questions=scenario["q"], answers=scenario["a"])
with self.assertRaises(LanguageMismatchError):
dto.test_language(lang=scenario["expected_lang"])

0 comments on commit 6f269c7

Please sign in to comment.