def test_selector_returns_one_random_if_experimentditribution_not_set( self): app = Application.get(1) logic_selector = ExperimentLogicSelector() assert logic_selector.get_experiments(app) in app.experiments
def set_app_apikey(self, application, app_id): apikey = self.get_unused_apikey() application.apikey = apikey Application.save(application) return Application.get(app_id)
def test_selector_returns_one_random_if_set_on_application(self): app = Application.get(1) app.experiment_distribution = OneRandomExperiment().get_name() logic_selector = ExperimentLogicSelector() assert logic_selector.get_experiments(app) in app.experiments
def data_for_app_GET(self): """ List all configurationkeys and rangeconstraints of specific application. Returns application with configurationkeys, rangeconstraints and exclusionconstraints """ app_id = self.request.swagger_data['id'] app = Application.get(app_id) if app is None: print_log(datetime.datetime.now(), 'GET', '/applications/' + str(id) + '/rangeconstraints', 'Get all things of one application', None) return self.createResponse(None, 400) if app.apikey is None: app = self.set_app_apikey(app, app_id) configurationkeys = app.configurationkeys ranges = list( concat(list(map(lambda _: _.rangeconstraints, configurationkeys)))) exclusions = self.get_app_exclusionconstraints(app_id) app_data = app.as_dict() app_data = assoc(app_data, 'configurationkeys', list(map(lambda _: _.as_dict(), configurationkeys))) app_data = assoc(app_data, 'rangeconstraints', list(map(lambda _: _.as_dict(), ranges))) app_data = assoc(app_data, 'exclusionconstraints', list(map(lambda _: _.as_dict(), exclusions))) return app_data
def configurationkeys_POST(self): """ Create new configurationkey to application. request.swagger_data['id'] takes the id and Application.get(app_id) returns the application by id. """ app_id = self.request.swagger_data['id'] application = Application.get(app_id) if application is None: print_log(datetime.datetime.now(), 'POST', '/applications/' + str(app_id) + '/configurationkeys', 'Create new configurationkey for application', 'Failed: No Application with id %s' % app_id) return self.createResponse({}, 400) new_confkey = self.request.swagger_data['configurationkey'] name = new_confkey.name type = new_confkey.type.lower() configurationkey = ConfigurationKey( application=application, name=name, type=type ) if self.is_valid_configurationkey(configurationkey): ConfigurationKey.save(configurationkey) print_log(datetime.datetime.now(), 'POST', '/applications/' + str(app_id) + '/configurationkeys', 'Create new configurationkey', 'Succeeded') return configurationkey.as_dict() print_log(datetime.datetime.now(), 'POST', '/applications/' + str(app_id) + '/configurationkeys', 'Create new configurationkey for application', 'Failed: Invalid ConfigurationKey') return self.createResponse({}, 400)
def configurationkeys_GET(self): """ List all configurationkeys with GET method """ app_id = self.request.swagger_data['id'] if Application.get(app_id) is None: print_log(datetime.datetime.now(), 'GET', '/applications/%s/configurationkeys'\ % app_id, 'Get configurationkeys', 'Failed') return self.createResponse(None, 400) app_conf_keys = ConfigurationKey.query().join(Application).filter(Application.id == app_id) return list(map(lambda _: _.as_dict(), app_conf_keys))
def applications_PUT(self): req_app = self.request.swagger_data['application'] req_app_id = self.request.swagger_data['id'] updated = Application.get(req_app_id) if req_app_id != req_app.id or updated is None or not self.is_valid_application( req_app): print_log( datetime.datetime.now(), 'PUT', '/applications/', 'Update Application', 'Failed: no such Application with id %s or ids didn\'t match' % req_app_id) return self.createResponse(None, 400) Application.update(updated.id, "name", req_app.name) Application.update(updated.id, "experiment_distribution", req_app.experiment_distribution) updated = Application.get(updated.id) return updated.as_dict()
def applications_GET_one(self): """ Find and return one application by id with GET method """ app_id = self.request.swagger_data['id'] app = Application.get(app_id) if app is None: print_log(datetime.datetime.now(), 'GET', '/applications/' + str(app_id), 'Get one application', None) return self.createResponse(None, 400) if app.apikey is None: app = self.set_app_apikey(app, app_id) return app.as_dict()
def applications_DELETE_one(self): """ Find and delete one application by id with delete method """ app_id = self.request.swagger_data['id'] app = Application.get(app_id) if not app: print_log(datetime.datetime.now(), 'DELETE', '/applications/' + str(app_id), 'Delete application', 'Failed') return self.createResponse(None, 400) Application.destroy(app) print_log(datetime.datetime.now(), 'DELETE', '/applications/' + str(app_id), 'Delete application', 'Succeeded') return {}
def configurationkeys_for_application_DELETE(self): """ Delete all configurationkeys of one specific application """ id = self.request.swagger_data['id'] app = Application.get(id) if not app: print_log(datetime.datetime.now(), 'DELETE', '/applications/' + str(id) + '/configurationkeys', 'Delete configurationkeys of application', 'Failed') return self.createResponse(None, 400) is_empty_list = list(map(lambda _: ConfigurationKey.destroy(_), app.configurationkeys)) for i in is_empty_list: if i != None: print_log(datetime.datetime.now(), 'DELETE', '/applications/' + str(id) + '/configurationkeys', 'Delete configurationkeys of application', 'Failed') return self.createResponse(None, 400) print_log(datetime.datetime.now(), 'DELETE', '/applications/' + str(id) + '/configurationkeys', 'Delete configurationkeys of application', 'Succeeded') return {}
def clients_GET(self): """ Explanation: maps as_dict() -function to every client-object (this is returned by client.all()) Creates a list and returns it. In future we might would like general json-serialization to make this even more simpler. """ app_id = self.request.swagger_data['appid'] if not Application.get(app_id): print_log('/applications/%s/clients failed' % app_id) return self.createResponse(None, 400) clients = Client.query()\ .join(Client.experimentgroups)\ .join(Experiment)\ .join(Application)\ .filter(Application.id == app_id) return list(map(lambda _: _.as_dict(), clients))
def test_returns_one_random_experiment(self): app = Application.get(1) one_random = OneRandomExperiment() assert one_random.get_experiments(app) in app.experiments