import unittest import logging import uuid from domain.chunk_d import DocumentD, ChunkD import proto.chunk_pb2 as chunk_pb2 class DocumentDTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.document_d = DocumentD(file_path='test_file', authors='BofA John Doe Herb Johnson Taylor Mason', publish_date='2021-01-01') def test_proto_roundtrip(self): document_proto = self.document_d.to_proto() roundtrip_document_d = DocumentD.from_proto(document_proto) self.assertEqual(self.document_d, roundtrip_document_d) class ChunkDTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.parent_reference_document_d = DocumentD( file_path='test_file', authors='BofA John Doe Herb Johnson Taylor Mason', publish_date='2021-01-01') def test_validate(self): with self.assertRaises(ValueError, msg="CHUNK_TYPE_PAGE must have a DocumentD parent_reference"): ChunkD(chunk_text='test chunk text', chunk_type=chunk_pb2.ChunkType.CHUNK_TYPE_PAGE, chunk_index=9, parent_reference=uuid.uuid4(), chunk_id=uuid.uuid4()) with self.assertRaises(ValueError, msg="CHUNK_TYPE_SENTENCE must have a uuid.UUID parent_reference"): ChunkD(chunk_text='test chunk text', chunk_type=chunk_pb2.ChunkType.CHUNK_TYPE_SENTENCE, chunk_index=0, parent_reference=self.parent_reference_document_d, chunk_id=uuid.uuid4()) ChunkD(chunk_text='test chunk text', chunk_type=chunk_pb2.ChunkType.CHUNK_TYPE_PAGE, chunk_index=9, parent_reference=self.parent_reference_document_d, chunk_id=uuid.uuid4()) ChunkD(chunk_text='test chunk text', chunk_type=chunk_pb2.ChunkType.CHUNK_TYPE_SENTENCE, chunk_index=0, parent_reference=uuid.uuid4(), chunk_id=uuid.uuid4()) def test_proto_roundtrip(self): test_chunk_d = ChunkD(chunk_id=uuid.uuid4(), parent_reference=self.parent_reference_document_d, chunk_text='test chunk text', chunk_type=chunk_pb2.ChunkType.CHUNK_TYPE_PAGE, chunk_index=9) chunk_proto = test_chunk_d.to_proto() roundtrip_chunk_d = ChunkD.from_proto(chunk_proto) self.assertEqual(test_chunk_d, roundtrip_chunk_d) if __name__ == '__main__': logging.basicConfig(level=logging.INFO) unittest.main()