def test_get_members(self): model = EmilyModel() # remove all conf files self.setup_func() # create farm requests.post(url='http://localhost:666/Emily/farms', data="{\"lb_method\": \"round_robin\"}", headers={'Content-Type': 'text/plain'}) model.load_farms() res = model.get_farms().items()[0][1] farm_id = res.farm_id # create farm member requests.post( url='http://localhost:666/Emily/farms/' + str(farm_id) + '/members', data="{\"url\": \"217684fa-b9c8-406a-b338-5387b3d4f371\"," " \"weight\": \"3\"}", headers={'Content-Type': 'text/plain'}) req_res = requests.get(url='http://localhost:666/Emily/farms/' + str(farm_id) + '/members') eq_(req_res.status_code, 200)
def test_get_farm_by_id(self): model = EmilyModel() # remove all conf files self.setup_func() requests.post(url='http://localhost:666/Emily/farms', data="{\"lb_method\": \"round_robin\"}", headers={'Content-Type': 'text/plain'}) model.load_farms() res = model.get_farms().items()[0] farm_id = res[1].farm_id req_res = requests.get('http://localhost:666/Emily/farms/' + farm_id) eq_(req_res.status_code, 200)
def test_get_farms(self): model = EmilyModel() # remove all conf files self.setup_func() # add one farm requests.post(url='http://localhost:666/Emily/farms', data="{\"lb_method\": \"round_robin\"}", headers={'Content-Type': 'text/plain'}) # get farms requests.get('http://localhost:666/Emily/farms') model.load_farms() eq_(1, len(model.get_farms()))
def test_create_more_then_one_member(self): model = EmilyModel() # remove all conf files self.setup_func() requests.post(url='http://localhost:666/Emily/farms', data="{\"lb_method\": \"round_robin\"}", headers={'Content-Type': 'text/plain'}) model.load_farms() res = model.get_farms().items()[0][1] farm_id = res.farm_id farm_members_before = res.members # create farm member requests.post( url='http://localhost:666/Emily/farms/' + str(farm_id) + '/members', data="{\"url\": \"217684fa-b9c8-406a-b338-5387b3d4f371\"," " \"weight\": \"3\"}", headers={'Content-Type': 'text/plain'}) requests.post( url='http://localhost:666/Emily/farms/' + str(farm_id) + '/members', data="{\"url\": \"217684fa-b9c8-406a-b338-5387b3d4f372\"," " \"weight\": \"3\"}", headers={'Content-Type': 'text/plain'}) farm_members_after = model.get_farm(farm_id).members eq_(len(farm_members_before) + 2, len(farm_members_after))
def test_add_farm(self): model = EmilyModel() farms_len_before = len(model.get_farms()) requests.post(url='http://localhost:666/Emily/farms', data="{\"lb_method\": \"round_robin\"}", headers={'Content-Type': 'text/plain'}) model.load_farms() farms_len_after = len(model.get_farms()) eq_(farms_len_before + 1, farms_len_after)
def test_text_plain_json(self): model = EmilyModel() farms_len_before = len(model.get_farms()) requests.post('http://localhost:666/Emily/farms', [('Content-Type', 'application/json')], {'lb_method': 'round_robin'}) model.load_farms() farms_len_after = len(model.get_farms()) eq_(farms_len_before + 1, farms_len_after)
def test_update_farm_by_id(self): port = str(8080) model = EmilyModel() # remove all conf files self.setup_func() requests.post(url='http://localhost:666/Emily/farms', data="{\"lb_method\": \"round_robin\"}", headers={'Content-Type': 'text/plain'}) model.load_farms() res = model.get_farms().items()[0] farm_id = res[1].farm_id requests.put('http://localhost:666/Emily/farms/' + farm_id, data="{\"port\": " + port + "}", headers={'Content-Type': 'text/plain'}) eq_(model.get_farm(farm_id).port, port)
def __init__(self, name): Flask.__init__(self, name) self.model = EmilyModel() self.view = EmilyView(self.model) self.route_view()
def __init__(self, model=None): if model is None: self.model = EmilyModel() else: self.model = model
class EmilyView: def __init__(self, model=None): if model is None: self.model = EmilyModel() else: self.model = model def farms_api(self): if request.method == 'GET': farms_list = [] for farm in self.model.get_farms(): farms_list.append(json.dumps(str(farm))) return json.dumps(farms_list) elif request.method == 'POST': args = self.request_data(request) res = make_response(self.model.create_farm(args)) return res.data else: logger.debug("unknown method: %s" % request.method.to_string()) raise ("in Emily_View.farms_api, unknown method: %s" % request.method.to_string()) def farm_api(self, farm_id): if request.method == 'GET': if self.model.get_farm(farm_id): response = self.model.get_farm(farm_id).to_json() else: response = json.dumps({"error": "farm not found"}), 404 elif request.method == 'PUT': args = self.request_data(request) response = make_response(self.model.update_farm(farm_id, args)) elif request.method == 'DELETE': response = make_response(self.model.delete_farm(farm_id)) else: logger.debug("unknown method: %s" % request.method.to_string()) raise ("in Emily_View.farm_api, unknown method: %s" % request.method.to_string()) return response def farm_members_api(self, farm_id): if request.method == 'GET': members_json = [] for member in self.model.get_farm_members(farm_id): members_json.append(json.dumps(member)) return json.dumps(members_json) elif request.method == 'POST': args = self.request_data(request) return make_response(self.model.create_farm_member(farm_id, args)) else: logger.debug("unknown method: %s" % request.method.to_string()) raise ("in Emily_View.farm_members_api, unknown method: %s" % request.method.to_string()) def farm_member_api(self, farm_id, member_id): if request.method == 'GET': return json.dumps( self.model.get_farm_member(farm_id, member_id).__dict__) elif request.method == 'DELETE': return json.dumps(self.model.delete_farm_member( farm_id, member_id)) else: logger.debug("unknown method: {}".format( request.method.to_string())) raise ("in Emily_View.farm_member_api, unknown method: {}".format( request.method.to_string())) @staticmethod def request_data(request): if request.headers['Content-Type'] == 'application/json': return json.loads(request.data) elif request.headers['Content-Type'] == 'text/plain': try: return json.loads(request.data) except Exception: raise Exception("couldn't parse request {} as json".format( str(request.data))) else: logger.debug("unknown content type: %s" % request.headers['Content-Type']) raise ("unknown content type: %s" % request.headers['Content-Type']) def view_api(self): routes = [{ 'rule': '/Emily/farms', 'view_func': self.farms_api, 'methods': ['GET', 'POST'] }, { 'rule': '/Emily/farms/<string:farm_id>', 'view_func': self.farm_api, 'methods': ['GET', 'PUT', 'DELETE'] }, { 'rule': '/Emily/farms/<string:farm_id>/members', 'view_func': self.farm_members_api, 'methods': ['GET', 'POST'] }, { 'rule': '/Emily/farms/<string:farm_id>/members/<string:member_id>', 'view_func': self.farm_member_api, 'methods': ['GET', 'DELETE'] }] return routes