def test_permalinks_work(self): db = InMemoryDemoDatabase() app = make_app(build_dir=self.TEST_DIR, demo_db=db) predictor = CountingPredictor() app.predictors = {"counting": predictor} app.testing = True client = app.test_client() def post(endpoint: str, data: JsonDict) -> Response: return client.post(endpoint, content_type="application/json", data=json.dumps(data)) data = {"some": "input"} response = post("/predict/counting", data=data) assert response.status_code == 200 result = json.loads(response.get_data()) slug = result.get("slug") assert slug is not None response = post("/permadata", data={"slug": "not the right slug"}) assert response.status_code == 400 response = post("/permadata", data={"slug": slug}) assert response.status_code == 200 result2 = json.loads(response.get_data()) assert set( result2.keys()) == {"modelName", "requestData", "responseData"} assert result2["modelName"] == "counting" assert result2["requestData"] == data assert result2["responseData"] == result
def get_app(self): options.parse_command_line([ "", "--database_url=sqlite://", ]) logger = log.get_logger() logger.disabled = True application = app.make_app() self.session = application.settings['session_factory'].make_session() return application
def setUp(self): super().setUp() self.TEST_DIR = pathlib.Path(tempfile.mkdtemp()) # Create index.html in TEST_DIR (self.TEST_DIR / 'index.html').touch() # pylint: disable=no-member if self.client is None: self.app = make_app(build_dir=self.TEST_DIR) self.app.predictors = PREDICTORS self.app.testing = True self.client = self.app.test_client()
def test_permalinks_fail_gracefully_with_no_database(self): app = make_app(build_dir=self.TEST_DIR) predictor = CountingPredictor() app.predictors = {"counting": predictor} app.testing = True client = app.test_client() # Make a prediction, no permalinks. data = {"some": "input"} response = client.post("/predict/counting", content_type="application/json", data=json.dumps(data)) assert response.status_code == 200 # With permalinks not enabled, the result shouldn't contain a slug. result = json.loads(response.get_data()) assert "slug" not in result # With permalinks not enabled, a post to the /permadata endpoint should be a 400. response = self.client.post("/permadata", data="""{"slug": "someslug"}""") assert response.status_code == 400
from server.repository import ApplicationRepository from server.services import get_unit_service_from_env from server.settings import Environment, settings from tornado.ioloop import IOLoop def init_app_options() -> dict: db = MotorClient(settings.DB.dsn).default debug = settings.ENVIRONMENT == Environment.DEVELOPMENT configurator = get_unit_service_from_env() docker_client = docker.from_env() return { 'debug': debug, 'repository': ApplicationRepository(db), 'configurator': configurator, 'docker': docker_client, } if __name__ == '__main__': options = init_app_options() app = make_app(options) try: app.listen(settings.SERVER_PORT) logging.info('Server started on port %s', settings.SERVER_PORT) IOLoop.current().start() except (KeyboardInterrupt, SystemExit): logging.info('Server graceful shutdown')
def app(): options = init_app_options() return make_app(options)
def test_missing_static_dir(self): fake_dir = self.TEST_DIR / 'this' / 'directory' / 'does' / 'not' / 'exist' with self.assertRaises(SystemExit) as cm: make_app(fake_dir) assert cm.code == -1 # pylint: disable=no-member
async def client(loop, test_client): app = make_app() return await test_client(app)