def setUp(self): self.api = BoxView('<box view api key>')
class BoxViewTestCase(unittest.TestCase): def setUp(self): self.api = BoxView('<box view api key>') def test_initials(self): # api key is required self.assertRaises(ValueError, BoxView) now = datetime.datetime.utcnow().replace(microsecond=0) dtfiso = now.isoformat() dfiso = now.date().isoformat() self.assertEqual(dtfiso, format_date(dtfiso)) self.assertEqual(dtfiso, format_date(now)) self.assertEqual(dfiso, format_date(now.date())) headers = {'Content-Type': 'text/plain'} self.assertEqual('text/plain', get_mimetype_from_headers(headers)) headers = {'Content-Type': 'text/plain; charset=utf-8'} self.assertEqual('text/plain', get_mimetype_from_headers(headers)) @patch.object(Session, 'request') def test_crate_document_from_url(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.create_document(url=test_url, name='Test Document') self.assertIsNotNone(result) self.assertEqual(result, test_document) # url of file param is required self.assertRaises(ValueError, self.api.create_document) @patch.object(Session, 'request') def test_create_document_from_url(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.create_document_from_url(test_url, name='Test Document') self.assertIsNotNone(result) self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_create_document_from_file(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_document) mock_request.return_value = response stream = six.BytesIO() result = self.api.create_document_from_file(stream, name='Test Document') self.assertEqual(result, test_document) result = self.api.create_document_from_file(__file__, name='Test Document') self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_get_document(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.get_document(test_document['id']) self.assertIsNotNone(result) self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_get_documents(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document_list) mock_request.return_value = response now = datetime.datetime.utcnow() result = self.api.get_documents(limit=10, created_before=now) self.assertIsNotNone(result) self.assertEqual(result, test_document_list) @patch.object(Session, 'request') def test_update_document(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.update_document(test_document['id'], name='TestDocument') self.assertIsNotNone(result) self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_delete_document(self, mock_request): response = Response() response.status_code = 204 mock_request.return_value = response self.api.delete_document(test_document['id']) @patch.object(Session, 'request') def test_get_document_content(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' response._content = 'test' response.raw = six.StringIO('test') mock_request.return_value = response stream = six.StringIO() mimetype = self.api.get_document_content(stream, test_document['id']) self.assertEqual(stream.getvalue(), response._content) self.assertEqual(mimetype, response.headers['Content-Type']) stream = six.StringIO() self.api.get_document_content(stream, test_document['id'], extension='.pdf') self.assertEqual(stream.getvalue(), response._content) stream = six.StringIO() self.api.get_document_content(stream, test_document['id'], extension='.zip') self.assertEqual(stream.getvalue(), response._content) stream = six.StringIO() # allowed only .zip and .pdf extensions self.assertRaises(ValueError, self.api.get_document_content, stream, test_document['id'], extension='.docx') @patch.object(Session, 'request') def test_get_document_content_to_string(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' response._content = 'test' response.raw = six.StringIO('test') mock_request.return_value = response doc_id = test_document['id'] result, mimetype = self.api.get_document_content_to_string(doc_id) self.assertIsNotNone(result) self.assertEqual(result, response._content) self.assertEqual(mimetype, response.headers['Content-Type']) @patch.object(Session, 'request') def test_get_document_content_to_file(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' response._content = 'test' response.raw = six.StringIO('test') mock_request.return_value = response filename = 'boxview.txt' mimetype = self.api.get_document_content_to_file(filename, test_document['id']) self.assertEqual(mimetype, response.headers['Content-Type']) self.assertTrue(os.path.exists(filename)) try: os.remove(filename) except OSError: pass @patch.object(Session, 'request') def test_get_document_content_mimetype(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' mock_request.return_value = response mimetype = self.api.get_document_content_mimetype(test_document['id']) self.assertEqual(mimetype, response.headers['Content-Type']) @patch.object(Session, 'request') def test_create_session(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_session) mock_request.return_value = response expires_at = datetime.datetime.utcnow() result = self.api.create_session(test_document['id'], duration=600, expires_at=expires_at) self.assertIsNotNone(result) self.assertEqual(result['id'], test_session['id']) @patch.object(Session, 'request') def test_ready_to_view(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.ready_to_view(test_document['id']) self.assertIsNotNone(result) self.assertTrue(bool(result)) response._content = json.dumps(dict(test_document, status='error')) result = self.api.ready_to_view(test_document['id']) self.assertFalse(bool(result)) @patch.object(Session, 'request') def test_request_error(self, mock_request): response = Response() response.status_code = 401 response._content = 'Unauthorized' response.reason = 'Unauthorized' mock_request.return_value = response self.assertRaises(BoxViewError, self.api.get_document, test_document['id'])
L = ( flts[0], flts[1], flts[2] ) print( "LAUNCH : successfully read 'L' field" ) continue assert O is not None, "LAUNCH : 'O' field not found?" assert L is not None, "LAUNCH : 'L' field not found?" bigbox = BigBox( O, L ) # if here, header is completely known and current line (should) contains the first point -- initialize outermost box and fall out of this if-statement assert O[0] <= flts[0] and flts[0] < O[0]+L[0] and O[1] <= flts[1] and flts[1] < O[1]+L[1] and O[2] <= flts[2] and flts[2] < O[2]+L[2], "LAUNCH : tried to insert point outside of BigBox!" bigbox.insert_point( ( flts[0], flts[1], flts[2] ) ) f.close() # scale the BigBox to fit exactly within a BOXVIEWMAXPIXELS x BOXVIEWMAXPIXELS x BOXVIEWMAXPIXELS cube (largest of L[0],L[1] becomes BOXVIEWMAXPIXELS, others scale appropriately) dilation = ( 2 * mainwin.get_screen().height() / 6.0 ) / max( abs(L[0]), abs(L[1]) ) BBW = dilation*L[0] BBH = dilation*L[1] BBD = dilation*L[2] bigboxview = BoxView( BBW, BBH ) # TO DO: be sure the aspect ratio corresponds to the actual dimensions of the box (only matters for non-cubical) smallboxviewXY = BoxView( BBW/2, BBH/2 ) # when the BigBox is non-cubical, need three different BoxViews for projections (all three could be different sizes) smallboxviewYZ = BoxView( BBH/2, BBD/2 ) # when the BigBox is non-cubical, need three different BoxViews for projections (all three could be different sizes) smallboxviewXZ = BoxView( BBW/2, BBD/2 ) # when the BigBox is non-cubical, need three different BoxViews for projections (all three could be different sizes) smallboxviews = { "XY":smallboxviewXY , "YZ":smallboxviewYZ , "XZ":smallboxviewXZ } # for convenience ##print( "LAUNCH : screen's resolution is %dx%d; " % ( mainwin.get_screen().width(), mainwin.get_screen().height() ) ) ##print( "LAUNCH : large image set to %dx%d; " % ( BBW, BBH ) ) ##print( "LAUNCH : small images set to %dx%d, %dx%d, %dx%d" % ( smallboxviewXY.Img.width, smallboxviewXY.Img.height, smallboxviewYZ.Img.width, smallboxviewYZ.Img.height, smallboxviewXZ.Img.width, smallboxviewXZ.Img.height ) ) # connect all signals/events firstbutton.connect( "clicked", on_first_button_click, bigbox, bigboxview, bigboximage ) prevbutton.connect( "clicked", on_prev_button_click, bigbox, bigboxview, bigboximage ) nextbutton.connect( "clicked", on_next_button_click, bigbox, bigboxview, bigboximage ) lastbutton.connect( "clicked", on_last_button_click, bigbox, bigboxview, bigboximage ) refinebutton.connect( "clicked", on_refine_button_click, bigbox, bigboxview, bigboximage )
class BoxViewTestCase(unittest.TestCase): def setUp(self): self.api = BoxView('<box view api key>') def test_initials(self): # api key is required self.assertRaises(ValueError, BoxView) now = datetime.datetime.utcnow().replace(microsecond=0) dtfiso = now.isoformat() dfiso = now.date().isoformat() self.assertEqual(dtfiso, format_date(dtfiso)) self.assertEqual(dtfiso, format_date(now)) self.assertEqual(dfiso, format_date(now.date())) headers = {'Content-Type': 'text/plain'} self.assertEqual('text/plain', get_mimetype_from_headers(headers)) headers = {'Content-Type': 'text/plain; charset=utf-8'} self.assertEqual('text/plain', get_mimetype_from_headers(headers)) @patch.object(Session, 'request') def test_crate_document_from_url(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.create_document(url=test_url, name='Test Document') self.assertIsNotNone(result) self.assertEqual(result, test_document) # url of file param is required self.assertRaises(ValueError, self.api.create_document) @patch.object(Session, 'request') def test_create_document_from_url(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.create_document_from_url(test_url, name='Test Document') self.assertIsNotNone(result) self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_create_document_from_file(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_document) mock_request.return_value = response stream = six.BytesIO() result = self.api.create_document_from_file(stream, name='Test Document') self.assertEqual(result, test_document) result = self.api.create_document_from_file(__file__, name='Test Document') self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_get_document(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.get_document(test_document['id']) self.assertIsNotNone(result) self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_get_documents(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document_list) mock_request.return_value = response now = datetime.datetime.utcnow() result = self.api.get_documents(limit=10, created_before=now) self.assertIsNotNone(result) self.assertEqual(result, test_document_list) @patch.object(Session, 'request') def test_update_document(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.update_document(test_document['id'], name='TestDocument') self.assertIsNotNone(result) self.assertEqual(result, test_document) @patch.object(Session, 'request') def test_delete_document(self, mock_request): response = Response() response.status_code = 204 mock_request.return_value = response self.api.delete_document(test_document['id']) @patch.object(Session, 'request') def test_get_document_content(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' response._content = 'test' response.raw = six.StringIO('test') mock_request.return_value = response stream = six.StringIO() mimetype = self.api.get_document_content(stream, test_document['id']) self.assertEqual(stream.getvalue(), response._content) self.assertEqual(mimetype, response.headers['Content-Type']) stream = six.StringIO() self.api.get_document_content(stream, test_document['id'], extension='.pdf') self.assertEqual(stream.getvalue(), response._content) stream = six.StringIO() self.api.get_document_content(stream, test_document['id'], extension='.zip') self.assertEqual(stream.getvalue(), response._content) stream = six.StringIO() # allowed only .zip and .pdf extensions self.assertRaises(ValueError, self.api.get_document_content, stream, test_document['id'], extension='.docx') @patch.object(Session, 'request') def test_get_document_content_to_string(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' response._content = 'test' response.raw = six.StringIO('test') mock_request.return_value = response doc_id = test_document['id'] result, mimetype = self.api.get_document_content_to_string(doc_id) self.assertIsNotNone(result) self.assertEqual(result, response._content) self.assertEqual(mimetype, response.headers['Content-Type']) @patch.object(Session, 'request') def test_get_document_content_to_file(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' response._content = 'test' response.raw = six.StringIO('test') mock_request.return_value = response filename = 'boxview.txt' mimetype = self.api.get_document_content_to_file( filename, test_document['id']) self.assertEqual(mimetype, response.headers['Content-Type']) self.assertTrue(os.path.exists(filename)) try: os.remove(filename) except OSError: pass @patch.object(Session, 'request') def test_get_document_content_mimetype(self, mock_request): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'text/plain' mock_request.return_value = response mimetype = self.api.get_document_content_mimetype(test_document['id']) self.assertEqual(mimetype, response.headers['Content-Type']) @patch.object(Session, 'request') def test_create_session(self, mock_request): response = Response() response.status_code = 201 response._content = json.dumps(test_session) mock_request.return_value = response expires_at = datetime.datetime.utcnow() result = self.api.create_session(test_document['id'], duration=600, expires_at=expires_at) self.assertIsNotNone(result) self.assertEqual(result['id'], test_session['id']) @patch.object(Session, 'request') def test_ready_to_view(self, mock_request): response = Response() response.status_code = 200 response._content = json.dumps(test_document) mock_request.return_value = response result = self.api.ready_to_view(test_document['id']) self.assertIsNotNone(result) self.assertTrue(bool(result)) response._content = json.dumps(dict(test_document, status='error')) result = self.api.ready_to_view(test_document['id']) self.assertFalse(bool(result)) @patch.object(Session, 'request') def test_request_error(self, mock_request): response = Response() response.status_code = 401 response._content = 'Unauthorized' response.reason = 'Unauthorized' mock_request.return_value = response self.assertRaises(BoxViewError, self.api.get_document, test_document['id'])