[docs]@classmethoddefsetUpClass(cls):# Make a small image and store in tmp filecls.tmp_dir=tempfile.TemporaryDirectory()cls.img_file=Path(cls.tmp_dir.name).joinpath('test.png')image=Image.new('RGBA',size=(WIDTH,HEIGHT),color=(155,0,0))image.save(cls.img_file,'png')image.close()# set up ONEcls.one=ONE(**TEST_DB)# Collect all notes to delete them latercls.notes=[]# make a new test session_,eid=register_new_session(cls.one,subject='ZM_1150')cls.eid=str(eid)
def_get_image(self,url):# This is a bit of a hack because when running the server locally, the request to the media folder failsrel_path=urlparse(url).path[1:]try:img_file=list(Path('/var/www/').rglob(rel_path))[0]exceptIndexError:img_file=http_download_file(url,target_dir=Path(self.tmp_dir.name),username=TEST_DB['username'],password=TEST_DB['password'],silent=True)returnimg_file
[docs]deftest_class_setup(self):# Tests that the creation of the class works and that the defaults are workingobject_id=str(uuid.uuid4())snp=Snapshot(object_id,one=self.one)self.assertEqual(snp.object_id,object_id)self.assertEqual(snp.content_type,'session')# defaultself.assertTrue(len(snp.images)==0)# Test with different content typesnp=Snapshot(object_id,content_type='probeinsertion',one=self.one)self.assertEqual(snp.object_id,object_id)self.assertEqual(snp.content_type,'probeinsertion')self.assertTrue(len(snp.images)==0)
[docs]deftest_snapshot_default(self):# Test a case where object_id and content type matchobject_id=self.one.alyx.rest('subjects','list',limit=1)[0]['id']snp=Snapshot(object_id,content_type='subject',one=self.one)withself.assertLogs('ibllib','INFO'):self.notes.append(snp.register_image(self.img_file,text='default size'))# Check image size is scaled to default width (defined in alyx settings.py)img_db=self._get_image(self.notes[-1]['image'])withImage.open(img_db)asim:self.assertEqual(im.size,(800,HEIGHT*800/WIDTH))# Test a case where they don't matchsnp=Snapshot(str(uuid.uuid4()),content_type='session',one=self.one)withself.assertLogs('ibllib','ERROR'):note=snp.register_image(self.img_file,text='default size')self.assertIsNone(note)
[docs]deftest_image_scaling(self):# make a new sessionobject_id=self.eidsnp=Snapshot(object_id,content_type='session',one=self.one)# Image in original sizeself.notes.append(snp.register_image(self.img_file,text='original size',width='orig'))img_db=self._get_image(self.notes[-1]['image'])withImage.open(img_db)asim:self.assertEqual(im.size,(WIDTH,HEIGHT))# Scale to width 100self.notes.append(snp.register_image(self.img_file,text='original size',width=100))img_db=self._get_image(self.notes[-1]['image'])withImage.open(img_db)asim:self.assertEqual(im.size,(100,HEIGHT*100/WIDTH))
[docs]deftest_register_multiple(self):expected_texts=['first','second','third']expected_sizes=[(800,HEIGHT*800/WIDTH),(WIDTH,HEIGHT),(200,HEIGHT*200/WIDTH)]object_id=self.one.alyx.rest('datasets','list',limit=1)[0]['url'][-36:]snp=Snapshot(object_id,content_type='dataset',one=self.one)# Register multiple figures by giving a listself.notes.extend(snp.register_images([self.img_file,self.img_file,self.img_file],texts=['first','second','third'],widths=[None,'orig',200]))foriinrange(3):self.assertEqual(self.notes[i-3]['text'],expected_texts[i])img_db=self._get_image(self.notes[i-3]['image'])withImage.open(img_db)asim:self.assertEqual(im.size,expected_sizes[i])# Registering multiple figures by adding to self.figuresself.assertEqual(len(snp.images),0)withself.assertLogs('ibllib','WARNING'):out=snp.register_images()self.assertIsNone(out)snp.images.extend([self.img_file,self.img_file,self.img_file])self.notes.extend(snp.register_images(texts=['always the same'],widths=[200]))foriinrange(3):self.assertEqual(self.notes[i-3]['text'],'always the same')img_db=self._get_image(self.notes[i-3]['image'])withImage.open(img_db)asim:self.assertEqual(im.size,expected_sizes[2])
[docs]@classmethoddeftearDownClass(cls):# Clean up tmp dircls.tmp_dir.cleanup()# Delete all notesfornoteincls.notes:cls.one.alyx.rest('notes','delete',id=note['id'])# Delete the new session that was madecls.one.alyx.rest('sessions','delete',id=cls.eid)