def test_encoded_datastream(self): # data content within a single chunk of data mockapi = Mock() mockapi.export.return_value = self.session.get( 'file://%s' % FIXTURES['sync1_export']) mockapi.upload.return_value = 'uploaded://1' self.obj.api = self.repo.api = mockapi section = self.archex.get_next_section() # get binary datastream info from first section dsinfo = self.archex.get_datastream_info(section) # fixture only has one binary content block # get binarycontent tag out of the way self.archex.get_next_section() # next section will be file contents self.archex.within_file = True dscontent = b''.join(self.archex.encoded_datastream()) # check decoded size and MD5 match data from fixture self.assertEqual(int(dsinfo['size']), len(dscontent)) self.assertEqual(dsinfo['digest'], md5sum(dscontent)) # data content across multiple chunks mockapi.export.return_value = self.session.get( 'file://%s' % FIXTURES['sync1_export']) self.obj.api = self.repo.api = mockapi # set read block size artificially low to ensure # datastream content is spread across multiple chunks self.archex.read_block_size = 1024 finished = False # iterate through the data, similar to object_data method, # but only handle binary content while not finished: try: section = self.archex.get_next_section() except StopIteration: finished = True # find the section with starting binary content if section == '<foxml:binaryContent>': # then decode the subsequent content self.archex.within_file = True dscontent = ''.join(self.archex.encoded_datastream()) self.assertEqual(int(dsinfo['size']), len(dscontent)) self.assertEqual(dsinfo['digest'], md5sum(dscontent)) # stop processing finished = True
def test_upload_generator(self): # test uploading content from a generator def data_generator(): yield 'line one of text\n' yield 'line two of text\n' yield 'line three of text\n' text_content = ''.join(data_generator()) content_md5 = md5sum(text_content) size = len(text_content) upload_id = self.rest_api.upload(data_generator(), size=size, content_type='text/plain') pattern = re.compile('uploaded://[0-9]+') self.assertTrue(pattern.match(upload_id)) # check that the *right* content was uploaded by adding # a datastream using the computed MD5 and generated upload id obj = load_fixture_data('basic-object.foxml') response = self.rest_api.ingest(obj) pid = response.text add_response = self.rest_api.addDatastream(pid, 'text', controlGroup='M', dsLocation=upload_id, mimeType='text/plain', checksumType='MD5', checksum=content_md5) self.assertTrue(add_response.status_code, requests.codes.created) # get the content from fedora and confirm it matches what was sent dsresponse = self.rest_api.getDatastreamDissemination(pid, 'text') self.assertEqual(text_content, dsresponse.text) # clean up test object self.rest_api.purgeObject(pid)
def test_encoded_datastream(self): # data content within a single chunk of data mockapi = Mock() mockapi.export.return_value = self.session.get('file://%s' % FIXTURES['sync1_export']) mockapi.upload.return_value = 'uploaded://1' self.obj.api = self.repo.api = mockapi section = self.archex.get_next_section() # get binary datastream info from first section dsinfo = self.archex.get_datastream_info(section) # fixture only has one binary content block # get binarycontent tag out of the way self.archex.get_next_section() # next section will be file contents self.archex.within_file = True dscontent = b''.join(self.archex.encoded_datastream()) # check decoded size and MD5 match data from fixture self.assertEqual(int(dsinfo['size']), len(dscontent)) self.assertEqual(dsinfo['digest'], md5sum(dscontent)) # data content across multiple chunks mockapi.export.return_value = self.session.get('file://%s' % FIXTURES['sync1_export']) self.obj.api = self.repo.api = mockapi # set read block size artificially low to ensure # datastream content is spread across multiple chunks self.archex.read_block_size = 1024 finished = False # iterate through the data, similar to object_data method, # but only handle binary content while not finished: try: section = self.archex.get_next_section() except StopIteration: finished = True # find the section with starting binary content if section == '<foxml:binaryContent>': # then decode the subsequent content self.archex.within_file = True dscontent = ''.join(self.archex.encoded_datastream()) self.assertEqual(int(dsinfo['size']), len(dscontent)) self.assertEqual(dsinfo['digest'], md5sum(dscontent)) # stop processing finished = True