def test_invalid_eq(self): doc1 = models.Document( id="1", uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", ) doc2 = models.Document( id="2", uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", ) assert doc1 != doc2
def test_to_json(self, mock_collection, mock_transcript): """Test serialization of Document instances.""" doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", date_range_start="20191026", transcripts=[mock_transcript], collections=[mock_collection], ) doc_json = doc.json() reloaded_doc = models.Document(**json.loads(doc_json)) for field in doc.__fields__: assert getattr(doc, field) == getattr(reloaded_doc, field) for field in doc.transcripts[0].__fields__: assert getattr(reloaded_doc.transcripts[0], field) == getattr(doc.transcripts[0], field) for field in doc.collections[0].__fields__: assert getattr(reloaded_doc.collections[0], field) == getattr(doc.collections[0], field)
def test_valid_eq(self): """Compare a search result doc and a hydrated doc.""" hydrated_doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", ) unhydrated_doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", pdf_generated_at="2012-10-26 16:12:00", ) assert hydrated_doc == unhydrated_doc
def test_pull(self, mock_api): """Check appropriate endpoint and ID passed to get function.""" # Prep mock document stub. mock_stub_doc = models.Document( id="1", uri="test_uri", title="Test Title", description="Test Description", doc_date="20100910", frontend_doc_date="2019-10-26 16:12:00", slug="test slug", source_updated_at="2019-10-26 16:12:00", source_created_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", ) # Prep mock rehydrated document mock_hydrated_doc_json = { "id": "1", "uri": "test_uri", "title": "Test Title", "description": "Test Description", "doc_date": "111111", "frontend_doc_date": "111111", "slug": "test slug", "source_updated_at": "2019-10-26 16:12:00", "source_created_at": "2019-10-26 16:12:00", "first_published_at": "2019-10-26 16:12:00", "source": "Test Source", "subjects": [], "transcripts": [], "media_files": [], "languages": [], "creators": [], "collections": [], "attachments": [], "links": [], "translations": [], "contributors": [], "original_coverages": [], "repositories": [], "classifications": [], } mock_api.get.return_value = mock_hydrated_doc_json # Pull the doc mock_stub_doc.pull() # Confirm doc is now hydrated assert mock_stub_doc.source == "Test Source"
def test_date_parsing(self): """Check that Document.date_range_start is properly parsed.""" """Compare a search result doc and a hydrated doc.""" doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", date_range_start="20191026", ) assert doc.date_range_start == date(2019, 10, 26)
def test_hydrate(self, mock_pull): doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", date_range_start="20191026", ) doc.hydrate() # Check that record was pulled mock_pull.assert_called_once()
def test_parse_child_records(self): test_subject = {"id": "1", "name": "test_subject"} doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", date_range_start="20191026", subjects=[test_subject], ) # Check that child records expanded assert isinstance(doc.subjects[0], models.Subject) assert doc.subjects[0].id == "1" assert doc.subjects[0].name == "test_subject"
def test_parse_child_records_empty(self): """Test that empty list fields are handled properly. """ doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", date_range_start="20191026", subjects=[], ) # Check that our subject field wasn't modified. assert isinstance(doc.subjects, list) # check that the other child records are still unhydrated. assert doc.publishers is None
def test_hydrate_recursive(self, mock_pull): """Check that recursion logic fires when parameter is enabled.""" mock_transcript = unittest.mock.create_autospec(models.Transcript, instance=True) mock_translation = unittest.mock.create_autospec(models.Translation, instance=True) mock_media_file = unittest.mock.create_autospec(models.MediaFile, instance=True) mock_collection = unittest.mock.create_autospec(models.Collection, instance=True) for mock in [ mock_transcript, mock_translation, mock_media_file, mock_collection ]: mock.copy.return_value = mock doc = models.Document( id=1, uri="test", title="test", description="test", doc_date="test", frontend_doc_date="test", slug="test", source_created_at="2019-10-26 16:12:00", source_updated_at="2019-10-26 16:12:00", first_published_at="2019-10-26 16:12:00", date_range_start="20191026", transcripts=[mock_transcript], translations=[mock_translation], media_files=[mock_media_file], collections=[mock_collection], ) doc.hydrate(recurse=True) # check that mock subordinate records were called. mock_transcript.hydrate.assert_called_once() mock_translation.hydrate.assert_called_once() mock_media_file.hydrate.assert_called_once() mock_collection.hydrate.assert_called_once()