From 6f269c718e45c46a032135b2f2cee71c207efa52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Jamnicki?= Date: Wed, 15 May 2024 17:59:54 +0000 Subject: [PATCH] test: add unittests for SyntheticQAPairs model --- juddges/data/models.py | 3 ++ tests/test_synthetic_qa_pairs_dto.py | 79 ++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/test_synthetic_qa_pairs_dto.py diff --git a/juddges/data/models.py b/juddges/data/models.py index 5e84b1e..92345dd 100644 --- a/juddges/data/models.py +++ b/juddges/data/models.py @@ -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" diff --git a/tests/test_synthetic_qa_pairs_dto.py b/tests/test_synthetic_qa_pairs_dto.py new file mode 100644 index 0000000..fd32263 --- /dev/null +++ b/tests/test_synthetic_qa_pairs_dto.py @@ -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"])