def test_pairtree_object(self, mock_pairtree_client): hobj = hathi.HathiObject(hathi_id='uva.1234') ptree_obj = hobj.pairtree_object() # client initialized mock_pairtree_client.PairtreeStorageClient \ .assert_called_with(hobj.pairtree_prefix, os.path.join(settings.HATHI_DATA, hobj.pairtree_prefix)) # object retrieved mock_pairtree_client.PairtreeStorageClient.return_value \ .get_object.assert_called_with(hobj.pairtree_id, create_if_doesnt_exist=False) # object returned assert ptree_obj == mock_pairtree_client.PairtreeStorageClient \ .return_value.get_object.return_value # test passing in existing pairtree client mock_pairtree_client.reset_mock() my_ptree_client = Mock(spec=pairtree_client.PairtreeStorageClient) ptree_obj = hobj.pairtree_object(my_ptree_client) # should not initialize mock_pairtree_client.PairtreeStorageClient.assert_not_called() # should get object from my client my_ptree_client.get_object.assert_called_with( hobj.pairtree_id, create_if_doesnt_exist=False)
def test_delete_pairtree_data(self): hobj = hathi.HathiObject(hathi_id='chi.79279237') with patch.object(hobj, 'pairtree_client') as mock_pairtree_client: hobj.delete_pairtree_data() # should initialize client mock_pairtree_client.assert_called() # should call delete boject mock_pairtree_client.return_value.delete_object \ .assert_called_with(hobj.pairtree_id) # should not raise an exception if deletion fails mock_pairtree_client.return_value.delete_object.side_effect \ = storage_exceptions.ObjectNotFoundException hobj.delete_pairtree_data()
def test_metsfile_path(self): hobj = hathi.HathiObject(hathi_id='chi.79279237') contents = ['79279237.mets.xml', '79279237.zip'] with patch.object(hathi.HathiObject, 'pairtree_object') as mock_ptree_obj_meth: mock_ptree_obj = mock_ptree_obj_meth.return_value mock_ptree_obj.list_parts.return_value = contents mock_ptree_obj.id_to_dirpath.return_value = \ '/tmp/ht_text_pd/chi/pairtree_root/79/27/92/37' metsfile_path = hobj.metsfile_path() mock_ptree_obj_meth.assert_called_with(ptree_client=None) assert metsfile_path == \ os.path.join(mock_ptree_obj.id_to_dirpath(), hobj.content_dir, contents[0]) # use pairtree client object if passed in my_ptree_client = Mock(spec=pairtree_client.PairtreeStorageClient) hobj.metsfile_path(my_ptree_client) mock_ptree_obj_meth.assert_called_with( ptree_client=my_ptree_client)
def test_content_dir(self): hobj = hathi.HathiObject(hathi_id='uva.1234') assert hobj.content_dir == pairtree_path.id_encode(hobj.pairtree_id)
def test_pairtree_id(self): hobj = hathi.HathiObject(hathi_id='uva.1234') assert hobj.pairtree_id == '1234'